refactor(core): use profile id to find connector (#2020)

This commit is contained in:
Narayan Bhat
2023-09-11 12:55:22 +05:30
committed by GitHub
parent 60c5fdb89a
commit 5b29c25210
37 changed files with 776 additions and 363 deletions

View File

@ -86,17 +86,14 @@ pub fn create_identity_from_certificate_and_key(
.change_context(errors::ApiClientError::CertificateDecodeFailed)
}
pub fn filter_mca_based_on_business_details(
pub fn filter_mca_based_on_business_profile(
merchant_connector_accounts: Vec<domain::MerchantConnectorAccount>,
payment_intent: Option<&PaymentIntent>,
) -> Vec<domain::MerchantConnectorAccount> {
if let Some(payment_intent) = payment_intent {
merchant_connector_accounts
.into_iter()
.filter(|mca| {
mca.business_country == payment_intent.business_country
&& mca.business_label == payment_intent.business_label
})
.filter(|mca| mca.profile_id == payment_intent.profile_id)
.collect::<Vec<_>>()
} else {
merchant_connector_accounts
@ -2102,43 +2099,10 @@ pub async fn verify_payment_intent_time_and_client_secret(
.transpose()
}
fn connector_needs_business_sub_label(connector_name: &str) -> bool {
let connectors_list = [api_models::enums::Connector::Cybersource];
connectors_list
.map(|connector| connector.to_string())
.contains(&connector_name.to_string())
}
/// Create the connector label
/// {connector_name}_{country}_{business_label}
pub fn get_connector_label(
business_country: api_models::enums::CountryAlpha2,
business_label: &str,
business_sub_label: Option<&String>,
connector_name: &str,
) -> String {
let mut connector_label = format!("{connector_name}_{business_country}_{business_label}");
// Business sub label is currently being used only for cybersource
// To ensure backwards compatibality, cybersource mca's created before this change
// will have the business_sub_label value as default.
//
// Even when creating the connector account, if no sub label is provided, default will be used
if connector_needs_business_sub_label(connector_name) {
if let Some(sub_label) = business_sub_label {
connector_label.push_str(&format!("_{sub_label}"));
} else {
connector_label.push_str("_default"); // For backwards compatibality
}
}
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,
business_country: Option<api_enums::CountryAlpha2>,
business_label: Option<&String>,
merchant_account: &domain::MerchantAccount,
) -> RouterResult<()> {
let primary_business_details = merchant_account
@ -2148,15 +2112,21 @@ pub fn validate_business_details(
.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
business_country
.zip(business_label)
.map(|(business_country, business_label)| {
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_or(errors::ApiErrorResponse::PreconditionFailed {
message: "business_details are not configured in the merchant account".to_string(),
})?;
.transpose()?;
Ok(())
}
@ -2236,8 +2206,8 @@ mod tests {
off_session: None,
client_secret: Some("1".to_string()),
active_attempt_id: "nopes".to_string(),
business_country: storage_enums::CountryAlpha2::AG,
business_label: "no".to_string(),
business_country: None,
business_label: None,
order_details: None,
allowed_payment_method_types: None,
connector_metadata: None,
@ -2283,8 +2253,8 @@ mod tests {
off_session: None,
client_secret: Some("1".to_string()),
active_attempt_id: "nopes".to_string(),
business_country: storage_enums::CountryAlpha2::AG,
business_label: "no".to_string(),
business_country: None,
business_label: None,
order_details: None,
allowed_payment_method_types: None,
connector_metadata: None,
@ -2330,8 +2300,8 @@ mod tests {
off_session: None,
client_secret: None,
active_attempt_id: "nopes".to_string(),
business_country: storage_enums::CountryAlpha2::AG,
business_label: "no".to_string(),
business_country: None,
business_label: None,
order_details: None,
allowed_payment_method_types: None,
connector_metadata: None,
@ -2423,12 +2393,15 @@ impl MerchantConnectorAccountType {
}
}
/// Query for merchant connector account either by business label or profile id
/// If profile_id is passed use it, or use connector_label to query merchant connector account
pub async fn get_merchant_connector_account(
state: &AppState,
merchant_id: &str,
connector_label: &str,
creds_identifier: Option<String>,
key_store: &domain::MerchantKeyStore,
profile_id: &String,
connector_name: &str,
) -> RouterResult<MerchantConnectorAccountType> {
let db = &*state.store;
match creds_identifier {
@ -2438,7 +2411,7 @@ pub async fn get_merchant_connector_account(
.await
.to_not_found_response(
errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
id: connector_label.to_string(),
id: format!("mcd_{merchant_id}_{creds_identifier}"),
},
)?;
@ -2470,17 +2443,20 @@ pub async fn get_merchant_connector_account(
Ok(MerchantConnectorAccountType::CacheVal(res))
}
None => db
.find_merchant_connector_account_by_merchant_id_connector_label(
merchant_id,
connector_label,
None => {
db.find_merchant_connector_account_by_profile_id_connector_name(
profile_id,
connector_name,
key_store,
)
.await
.map(MerchantConnectorAccountType::DbVal)
.change_context(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
id: connector_label.to_string(),
}),
.to_not_found_response(
errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
id: format!("profile id {profile_id} and connector name {connector_name}"),
},
)
}
.map(MerchantConnectorAccountType::DbVal),
}
}