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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
refactor(connector): [Multisafepay] handle authorize and psync 2xx failure error response (#4124)
AkshayaFoiger9ebe0f4371f1
diff --git a/crates/router/src/connector/multisafepay.rs b/crates/router/src/connector/multisafepay.rsindex fa6bb5dad3..bd20228283 100644--- a/crates/router/src/connector/multisafepay.rs+++ b/crates/router/src/connector/multisafepay.rs@@ -2,6 +2,7 @@ pub mod transformers;use std::fmt::Debug;+use common_enums::AttemptStatus;use common_utils::request::RequestContent;use error_stack::{report, ResultExt};use masking::ExposeInterface;@@ -84,14 +85,16 @@ impl ConnectorCommon for Multisafepay {event_builder.map(|i| i.set_error_response_body(&response));router_env::logger::info!(connector_response=?response);- Ok(ErrorResponse {- status_code: res.status_code,- code: response.error_code.to_string(),- message: response.error_info,- reason: None,- attempt_status: None,- connector_transaction_id: None,- })+ let attempt_status = Option::<AttemptStatus>::from(response.clone());++ Ok(ErrorResponse::from((+ Some(response.error_code.to_string()),+ Some(response.error_info.clone()),+ Some(response.error_info),+ res.status_code,+ attempt_status,+ None,+ )))}}diff --git a/crates/router/src/connector/multisafepay/transformers.rs b/crates/router/src/connector/multisafepay/transformers.rsindex de0a52fe56..b26c260f93 100644--- a/crates/router/src/connector/multisafepay/transformers.rs+++ b/crates/router/src/connector/multisafepay/transformers.rs@@ -1,3 +1,4 @@+use common_enums::AttemptStatus;use common_utils::pii::{Email, IpAddress};use masking::ExposeInterface;use serde::{Deserialize, Serialize};@@ -587,6 +588,7 @@ pub enum MultisafepayPaymentStatus {#[default]Initialized,Void,+ Uncleared,}#[derive(Debug, Clone, Eq, Serialize, Deserialize, PartialEq)]@@ -595,12 +597,13 @@ pub enum MandateType {Unscheduled,}-impl From<MultisafepayPaymentStatus> for enums::AttemptStatus {+impl From<MultisafepayPaymentStatus> for AttemptStatus {fn from(item: MultisafepayPaymentStatus) -> Self {match item {MultisafepayPaymentStatus::Completed => Self::Charged,MultisafepayPaymentStatus::Declined => Self::Failure,MultisafepayPaymentStatus::Initialized => Self::AuthenticationPending,+ MultisafepayPaymentStatus::Uncleared => Self::Pending,MultisafepayPaymentStatus::Void => Self::Voided,}}@@ -617,8 +620,8 @@ pub struct Data {pub capture: Option<String>,pub payment_url: Option<Url>,pub status: Option<MultisafepayPaymentStatus>,- pub error_code: Option<i32>,- pub error_info: Option<String>,+ pub reason: Option<String>,+ pub reason_code: Option<String>,pub payment_details: Option<MultisafepayPaymentDetails>,}@@ -677,44 +680,60 @@ impl<F, T>MultisafepayPaymentStatus::Declined};- let status = payment_response.data.status.unwrap_or(default_status);+ let status = enums::AttemptStatus::from(+ payment_response.data.status.unwrap_or(default_status),+ );Ok(Self {- status: enums::AttemptStatus::from(status),- response: Ok(types::PaymentsResponseData::TransactionResponse {- resource_id: types::ResponseId::ConnectorTransactionId(- payment_response.data.order_id.clone(),- ),- redirection_data,- mandate_reference: payment_response- .data- .payment_details- .and_then(|payment_details| payment_details.recurring_id)- .map(|id| types::MandateReference {- connector_mandate_id: Some(id.expose()),- payment_method_id: None,- }),- connector_metadata: None,- network_txn_id: None,- connector_response_reference_id: Some(- payment_response.data.order_id.clone(),- ),- incremental_authorization_allowed: None,- }),+ status,+ response: if utils::is_payment_failure(status) {+ Err(types::ErrorResponse::from((+ payment_response.data.reason_code,+ payment_response.data.reason.clone(),+ payment_response.data.reason,+ item.http_code,+ Some(status),+ Some(payment_response.data.order_id),+ )))+ } else {+ Ok(types::PaymentsResponseData::TransactionResponse {+ resource_id: types::ResponseId::ConnectorTransactionId(+ payment_response.data.order_id.clone(),+ ),+ redirection_data,+ mandate_reference: payment_response+ .data+ .payment_details+ .and_then(|payment_details| payment_details.recurring_id)+ .map(|id| types::MandateReference {+ connector_mandate_id: Some(id.expose()),+ payment_method_id: None,+ }),+ connector_metadata: None,+ network_txn_id: None,+ connector_response_reference_id: Some(+ payment_response.data.order_id.clone(),+ ),+ incremental_authorization_allowed: None,+ })+ },+ ..item.data+ })+ }+ MultisafepayAuthResponse::ErrorResponse(error_response) => {+ let attempt_status = Option::<AttemptStatus>::from(error_response.clone());+ Ok(Self {+ response: Err(types::ErrorResponse::from((+ Some(error_response.error_code.to_string()),+ Some(error_response.error_info.clone()),+ Some(error_response.error_info),+ item.http_code,+ attempt_status,+ None,+ ))),..item.data})}- MultisafepayAuthResponse::ErrorResponse(error_response) => Ok(Self {- response: Err(types::ErrorResponse {- code: error_response.error_code.to_string(),- message: error_response.error_info.clone(),- reason: Some(error_response.error_info),- status_code: item.http_code,- attempt_status: None,- connector_transaction_id: None,- }),- ..item.data- }),}}}@@ -812,17 +831,20 @@ impl TryFrom<types::RefundsResponseRouterData<api::Execute, MultisafepayRefundRe..item.data})}- MultisafepayRefundResponse::ErrorResponse(error_response) => Ok(Self {- response: Err(types::ErrorResponse {- code: error_response.error_code.to_string(),- message: error_response.error_info.clone(),- reason: Some(error_response.error_info),- status_code: item.http_code,- attempt_status: None,- connector_transaction_id: None,- }),- ..item.data- }),+ MultisafepayRefundResponse::ErrorResponse(error_response) => {+ let attempt_status = Option::<AttemptStatus>::from(error_response.clone());+ Ok(Self {+ response: Err(types::ErrorResponse {+ code: error_response.error_code.to_string(),+ message: error_response.error_info.clone(),+ reason: Some(error_response.error_info),+ status_code: item.http_code,+ attempt_status,+ connector_transaction_id: None,+ }),+ ..item.data+ })+ }}}}@@ -851,14 +873,14 @@ impl TryFrom<types::RefundsResponseRouterData<api::RSync, MultisafepayRefundResp})}MultisafepayRefundResponse::ErrorResponse(error_response) => Ok(Self {- response: Err(types::ErrorResponse {- code: error_response.error_code.to_string(),- message: error_response.error_info.clone(),- reason: Some(error_response.error_info),- status_code: item.http_code,- attempt_status: None,- connector_transaction_id: None,- }),+ response: Err(types::ErrorResponse::from((+ Some(error_response.error_code.to_string()),+ Some(error_response.error_info.clone()),+ Some(error_response.error_info),+ item.http_code,+ None,+ None,+ ))),..item.data}),}@@ -870,3 +892,47 @@ pub struct MultisafepayErrorResponse {pub error_code: i32,pub error_info: String,}++impl From<MultisafepayErrorResponse> for Option<AttemptStatus> {+ fn from(error_data: MultisafepayErrorResponse) -> Self {+ match error_data.error_code {+ 10001 // InvalidAmount+ | 1002 // InvalidCurrency+ | 1003 // InvalidAccountID+ | 1004 // InvalidSiteID+ | 1005 // InvalidSecurityCode+ | 1006 // InvalidTransactionID+ | 1007 // InvalidIPAddress+ | 1008 // InvalidDescription+ | 1010 // InvalidVariable+ | 1011 // InvalidCustomerAccountID+ | 1012 // InvalidCustomerSecurityCode+ | 1013 // InvalidSignature+ | 1015 //UnknownAccountID+ | 1016 // MissingData+ | 1018 // InvalidCountryCode+ | 1025 // MultisafepayErrorCodes::IncorrectCustomerIPAddress+ | 1026 // MultisafepayErrorCodes::MultipleCurrenciesInCart+ | 1027 // MultisafepayErrorCodes::CartCurrencyDifferentToOrderCurrency+ | 1028 // IncorrectCustomTaxRate+ | 1029 // IncorrectItemTaxRate+ | 1030 // IncorrectItemCurrency+ | 1031 // IncorrectItemPrice+ | 1035 // InvalidSignatureRefund+ | 1036 // InvalidIdealIssuerID+ | 5001 // CartDataNotValidated+ | 1032 // InvalidAPIKey+ => {+ Some(AttemptStatus::AuthenticationFailed)+ }++ 1034 // CannotRefundTransaction+ | 1022 // CannotInitiateTransaction+ | 1024 //TransactionDeclined+ => Some(AttemptStatus::Failure),+ 1017 // InsufficientFunds+ => Some(AttemptStatus::AuthorizationFailed),+ _ => None,+ }+ }+}diff --git a/crates/router/src/connector/utils.rs b/crates/router/src/connector/utils.rsindex b2a148b176..645d850ebe 100644--- a/crates/router/src/connector/utils.rs+++ b/crates/router/src/connector/utils.rs@@ -1916,6 +1916,39 @@ pub fn is_refund_failure(status: enums::RefundStatus) -> bool {}}+impl+ From<(+ Option<String>,+ Option<String>,+ Option<String>,+ u16,+ Option<enums::AttemptStatus>,+ Option<String>,+ )> for types::ErrorResponse+{+ fn from(+ (code, message, reason, http_code, attempt_status, connector_transaction_id): (+ Option<String>,+ Option<String>,+ Option<String>,+ u16,+ Option<enums::AttemptStatus>,+ Option<String>,+ ),+ ) -> Self {+ Self {+ code: code.unwrap_or(consts::NO_ERROR_CODE.to_string()),+ message: message+ .clone()+ .unwrap_or(consts::NO_ERROR_MESSAGE.to_string()),+ reason,+ status_code: http_code,+ attempt_status,+ connector_transaction_id,+ }+ }+}+#[cfg(test)]mod error_code_error_message_tests {#![allow(clippy::unwrap_used)]