mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-28 20:23:43 +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"
|
"Discover"
|
||||||
],
|
],
|
||||||
"accepted_currencies": {
|
"accepted_currencies": {
|
||||||
"enable_all":false,
|
"type": "enable_only",
|
||||||
"disable_only": ["INR", "CAD", "AED","JPY"],
|
"list": ["USD", "EUR"]
|
||||||
"enable_only": ["EUR","USD"]
|
|
||||||
},
|
},
|
||||||
"accepted_countries": {
|
"accepted_countries": {
|
||||||
"enable_all":false,
|
"type": "disable_only",
|
||||||
"disable_only": ["FR", "DE","IN"],
|
"list": ["FR", "DE","IN"]
|
||||||
"enable_only": ["UK","AU"]
|
|
||||||
},
|
},
|
||||||
"minimum_amount": 1,
|
"minimum_amount": 1,
|
||||||
"maximum_amount": 68607706,
|
"maximum_amount": 68607706,
|
||||||
@ -310,18 +308,16 @@ pub struct PaymentMethods {
|
|||||||
/// List of currencies accepted or has the processing capabilities of the processor
|
/// List of currencies accepted or has the processing capabilities of the processor
|
||||||
#[schema(example = json!(
|
#[schema(example = json!(
|
||||||
{
|
{
|
||||||
"enable_all":false,
|
"type": "enable_only",
|
||||||
"disable_only": ["INR", "CAD", "AED","JPY"],
|
"list": ["USD", "EUR"]
|
||||||
"enable_only": ["EUR","USD"]
|
|
||||||
}
|
}
|
||||||
))]
|
))]
|
||||||
pub accepted_currencies: Option<AcceptedCurrencies>,
|
pub accepted_currencies: Option<AcceptedCurrencies>,
|
||||||
/// List of Countries accepted or has the processing capabilities of the processor
|
/// List of Countries accepted or has the processing capabilities of the processor
|
||||||
#[schema(example = json!(
|
#[schema(example = json!(
|
||||||
{
|
{
|
||||||
"enable_all":false,
|
"type": "disable_only",
|
||||||
"disable_only": ["FR", "DE","IN"],
|
"list": ["FR", "DE","IN"]
|
||||||
"enable_only": ["UK","AU"]
|
|
||||||
}
|
}
|
||||||
))]
|
))]
|
||||||
pub accepted_countries: Option<AcceptedCountries>,
|
pub accepted_countries: Option<AcceptedCountries>,
|
||||||
@ -343,28 +339,26 @@ pub struct PaymentMethods {
|
|||||||
pub payment_experience: Option<Vec<api_enums::PaymentExperience>>,
|
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)]
|
#[derive(Eq, PartialEq, Hash, Debug, Clone, serde::Serialize, Deserialize, ToSchema)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct AcceptedCurrencies {
|
pub struct AcceptedCurrencies {
|
||||||
/// True in case all currencies are supported
|
/// type of accepted currencies (disable_only, enable_only)
|
||||||
pub enable_all: bool,
|
#[serde(rename = "type")]
|
||||||
/// List of disabled currencies, provide in case only few of currencies are not supported
|
pub accept_type: String,
|
||||||
pub disable_only: Option<Vec<api_enums::Currency>>,
|
/// List of currencies of the provided type
|
||||||
/// List of enable currencies, provide in case only few of currencies are supported
|
pub list: Option<Vec<api_enums::Currency>>,
|
||||||
pub enable_only: 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)]
|
#[derive(Eq, PartialEq, Hash, Debug, Clone, serde::Serialize, Deserialize, ToSchema)]
|
||||||
#[serde(deny_unknown_fields)]
|
#[serde(deny_unknown_fields)]
|
||||||
pub struct AcceptedCountries {
|
pub struct AcceptedCountries {
|
||||||
/// True in case all countries are supported
|
/// Type of accepted countries (disable_only, enable_only)
|
||||||
pub enable_all: bool,
|
#[serde(rename = "type")]
|
||||||
/// List of disabled countries, provide in case only few of countries are not supported
|
pub accept_type: String,
|
||||||
pub disable_only: Option<Vec<String>>,
|
/// List of countries of the provided type
|
||||||
/// List of enable countries, provide in case only few of countries are supported
|
pub list: Option<Vec<String>>,
|
||||||
pub enable_only: Option<Vec<String>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
|
#[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
|
/// List of Countries accepted or has the processing capabilities of the processor
|
||||||
#[schema(example = json!(
|
#[schema(example = json!(
|
||||||
{
|
{
|
||||||
"enable_all":false,
|
"type": "disable_only",
|
||||||
"disable_only": ["FR", "DE","IN"],
|
"list": ["FR", "DE","IN"]
|
||||||
"enable_only": ["UK","AU"]
|
|
||||||
}
|
}
|
||||||
))]
|
))]
|
||||||
pub accepted_countries: Option<admin::AcceptedCountries>,
|
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
|
/// List of currencies accepted or has the processing capabilities of the processor
|
||||||
#[schema(example = json!(
|
#[schema(example = json!(
|
||||||
{
|
{
|
||||||
"enable_all":false,
|
"type": "enable_only",
|
||||||
"disable_only": ["INR", "CAD", "AED","JPY"],
|
"list": ["USD", "EUR"]
|
||||||
"enable_only": ["EUR","USD"]
|
|
||||||
}
|
}
|
||||||
))]
|
))]
|
||||||
pub accepted_currencies: Option<admin::AcceptedCurrencies>,
|
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 error_stack::{report, FutureExt, ResultExt};
|
||||||
use storage_models::{enums, merchant_account};
|
use storage_models::{enums, merchant_account};
|
||||||
use uuid::Uuid;
|
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)
|
// Payment Connector API - Every merchant and connector can have an instance of (merchant <> connector)
|
||||||
// with unique merchant_connector_id for Create Operation
|
// 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 {
|
let payment_methods_enabled = match req.payment_methods_enabled {
|
||||||
Some(val) => {
|
Some(val) => {
|
||||||
for pm in val.into_iter() {
|
for pm in val.into_iter() {
|
||||||
validate_pm_enabled(&pm)?;
|
|
||||||
let pm_value = utils::Encode::<api::PaymentMethods>::encode_to_value(&pm)
|
let pm_value = utils::Encode::<api::PaymentMethods>::encode_to_value(&pm)
|
||||||
.change_context(errors::ApiErrorResponse::InternalServerError)
|
.change_context(errors::ApiErrorResponse::InternalServerError)
|
||||||
.attach_printable(
|
.attach_printable(
|
||||||
@ -419,9 +387,6 @@ pub async fn update_payment_connector(
|
|||||||
pm_enabled
|
pm_enabled
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|payment_method| {
|
.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)
|
utils::Encode::<api::PaymentMethods>::encode_to_value(payment_method)
|
||||||
})
|
})
|
||||||
.collect::<Vec<serde_json::Value>>()
|
.collect::<Vec<serde_json::Value>>()
|
||||||
|
|||||||
@ -482,19 +482,25 @@ fn filter_pm_country_based(
|
|||||||
) -> (Option<admin::AcceptedCountries>, Option<Vec<String>>, bool) {
|
) -> (Option<admin::AcceptedCountries>, Option<Vec<String>>, bool) {
|
||||||
match (accepted_countries, req_country_list) {
|
match (accepted_countries, req_country_list) {
|
||||||
(None, None) => (None, None, true),
|
(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), None) => (Some(l.to_owned()), None, true),
|
||||||
(Some(l), Some(ref r)) => {
|
(Some(l), Some(ref r)) => {
|
||||||
let enable_only = if l.enable_all {
|
let list = if l.accept_type == "enable_only" {
|
||||||
filter_disabled_enum_based(&l.disable_only, &Some(r.to_owned()))
|
filter_accepted_enum_based(&l.list, &Some(r.to_owned()))
|
||||||
} else {
|
} 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 {
|
Some(admin::AcceptedCountries {
|
||||||
enable_all: l.enable_all,
|
accept_type: l.accept_type.to_owned(),
|
||||||
enable_only,
|
list,
|
||||||
disable_only: None,
|
|
||||||
}),
|
}),
|
||||||
Some(r.to_vec()),
|
Some(r.to_vec()),
|
||||||
true,
|
true,
|
||||||
@ -513,19 +519,25 @@ fn filter_pm_currencies_based(
|
|||||||
) {
|
) {
|
||||||
match (accepted_currency, req_currency_list) {
|
match (accepted_currency, req_currency_list) {
|
||||||
(None, None) => (None, None, true),
|
(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), None) => (Some(l.to_owned()), None, true),
|
||||||
(Some(l), Some(ref r)) => {
|
(Some(l), Some(ref r)) => {
|
||||||
let enable_only = if l.enable_all {
|
let list = if l.accept_type == "enable_only" {
|
||||||
filter_disabled_enum_based(&l.disable_only, &Some(r.to_owned()))
|
filter_accepted_enum_based(&l.list, &Some(r.to_owned()))
|
||||||
} else {
|
} 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 {
|
Some(admin::AcceptedCurrencies {
|
||||||
enable_all: l.enable_all,
|
accept_type: l.accept_type.to_owned(),
|
||||||
enable_only,
|
list,
|
||||||
disable_only: None,
|
|
||||||
}),
|
}),
|
||||||
Some(r.to_vec()),
|
Some(r.to_vec()),
|
||||||
true,
|
true,
|
||||||
@ -611,14 +623,14 @@ async fn filter_payment_country_based(
|
|||||||
Ok(address.map_or(true, |address| {
|
Ok(address.map_or(true, |address| {
|
||||||
address.country.as_ref().map_or(true, |country| {
|
address.country.as_ref().map_or(true, |country| {
|
||||||
pm.accepted_countries.as_ref().map_or(true, |ac| {
|
pm.accepted_countries.as_ref().map_or(true, |ac| {
|
||||||
if ac.enable_all {
|
if ac.accept_type == "enable_only" {
|
||||||
ac.disable_only.as_ref().map_or(true, |disable_countries| {
|
ac.list
|
||||||
disable_countries.contains(country)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
ac.enable_only
|
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or(false, |enable_countries| enable_countries.contains(country))
|
.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 {
|
) -> bool {
|
||||||
payment_intent.currency.map_or(true, |currency| {
|
payment_intent.currency.map_or(true, |currency| {
|
||||||
pm.accepted_currencies.as_ref().map_or(true, |ac| {
|
pm.accepted_currencies.as_ref().map_or(true, |ac| {
|
||||||
if ac.enable_all {
|
if ac.accept_type == "enable_only" {
|
||||||
ac.disable_only.as_ref().map_or(true, |disable_currencies| {
|
ac.list.as_ref().map_or(false, |enable_currencies| {
|
||||||
disable_currencies.contains(¤cy.foreign_into())
|
enable_currencies.contains(¤cy.foreign_into())
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
ac.enable_only.as_ref().map_or(false, |enable_currencies| {
|
ac.list.as_ref().map_or(true, |disable_currencies| {
|
||||||
enable_currencies.contains(¤cy.foreign_into())
|
!disable_currencies.contains(¤cy.foreign_into())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user