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: filter out payment_methods which does not support mandates during list api call (#1318)
Chethan Rao07aef53a5cd4
diff --git a/config/config.example.toml b/config/config.example.tomlindex 4f54a4a6c2..06ff14b981 100644--- a/config/config.example.toml+++ b/config/config.example.toml@@ -290,6 +290,10 @@ refund_tolerance = 100 # Fake delay tolerance for dummy connector refurefund_retrieve_duration = 500 # Fake delay duration for dummy connector refund syncrefund_retrieve_tolerance = 100 # Fake delay tolerance for dummy connector refund sync+[mandates.supported_payment_methods]+card.credit = {connector_list = "stripe,adyen"} # Mandate supported payment method type and connector for card+pay_later.klarna = {connector_list = "adyen"} # Mandate supported payment method type and connector for pay_later+# Required fields info used while listing the payment_method_data[required_fields.pay_later] # payment_method = "pay_later"afterpay_clearpay = {fields = {stripe = [ # payment_method_type = afterpay_clearpay, connector = "stripe"diff --git a/config/development.toml b/config/development.tomlindex ce57de8ec9..05450f5145 100644--- a/config/development.toml+++ b/config/development.toml@@ -307,5 +307,12 @@ refund_retrieve_tolerance = 100[delayed_session_response]connectors_with_delayed_session_response = "trustpay"+[mandates.supported_payment_methods]+pay_later.klarna = {connector_list = "adyen"}+wallet.google_pay = {connector_list = "stripe,adyen"}+wallet.apple_pay = {connector_list = "stripe,adyen"}+card.credit = {connector_list = "stripe,adyen,authorizedotnet,globalpay,worldpay,multisafepay,nmi,nexinets,noon"}+card.debit = {connector_list = "stripe,adyen,authorizedotnet,globalpay,worldpay,multisafepay,nmi,nexinets,noon"}+[connector_request_reference_id_config]merchant_ids_send_payment_id_as_connector_request_id = []diff --git a/crates/router/src/configs/defaults.rs b/crates/router/src/configs/defaults.rsindex b6c63c4950..2b566d2664 100644--- a/crates/router/src/configs/defaults.rs+++ b/crates/router/src/configs/defaults.rs@@ -1,4 +1,4 @@-use std::collections::HashMap;+use std::collections::{HashMap, HashSet};use api_models::{enums, payment_methods::RequiredFieldInfo};@@ -143,6 +143,89 @@ impl Default for super::settings::DrainerSettings {}}+use super::settings::{+ Mandates, SupportedConnectorsForMandate, SupportedPaymentMethodTypesForMandate,+ SupportedPaymentMethodsForMandate,+};++impl Default for Mandates {+ fn default() -> Self {+ Self {+ supported_payment_methods: SupportedPaymentMethodsForMandate(HashMap::from([+ (+ enums::PaymentMethod::PayLater,+ SupportedPaymentMethodTypesForMandate(HashMap::from([(+ enums::PaymentMethodType::Klarna,+ SupportedConnectorsForMandate {+ connector_list: HashSet::from([enums::Connector::Adyen]),+ },+ )])),+ ),+ (+ enums::PaymentMethod::Wallet,+ SupportedPaymentMethodTypesForMandate(HashMap::from([+ (+ enums::PaymentMethodType::GooglePay,+ SupportedConnectorsForMandate {+ connector_list: HashSet::from([+ enums::Connector::Stripe,+ enums::Connector::Adyen,+ ]),+ },+ ),+ (+ enums::PaymentMethodType::ApplePay,+ SupportedConnectorsForMandate {+ connector_list: HashSet::from([+ enums::Connector::Stripe,+ enums::Connector::Adyen,+ ]),+ },+ ),+ ])),+ ),+ (+ enums::PaymentMethod::Card,+ SupportedPaymentMethodTypesForMandate(HashMap::from([+ (+ enums::PaymentMethodType::Credit,+ SupportedConnectorsForMandate {+ connector_list: HashSet::from([+ enums::Connector::Stripe,+ enums::Connector::Adyen,+ enums::Connector::Authorizedotnet,+ enums::Connector::Globalpay,+ enums::Connector::Worldpay,+ enums::Connector::Multisafepay,+ enums::Connector::Nmi,+ enums::Connector::Nexinets,+ enums::Connector::Noon,+ ]),+ },+ ),+ (+ enums::PaymentMethodType::Debit,+ SupportedConnectorsForMandate {+ connector_list: HashSet::from([+ enums::Connector::Stripe,+ enums::Connector::Adyen,+ enums::Connector::Authorizedotnet,+ enums::Connector::Globalpay,+ enums::Connector::Worldpay,+ enums::Connector::Multisafepay,+ enums::Connector::Nmi,+ enums::Connector::Nexinets,+ enums::Connector::Noon,+ ]),+ },+ ),+ ])),+ ),+ ])),+ }+ }+}+impl Default for super::settings::RequiredFields {fn default() -> Self {Self(HashMap::from([diff --git a/crates/router/src/configs/settings.rs b/crates/router/src/configs/settings.rsindex a0b2f62c79..f629ab4283 100644--- a/crates/router/src/configs/settings.rs+++ b/crates/router/src/configs/settings.rs@@ -83,6 +83,7 @@ pub struct Settings {pub dummy_connector: DummyConnector,#[cfg(feature = "email")]pub email: EmailSettings,+ pub mandates: Mandates,pub required_fields: RequiredFields,pub delayed_session_response: DelayedSessionConfig,pub connector_request_reference_id_config: ConnectorRequestReferenceIdConfig,@@ -127,6 +128,27 @@ pub struct DummyConnector {pub refund_retrieve_tolerance: u64,}+#[derive(Debug, Deserialize, Clone)]+pub struct Mandates {+ pub supported_payment_methods: SupportedPaymentMethodsForMandate,+}++#[derive(Debug, Deserialize, Clone)]+pub struct SupportedPaymentMethodsForMandate(+ pub HashMap<enums::PaymentMethod, SupportedPaymentMethodTypesForMandate>,+);++#[derive(Debug, Deserialize, Clone)]+pub struct SupportedPaymentMethodTypesForMandate(+ pub HashMap<enums::PaymentMethodType, SupportedConnectorsForMandate>,+);++#[derive(Debug, Deserialize, Clone)]+pub struct SupportedConnectorsForMandate {+ #[serde(deserialize_with = "connector_deser")]+ pub connector_list: HashSet<api_models::enums::Connector>,+}+#[derive(Debug, Deserialize, Clone, Default)]pub struct PaymentMethodTokenFilter {#[serde(deserialize_with = "pm_deser")]diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rsindex 90eb2168bf..36b58a1476 100644--- a/crates/router/src/core/payment_methods/cards.rs+++ b/crates/router/src/core/payment_methods/cards.rs@@ -861,6 +861,7 @@ pub async fn list_payment_methods(address.as_ref(),mca.connector_name,pm_config_mapping,+ &state.conf.mandates.supported_payment_methods,).await?;}@@ -1224,6 +1225,7 @@ pub async fn filter_payment_methods(address: Option<&domain::Address>,connector: String,config: &settings::ConnectorFilters,+ supported_payment_methods_for_mandate: &settings::SupportedPaymentMethodsForMandate,) -> errors::CustomResult<(), errors::ApiErrorResponse> {for payment_method in payment_methods.into_iter() {let parse_result = serde_json::from_value::<PaymentMethodsEnabled>(payment_method);@@ -1303,6 +1305,27 @@ pub async fn filter_payment_methods(&payment_method_object.payment_method_type,);+ let connector_variant = api_enums::Connector::from_str(connector.as_str())+ .into_report()+ .change_context(errors::ConnectorError::InvalidConnectorName)+ .change_context(errors::ApiErrorResponse::InvalidDataValue {+ field_name: "connector",+ })+ .attach_printable_lazy(|| {+ format!("unable to parse connector name {connector:?}")+ })?;+ let filter7 = payment_attempt+ .and_then(|attempt| attempt.mandate_details.as_ref())+ .map(|_mandate_details| {+ filter_pm_based_on_supported_payments_for_mandate(+ supported_payment_methods_for_mandate,+ &payment_method,+ &payment_method_object.payment_method_type,+ connector_variant,+ )+ })+ .unwrap_or(true);+let connector = connector.clone();let response_pm_type = ResponsePaymentMethodIntermediate::new(@@ -1311,7 +1334,7 @@ pub async fn filter_payment_methods(payment_method,);- if filter && filter2 && filter3 && filter4 && filter5 && filter6 {+ if filter && filter2 && filter3 && filter4 && filter5 && filter6 && filter7 {resp.push(response_pm_type);}}@@ -1321,6 +1344,20 @@ pub async fn filter_payment_methods(Ok(())}+fn filter_pm_based_on_supported_payments_for_mandate(+ supported_payment_methods_for_mandate: &settings::SupportedPaymentMethodsForMandate,+ payment_method: &api_enums::PaymentMethod,+ payment_method_type: &api_enums::PaymentMethodType,+ connector: api_enums::Connector,+) -> bool {+ supported_payment_methods_for_mandate+ .0+ .get(payment_method)+ .and_then(|payment_method_type_hm| payment_method_type_hm.0.get(payment_method_type))+ .map(|supported_connectors| supported_connectors.connector_list.contains(&connector))+ .unwrap_or(false)+}+fn filter_pm_based_on_config<'a>(config: &'a crate::configs::settings::ConnectorFilters,connector: &'a str,