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(payment_methods): avoid repeated MCA DB queries in customer payment methods list API (#12030)

Shankar Singh C96158bc0a3f5
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index 8223d4581e..b45fd65e07 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -4793,6 +4793,23 @@ pub async fn list_customer_payment_method(
.and_then(|business_profile| business_profile.is_connector_agnostic_mit_enabled)
.unwrap_or(false);
+ let merchant_connector_accounts = state
+ .store
+ .find_merchant_connector_account_by_merchant_id_and_disabled_list(
+ platform.get_processor().get_account().get_id(),
+ true,
+ platform.get_provider().get_key_store(),
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
+ id: platform
+ .get_processor()
+ .get_account()
+ .get_id()
+ .get_string_repr()
+ .to_owned(),
+ })?;
+
for pm in resp.into_iter() {
let parent_payment_method_token = generate_id(consts::ID_LENGTH, "token");
@@ -4841,13 +4858,11 @@ pub async fn list_customer_payment_method(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to deserialize to Payment Mandate Reference ")?;
let mca_enabled = get_mca_status(
- state,
- platform.get_processor().get_key_store(),
profile_id.clone(),
- platform.get_processor().get_account().get_id(),
is_connector_agnostic_mit_enabled,
Some(connector_mandate_details),
pm.network_transaction_id.as_ref(),
+ &merchant_connector_accounts,
)
.await?;
@@ -4870,7 +4885,7 @@ pub async fn list_customer_payment_method(
card: pm_list_context.card_details,
metadata: pm.metadata,
payment_method_issuer_code: pm.payment_method_issuer_code,
- recurring_enabled: mca_enabled,
+ recurring_enabled: Some(mca_enabled),
installment_payment_enabled: Some(false),
payment_experience: Some(vec![api_models::enums::PaymentExperience::RedirectToUrl]),
created: Some(pm.created_at),
@@ -4886,7 +4901,7 @@ pub async fn list_customer_payment_method(
&& customer.default_payment_method_id == Some(pm.payment_method_id),
billing: payment_method_billing,
};
- if requires_cvv || mca_enabled.unwrap_or(false) {
+ if requires_cvv || mca_enabled {
customer_pms.push(pma.to_owned());
}
@@ -5098,38 +5113,23 @@ pub async fn perform_surcharge_ops(
#[cfg(feature = "v1")]
pub async fn get_mca_status(
- state: &routes::SessionState,
- key_store: &domain::MerchantKeyStore,
profile_id: Option<id_type::ProfileId>,
- merchant_id: &id_type::MerchantId,
+
is_connector_agnostic_mit_enabled: bool,
connector_mandate_details: Option<CommonMandateReference>,
network_transaction_id: Option<&String>,
-) -> errors::RouterResult<Option<bool>> {
- if is_connector_agnostic_mit_enabled && network_transaction_id.is_some() {
- return Ok(Some(true));
- }
- if let Some(connector_mandate_details) = connector_mandate_details {
- let mcas = state
- .store
- .find_merchant_connector_account_by_merchant_id_and_disabled_list(
- merchant_id,
- true,
- key_store,
- )
- .await
- .change_context(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
- id: merchant_id.get_string_repr().to_owned(),
- })?;
+ merchant_connector_accounts: &domain::MerchantConnectorAccounts,
+) -> errors::RouterResult<bool> {
+ let agnostic_mit = is_connector_agnostic_mit_enabled && network_transaction_id.is_some();
- return Ok(Some(
- mcas.is_merchant_connector_account_id_in_connector_mandate_details(
- profile_id.as_ref(),
- &connector_mandate_details,
- ),
- ));
- }
- Ok(Some(false))
+ let mandate_match = connector_mandate_details.is_some_and(|details| {
+ merchant_connector_accounts.is_merchant_connector_account_id_in_connector_mandate_details(
+ profile_id.as_ref(),
+ &details,
+ )
+ });
+
+ Ok(agnostic_mit || mandate_match)
}
#[cfg(feature = "v2")]