Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

fix(routing): remove duplicate MCA call for each payment method type (#10324)

Jagan1840199ca697
diff --git a/crates/router/src/core/payments/routing.rs b/crates/router/src/core/payments/routing.rs
index 33bfd75820..f2103092bc 100644
--- a/crates/router/src/core/payments/routing.rs
+++ b/crates/router/src/core/payments/routing.rs
@@ -953,6 +953,7 @@ pub async fn perform_cgraph_filtering(
eligible_connectors: Option<&Vec<api_enums::RoutableConnectors>>,
profile_id: &common_utils::id_type::ProfileId,
transaction_type: &api_enums::TransactionType,
+ active_mca_ids: &std::collections::HashSet<common_utils::id_type::MerchantConnectorAccountId>,
) -> RoutingResult<Vec<routing_types::RoutableConnectorChoice>> {
let context = euclid_graph::AnalysisContext::from_dir_values(
backend_input
@@ -962,24 +963,6 @@ pub async fn perform_cgraph_filtering(
let cached_cgraph = get_merchant_cgraph(state, key_store, profile_id, transaction_type).await?;
- let db_mcas = state
- .store
- .find_merchant_connector_account_by_merchant_id_and_disabled_list(
- &state.into(),
- &key_store.merchant_id,
- false,
- key_store,
- )
- .await
- .unwrap_or_else(|_| {
- hyperswitch_domain_models::merchant_connector_account::MerchantConnectorAccounts::new(
- vec![],
- )
- });
-
- let active_mca_ids: std::collections::HashSet<_> =
- db_mcas.iter().map(|mca| mca.get_id().clone()).collect();
-
let mut final_selection = Vec::new();
for choice in chosen {
@@ -1031,6 +1014,7 @@ pub async fn perform_eligibility_analysis(
routing::TransactionData::Payout(payout_data) => make_dsl_input_for_payouts(payout_data)?,
};
+ let active_mca_ids = get_active_mca_ids(state, key_store).await?;
perform_cgraph_filtering(
state,
key_store,
@@ -1039,6 +1023,7 @@ pub async fn perform_eligibility_analysis(
eligible_connectors,
profile_id,
&api_enums::TransactionType::from(transaction_data),
+ &active_mca_ids,
)
.await
}
@@ -1079,7 +1064,7 @@ pub async fn perform_fallback_routing(
#[cfg(feature = "payouts")]
routing::TransactionData::Payout(payout_data) => make_dsl_input_for_payouts(payout_data)?,
};
-
+ let active_mca_ids = get_active_mca_ids(state, key_store).await?;
perform_cgraph_filtering(
state,
key_store,
@@ -1088,6 +1073,7 @@ pub async fn perform_fallback_routing(
eligible_connectors,
business_profile.get_id(),
&api_enums::TransactionType::from(transaction_data),
+ &active_mca_ids,
)
.await
}
@@ -1220,6 +1206,7 @@ pub async fn perform_session_flow_routing<'a>(
api_enums::PaymentMethodType,
Vec<routing_types::SessionRoutingChoice>,
> = FxHashMap::default();
+ let active_mca_ids = get_active_mca_ids(state, key_store).await?;
for (pm_type, allowed_connectors) in pm_type_map {
let euclid_pmt: euclid_enums::PaymentMethodType = pm_type;
@@ -1241,6 +1228,7 @@ pub async fn perform_session_flow_routing<'a>(
&session_pm_input,
transaction_type,
business_profile,
+ &active_mca_ids,
)
.await?;
@@ -1371,6 +1359,7 @@ pub async fn perform_session_flow_routing(
Vec<routing_types::SessionRoutingChoice>,
> = FxHashMap::default();
let mut final_routing_approach = None;
+ let active_mca_ids = get_active_mca_ids(session_input.state, session_input.key_store).await?;
for (pm_type, allowed_connectors) in pm_type_map {
let euclid_pmt: euclid_enums::PaymentMethodType = pm_type;
@@ -1394,6 +1383,7 @@ pub async fn perform_session_flow_routing(
&session_pm_input,
transaction_type,
business_profile,
+ &active_mca_ids,
)
.await?;
@@ -1433,6 +1423,7 @@ async fn perform_session_routing_for_pm_type(
session_pm_input: &SessionRoutingPmTypeInput<'_>,
transaction_type: &api_enums::TransactionType,
_business_profile: &domain::Profile,
+ active_mca_ids: &std::collections::HashSet<common_utils::id_type::MerchantConnectorAccountId>,
) -> RoutingResult<(
Option<Vec<api_models::routing::RoutableConnectorChoice>>,
Option<common_enums::RoutingApproach>,
@@ -1493,6 +1484,7 @@ async fn perform_session_routing_for_pm_type(
None,
session_pm_input.profile_id,
transaction_type,
+ active_mca_ids,
)
.await?;
@@ -1513,6 +1505,7 @@ async fn perform_session_routing_for_pm_type(
None,
session_pm_input.profile_id,
transaction_type,
+ active_mca_ids,
)
.await?;
}
@@ -1571,6 +1564,7 @@ async fn perform_session_routing_for_pm_type<'a>(
session_pm_input: &SessionRoutingPmTypeInput<'_>,
transaction_type: &api_enums::TransactionType,
business_profile: &domain::Profile,
+ active_mca_ids: &std::collections::HashSet<common_utils::id_type::MerchantConnectorAccountId>,
) -> RoutingResult<Option<Vec<api_models::routing::RoutableConnectorChoice>>> {
let profile_wrapper = admin::ProfileWrapper::new(business_profile.clone());
let chosen_connectors = get_chosen_connectors(
@@ -1590,6 +1584,7 @@ async fn perform_session_routing_for_pm_type<'a>(
None,
session_pm_input.profile_id,
transaction_type,
+ active_mca_ids,
)
.await?;
@@ -1606,6 +1601,7 @@ async fn perform_session_routing_for_pm_type<'a>(
None,
session_pm_input.profile_id,
transaction_type,
+ active_mca_ids,
)
.await?;
}
@@ -2742,3 +2738,27 @@ where
Ok(routable_connectors)
}
}
+
+pub async fn get_active_mca_ids(
+ state: &SessionState,
+ key_store: &domain::MerchantKeyStore,
+) -> RoutingResult<std::collections::HashSet<common_utils::id_type::MerchantConnectorAccountId>> {
+ let db_mcas = state
+ .store
+ .find_merchant_connector_account_by_merchant_id_and_disabled_list(
+ &state.into(),
+ &key_store.merchant_id,
+ false,
+ key_store,
+ )
+ .await
+ .unwrap_or_else(|_| {
+ hyperswitch_domain_models::merchant_connector_account::MerchantConnectorAccounts::new(
+ vec![],
+ )
+ });
+
+ let active_mca_ids: std::collections::HashSet<_> =
+ db_mcas.iter().map(|mca| mca.get_id().clone()).collect();
+ Ok(active_mca_ids)
+}
diff --git a/crates/router/src/core/routing.rs b/crates/router/src/core/routing.rs
index 86f05bbd0f..d51300d305 100644
--- a/crates/router/src/core/routing.rs
+++ b/crates/router/src/core/routing.rs
@@ -56,7 +56,9 @@ use crate::{core::admin, utils::ValueExt};
use crate::{
core::{
errors::{self, CustomResult, RouterResponse},
- metrics, utils as core_utils,
+ metrics,
+ payments::routing::get_active_mca_ids,
+ utils as core_utils,
},
db::StorageInterface,
routes::SessionState,
@@ -2596,6 +2598,9 @@ impl RoutableConnectors {
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to construct dsl input")?;
+ let active_mca_ids = get_active_mca_ids(state, key_store)
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)?;
let connectors = payments_routing::perform_cgraph_filtering(
state,
key_store,
@@ -2604,6 +2609,7 @@ impl RoutableConnectors {
None,
profile_id,
&common_enums::TransactionType::Payment,
+ &active_mca_ids,
)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)