Skip to content

Commit

Permalink
feat(pm_list): add available capture methods filter (#999)
Browse files Browse the repository at this point in the history
Co-authored-by: Arun Raj M <[email protected]>
  • Loading branch information
prajjwalkumar17 and jarnura authored May 3, 2023
1 parent 03a9643 commit 36cc13d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
24 changes: 24 additions & 0 deletions config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
11 changes: 9 additions & 2 deletions crates/router/src/configs/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -133,7 +134,7 @@ pub struct ConnectorFilters(pub HashMap<String, PaymentMethodFilters>);

#[derive(Debug, Deserialize, Clone, Default)]
#[serde(transparent)]
pub struct PaymentMethodFilters(pub HashMap<PaymentMethodFilterKey, CurrencyCountryFilter>);
pub struct PaymentMethodFilters(pub HashMap<PaymentMethodFilterKey, CurrencyCountryFlowFilter>);

#[derive(Debug, Deserialize, Clone, PartialEq, Eq, Hash)]
#[serde(untagged)]
Expand All @@ -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<HashSet<api_models::enums::Currency>>,
#[serde(deserialize_with = "string_set_deser")]
pub country: Option<HashSet<api_models::enums::CountryAlpha2>>,
pub not_available_flows: Option<NotAvailableFlows>,
}
#[derive(Debug, Deserialize, Copy, Clone, Default)]
#[serde(default)]
pub struct NotAvailableFlows {
pub capture_method: Option<enums::CaptureMethod>,
}

fn string_set_deser<'a, D>(
Expand Down
31 changes: 28 additions & 3 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Vec<api_enums::CardNetwork>>,
country: &Option<api_enums::CountryAlpha2>,
currency: Option<api_enums::Currency>,
Expand All @@ -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
Expand All @@ -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<api_enums::CountryAlpha2>,
currency: Option<api_enums::Currency>,
Expand All @@ -1207,7 +1232,7 @@ fn card_network_filter(
}

fn global_country_currency_filter(
item: &settings::CurrencyCountryFilter,
item: &settings::CurrencyCountryFlowFilter,
country: &Option<api_enums::CountryAlpha2>,
currency: Option<api_enums::Currency>,
) -> bool {
Expand Down

0 comments on commit 36cc13d

Please sign in to comment.