mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-28 04:04:55 +08:00
refactor: Pass country and currency as json format in MCA (#656)
This commit is contained in:
@ -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<AcceptedCurrencies>,
|
||||
/// 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<AcceptedCountries>,
|
||||
@ -343,28 +339,26 @@ pub struct PaymentMethods {
|
||||
pub payment_experience: Option<Vec<api_enums::PaymentExperience>>,
|
||||
}
|
||||
|
||||
/// 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<Vec<api_enums::Currency>>,
|
||||
/// List of enable currencies, provide in case only few of currencies are supported
|
||||
pub enable_only: Option<Vec<api_enums::Currency>>,
|
||||
/// 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<Vec<api_enums::Currency>>,
|
||||
}
|
||||
|
||||
/// 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<Vec<String>>,
|
||||
/// List of enable countries, provide in case only few of countries are supported
|
||||
pub enable_only: Option<Vec<String>>,
|
||||
/// 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<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
|
||||
|
||||
@ -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<admin::AcceptedCountries>,
|
||||
@ -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<admin::AcceptedCurrencies>,
|
||||
|
||||
@ -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<S: Into<String>>(
|
||||
})
|
||||
}
|
||||
|
||||
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::<api::PaymentMethods>::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::<api::PaymentMethods>::encode_to_value(payment_method)
|
||||
})
|
||||
.collect::<Vec<serde_json::Value>>()
|
||||
|
||||
@ -482,19 +482,25 @@ fn filter_pm_country_based(
|
||||
) -> (Option<admin::AcceptedCountries>, Option<Vec<String>>, 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())
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user