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): [WorldpayWPG] Refactored payout flow (#10226)
Vani Gupta7a2c48dc4074
diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rsindex 3f875735d9..57bc93310c 100644--- a/crates/api_models/src/payment_methods.rs+++ b/crates/api_models/src/payment_methods.rs@@ -2691,6 +2691,7 @@ pub struct TokenizedCardValue1 {pub nickname: Option<String>,pub card_last_four: Option<String>,pub card_token: Option<String>,+ pub card_network: Option<api_enums::CardNetwork>,}#[derive(Debug, serde::Serialize, serde::Deserialize)]diff --git a/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rs b/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rsindex 571c7fdd79..83a4dd4f8b 100644--- a/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rs+++ b/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rs@@ -28,10 +28,12 @@ use hyperswitch_interfaces::{consts, errors};use masking::Secret;use serde::{Deserialize, Serialize};+#[cfg(feature = "payouts")]+use crate::types::PayoutsResponseRouterData;use crate::{types::{PaymentsCancelResponseRouterData, PaymentsCaptureResponseRouterData,- PayoutsResponseRouterData, RefundsResponseRouterData, ResponseRouterData,+ RefundsResponseRouterData, ResponseRouterData,},utils::{self as connector_utils, CardData, PaymentsAuthorizeRequestData, PaymentsSyncRequestData,@@ -136,12 +138,14 @@ pub struct Reply {ok: Option<OkResponse>,}+#[cfg(feature = "payouts")]#[derive(Debug, Deserialize, Serialize)]#[serde(rename_all = "camelCase")]pub struct PayoutResponse {pub reply: PayoutReply,}+#[cfg(feature = "payouts")]#[derive(Debug, Deserialize, Serialize)]#[serde(rename_all = "camelCase")]pub struct PayoutReply {@@ -149,6 +153,7 @@ pub struct PayoutReply {pub error: Option<WorldpayXmlErrorResponse>,}+#[cfg(feature = "payouts")]#[derive(Debug, Deserialize, Serialize)]#[serde(rename_all = "camelCase")]pub struct OkPayoutResponse {@@ -386,6 +391,7 @@ struct Date {year: Secret<String>,}+#[cfg(feature = "payouts")]#[derive(Clone, Debug, Serialize, Deserialize)]#[serde(rename_all = "camelCase")]pub enum PayoutOutcome {@@ -396,6 +402,7 @@ pub enum PayoutOutcome {CancelReceived,}+#[cfg(feature = "payouts")]impl From<PayoutOutcome> for enums::PayoutStatus {fn from(item: PayoutOutcome) -> Self {match item {@@ -407,6 +414,7 @@ impl From<PayoutOutcome> for enums::PayoutStatus {}}+#[cfg(feature = "payouts")]#[derive(Debug, Default, Serialize, Deserialize)]pub struct WorldpayxmlPayoutConnectorMetadataObject {pub purpose_of_payment: Option<String>,@@ -1176,6 +1184,7 @@ impl TryFrom<&RefundSyncRouterData> for PaymentService {}}+#[cfg(feature = "payouts")]impl TryFrom<(ApplePayDecrypt, Option<CardAddress>, Option<String>)> for PaymentDetails {type Error = error_stack::Report<errors::ConnectorError>;fn try_from(@@ -1221,6 +1230,7 @@ impl TryFrom<(ApplePayDecrypt, Option<CardAddress>, Option<String>)> for Payment}}+#[cfg(feature = "payouts")]impl TryFrom<(CardPayout, Option<CardAddress>, Option<String>)> for PaymentDetails {type Error = error_stack::Report<errors::ConnectorError>;fn try_from(@@ -1266,6 +1276,7 @@ impl TryFrom<(CardPayout, Option<CardAddress>, Option<String>)> for PaymentDetai}}+#[cfg(feature = "payouts")]impl TryFrom<Option<&pii::SecretSerdeValue>> for WorldpayxmlPayoutConnectorMetadataObject {type Error = error_stack::Report<errors::ConnectorError>;fn try_from(meta_data: Option<&pii::SecretSerdeValue>) -> Result<Self, Self::Error> {@@ -1278,6 +1289,7 @@ impl TryFrom<Option<&pii::SecretSerdeValue>> for WorldpayxmlPayoutConnectorMetad}}+#[cfg(feature = "payouts")]impl TryFrom<&WorldpayxmlRouterData<&PayoutsRouterData<PoFulfill>>> for PaymentService {type Error = error_stack::Report<errors::ConnectorError>;fn try_from(@@ -1373,6 +1385,7 @@ impl TryFrom<&WorldpayxmlRouterData<&PayoutsRouterData<PoFulfill>>> for PaymentS}}+#[cfg(feature = "payouts")]impl TryFrom<PayoutsResponseRouterData<PoFulfill, PayoutResponse>>for PayoutsRouterData<PoFulfill>{@@ -1382,44 +1395,43 @@ impl TryFrom<PayoutsResponseRouterData<PoFulfill, PayoutResponse>>) -> Result<Self, Self::Error> {let reply = item.response.reply;- validate_payout_reply(&reply)?;-- if let Some(ok_status) = reply.ok {- Ok(Self {+ match (reply.error, reply.ok) {+ (Some(error), None) => Ok(Self {+ status: common_enums::AttemptStatus::Failure,response: Ok(PayoutsResponseData {- status: Some(enums::PayoutStatus::from(PayoutOutcome::RefundReceived)),- connector_payout_id: ok_status.refund_received.map(|id| id.order_code),+ status: Some(enums::PayoutStatus::from(PayoutOutcome::Error)),+ connector_payout_id: None,payout_eligible: None,should_add_next_step_to_process_tracker: false,- error_code: None,- error_message: None,+ error_code: Some(error.code),+ error_message: Some(error.message),payout_connector_metadata: None,}),..item.data- })- } else {- let error = reply- .error- .ok_or(errors::ConnectorError::UnexpectedResponseError(- bytes::Bytes::from("Missing reply.error".to_string()),- ))?;- Ok(Self {- status: common_enums::AttemptStatus::Failure,+ }),+ (None, Some(ok_status)) => Ok(Self {response: Ok(PayoutsResponseData {- status: Some(enums::PayoutStatus::from(PayoutOutcome::Error)),- connector_payout_id: None,+ status: Some(enums::PayoutStatus::from(PayoutOutcome::RefundReceived)),+ connector_payout_id: ok_status.refund_received.map(|id| id.order_code),payout_eligible: None,should_add_next_step_to_process_tracker: false,- error_code: Some(error.code),- error_message: Some(error.message),+ error_code: None,+ error_message: None,payout_connector_metadata: None,}),..item.data- })+ }),+ _ => Err(+ errors::ConnectorError::UnexpectedResponseError(bytes::Bytes::from(+ "Either reply.error or reply.ok must be present in the response",+ ))+ .into(),+ ),}}}+#[cfg(feature = "payouts")]impl TryFrom<&PayoutsRouterData<PoCancel>> for PaymentService {type Error = error_stack::Report<errors::ConnectorError>;fn try_from(item: &PayoutsRouterData<PoCancel>) -> Result<Self, Self::Error> {@@ -1451,6 +1463,7 @@ impl TryFrom<&PayoutsRouterData<PoCancel>> for PaymentService {}}+#[cfg(feature = "payouts")]impl TryFrom<PayoutsResponseRouterData<PoCancel, PayoutResponse>> for PayoutsRouterData<PoCancel> {type Error = error_stack::Report<errors::ConnectorError>;fn try_from(@@ -1458,40 +1471,38 @@ impl TryFrom<PayoutsResponseRouterData<PoCancel, PayoutResponse>> for PayoutsRou) -> Result<Self, Self::Error> {let reply = item.response.reply;- validate_payout_reply(&reply)?;-- if let Some(ok_status) = reply.ok {- Ok(Self {+ match (reply.error, reply.ok) {+ (Some(error), None) => Ok(Self {+ status: common_enums::AttemptStatus::Failure,response: Ok(PayoutsResponseData {- status: Some(enums::PayoutStatus::from(PayoutOutcome::CancelReceived)),- connector_payout_id: ok_status.cancel_received.map(|id| id.order_code),+ status: Some(enums::PayoutStatus::from(PayoutOutcome::Error)),+ connector_payout_id: None,payout_eligible: None,should_add_next_step_to_process_tracker: false,- error_code: None,- error_message: None,+ error_code: Some(error.code),+ error_message: Some(error.message),payout_connector_metadata: None,}),..item.data- })- } else {- let error = reply- .error- .ok_or(errors::ConnectorError::UnexpectedResponseError(- bytes::Bytes::from("Missing reply.error".to_string()),- ))?;- Ok(Self {- status: common_enums::AttemptStatus::Failure,+ }),+ (None, Some(ok_status)) => Ok(Self {response: Ok(PayoutsResponseData {- status: Some(enums::PayoutStatus::from(PayoutOutcome::Error)),- connector_payout_id: None,+ status: Some(enums::PayoutStatus::from(PayoutOutcome::CancelReceived)),+ connector_payout_id: ok_status.refund_received.map(|id| id.order_code),payout_eligible: None,should_add_next_step_to_process_tracker: false,- error_code: Some(error.code),- error_message: Some(error.message),+ error_code: None,+ error_message: None,payout_connector_metadata: None,}),..item.data- })+ }),+ _ => Err(+ errors::ConnectorError::UnexpectedResponseError(bytes::Bytes::from(+ "Either reply.error or reply.ok must be present in the response",+ ))+ .into(),+ ),}}}@@ -1511,20 +1522,6 @@ fn validate_reply(reply: &Reply) -> Result<(), errors::ConnectorError> {}}-fn validate_payout_reply(reply: &PayoutReply) -> Result<(), errors::ConnectorError> {- if (reply.error.is_some() && reply.ok.is_some())- || (reply.error.is_none() && reply.ok.is_none())- {- Err(errors::ConnectorError::UnexpectedResponseError(- bytes::Bytes::from(- "Either reply.error_data or reply.ok must be present in the response".to_string(),- ),- ))- } else {- Ok(())- }-}-fn validate_order_status(order_status: &OrderStatus) -> Result<(), errors::ConnectorError> {if (order_status.payment.is_some() && order_status.error.is_some())|| (order_status.payment.is_none() && order_status.error.is_none())@@ -1582,6 +1579,7 @@ fn process_payment_response(}}+#[cfg(feature = "payouts")]pub fn map_purpose_code(value: Option<String>) -> Option<String> {let code = match value?.as_str() {"Family Support" => "00",diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rsindex 8577ef1fbf..66a1811f06 100644--- a/crates/router/src/core/payment_methods/cards.rs+++ b/crates/router/src/core/payment_methods/cards.rs@@ -4954,6 +4954,7 @@ impl TempLockerCardSupport {.clone().expose_option().unwrap_or_default();+ let card_network = card.card_network.clone();let value1 = payment_methods::mk_card_value1(card_number,card_exp_year,@@ -4962,6 +4963,7 @@ impl TempLockerCardSupport {None,None,None,+ card_network,).change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Error getting Value1 for locker")?;diff --git a/crates/router/src/core/payment_methods/transformers.rs b/crates/router/src/core/payment_methods/transformers.rsindex 8b0516c08e..d7a6eeaf2c 100644--- a/crates/router/src/core/payment_methods/transformers.rs+++ b/crates/router/src/core/payment_methods/transformers.rs@@ -2,6 +2,7 @@ pub use ::payment_methods::controller::{DataDuplicationCheck, DeleteCardResp};#[cfg(feature = "v2")]use api_models::payment_methods::PaymentMethodResponseItem;use api_models::{enums as api_enums, payment_methods::Card};+use common_enums::CardNetwork;use common_utils::{ext_traits::{Encode, StringExt},id_type,@@ -862,6 +863,7 @@ pub fn mk_crud_locker_request(Ok(request)}+#[allow(clippy::too_many_arguments)]pub fn mk_card_value1(card_number: cards::CardNumber,exp_year: String,@@ -870,6 +872,7 @@ pub fn mk_card_value1(nickname: Option<String>,card_last_four: Option<String>,card_token: Option<String>,+ card_network: Option<CardNetwork>,) -> CustomResult<String, errors::VaultError> {let value1 = api::TokenizedCardValue1 {card_number: card_number.peek().clone(),@@ -879,6 +882,7 @@ pub fn mk_card_value1(nickname,card_last_four,card_token,+ card_network,};let value1_req = value1.encode_to_string_of_json()diff --git a/crates/router/src/core/payment_methods/vault.rs b/crates/router/src/core/payment_methods/vault.rsindex c53704a430..61b39523ec 100644--- a/crates/router/src/core/payment_methods/vault.rs+++ b/crates/router/src/core/payment_methods/vault.rs@@ -486,6 +486,7 @@ impl Vaultable for api::CardPayout {nickname: None,card_last_four: None,card_token: None,+ card_network: self.card_network.clone(),};value1@@ -534,7 +535,7 @@ impl Vaultable for api::CardPayout {expiry_month: value1.exp_month.into(),expiry_year: value1.exp_year.into(),card_holder_name: value1.name_on_card.map(masking::Secret::new),- card_network: None,+ card_network: value1.card_network,};let supp_data = SupplementaryVaultData {@@ -561,6 +562,7 @@ pub struct TokenizedWalletSensitiveValues {#[derive(Debug, serde::Serialize, serde::Deserialize)]pub struct TokenizedWalletInsensitiveValues {pub customer_id: Option<id_type::CustomerId>,+ pub card_network: Option<common_enums::CardNetwork>,}#[cfg(feature = "payouts")]@@ -612,7 +614,20 @@ impl Vaultable for api::WalletPayout {&self,customer_id: Option<id_type::CustomerId>,) -> CustomResult<String, errors::VaultError> {- let value2 = TokenizedWalletInsensitiveValues { customer_id };+ let value2 = match self {+ Self::Paypal(_paypal_data) => TokenizedWalletInsensitiveValues {+ customer_id,+ card_network: None,+ },+ Self::Venmo(_venmo_data) => TokenizedWalletInsensitiveValues {+ customer_id,+ card_network: None,+ },+ Self::ApplePayDecrypt(apple_pay_decrypt_data) => TokenizedWalletInsensitiveValues {+ customer_id,+ card_network: apple_pay_decrypt_data.card_network.clone(),+ },+ };value2.encode_to_string_of_json()@@ -651,7 +666,7 @@ impl Vaultable for api::WalletPayout {expiry_month,expiry_year,card_holder_name: value1.card_holder_name,- card_network: None,+ card_network: value2.card_network,})}_ => Err(errors::VaultError::ResponseDeserializationFailed)?,