Skip to content

Commit

Permalink
feat(payment_methods): pass field in apple pay seesion call based on …
Browse files Browse the repository at this point in the history
…config
  • Loading branch information
ShankarSinghC committed May 9, 2024
1 parent 1d42b63 commit c62bddc
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 0 deletions.
27 changes: 27 additions & 0 deletions crates/api_models/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,13 @@ pub enum FieldType {
UserBillingAddressPincode,
UserBillingAddressState,
UserBillingAddressCountry { options: Vec<String> },
UserShippingName,
UserShippingAddressLine1,
UserShippingAddressLine2,
UserShippingAddressCity,
UserShippingAddressPincode,
UserShippingAddressState,
UserShippingAddressCountry { options: Vec<String> },
UserBlikCode,
UserBank,
Text,
Expand All @@ -437,6 +444,17 @@ impl FieldType {
Self::UserBillingAddressCountry { options: vec![] },
]
}
pub fn get_shipping_variants() -> Vec<Self> {
vec![
Self::UserShippingName,
Self::UserShippingAddressLine1,
Self::UserShippingAddressLine2,
Self::UserShippingAddressCity,
Self::UserShippingAddressPincode,
Self::UserShippingAddressState,
Self::UserShippingAddressCountry { options: vec![] },
]
}
}

impl PartialEq for FieldType {
Expand Down Expand Up @@ -475,6 +493,15 @@ impl PartialEq for FieldType {
(Self::UserBillingAddressCountry { .. }, Self::UserBillingAddressCountry { .. }) => {
true
}
(Self::UserShippingName, Self::UserShippingName) => true,
(Self::UserShippingAddressLine1, Self::UserShippingAddressLine1) => true,
(Self::UserShippingAddressLine2, Self::UserShippingAddressLine2) => true,
(Self::UserShippingAddressCity, Self::UserShippingAddressCity) => true,
(Self::UserShippingAddressPincode, Self::UserShippingAddressPincode) => true,
(Self::UserShippingAddressState, Self::UserShippingAddressState) => true,
(Self::UserShippingAddressCountry { .. }, Self::UserShippingAddressCountry { .. }) => {
true
}
(Self::UserBlikCode, Self::UserBlikCode) => true,
(Self::UserBank, Self::UserBank) => true,
(Self::Text, Self::Text) => true,
Expand Down
2 changes: 2 additions & 0 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4201,6 +4201,8 @@ pub struct ApplePayPaymentRequest {
pub merchant_identifier: Option<String>,
/// The required billing contacht fields for connector
pub required_billing_contact_fields: Option<Vec<String>>,
/// The required shipping contacht fields for connector
pub required_shipping_contact_fields: Option<Vec<String>>,
}

#[derive(Debug, Clone, Eq, PartialEq, serde::Serialize, ToSchema, serde::Deserialize)]
Expand Down
134 changes: 134 additions & 0 deletions crates/router/src/configs/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5855,6 +5855,73 @@ impl Default for super::settings::RequiredFields {
value: None,
}
),
(
"shipping.address.first_name".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.billing.address.first_name".to_string(),
display_name: "billing_first_name".to_string(),
field_type: enums::FieldType::UserShippingName,
value: None,
}
),
(
"shipping.address.last_name".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.last_name".to_string(),
display_name: "shipping_last_name".to_string(),
field_type: enums::FieldType::UserShippingName,
value: None,
}
),
(
"shipping.address.city".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.city".to_string(),
display_name: "city".to_string(),
field_type: enums::FieldType::UserShippingAddressCity,
value: None,
}
),
(
"shipping.address.state".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.state".to_string(),
display_name: "state".to_string(),
field_type: enums::FieldType::UserShippingAddressState,
value: None,
}
),
(
"shipping.address.zip".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.zip".to_string(),
display_name: "zip".to_string(),
field_type: enums::FieldType::UserShippingAddressPincode,
value: None,
}
),
(
"shipping.address.country".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.country".to_string(),
display_name: "country".to_string(),
field_type: enums::FieldType::UserShippingAddressCountry{
options: vec![
"ALL".to_string(),
]
},
value: None,
}
),
(
"shipping.address.line1".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.line1".to_string(),
display_name: "line1".to_string(),
field_type: enums::FieldType::UserShippingAddressLine1,
value: None,
}
),
]
),
common: HashMap::new(),
Expand Down Expand Up @@ -5942,6 +6009,73 @@ impl Default for super::settings::RequiredFields {
value: None,
}
),
(
"shipping.address.first_name".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.billing.address.first_name".to_string(),
display_name: "billing_first_name".to_string(),
field_type: enums::FieldType::UserShippingName,
value: None,
}
),
(
"shipping.address.last_name".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.last_name".to_string(),
display_name: "shipping_last_name".to_string(),
field_type: enums::FieldType::UserShippingName,
value: None,
}
),
(
"shipping.address.city".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.city".to_string(),
display_name: "city".to_string(),
field_type: enums::FieldType::UserShippingAddressCity,
value: None,
}
),
(
"shipping.address.state".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.state".to_string(),
display_name: "state".to_string(),
field_type: enums::FieldType::UserShippingAddressState,
value: None,
}
),
(
"shipping.address.zip".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.zip".to_string(),
display_name: "zip".to_string(),
field_type: enums::FieldType::UserShippingAddressPincode,
value: None,
}
),
(
"shipping.address.country".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.country".to_string(),
display_name: "country".to_string(),
field_type: enums::FieldType::UserShippingAddressCountry{
options: vec![
"ALL".to_string(),
]
},
value: None,
}
),
(
"shipping.address.line1".to_string(),
RequiredFieldInfo {
required_field: "payment_method_data.shipping.address.line1".to_string(),
display_name: "line1".to_string(),
field_type: enums::FieldType::UserShippingAddressLine1,
value: None,
}
),
]
),
common: HashMap::new(),
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/connector/bluesnap/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ impl TryFrom<types::PaymentsSessionResponseRouterData<BluesnapWalletTokenRespons
supported_networks: Some(payment_request_data.supported_networks),
merchant_identifier: Some(session_token_data.merchant_identifier),
required_billing_contact_fields: None,
required_shipping_contact_fields: None,
}),
connector: "bluesnap".to_string(),
delayed_session_token: false,
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/connector/payme/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ impl<F>
supported_networks: None,
merchant_identifier: None,
required_billing_contact_fields: None,
required_shipping_contact_fields: None,
},
),
connector: "payme".to_string(),
Expand Down
1 change: 1 addition & 0 deletions crates/router/src/connector/trustpay/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,7 @@ pub fn get_apple_pay_session<F, T>(
total: apple_pay_init_result.total.into(),
merchant_identifier: None,
required_billing_contact_fields: None,
required_shipping_contact_fields: None,
}),
connector: "trustpay".to_string(),
delayed_session_token: true,
Expand Down
29 changes: 29 additions & 0 deletions crates/router/src/core/payments/flows/session_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,32 @@ async fn create_applepay_session_token(
}
});

