Skip to content

Commit

Permalink
fix(filter_mca): update helper function for filtering MCAs for paymen…
Browse files Browse the repository at this point in the history
…ts (#5529)
  • Loading branch information
kashif-m authored Aug 7, 2024
1 parent f51b6c9 commit 1715cf0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 41 deletions.
11 changes: 7 additions & 4 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use api_models::{
pm_auth::PaymentMethodAuthConfig,
surcharge_decision_configs as api_surcharge_decision_configs,
};
use common_enums::enums::MerchantStorageScheme;
use common_enums::{enums::MerchantStorageScheme, ConnectorType};
use common_utils::{
consts,
crypto::Encryptable,
Expand Down Expand Up @@ -2368,9 +2368,12 @@ pub async fn list_payment_methods(
)
.await?;

// filter out connectors based on the business country
let filtered_mcas =
helpers::filter_mca_based_on_business_profile(all_mcas.clone(), profile_id.clone());
// filter out payment connectors based on profile_id
let filtered_mcas = helpers::filter_mca_based_on_profile_and_connector_type(
all_mcas.clone(),
profile_id.as_ref(),
ConnectorType::PaymentProcessor,
);

logger::debug!(mca_before_filtering=?filtered_mcas);

Expand Down
37 changes: 13 additions & 24 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,31 +121,18 @@ pub fn create_certificate(
.change_context(errors::ApiClientError::CertificateDecodeFailed)
}

pub fn filter_mca_based_on_business_profile(
merchant_connector_accounts: Vec<domain::MerchantConnectorAccount>,
profile_id: Option<String>,
) -> Vec<domain::MerchantConnectorAccount> {
if let Some(profile_id) = profile_id {
merchant_connector_accounts
.into_iter()
.filter(|mca| {
mca.profile_id.as_ref() == Some(&profile_id)
&& mca.connector_type == ConnectorType::PaymentProcessor
})
.collect::<Vec<_>>()
} else {
merchant_connector_accounts
}
}

pub fn filter_mca_based_on_connector_type(
pub fn filter_mca_based_on_profile_and_connector_type(
merchant_connector_accounts: Vec<domain::MerchantConnectorAccount>,
profile_id: Option<&String>,
connector_type: ConnectorType,
) -> Vec<domain::MerchantConnectorAccount> {
merchant_connector_accounts
.into_iter()
.filter(|mca| mca.connector_type == connector_type)
.collect::<Vec<_>>()
.filter(|mca| {
profile_id.map_or(true, |id| mca.profile_id.as_ref() == Some(id))
&& mca.connector_type == connector_type
})
.collect()
}

#[instrument(skip_all)]
Expand Down Expand Up @@ -4299,10 +4286,12 @@ where
.await
.to_not_found_response(errors::ApiErrorResponse::InternalServerError)?;

let profile_specific_merchant_connector_account_list = filter_mca_based_on_business_profile(
merchant_connector_account_list,
Some(profile_id.to_string()),
);
let profile_specific_merchant_connector_account_list =
filter_mca_based_on_profile_and_connector_type(
merchant_connector_account_list,
Some(profile_id),
ConnectorType::PaymentProcessor,
);

let mut connector_data_list = vec![pre_decided_connector_data_first.clone()];

Expand Down
7 changes: 5 additions & 2 deletions crates/router/src/core/payments/operations/payment_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,11 @@ where
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("profile_id is not set in payment_intent")?;

let filtered_connector_accounts =
helpers::filter_mca_based_on_business_profile(all_connector_accounts, Some(profile_id));
let filtered_connector_accounts = helpers::filter_mca_based_on_profile_and_connector_type(
all_connector_accounts,
Some(&profile_id),
common_enums::ConnectorType::PaymentProcessor,
);

let requested_payment_method_types = request.wallets.clone();
let mut connector_and_supporting_payment_method_type = Vec::new();
Expand Down
16 changes: 12 additions & 4 deletions crates/router/src/core/payments/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,18 @@ pub async fn refresh_cgraph_cache<'a>(
}
};

let merchant_connector_accounts = payments_oss::helpers::filter_mca_based_on_business_profile(
merchant_connector_accounts,
profile_id,
);
let connector_type = match transaction_type {
api_enums::TransactionType::Payment => common_enums::ConnectorType::PaymentProcessor,
#[cfg(feature = "payouts")]
api_enums::TransactionType::Payout => common_enums::ConnectorType::PayoutProcessor,
};

let merchant_connector_accounts =
payments_oss::helpers::filter_mca_based_on_profile_and_connector_type(
merchant_connector_accounts,
profile_id.as_ref(),
connector_type,
);

let api_mcas = merchant_connector_accounts
.into_iter()
Expand Down
12 changes: 5 additions & 7 deletions crates/router/src/core/payout_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,10 @@ pub async fn filter_payout_methods(
)
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
// fetch all mca based on profile id
let filtered_mca_on_profile =
helpers::filter_mca_based_on_business_profile(all_mcas, Some(payout.profile_id.clone()));
//Since we just need payout connectors here, filter mca based on connector type.
let filtered_mca = helpers::filter_mca_based_on_connector_type(
filtered_mca_on_profile.clone(),
// Filter MCAs based on profile_id and connector_type
let filtered_mcas = helpers::filter_mca_based_on_profile_and_connector_type(
all_mcas,
Some(&payout.profile_id),
common_enums::ConnectorType::PayoutProcessor,
);
let address = db
Expand All @@ -306,7 +304,7 @@ pub async fn filter_payout_methods(
let mut card_hash_set: HashSet<common_enums::PaymentMethodType> = HashSet::new();
let mut wallet_hash_set: HashSet<common_enums::PaymentMethodType> = HashSet::new();
let payout_filter_config = &state.conf.payout_method_filters.clone();
for mca in &filtered_mca {
for mca in &filtered_mcas {
let payout_methods = match &mca.payment_methods_enabled {
Some(pm) => pm,
None => continue,
Expand Down

0 comments on commit 1715cf0

Please sign in to comment.