refactor(multiple_mca): make primary_business_detail optional and remove default values (#1677)

Co-authored-by: Arun Raj M <jarnura47@gmail.com>
This commit is contained in:
Narayan Bhat
2023-08-01 12:45:44 +05:30
committed by GitHub
parent 8a638e4a08
commit 9c7ac6246d
8 changed files with 85 additions and 127 deletions

View File

@ -22,7 +22,6 @@ kv_store = []
accounts_cache = []
openapi = ["olap", "oltp", "payouts"]
vergen = ["router_env/vergen"]
multiple_mca = ["api_models/multiple_mca"]
dummy_connector = ["api_models/dummy_connector"]
external_access_dc = ["dummy_connector"]
detailed_errors = ["api_models/detailed_errors", "error-stack/serde"]

View File

@ -39,31 +39,6 @@ pub fn create_merchant_publishable_key() -> String {
)
}
fn get_primary_business_details(
request: &api::MerchantAccountCreate,
) -> Vec<PrimaryBusinessDetails> {
// In this case, business details is not optional, it will always be passed
#[cfg(feature = "multiple_mca")]
{
request.primary_business_details.to_owned()
}
// In this case, business details will be optional, if it is not passed, then create the
// default value
#[cfg(not(feature = "multiple_mca"))]
{
request
.primary_business_details
.to_owned()
.unwrap_or_else(|| {
vec![PrimaryBusinessDetails {
country: enums::CountryAlpha2::US,
business: "default".to_string(),
}]
})
}
}
pub async fn create_merchant_account(
db: &dyn StorageInterface,
req: api::MerchantAccountCreate,
@ -77,7 +52,7 @@ pub async fn create_merchant_account(
let publishable_key = Some(create_merchant_publishable_key());
let primary_business_details = utils::Encode::<Vec<PrimaryBusinessDetails>>::encode_to_value(
&get_primary_business_details(&req),
&req.primary_business_details.unwrap_or_default(),
)
.change_context(errors::ApiErrorResponse::InvalidDataValue {
field_name: "primary_business_details",
@ -384,27 +359,6 @@ async fn validate_merchant_id<S: Into<String>>(
.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)
}
fn get_business_details_wrapper(
request: &api::MerchantConnectorCreate,
_merchant_account: &domain::MerchantAccount,
) -> RouterResult<(enums::CountryAlpha2, String)> {
#[cfg(feature = "multiple_mca")]
{
// The fields are mandatory
Ok((request.business_country, request.business_label.to_owned()))
}
#[cfg(not(feature = "multiple_mca"))]
{
// If the value is not passed, then take it from Merchant account
helpers::get_business_details(
request.business_country,
request.business_label.as_ref(),
_merchant_account,
)
}
}
fn validate_certificate_in_mca_metadata(
connector_metadata: Secret<serde_json::Value>,
) -> RouterResult<()> {
@ -465,11 +419,15 @@ pub async fn create_payment_connector(
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
let (business_country, business_label) = get_business_details_wrapper(&req, &merchant_account)?;
helpers::validate_business_details(
req.business_country,
&req.business_label,
&merchant_account,
)?;
let connector_label = helpers::get_connector_label(
business_country,
&business_label,
req.business_country,
&req.business_label,
req.business_sub_label.as_ref(),
&req.connector_name.to_string(),
);
@ -532,8 +490,8 @@ pub async fn create_payment_connector(
metadata: req.metadata,
frm_configs,
connector_label: connector_label.clone(),
business_country,
business_label,
business_country: req.business_country,
business_label: req.business_label.clone(),
business_sub_label: req.business_sub_label,
created_at: common_utils::date_time::now(),
modified_at: common_utils::date_time::now(),

View File

@ -1913,6 +1913,32 @@ pub fn get_connector_label(
connector_label
}
/// Check whether the business details are configured in the merchant account
pub fn validate_business_details(
business_country: api_enums::CountryAlpha2,
business_label: &String,
merchant_account: &domain::MerchantAccount,
) -> RouterResult<()> {
let primary_business_details = merchant_account
.primary_business_details
.clone()
.parse_value::<Vec<api_models::admin::PrimaryBusinessDetails>>("PrimaryBusinessDetails")
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("failed to parse primary business details")?;
primary_business_details
.iter()
.find(|business_details| {
&business_details.business == business_label
&& business_details.country == business_country
})
.ok_or(errors::ApiErrorResponse::PreconditionFailed {
message: "business_details are not configured in the merchant account".to_string(),
})?;
Ok(())
}
/// Do lazy parsing of primary business details
/// If both country and label are passed, no need to parse business details from merchant_account
/// If any one is missing, get it from merchant_account
@ -1923,42 +1949,29 @@ pub fn get_business_details(
business_label: Option<&String>,
merchant_account: &domain::MerchantAccount,
) -> RouterResult<(api_enums::CountryAlpha2, String)> {
let (business_country, business_label) = match business_country.zip(business_label) {
let primary_business_details = merchant_account
.primary_business_details
.clone()
.parse_value::<Vec<api_models::admin::PrimaryBusinessDetails>>("PrimaryBusinessDetails")
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("failed to parse primary business details")?;
match business_country.zip(business_label) {
Some((business_country, business_label)) => {
(business_country.to_owned(), business_label.to_owned())
Ok((business_country.to_owned(), business_label.to_owned()))
}
None => {
// Parse the primary business details from merchant account
let primary_business_details: Vec<api_models::admin::PrimaryBusinessDetails> =
merchant_account
.primary_business_details
.clone()
.parse_value("PrimaryBusinessDetails")
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("failed to parse primary business details")?;
if primary_business_details.len() == 1 {
let primary_business_details = primary_business_details.first().ok_or(
errors::ApiErrorResponse::MissingRequiredField {
field_name: "primary_business_details",
},
)?;
(
business_country.unwrap_or_else(|| primary_business_details.country.to_owned()),
business_label
.map(ToString::to_string)
.unwrap_or_else(|| primary_business_details.business.to_owned()),
)
} else {
// If primary business details are not present or more than one
Err(report!(errors::ApiErrorResponse::MissingRequiredField {
field_name: "business_country, business_label"
}))?
}
}
};
Ok((business_country, business_label))
_ => match primary_business_details.first() {
Some(business_details) if primary_business_details.len() == 1 => Ok((
business_country.unwrap_or_else(|| business_details.country.to_owned()),
business_label
.map(ToString::to_string)
.unwrap_or_else(|| business_details.business.to_owned()),
)),
_ => Err(report!(errors::ApiErrorResponse::MissingRequiredField {
field_name: "business_country, business_label"
})),
},
}
}
#[inline]

View File

@ -589,11 +589,29 @@ impl PaymentCreate {
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to convert order details to value")?;
let (business_country, business_label) = helpers::get_business_details(
request.business_country,
request.business_label.as_ref(),
merchant_account,
)?;
let (business_country, business_label) =
match (request.business_country, request.business_label.as_ref()) {
(Some(business_country), Some(business_label)) => {
helpers::validate_business_details(
business_country,
business_label,
merchant_account,
)?;
Ok((business_country, business_label.clone()))
}
(None, Some(_)) => Err(errors::ApiErrorResponse::MissingRequiredField {
field_name: "business_country",
}),
(Some(_), None) => Err(errors::ApiErrorResponse::MissingRequiredField {
field_name: "business_label",
}),
(None, None) => Ok(helpers::get_business_details(
request.business_country,
request.business_label.as_ref(),
merchant_account,
)?),
}?;
let allowed_payment_method_types = request
.get_allowed_payment_method_types_as_value()