let required_shipping_contact_fields =
if business_profile.collect_shipping_details_from_wallet_connector == Some(true) {
let shipping_variants = enums::FieldType::get_shipping_variants();

is_dynamic_fields_required(
state,
enums::PaymentMethod::Wallet,
enums::PaymentMethodType::ApplePay,
&connector.connector_name,
shipping_variants,
)
.and_then(|required_shippiing_contact_fields| {
if required_shippiing_contact_fields {
Some(vec![
"postalAddress".to_string(),
"phone".to_string(),
"email".to_string(),
])
} else {
None
}
})
} else {
None
};

// Get apple pay payment request
let applepay_payment_request = get_apple_pay_payment_request(
amount_info,
Expand All @@ -321,6 +347,7 @@ async fn create_applepay_session_token(
apple_pay_session_request.merchant_identifier.as_str(),
merchant_business_country,
required_billing_contact_fields,
required_shipping_contact_fields,
)?;

let applepay_session_request = build_apple_pay_session_request(
Expand Down Expand Up @@ -422,6 +449,7 @@ fn get_apple_pay_payment_request(
merchant_identifier: &str,
merchant_business_country: Option<api_models::enums::CountryAlpha2>,
required_billing_contact_fields: Option<Vec<String>>,
required_shipping_contact_fields: Option<Vec<String>>,
) -> RouterResult<payment_types::ApplePayPaymentRequest> {
let applepay_payment_request = payment_types::ApplePayPaymentRequest {
country_code: merchant_business_country.or(session_data.country).ok_or(
Expand All @@ -435,6 +463,7 @@ fn get_apple_pay_payment_request(
supported_networks: Some(payment_request_data.supported_networks),
merchant_identifier: Some(merchant_identifier.to_string()),
required_billing_contact_fields,
required_shipping_contact_fields,
};
Ok(applepay_payment_request)
}
Expand Down

0 comments on commit c62bddc

Please sign in to comment.