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
feat(connector): [Worldpay/Worldpayxml] pass additional Mastercard authentication fields (#12874)
DEEPANSHU BANSAL9e4405534f15
diff --git a/crates/hyperswitch_connectors/src/connectors/worldpay/requests.rs b/crates/hyperswitch_connectors/src/connectors/worldpay/requests.rsindex b20aa2afa3..976d00ed67 100644--- a/crates/hyperswitch_connectors/src/connectors/worldpay/requests.rs+++ b/crates/hyperswitch_connectors/src/connectors/worldpay/requests.rs@@ -1,3 +1,4 @@+use common_utils::pii;use hyperswitch_masking::Secret;use serde::{Deserialize, Serialize};@@ -38,6 +39,13 @@ pub struct Instruction {pub token_creation: Option<TokenCreation>,/// For specifying CIT vs MITpub customer_agreement: Option<CustomerAgreement>,+ /// Additional Mastercard authentication data (shopper email, phone number+ /// and IP address), sent whenever available.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub customer: Option<InstructionCustomer>,+ /// Shipping details used for Mastercard authentication / risk assessment.+ #[serde(skip_serializing_if = "Option::is_none")]+ pub shipping: Option<Shipping>,}#[derive(Clone, Debug, PartialEq, Serialize)]@@ -161,6 +169,45 @@ pub struct BillingAddress {pub country_code: common_enums::CountryAlpha2,}+/// Additional Mastercard authentication data, sent under `instruction.customer`+/// whenever available.+#[derive(Clone, Debug, PartialEq, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct InstructionCustomer {+ #[serde(skip_serializing_if = "Option::is_none")]+ pub email: Option<pii::Email>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub phone: Option<Secret<String>>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub ip_address: Option<Secret<String, pii::IpAddress>>,+}++/// Shipping details, sent under `instruction.shipping` whenever available.+#[derive(Clone, Debug, PartialEq, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct Shipping {+ #[serde(skip_serializing_if = "Option::is_none")]+ pub first_name: Option<Secret<String>>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub last_name: Option<Secret<String>>,+ pub address: ShippingAddress,+}++#[derive(Clone, Debug, PartialEq, Serialize)]+#[serde(rename_all = "camelCase")]+pub struct ShippingAddress {+ pub address1: Secret<String>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub address2: Option<Secret<String>>,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub address3: Option<Secret<String>>,+ pub city: String,+ #[serde(skip_serializing_if = "Option::is_none")]+ pub state: Option<Secret<String>>,+ pub postal_code: Secret<String>,+ pub country_code: common_enums::CountryAlpha2,+}+#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]#[serde(rename_all = "camelCase")]pub struct Customer {diff --git a/crates/hyperswitch_connectors/src/connectors/worldpay/transformers.rs b/crates/hyperswitch_connectors/src/connectors/worldpay/transformers.rsindex 88f248e001..4d6624badb 100644--- a/crates/hyperswitch_connectors/src/connectors/worldpay/transformers.rs+++ b/crates/hyperswitch_connectors/src/connectors/worldpay/transformers.rs@@ -85,7 +85,9 @@ fn fetch_payment_instrument(card_number: card.card_number,},cvc: card.card_cvc,- card_holder_name: billing_address.and_then(|address| address.get_optional_full_name()),+ card_holder_name: billing_address+ .and_then(|address| address.get_optional_full_name())+ .map(normalize_cardholder_name),billing_address: billing_address.and_then(|addr| addr.address.clone()).and_then(|address| {@@ -263,6 +265,10 @@ trait WorldpayPaymentsRequestData {fn get_payment_method_type(&self) -> Option<enums::PaymentMethodType>;fn get_connector_request_reference_id(&self) -> String;fn get_is_mandate_payment(&self) -> bool;+ fn get_optional_email(&self) -> Option<pii::Email>;+ fn get_optional_phone_number(&self) -> Option<Secret<String>>;+ fn get_optional_shipping_address(&self) -> Option<&address::Address>;+ fn get_optional_ip_address(&self) -> Option<Secret<String, pii::IpAddress>>;fn get_settlement_info(&self, _amount: i64) -> Option<AutoSettlement> {None}@@ -326,6 +332,30 @@ impl WorldpayPaymentsRequestDatafn get_is_mandate_payment(&self) -> bool {true}++ fn get_optional_email(&self) -> Option<pii::Email> {+ self.request+ .clone()+ .or_else(|| self.get_optional_billing_email())+ }++ fn get_optional_phone_number(&self) -> Option<Secret<String>> {+ self.get_optional_billing_phone_number()+ .or_else(|| self.get_optional_shipping_phone_number())+ }++ fn get_optional_shipping_address(&self) -> Option<&address::Address> {+ self.get_optional_shipping()+ }++ fn get_optional_ip_address(&self) -> Option<Secret<String, pii::IpAddress>> {+ self.request+ .browser_info+ .as_ref()+ .and_then(|browser_info| browser_info.ip_address)+ .map(|ip| Secret::new(ip.to_string()))+ }}impl WorldpayPaymentsRequestData@@ -387,6 +417,30 @@ impl WorldpayPaymentsRequestDataself.request.is_mandate_payment()}+ fn get_optional_email(&self) -> Option<pii::Email> {+ self.request+ .clone()+ .or_else(|| self.get_optional_billing_email())+ }++ fn get_optional_phone_number(&self) -> Option<Secret<String>> {+ self.get_optional_billing_phone_number()+ .or_else(|| self.get_optional_shipping_phone_number())+ }++ fn get_optional_shipping_address(&self) -> Option<&address::Address> {+ self.get_optional_shipping()+ }++ fn get_optional_ip_address(&self) -> Option<Secret<String, pii::IpAddress>> {+ self.request+ .browser_info+ .as_ref()+ .and_then(|browser_info| browser_info.ip_address)+ .map(|ip| Secret::new(ip.to_string()))+ }+fn get_settlement_info(&self, amount: i64) -> Option<AutoSettlement> {match (self.request.capture_method.unwrap_or_default(), amount) {(_, 0) => None,@@ -400,6 +454,74 @@ impl WorldpayPaymentsRequestData}}+// Mastercard requires the cardholder name to contain only English (ASCII)+// characters and to match the name exactly as printed on the card. Accented+// characters are transliterated to their closest ASCII equivalent.+fn normalize_cardholder_name(name: Secret<String>) -> Secret<String> {+ Secret::new(unidecode::unidecode(&name.expose()))+}++// Worldpay's `instruction.customer.phone` must contain digits only, so any+// formatting characters (e.g. a leading `+` carried over from the country code)+// are stripped. Returns `None` if nothing numeric remains.+fn normalize_phone_number(phone: Secret<String>) -> Option<Secret<String>> {+ let digits: String = phone+ .expose()+ .chars()+ .filter(|c| c.is_ascii_digit())+ .collect();+ (!digits.is_empty()).then(|| Secret::new(digits))+}++// Dangling helper function to build the optional `instruction.customer` object+// holding the additional Mastercard authentication data (email, phone number+// and IP address). Returns `None` when none of the fields are available so the+// object is omitted entirely from the request.+fn create_instruction_customer(+ email: Option<pii::Email>,+ phone: Option<Secret<String>>,+ ip_address: Option<Secret<String, pii::IpAddress>>,+) -> Option<InstructionCustomer> {+ if email.is_some() || phone.is_some() || ip_address.is_some() {+ Some(InstructionCustomer {+ email,+ phone,+ ip_address,+ })+ } else {+ None+ }+}++// Dangling helper function to build the optional `instruction.shipping` object.+// Worldpay requires the shipping address to contain at least address line 1,+// city, postal code and country, so the object is omitted unless all of these+// are present.+fn create_shipping(shipping_address: Option<&address::Address>) -> Option<Shipping> {+ shipping_address+ .and_then(|addr| addr.address.clone())+ .and_then(+ |address| match (address.line1, address.city, address.zip, address.country) {+ (Some(address1), Some(city), Some(postal_code), Some(country_code)) => {+ Some(Shipping {+ first_name: address.first_name,+ last_name: address.last_name,+ address: ShippingAddress {+ address1,+ address2: address.line2,+ address3: address.line3,+ city,+ state: address.state,+ postal_code,+ country_code,+ },+ })+ }+ _ => None,+ },+ )+}+// Dangling helper function to create ThreeDS requestfn create_three_ds_request<T: WorldpayPaymentsRequestData>(router_data: &T,@@ -544,6 +666,16 @@ impl<T: WorldpayPaymentsRequestData> TryFrom<(&WorldpayRouterData<&T>, &Secret<Sitem.router_data.get_mandate_id(),);+ // Additional Mastercard authentication data, forwarded whenever available.+ let customer = create_instruction_customer(+ item.router_data.get_optional_email(),+ item.router_data+ .get_optional_phone_number()+ .and_then(normalize_phone_number),+ item.router_data.get_optional_ip_address(),+ );+ let shipping = create_shipping(item.router_data.get_optional_shipping_address());+Ok(Self {instruction: Instruction {settlement: item.router_data.get_settlement_info(item.amount),@@ -567,6 +699,8 @@ impl<T: WorldpayPaymentsRequestData> TryFrom<(&WorldpayRouterData<&T>, &Secret<Sthree_ds,token_creation,customer_agreement,+ customer,+ shipping,},merchant: Merchant {entity: entity_id.clone(),diff --git a/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rs b/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rsindex e720200c5d..a61bb4ebab 100644--- a/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rs+++ b/crates/hyperswitch_connectors/src/connectors/worldpayxml/transformers.rs@@ -51,7 +51,7 @@ use crate::{utils::{self as connector_utils, AddressDetailsData, BrowserInformationData, CardData,ForeignTryFrom, PaymentsAuthorizeRequestData, PaymentsCompleteAuthorizeRequestData,- PaymentsSyncRequestData, RouterData as _,+ PaymentsSyncRequestData, PhoneDetailsData, RouterData as _,},};@@ -461,6 +461,8 @@ struct WorldpayxmlAddressData {#[serde(skip_serializing_if = "Option::is_none")]state: Option<Secret<String>>,country_code: common_enums::CountryAlpha2,+ #[serde(skip_serializing_if = "Option::is_none")]+ telephone_number: Option<Secret<String>>,}#[derive(Debug, Serialize, Deserialize)]@@ -572,7 +574,7 @@ enum PaymentMethod {CardSSL(CardSSL),#[serde(rename = "FF_DISBURSE-SSL")]- FastAccessSSL(FastAccessData),+ FastAccessSSL(Box<FastAccessData>),#[serde(rename = "PAYWITHGOOGLE-SSL")]PayWithGoogleSSL(GooglePayData),@@ -777,7 +779,10 @@ impl TryFrom<(&Card, Option<enums::CaptureMethod>, Option<Session>)> for Paymentyear: card_data.get_expiry_year_4_digit(),},},- card_holder_name: card_data.card_holder_name.to_owned(),+ card_holder_name: card_data+ .card_holder_name+ .as_ref()+ .map(|name| normalize_cardholder_name(name.clone())),cvc: Some(card_data.card_cvc.to_owned()),}),session,@@ -1132,6 +1137,13 @@ impl}}+// Mastercard requires the cardholder name to contain only English (ASCII)+// characters and to match the name exactly as printed on the card. Accented+// characters are transliterated to their closest ASCII equivalent.+fn normalize_cardholder_name(name: Secret<String>) -> Secret<String> {+ Secret::new(unidecode::unidecode(&name.expose()))+}+fn get_address_details(data: &Address) -> Option<WorldpayxmlPayinAddress> {let address1_option = data.address@@ -1149,6 +1161,12 @@ fn get_address_details(data: &Address) -> Option<WorldpayxmlPayinAddress> {.address.as_ref().and_then(|address| address.get_optional_city());+ let telephone_number = data.phone.as_ref().and_then(|phone| {+ phone+ .get_number_with_country_code()+ .or_else(|_| phone.get_number())+ .ok()+ });if let (Some(address1), Some(postal_code), Some(country_code), Some(city), Some(address_data)) = (address1_option,@@ -1168,6 +1186,7 @@ fn get_address_details(data: &Address) -> Option<WorldpayxmlPayinAddress> {city,state: address_data.get_optional_state(),country_code,+ telephone_number,},})} else {@@ -3052,13 +3071,13 @@ impl TryFrom<&WorldpayxmlRouterData<&PayoutsRouterData<PoFulfill>>> for PaymentSlet payment_details = PaymentDetails {action: None,- payment_method: PaymentMethod::FastAccessSSL(FastAccessData {+ payment_method: PaymentMethod::FastAccessSSL(Box::new(FastAccessData {recipient: Recipient {payment_instrument,address,},purpose_of_payment: purpose_of_payment_code,- }),+ })),session: None,stored_credentials: None,};