Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

feat(connector): [SANTANDER] Added length validations and proper error message mapping (#11832)

Sayak Bhattacharya693d29d1aa89
diff --git a/crates/hyperswitch_connectors/src/connectors/santander.rs b/crates/hyperswitch_connectors/src/connectors/santander.rs
index f7f02e1e8d..872fa8ba87 100644
--- a/crates/hyperswitch_connectors/src/connectors/santander.rs
+++ b/crates/hyperswitch_connectors/src/connectors/santander.rs
@@ -468,12 +468,58 @@ impl ConnectorCommon for Santander {
.cloned()
.unwrap_or_else(|| NO_ERROR_MESSAGE.to_string());
+ let reason = response
+ .violacoes
+ .first()
+ .and_then(|v| match (&v.propriedade, &v.razao) {
+ (Some(prop), Some(raz)) => Some(format!("{}: {}", prop, raz)),
+ (Some(prop), None) => Some(prop.clone()),
+ (None, Some(raz)) => Some(raz.clone()),
+ (None, None) => None,
+ })
+ .or_else(|| Some(message.clone()));
+
Ok(ErrorResponse {
status_code: res.status_code,
code: response.status.to_string(),
message,
- // reason: response.detail.clone(),
- reason: Some(response.title.clone()),
+ reason,
+ attempt_status: None,
+ connector_transaction_id: None,
+ connector_response_reference_id: None,
+ network_advice_code: None,
+ network_decline_code: None,
+ network_error_message: None,
+ connector_metadata: None,
+ })
+ }
+ SantanderErrorResponse::PixAutomatico(response) => {
+ let (code, message, description) = match response {
+ responses::SantanderPixAutomaticoErrorResponse::PixAutomaticoVariant1(err) => {
+ let first_error = err.errors.first();
+ (
+ first_error
+ .map(|e| e.code.to_string())
+ .unwrap_or_else(|| NO_ERROR_CODE.to_string()),
+ first_error
+ .map(|e| e.message.clone())
+ .unwrap_or_else(|| NO_ERROR_MESSAGE.to_string()),
+ first_error
+ .map(|e| e.description.clone())
+ .unwrap_or_else(|| NO_ERROR_MESSAGE.to_string()),
+ )
+ }
+ responses::SantanderPixAutomaticoErrorResponse::PixAutomaticoVariant2(err) => (
+ err.code.to_string(),
+ err.message.clone(),
+ err.description.clone(),
+ ),
+ };
+ Ok(ErrorResponse {
+ status_code: res.status_code,
+ code,
+ message,
+ reason: Some(description),
attempt_status: None,
connector_transaction_id: None,
connector_response_reference_id: None,
@@ -483,19 +529,6 @@ impl ConnectorCommon for Santander {
connector_metadata: None,
})
}
- SantanderErrorResponse::PixAutomatico(response) => Ok(ErrorResponse {
- status_code: res.status_code,
- code: response.code.to_string(),
- message: response.message.clone(),
- reason: Some(response.description.clone()),
- attempt_status: None,
- connector_transaction_id: None,
- connector_response_reference_id: None,
- network_advice_code: None,
- network_decline_code: None,
- network_error_message: None,
- connector_metadata: None,
- }),
SantanderErrorResponse::Boleto(response) => Ok(ErrorResponse {
status_code: res.status_code,
code: response.error_code.to_string(),
diff --git a/crates/hyperswitch_connectors/src/connectors/santander/requests.rs b/crates/hyperswitch_connectors/src/connectors/santander/requests.rs
index 62f69c99af..fa10b9b60e 100644
--- a/crates/hyperswitch_connectors/src/connectors/santander/requests.rs
+++ b/crates/hyperswitch_connectors/src/connectors/santander/requests.rs
@@ -188,13 +188,13 @@ pub struct SantanderPixAutomaticCalendarRequest {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SantanderPixAutomaticDestinationRequest {
- pub agencia: String,
- pub conta: String,
+ pub agencia: Secret<String>,
+ pub conta: Secret<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cpf: Option<Secret<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cnpj: Option<Secret<String>>,
- pub ispb_participante: String,
+ pub ispb_participante: Secret<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
diff --git a/crates/hyperswitch_connectors/src/connectors/santander/responses.rs b/crates/hyperswitch_connectors/src/connectors/santander/responses.rs
index aede089785..74e3579b42 100644
--- a/crates/hyperswitch_connectors/src/connectors/santander/responses.rs
+++ b/crates/hyperswitch_connectors/src/connectors/santander/responses.rs
@@ -533,19 +533,33 @@ pub struct SantanderTime {
}
#[derive(Debug, Clone, Serialize, Deserialize)]
-pub struct SantanderPixAutomaticoErrorResponse {
+pub struct SantanderPixAutomaticoErrorItem {
pub code: i32,
pub message: String,
pub level: String,
pub description: String,
}
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct SantanderPixAutomaticoErrorVariant1 {
+ pub errors: Vec<SantanderPixAutomaticoErrorItem>,
+}
+
+#[derive(Debug, Serialize, Deserialize)]
+#[serde(untagged)]
+pub enum SantanderPixAutomaticoErrorResponse {
+ // Pix Automatico API returns errors in an array format
+ PixAutomaticoVariant1(SantanderPixAutomaticoErrorVariant1),
+ // Pix Automatico API returns 401 Unauthorized when access token is expired (single object)
+ PixAutomaticoVariant2(SantanderPixAutomaticoErrorItem),
+}
+
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SantanderErrorResponse {
PixQrCode(SantanderPixQRCodeErrorResponse),
Boleto(SantanderBoletoErrorResponse),
- // Pix Automatico API returns 401 Unauthorized when access token is expired
+ // Pix Automatico API error responses
PixAutomatico(SantanderPixAutomaticoErrorResponse),
Generic(SantanderGenericErrorResponse),
}
diff --git a/crates/hyperswitch_connectors/src/connectors/santander/transformers.rs b/crates/hyperswitch_connectors/src/connectors/santander/transformers.rs
index d1659c18d2..eada4ec5ab 100644
--- a/crates/hyperswitch_connectors/src/connectors/santander/transformers.rs
+++ b/crates/hyperswitch_connectors/src/connectors/santander/transformers.rs
@@ -905,6 +905,31 @@ impl
.collect::<Vec<_>>()
});
+ if let Some(ref info_list) = info_adicionais {
+ for info in info_list {
+ let nome_str: &str = &info.nome.clone().expose();
+ let valor_str: &str = &info.valor;
+ if nome_str.len() > 50 {
+ return Err(errors::ConnectorError::MaxFieldLengthViolated {
+ connector: "Santander".to_string(),
+ field_name: "metadata.key".to_string(),
+ max_length: 50,
+ received_length: nome_str.len(),
+ }
+ .into());
+ }
+ if valor_str.len() > 150 {
+ return Err(errors::ConnectorError::MaxFieldLengthViolated {
+ connector: "Santander".to_string(),
+ field_name: "metadata.value".to_string(),
+ max_length: 150,
+ received_length: valor_str.len(),
+ }
+ .into());
+ }
+ }
+ }
+
let chave = value
.0
.router_data
@@ -914,6 +939,26 @@ impl
.and_then(|data| data.get_pix_key_and_value().1)
.or(mca_chave);
+ let solicitacao_pagador = value
+ .0
+ .router_data
+ .request
+ .billing_descriptor
+ .clone()
+ .and_then(|data| data.statement_descriptor);
+
+ if let Some(ref solicitacao) = solicitacao_pagador {
+ if solicitacao.len() > 140 {
+ return Err(errors::ConnectorError::MaxFieldLengthViolated {
+ connector: "Santander".to_string(),
+ field_name: "statement_descriptor".to_string(),
+ max_length: 140,
+ received_length: solicitacao.len(),
+ }
+ .into());
+ }
+ }
+
Ok(Self::PixQR(Box::new(SantanderPixQRPaymentRequest {
calendario: calendar,
devedor: debtor,
@@ -921,13 +966,7 @@ impl
original: value.0.amount.to_owned(),
}),
chave,
- solicitacao_pagador: value
- .0
- .router_data
- .request
- .billing_descriptor
- .clone()
- .and_then(|data| data.statement_descriptor),
+ solicitacao_pagador,
info_adicionais,
})))
}
@@ -1016,14 +1055,32 @@ impl TryFrom<&SantanderRouterData<&PaymentsAuthorizeRouterData>>
original: value.amount.to_owned(),
};
- let info_adicional = value.router_data.description.clone().or_else(|| {
- value
+ let (info_adicional, info_source) =
+ if let Some(desc) = value.router_data.description.clone() {
+ (Some(desc), "description")
+ } else if let Some(stmt_desc) = value
.router_data
.request
.billing_descriptor
.as_ref()
.and_then(|bd| bd.statement_descriptor.clone())
- });
+ {
+ (Some(stmt_desc), "statement_descriptor")
+ } else {
+ (None, "")
+ };
+
+ if let Some(ref info) = info_adicional {
+ if info.len() > 140 {
+ return Err(errors::ConnectorError::MaxFieldLengthViolated {
+ connector: "Santander".to_string(),
+ field_name: info_source.to_string(),
+ max_length: 140,
+ received_length: info.len(),
+ }
+ .into());
+ }
+ }
let devedor = Some(SantanderDebtor {
cpf: None,
@@ -2146,10 +2203,11 @@ impl
}
};
- let contrato = cit_data
- .contract_id
- .clone()
- .unwrap_or_else(|| item.payment_id.clone());
+ let contrato = item.request.merchant_order_reference_id.clone().ok_or(
+ errors::ConnectorError::MissingRequiredField {
+ field_name: "merchant_order_reference_id",
+ },
+ )?;
let politica_retentativa = if cit_data.retry_policy.unwrap_or(false) {
RetryPolicy::Permite3r7d
@@ -2189,6 +2247,16 @@ impl
field_name: "description",
})?;
+ if objeto.len() > 35 {
+ return Err(errors::ConnectorError::MaxFieldLengthViolated {
+ connector: "Santander".to_string(),
+ field_name: "description".to_string(),
+ max_length: 35,
+ received_length: objeto.len(),
+ }
+ .into());
+ }
+
let loc = item
.session_token
.as_ref()
@@ -2424,14 +2492,16 @@ impl TryFrom<&PaymentsPushNotificationRouterData> for SantanderPixAutomaticSolic
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "payment_method_data.bank_transfer.branch_code",
})?
- .expose(),
+ .expose()
+ .into(),
conta: bank_transfer_data
.0
.clone()
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "payment_method_data.bank_transfer.account_number",
})?
- .expose(),
+ .expose()
+ .into(),
cpf,
cnpj,
ispb_participante: bank_transfer_data
@@ -2440,7 +2510,8 @@ impl TryFrom<&PaymentsPushNotificationRouterData> for SantanderPixAutomaticSolic
.ok_or(errors::ConnectorError::MissingRequiredField {
field_name: "payment_method_data.bank_transfer.bank_identifier",
})?
- .expose(),
+ .expose()
+ .into(),
},
})
}
diff --git a/crates/hyperswitch_domain_models/src/router_request_types.rs b/crates/hyperswitch_domain_models/src/router_request_types.rs
index 62dfc67c1d..c8219d7e16 100644
--- a/crates/hyperswitch_domain_models/src/router_request_types.rs
+++ b/crates/hyperswitch_domain_models/src/router_request_types.rs
@@ -1821,6 +1821,7 @@ pub struct SetupMandateRequestData {
Option<common_types::payments::PartnerMerchantIdentifierDetails>,
pub authentication_data: Option<AuthenticationData>,
pub connector_intent_metadata: Option<ConnectorMetadata>,
+ pub merchant_order_reference_id: Option<String>,
}
#[derive(Debug, Clone)]
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index 2635e5ae20..0e2df80b60 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -1583,6 +1583,7 @@ pub async fn construct_payment_router_data_for_setup_mandate<'a>(
authentication_data: None,
feature_metadata: None,
connector_intent_metadata: None,
+ merchant_order_reference_id: None,
};
let connector_mandate_request_reference_id = payment_data
.payment_attempt
@@ -6192,6 +6193,11 @@ impl<F: Clone> TryFrom<PaymentAdditionalData<'_, F>> for types::SetupMandateRequ
let billing_descriptor = payment_data.payment_intent.get_billing_descriptor();
+ let merchant_order_reference_id = payment_data
+ .payment_intent
+ .merchant_order_reference_id
+ .clone();
+
Ok(Self {
currency: payment_data.currency,
confirm: true,
@@ -6270,6 +6276,7 @@ impl<F: Clone> TryFrom<PaymentAdditionalData<'_, F>> for types::SetupMandateRequ
.attach_printable("Failed parsing ConnectorMetadata")
})
.transpose()?,
+ merchant_order_reference_id,
})
}
}