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

refactor(core): Add support for disabling surcharge calculation if MCA is disabled (#13224)

Swangi Kumari638278140589
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index a9621978fd..4096de6b6e 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -4540,11 +4540,31 @@ pub async fn build_merchant_enabled_pms_context(
};
// ---- sdk_next_action ----
- let has_surcharge_processor = business_profile
+ // Gate on the surcharge MCA `disabled` flag via the shared helper. Fetch is
+ // skipped entirely when no surcharge connector is configured on the profile.
+ let has_surcharge_processor = match business_profile
.surcharge_connector_details
.as_ref()
.and_then(|details| details.surcharge_connector_id.as_ref())
- .is_some();
+ {
+ Some(surcharge_connector_id) => helpers::fetch_active_surcharge_mca(
+ state,
+ platform.get_processor().get_account().get_id(),
+ platform.get_processor().get_key_store(),
+ surcharge_connector_id,
+ )
+ .await
+ .unwrap_or_else(|err| {
+ logger::warn!(
+ error=?err,
+ surcharge_connector_id = %surcharge_connector_id.get_string_repr(),
+ "Failed to fetch surcharge MCA for PML; treating as absent"
+ );
+ None
+ })
+ .is_some(),
+ None => false,
+ };
let sdk_next_action = payment_method_utils::get_sdk_next_action_for_payment_method_list(
state,
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index bf03ae9b6d..641350cc20 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -13858,19 +13858,29 @@ async fn calculate_external_surcharge(
payment_intent: storage::PaymentIntent,
inputs: SurchargeCalculationInputs,
) -> RouterResult<Option<api_models::payment_methods::SurchargeDetailsResponse>> {
- let surcharge_details = match (
- surcharge_connector_id,
- inputs.card_iin.clone(),
- inputs.currency,
- ) {
- (Some(surcharge_connector_id), Some(card_iin), Some(currency)) => {
- let processor = platform.get_processor();
+ let processor = platform.get_processor();
+ // Fetch + disabled-gate up front; a disabled MCA yields `None` and the tuple
+ // match below falls through to the no-op arm without any early return.
+ let surcharge_mca = match surcharge_connector_id.as_ref() {
+ Some(id) => {
+ helpers::fetch_active_surcharge_mca(
+ state,
+ processor.get_account().get_id(),
+ processor.get_key_store(),
+ id,
+ )
+ .await?
+ }
+ None => None,
+ };
+ let surcharge_details = match (surcharge_mca, inputs.card_iin.clone(), inputs.currency) {
+ (Some(surcharge_mca), Some(card_iin), Some(currency)) => {
match run_external_surcharge_ucs(
state,
processor,
payment_id,
profile_id,
- &surcharge_connector_id,
+ surcharge_mca,
card_iin,
currency,
&inputs,
@@ -13922,7 +13932,10 @@ async fn calculate_external_surcharge(
Ok(surcharge_details)
}
-// Shared UCS surcharge call for /eligibility and MIT /confirm.
+// Fire the UCS `surcharge_calculate` gRPC for `payment_id` using the given
+// surcharge MCA and return the connector's surcharge amount + id. Best-effort:
+// any UCS error is swallowed and returned as `Ok(None)` so the caller can
+// proceed without a surcharge.
#[cfg(all(feature = "oltp", feature = "v1"))]
#[allow(clippy::too_many_arguments)]
async fn run_external_surcharge_ucs(
@@ -13930,7 +13943,7 @@ async fn run_external_surcharge_ucs(
processor: &domain::Processor,
payment_id: &id_type::PaymentId,
profile_id: &id_type::ProfileId,
- surcharge_connector_id: &id_type::MerchantConnectorAccountId,
+ surcharge_mca: domain::MerchantConnectorAccount,
card_iin: String,
currency: storage_enums::Currency,
inputs: &SurchargeCalculationInputs,
@@ -13941,18 +13954,6 @@ async fn run_external_surcharge_ucs(
let storage_scheme = processor.get_account().storage_scheme;
let key_store = processor.get_key_store();
- let surcharge_mca = state
- .store
- .find_by_merchant_connector_account_merchant_id_merchant_connector_id(
- &merchant_id,
- surcharge_connector_id,
- key_store,
- )
- .await
- .to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
- id: surcharge_connector_id.get_string_repr().to_string(),
- })?;
-
let previous_connector_surcharge_id = previous_connector_surcharge_id(
state,
payment_id,
@@ -14045,15 +14046,36 @@ async fn calculate_mit_external_surcharge(
let payment_method_type = payment_attempt.payment_method_type;
let profile_id = payment_intent.profile_id.clone();
+ // Fetch + disabled-gate up front. A missing id, a disabled MCA, or a fetch
+ // error all collapse to `None` so the tuple match below handles them via
+ // the no-op arm — no early returns from inside the arm.
+ let surcharge_mca = match surcharge_connector_id.as_ref() {
+ Some(id) => helpers::fetch_active_surcharge_mca(
+ state,
+ processor.get_account().get_id(),
+ processor.get_key_store(),
+ id,
+ )
+ .await
+ .unwrap_or_else(|err| {
+ logger::warn!(
+ error=?err,
+ "MIT confirm: failed to fetch surcharge MCA; proceeding without surcharge"
+ );
+ None
+ }),
+ None => None,
+ };
+
match (
- surcharge_connector_id,
+ surcharge_mca,
card_iin,
currency,
payment_method,
profile_id,
) {
(
- Some(surcharge_connector_id),
+ Some(surcharge_mca),
Some(card_iin),
Some(currency),
Some(payment_method),
@@ -14095,7 +14117,7 @@ async fn calculate_mit_external_surcharge(
processor,
&payment_attempt.payment_id,
&profile_id,
- &surcharge_connector_id,
+ surcharge_mca,
card_iin,
currency,
&inputs,
@@ -14170,7 +14192,7 @@ pub async fn payments_submit_eligibility(
let surcharge_details = match sdk_next_action.next_action {
api_models::payments::NextActionCall::Deny { .. } => None,
_ => {
- calculate_external_surcharge(
+ Box::pin(calculate_external_surcharge(
&state_for_surcharge,
&platform_for_surcharge,
&payment_id,
@@ -14178,7 +14200,7 @@ pub async fn payments_submit_eligibility(
surcharge_connector_id,
payment_intent_for_surcharge,
surcharge_inputs,
- )
+ ))
.await?
}
};
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index 0d610a830b..f6b3d59f10 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -8819,6 +8819,39 @@ pub fn get_external_surcharge_redis_key(payment_id: &id_type::PaymentId) -> Stri
format!("{}_external_surcharge", payment_id.get_string_repr())
}
+// Fetch the surcharge MCA and gate on its `disabled` flag. The profile-level
+// `surcharge_connector_details` is not cleared when the MCA is disabled, so the
+// disabled flag on the MCA row itself is the source of truth. Returns `Ok(None)`
+// when the MCA is disabled so callers can short-circuit before any pre-work.
+#[cfg(feature = "v1")]
+pub async fn fetch_active_surcharge_mca(
+ state: &SessionState,
+ merchant_id: &id_type::MerchantId,
+ key_store: &domain::MerchantKeyStore,
+ surcharge_connector_id: &id_type::MerchantConnectorAccountId,
+) -> RouterResult<Option<domain::MerchantConnectorAccount>> {
+ let surcharge_mca = state
+ .store
+ .find_by_merchant_connector_account_merchant_id_merchant_connector_id(
+ merchant_id,
+ surcharge_connector_id,
+ key_store,
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
+ id: surcharge_connector_id.get_string_repr().to_string(),
+ })?;
+ if surcharge_mca.disabled.unwrap_or(false) {
+ logger::warn!(
+ surcharge_connector_id = %surcharge_connector_id.get_string_repr(),
+ "Surcharge MCA is disabled; skipping external surcharge calculation"
+ );
+ Ok(None)
+ } else {
+ Ok(Some(surcharge_mca))
+ }
+}
+
pub fn check_integrity_based_on_flow<T, Request>(
request: &Request,
payment_response_data: &Result<PaymentsResponseData, ErrorResponse>,