From 301736fc25bc80f5f0da68377d16ab68132b8839 Mon Sep 17 00:00:00 2001 From: Manoj Ghorela <118727120+manoj-juspay@users.noreply.github.com> Date: Sun, 26 Feb 2023 18:18:57 +0530 Subject: [PATCH] refactor: Pass country and currency as json format in MCA (#656) --- crates/api_models/src/admin.rs | 46 ++++++-------- crates/api_models/src/payment_methods.rs | 10 ++- crates/router/src/core/admin.rs | 37 +---------- .../router/src/core/payment_methods/cards.rs | 62 +++++++++++-------- 4 files changed, 62 insertions(+), 93 deletions(-) diff --git a/crates/api_models/src/admin.rs b/crates/api_models/src/admin.rs index 6f02a8713b..b517371ffc 100644 --- a/crates/api_models/src/admin.rs +++ b/crates/api_models/src/admin.rs @@ -271,14 +271,12 @@ pub struct PaymentConnectorCreate { "Discover" ], "accepted_currencies": { - "enable_all":false, - "disable_only": ["INR", "CAD", "AED","JPY"], - "enable_only": ["EUR","USD"] + "type": "enable_only", + "list": ["USD", "EUR"] }, "accepted_countries": { - "enable_all":false, - "disable_only": ["FR", "DE","IN"], - "enable_only": ["UK","AU"] + "type": "disable_only", + "list": ["FR", "DE","IN"] }, "minimum_amount": 1, "maximum_amount": 68607706, @@ -310,18 +308,16 @@ pub struct PaymentMethods { /// List of currencies accepted or has the processing capabilities of the processor #[schema(example = json!( { - "enable_all":false, - "disable_only": ["INR", "CAD", "AED","JPY"], - "enable_only": ["EUR","USD"] + "type": "enable_only", + "list": ["USD", "EUR"] } ))] pub accepted_currencies: Option, /// List of Countries accepted or has the processing capabilities of the processor #[schema(example = json!( { - "enable_all":false, - "disable_only": ["FR", "DE","IN"], - "enable_only": ["UK","AU"] + "type": "disable_only", + "list": ["FR", "DE","IN"] } ))] pub accepted_countries: Option, @@ -343,28 +339,26 @@ pub struct PaymentMethods { pub payment_experience: Option>, } -/// List of enabled and disabled currencies +/// List of enabled and disabled currencies, empty in case all currencies are enabled #[derive(Eq, PartialEq, Hash, Debug, Clone, serde::Serialize, Deserialize, ToSchema)] #[serde(deny_unknown_fields)] pub struct AcceptedCurrencies { - /// True in case all currencies are supported - pub enable_all: bool, - /// List of disabled currencies, provide in case only few of currencies are not supported - pub disable_only: Option>, - /// List of enable currencies, provide in case only few of currencies are supported - pub enable_only: Option>, + /// type of accepted currencies (disable_only, enable_only) + #[serde(rename = "type")] + pub accept_type: String, + /// List of currencies of the provided type + pub list: Option>, } -/// List of enabled and disabled countries +/// List of enabled and disabled countries, empty in case all countries are enabled #[derive(Eq, PartialEq, Hash, Debug, Clone, serde::Serialize, Deserialize, ToSchema)] #[serde(deny_unknown_fields)] pub struct AcceptedCountries { - /// True in case all countries are supported - pub enable_all: bool, - /// List of disabled countries, provide in case only few of countries are not supported - pub disable_only: Option>, - /// List of enable countries, provide in case only few of countries are supported - pub enable_only: Option>, + /// Type of accepted countries (disable_only, enable_only) + #[serde(rename = "type")] + pub accept_type: String, + /// List of countries of the provided type + pub list: Option>, } #[derive(Debug, Clone, Serialize, Deserialize, ToSchema)] diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rs index 4bbbf33a6d..ff5c0affc0 100644 --- a/crates/api_models/src/payment_methods.rs +++ b/crates/api_models/src/payment_methods.rs @@ -324,9 +324,8 @@ pub struct ListPaymentMethod { /// List of Countries accepted or has the processing capabilities of the processor #[schema(example = json!( { - "enable_all":false, - "disable_only": ["FR", "DE","IN"], - "enable_only": ["UK","AU"] + "type": "disable_only", + "list": ["FR", "DE","IN"] } ))] pub accepted_countries: Option, @@ -334,9 +333,8 @@ pub struct ListPaymentMethod { /// List of currencies accepted or has the processing capabilities of the processor #[schema(example = json!( { - "enable_all":false, - "disable_only": ["INR", "CAD", "AED","JPY"], - "enable_only": ["EUR","USD"] + "type": "enable_only", + "list": ["USD", "EUR"] } ))] pub accepted_currencies: Option, diff --git a/crates/router/src/core/admin.rs b/crates/router/src/core/admin.rs index 3e00620664..4bd7aab5ee 100644 --- a/crates/router/src/core/admin.rs +++ b/crates/router/src/core/admin.rs @@ -1,4 +1,4 @@ -use common_utils::{ext_traits::ValueExt, fp_utils::when}; +use common_utils::ext_traits::ValueExt; use error_stack::{report, FutureExt, ResultExt}; use storage_models::{enums, merchant_account}; use uuid::Uuid; @@ -240,37 +240,6 @@ async fn validate_merchant_id>( }) } -fn validate_pm_enabled(pm: &api::PaymentMethods) -> RouterResult<()> { - if let Some(ac) = pm.accepted_countries.to_owned() { - when(ac.enable_all && ac.enable_only.is_some(), || { - Err(errors::ApiErrorResponse::PreconditionFailed { - message: "In case all countries are enabled,provide the disable_only country" - .to_string(), - }) - })?; - when(!ac.enable_all && ac.disable_only.is_some(), || { - Err(errors::ApiErrorResponse::PreconditionFailed { - message: "In case enable_all is false, provide the enable_only country".to_string(), - }) - })?; - }; - if let Some(ac) = pm.accepted_currencies.to_owned() { - when(ac.enable_all && ac.enable_only.is_some(), || { - Err(errors::ApiErrorResponse::PreconditionFailed { - message: "In case all currencies are enabled, provide the disable_only currency" - .to_string(), - }) - })?; - when(!ac.enable_all && ac.disable_only.is_some(), || { - Err(errors::ApiErrorResponse::PreconditionFailed { - message: "In case enable_all is false, provide the enable_only currency" - .to_string(), - }) - })?; - }; - Ok(()) -} - // Payment Connector API - Every merchant and connector can have an instance of (merchant <> connector) // with unique merchant_connector_id for Create Operation @@ -291,7 +260,6 @@ pub async fn create_payment_connector( let payment_methods_enabled = match req.payment_methods_enabled { Some(val) => { for pm in val.into_iter() { - validate_pm_enabled(&pm)?; let pm_value = utils::Encode::::encode_to_value(&pm) .change_context(errors::ApiErrorResponse::InternalServerError) .attach_printable( @@ -419,9 +387,6 @@ pub async fn update_payment_connector( pm_enabled .iter() .flat_map(|payment_method| { - validate_pm_enabled(payment_method) - .change_context(errors::ParsingError) - .attach_printable("Validation for accepted country and currency failed")?; utils::Encode::::encode_to_value(payment_method) }) .collect::>() diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs index 57869ef9c0..96fe75ed10 100644 --- a/crates/router/src/core/payment_methods/cards.rs +++ b/crates/router/src/core/payment_methods/cards.rs @@ -482,19 +482,25 @@ fn filter_pm_country_based( ) -> (Option, Option>, bool) { match (accepted_countries, req_country_list) { (None, None) => (None, None, true), - (None, Some(ref r)) => (None, Some(r.to_vec()), false), + (None, Some(ref r)) => ( + Some(admin::AcceptedCountries { + accept_type: "enable_only".to_owned(), + list: Some(r.to_vec()), + }), + Some(r.to_vec()), + true, + ), (Some(l), None) => (Some(l.to_owned()), None, true), (Some(l), Some(ref r)) => { - let enable_only = if l.enable_all { - filter_disabled_enum_based(&l.disable_only, &Some(r.to_owned())) + let list = if l.accept_type == "enable_only" { + filter_accepted_enum_based(&l.list, &Some(r.to_owned())) } else { - filter_accepted_enum_based(&l.enable_only, &Some(r.to_owned())) + filter_disabled_enum_based(&l.list, &Some(r.to_owned())) }; ( Some(admin::AcceptedCountries { - enable_all: l.enable_all, - enable_only, - disable_only: None, + accept_type: l.accept_type.to_owned(), + list, }), Some(r.to_vec()), true, @@ -513,19 +519,25 @@ fn filter_pm_currencies_based( ) { match (accepted_currency, req_currency_list) { (None, None) => (None, None, true), - (None, Some(ref r)) => (None, Some(r.to_vec()), false), + (None, Some(ref r)) => ( + Some(admin::AcceptedCurrencies { + accept_type: "enable_only".to_owned(), + list: Some(r.to_vec()), + }), + Some(r.to_vec()), + true, + ), (Some(l), None) => (Some(l.to_owned()), None, true), (Some(l), Some(ref r)) => { - let enable_only = if l.enable_all { - filter_disabled_enum_based(&l.disable_only, &Some(r.to_owned())) + let list = if l.accept_type == "enable_only" { + filter_accepted_enum_based(&l.list, &Some(r.to_owned())) } else { - filter_accepted_enum_based(&l.enable_only, &Some(r.to_owned())) + filter_disabled_enum_based(&l.list, &Some(r.to_owned())) }; ( Some(admin::AcceptedCurrencies { - enable_all: l.enable_all, - enable_only, - disable_only: None, + accept_type: l.accept_type.to_owned(), + list, }), Some(r.to_vec()), true, @@ -611,14 +623,14 @@ async fn filter_payment_country_based( Ok(address.map_or(true, |address| { address.country.as_ref().map_or(true, |country| { pm.accepted_countries.as_ref().map_or(true, |ac| { - if ac.enable_all { - ac.disable_only.as_ref().map_or(true, |disable_countries| { - disable_countries.contains(country) - }) - } else { - ac.enable_only + if ac.accept_type == "enable_only" { + ac.list .as_ref() .map_or(false, |enable_countries| enable_countries.contains(country)) + } else { + ac.list.as_ref().map_or(true, |disable_countries| { + !disable_countries.contains(country) + }) } }) }) @@ -631,13 +643,13 @@ fn filter_payment_currency_based( ) -> bool { payment_intent.currency.map_or(true, |currency| { pm.accepted_currencies.as_ref().map_or(true, |ac| { - if ac.enable_all { - ac.disable_only.as_ref().map_or(true, |disable_currencies| { - disable_currencies.contains(¤cy.foreign_into()) + if ac.accept_type == "enable_only" { + ac.list.as_ref().map_or(false, |enable_currencies| { + enable_currencies.contains(¤cy.foreign_into()) }) } else { - ac.enable_only.as_ref().map_or(false, |enable_currencies| { - enable_currencies.contains(¤cy.foreign_into()) + ac.list.as_ref().map_or(true, |disable_currencies| { + !disable_currencies.contains(¤cy.foreign_into()) }) } })