From 36cc13d44bb61b840195e1a24f1bebdb0115d13b Mon Sep 17 00:00:00 2001 From: Prajjwal Kumar Date: Wed, 3 May 2023 16:10:05 +0530 Subject: [PATCH] feat(pm_list): add available capture methods filter (#999) Co-authored-by: Arun Raj M --- config/development.toml | 24 ++++++++++++++ crates/router/src/configs/settings.rs | 11 +++++-- .../router/src/core/payment_methods/cards.rs | 31 +++++++++++++++++-- 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/config/development.toml b/config/development.toml index 07a005ae9a23..7ba940bc5827 100644 --- a/config/development.toml +++ b/config/development.toml @@ -177,9 +177,33 @@ ideal = { country = "NL", currency = "EUR" } [pm_filters.braintree] paypal = { currency = "AUD,BRL,CAD,CNY,CZK,DKK,EUR,HKD,HUF,ILS,JPY,MYR,MXN,TWD,NZD,NOK,PHP,PLN,GBP,RUB,SGD,SEK,CHF,THB,USD" } +credit = { not_available_flows = {capture_method="manual"} } +debit = { not_available_flows = {capture_method="manual"} } [pm_filters.klarna] klarna = { country = "AU,AT,BE,CA,CZ,DK,FI,FR,DE,GR,IE,IT,NL,NZ,NO,PL,PT,ES,SE,CH,GB,US", currency = "AUD,EUR,EUR,CAD,CZK,DKK,EUR,EUR,EUR,EUR,EUR,EUR,EUR,NZD,NOK,PLN,EUR,EUR,SEK,CHF,GBP,USD" } +credit = { not_available_flows = {capture_method="manual"} } +debit = { not_available_flows = {capture_method="manual"} } + +[pm_filters.zen] +credit = { not_available_flows = {capture_method="manual"} } +debit = { not_available_flows = {capture_method="manual"} } + +[pm_filters.aci] +credit = { not_available_flows = {capture_method="manual"} } +debit = { not_available_flows = {capture_method="manual"} } + +[pm_filters.mollie] +credit = { not_available_flows = {capture_method="manual"} } +debit = { not_available_flows = {capture_method="manual"} } + +[pm_filters.multisafepay] +credit = { not_available_flows = {capture_method="manual"} } +debit = { not_available_flows = {capture_method="manual"} } + +[pm_filters.trustpay] +credit = { not_available_flows = {capture_method="manual"} } +debit = { not_available_flows = {capture_method="manual"} } [pm_filters.authorizedotnet] google_pay = { currency = "CHF,DKK,EUR,GBP,NOK,PLN,SEK,USD,AUD,NZD,CAD" } diff --git a/crates/router/src/configs/settings.rs b/crates/router/src/configs/settings.rs index 243b05e2c453..2a95d3b3fd34 100644 --- a/crates/router/src/configs/settings.rs +++ b/crates/router/src/configs/settings.rs @@ -4,6 +4,7 @@ use std::{ str::FromStr, }; +use api_models::enums; use common_utils::ext_traits::ConfigExt; use config::{Environment, File}; #[cfg(feature = "kms")] @@ -133,7 +134,7 @@ pub struct ConnectorFilters(pub HashMap); #[derive(Debug, Deserialize, Clone, Default)] #[serde(transparent)] -pub struct PaymentMethodFilters(pub HashMap); +pub struct PaymentMethodFilters(pub HashMap); #[derive(Debug, Deserialize, Clone, PartialEq, Eq, Hash)] #[serde(untagged)] @@ -144,11 +145,17 @@ pub enum PaymentMethodFilterKey { #[derive(Debug, Deserialize, Clone, Default)] #[serde(default)] -pub struct CurrencyCountryFilter { +pub struct CurrencyCountryFlowFilter { #[serde(deserialize_with = "currency_set_deser")] pub currency: Option>, #[serde(deserialize_with = "string_set_deser")] pub country: Option>, + pub not_available_flows: Option, +} +#[derive(Debug, Deserialize, Copy, Clone, Default)] +#[serde(default)] +pub struct NotAvailableFlows { + pub capture_method: Option, } fn string_set_deser<'a, D>( diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs index 3d141fc11f2e..835f076fb9e4 100644 --- a/crates/router/src/core/payment_methods/cards.rs +++ b/crates/router/src/core/payment_methods/cards.rs @@ -17,7 +17,7 @@ use common_utils::{ }; use error_stack::{report, IntoReport, ResultExt}; use router_env::{instrument, tracing}; -use storage_models::payment_method; +use storage_models::{enums as storage_enums, payment_method}; #[cfg(feature = "basilisk")] use crate::scheduler::metrics as scheduler_metrics; @@ -1127,6 +1127,7 @@ async fn filter_payment_methods( config, &connector, &payment_method_object.payment_method_type, + payment_attempt, &mut payment_method_object.card_networks, &address.and_then(|inner| inner.country), payment_attempt @@ -1161,6 +1162,7 @@ fn filter_pm_based_on_config<'a>( config: &'a crate::configs::settings::ConnectorFilters, connector: &'a str, payment_method_type: &'a api_enums::PaymentMethodType, + payment_attempt: Option<&storage::PaymentAttempt>, card_network: &mut Option>, country: &Option, currency: Option, @@ -1171,7 +1173,14 @@ fn filter_pm_based_on_config<'a>( .and_then(|inner| match payment_method_type { api_enums::PaymentMethodType::Credit | api_enums::PaymentMethodType::Debit => { card_network_filter(country, currency, card_network, inner); - None + + payment_attempt + .and_then(|inner| inner.capture_method) + .and_then(|capture_method| { + (capture_method == storage_enums::CaptureMethod::Manual).then(|| { + filter_pm_based_on_capture_method_used(inner, payment_method_type) + }) + }) } payment_method_type => inner .0 @@ -1183,6 +1192,22 @@ fn filter_pm_based_on_config<'a>( .unwrap_or(true) } +///Filters the payment method list on basis of Capture methods, checks whether the connector issues Manual payments using cards or not if not it won't be visible in payment methods list +fn filter_pm_based_on_capture_method_used( + payment_method_filters: &settings::PaymentMethodFilters, + payment_method_type: &api_enums::PaymentMethodType, +) -> bool { + payment_method_filters + .0 + .get(&settings::PaymentMethodFilterKey::PaymentMethodType( + *payment_method_type, + )) + .and_then(|v| v.not_available_flows) + .and_then(|v| v.capture_method) + .map(|v| !matches!(v, api_enums::CaptureMethod::Manual)) + .unwrap_or(true) +} + fn card_network_filter( country: &Option, currency: Option, @@ -1207,7 +1232,7 @@ fn card_network_filter( } fn global_country_currency_filter( - item: &settings::CurrencyCountryFilter, + item: &settings::CurrencyCountryFlowFilter, country: &Option, currency: Option, ) -> bool {