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

feat(euclid): add configs to disable pre-routing for certain specific PMs and PMTs (#10470)

Prajjwal Kumar6458e80bd3e9
diff --git a/crates/common_utils/src/id_type/merchant.rs b/crates/common_utils/src/id_type/merchant.rs
index d11e9742a7..ffea3110e2 100644
--- a/crates/common_utils/src/id_type/merchant.rs
+++ b/crates/common_utils/src/id_type/merchant.rs
@@ -120,6 +120,11 @@ impl MerchantId {
format!("guard_blocklist_for_{}", self.get_string_repr())
}
+ /// get_pre_routing_disabled_pm_pmt_key
+ pub fn get_pre_routing_disabled_pm_pmt_key(&self) -> String {
+ format!("pre_routing_disabled_pm_pmt_for_{}", self.get_string_repr())
+ }
+
/// get_merchant_fingerprint_secret_key
pub fn get_merchant_fingerprint_secret_key(&self) -> String {
format!("fingerprint_secret_{}", self.get_string_repr())
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index 7e64920415..fe6df6f323 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -85,7 +85,11 @@ use crate::{
},
payments::{
helpers,
- routing::{self, SessionFlowRoutingInput},
+ routing::{
+ self,
+ utils::{load_skip_pre_routing_config, perform_pre_routing},
+ SessionFlowRoutingInput,
+ },
},
utils as core_utils,
},
@@ -2898,15 +2902,27 @@ pub async fn list_payment_methods(
if let Some((payment_attempt, payment_intent)) =
payment_attempt.as_ref().zip(payment_intent.as_ref())
{
- let routing_enabled_pms = &router_consts::ROUTING_ENABLED_PAYMENT_METHODS;
+ let pre_routing_disabled_pm_pmt_key = &platform
+ .get_processor()
+ .get_account()
+ .get_id()
+ .get_pre_routing_disabled_pm_pmt_key();
+
+ let skip_pre_routing =
+ load_skip_pre_routing_config(&state, pre_routing_disabled_pm_pmt_key.to_string()).await;
+ let routing_enabled_pms = &router_consts::ROUTING_ENABLED_PAYMENT_METHODS;
let routing_enabled_pm_types = &router_consts::ROUTING_ENABLED_PAYMENT_METHOD_TYPES;
let mut chosen = api::SessionConnectorDatas::new(Vec::new());
for intermediate in &response {
- if routing_enabled_pm_types.contains(&intermediate.payment_method_type)
- || routing_enabled_pms.contains(&intermediate.payment_method)
- {
+ if perform_pre_routing(
+ routing_enabled_pms,
+ routing_enabled_pm_types,
+ &intermediate.payment_method,
+ &intermediate.payment_method_type,
+ &skip_pre_routing,
+ ) {
let connector_data = helpers::get_connector_data_with_token(
&state,
intermediate.connector.to_string(),
@@ -2924,6 +2940,7 @@ pub async fn list_payment_methods(
});
}
}
+
let sfr = SessionFlowRoutingInput {
state: &state,
country: billing_address.clone().and_then(|ad| ad.country),
@@ -2943,9 +2960,13 @@ pub async fn list_payment_methods(
.attach_printable("error performing session flow routing")?;
response.retain(|intermediate| {
- if !routing_enabled_pm_types.contains(&intermediate.payment_method_type)
- && !routing_enabled_pms.contains(&intermediate.payment_method)
- {
+ if !perform_pre_routing(
+ routing_enabled_pms,
+ routing_enabled_pm_types,
+ &intermediate.payment_method,
+ &intermediate.payment_method_type,
+ &skip_pre_routing,
+ ) {
return true;
}
diff --git a/crates/router/src/core/payments/routing/utils.rs b/crates/router/src/core/payments/routing/utils.rs
index 30d0670262..d3c95be1d1 100644
--- a/crates/router/src/core/payments/routing/utils.rs
+++ b/crates/router/src/core/payments/routing/utils.rs
@@ -1,6 +1,7 @@
use std::{
collections::{HashMap, HashSet},
str::FromStr,
+ sync::LazyLock,
};
use api_models::{
@@ -2196,3 +2197,95 @@ pub enum ContractUpdationStatusEventResponse {
ContractUpdationSucceeded,
ContractUpdationFailed,
}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct PreRoutingSkipRule {
+ /// The payment method for which specific payment method types should skip pre-routing.
+ /// IMPORTANT:
+ /// - Do NOT add multiple entries with the same `payment_method`.
+ /// - Each `payment_method` should appear **only once** in the config.
+ ///
+ /// Example:
+ /// {
+ /// bank_redirect: ["interac", "ach", "blik"]
+ /// }
+ ///
+ pub payment_method: common_enums::PaymentMethod,
+
+ /// The list of payment method types under this payment method
+ /// for which pre-routing must be skipped.
+ ///
+ /// This is a Vec because a single PM can have many PMTs
+ /// that need to skip pre-routing:
+ ///
+ /// Example:
+ /// payment_method_types = ["interac", "ach", "blik"]
+ ///
+ pub payment_method_types: Vec<common_enums::PaymentMethodType>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize, Default)]
+pub struct MerchantPreRoutingConfig {
+ pub skip_rules: Vec<PreRoutingSkipRule>,
+}
+
+/// Loads the merchant's pre-routing skip configuration and converts it into:
+/// HashMap<PaymentMethod, HashSet<PaymentMethodType>>
+///
+/// Example output:
+/// {
+/// bank_redirect: { interac, ach, blik },
+/// bank_debit: { sepa_debit }
+/// }
+pub async fn load_skip_pre_routing_config(
+ state: &SessionState,
+ pre_routing_disabled_pm_pmt_key: String,
+) -> HashMap<enums::PaymentMethod, HashSet<enums::PaymentMethodType>> {
+ let merchant_cfg = state
+ .store
+ .find_config_by_key_from_db(&pre_routing_disabled_pm_pmt_key)
+ .await
+ .ok()
+ .and_then(|cfg| serde_json::from_str::<MerchantPreRoutingConfig>(&cfg.config).ok())
+ .unwrap_or_default();
+
+ let mut skip_map: HashMap<enums::PaymentMethod, HashSet<enums::PaymentMethodType>> =
+ HashMap::new();
+
+ for rule in merchant_cfg.skip_rules.iter() {
+ skip_map
+ .entry(rule.payment_method)
+ .or_default()
+ .extend(rule.payment_method_types.iter().copied());
+ }
+
+ skip_map
+}
+
+/// Returns `true` if pre-routing should be skipped for
+/// the given (payment_method, payment_method_type) pair.
+pub fn should_skip_prerouting(
+ skip_map: &HashMap<enums::PaymentMethod, HashSet<enums::PaymentMethodType>>,
+ pm: &enums::PaymentMethod,
+ pmt: &enums::PaymentMethodType,
+) -> bool {
+ skip_map
+ .get(pm)
+ .map(|set| set.contains(pmt))
+ .unwrap_or(false)
+}
+
+pub fn perform_pre_routing(
+ allowed_pm_for_pre_routing: &LazyLock<HashSet<enums::PaymentMethod>>,
+ allowed_pmt_for_pre_routing: &LazyLock<HashSet<enums::PaymentMethodType>>,
+ payment_method: &enums::PaymentMethod,
+ payment_method_type: &enums::PaymentMethodType,
+ skip_map: &HashMap<enums::PaymentMethod, HashSet<enums::PaymentMethodType>>,
+) -> bool {
+ let should_skip_prerouting =
+ should_skip_prerouting(skip_map, payment_method, payment_method_type);
+
+ let pm_allowed = allowed_pm_for_pre_routing.contains(payment_method);
+ let pmt_allowed = allowed_pmt_for_pre_routing.contains(payment_method_type);
+ (pm_allowed || pmt_allowed) && !should_skip_prerouting
+}