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(routing): update configs on updation of mca (#9659)

Prajjwal Kumar286e18b226a5
diff --git a/crates/router/src/core/admin.rs b/crates/router/src/core/admin.rs
index 0a2252be15..30f7094348 100644
--- a/crates/router/src/core/admin.rs
+++ b/crates/router/src/core/admin.rs
@@ -23,7 +23,7 @@ use masking::{ExposeInterface, PeekInterface, Secret};
use pm_auth::types as pm_auth_types;
use uuid::Uuid;
-use super::routing::helpers::redact_cgraph_cache;
+use super::routing::helpers::{redact_cgraph_cache, update_default_fallback_on_mca_update};
#[cfg(any(feature = "v1", feature = "v2"))]
use crate::types::transformers::ForeignFrom;
use crate::{
@@ -2894,6 +2894,9 @@ pub async fn update_connector(
// redact cgraph cache on connector updation
redact_cgraph_cache(&state, merchant_id, &profile_id).await?;
+ // update default fallback config
+ update_default_fallback_on_mca_update(&state, merchant_id, &profile_id, &updated_mca).await?;
+
let response = updated_mca.foreign_try_into()?;
Ok(service_api::ApplicationResponse::Json(response))
diff --git a/crates/router/src/core/routing.rs b/crates/router/src/core/routing.rs
index 0078bfcea0..d5d73c647c 100644
--- a/crates/router/src/core/routing.rs
+++ b/crates/router/src/core/routing.rs
@@ -896,6 +896,23 @@ pub async fn link_routing_config(
}
}
}
+
+ // redact cgraph cache on rule activation
+ helpers::redact_cgraph_cache(
+ &state,
+ merchant_context.get_merchant_account().get_id(),
+ business_profile.get_id(),
+ )
+ .await?;
+
+ // redact routing cache on rule activation
+ helpers::redact_routing_cache(
+ &state,
+ merchant_context.get_merchant_account().get_id(),
+ business_profile.get_id(),
+ )
+ .await?;
+
metrics::ROUTING_LINK_CONFIG_SUCCESS_RESPONSE.add(1, &[]);
Ok(service_api::ApplicationResponse::Json(
routing_algorithm.foreign_into(),
@@ -1109,12 +1126,28 @@ pub async fn unlink_routing_config(
db,
key_manager_state,
merchant_context.get_merchant_key_store(),
- business_profile,
+ business_profile.clone(),
routing_algorithm,
&transaction_type,
)
.await?;
+ // redact cgraph cache on rule activation
+ helpers::redact_cgraph_cache(
+ &state,
+ merchant_context.get_merchant_account().get_id(),
+ business_profile.get_id(),
+ )
+ .await?;
+
+ // redact routing cache on rule activation
+ helpers::redact_routing_cache(
+ &state,
+ merchant_context.get_merchant_account().get_id(),
+ business_profile.get_id(),
+ )
+ .await?;
+
metrics::ROUTING_UNLINK_CONFIG_SUCCESS_RESPONSE.add(1, &[]);
Ok(service_api::ApplicationResponse::Json(response))
}
diff --git a/crates/router/src/core/routing/helpers.rs b/crates/router/src/core/routing/helpers.rs
index 27a83ea0b9..03e24ca2bf 100644
--- a/crates/router/src/core/routing/helpers.rs
+++ b/crates/router/src/core/routing/helpers.rs
@@ -2,11 +2,9 @@
//!
//! Functions that are used to perform the retrieval of merchant's
//! routing dict, configs, defaults
-use std::fmt::Debug;
-#[cfg(all(feature = "dynamic_routing", feature = "v1"))]
-use std::str::FromStr;
#[cfg(all(feature = "dynamic_routing", feature = "v1"))]
use std::sync::Arc;
+use std::{fmt::Debug, str::FromStr};
#[cfg(feature = "v1")]
use api_models::open_router;
@@ -2761,3 +2759,79 @@ pub async fn redact_cgraph_cache(
Ok(())
}
+
+pub async fn redact_routing_cache(
+ state: &SessionState,
+ merchant_id: &id_type::MerchantId,
+ profile_id: &id_type::ProfileId,
+) -> RouterResult<()> {
+ let routing_payments_key = format!(
+ "routing_config_{}_{}",
+ merchant_id.get_string_repr(),
+ profile_id.get_string_repr(),
+ );
+ let routing_payouts_key = format!(
+ "routing_config_po_{}_{}",
+ merchant_id.get_string_repr(),
+ profile_id.get_string_repr(),
+ );
+
+ let routing_payouts_cache_key = cache::CacheKind::Routing(routing_payouts_key.clone().into());
+ let routing_payments_cache_key = cache::CacheKind::CGraph(routing_payments_key.clone().into());
+ cache::redact_from_redis_and_publish(
+ state.store.get_cache_store().as_ref(),
+ [routing_payouts_cache_key, routing_payments_cache_key],
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to invalidate the routing cache")?;
+
+ Ok(())
+}
+
+pub async fn update_default_fallback_on_mca_update(
+ state: &SessionState,
+ merchant_id: &id_type::MerchantId,
+ profile_id: &id_type::ProfileId,
+ updated_mca: &hyperswitch_domain_models::merchant_connector_account::MerchantConnectorAccount,
+) -> RouterResult<()> {
+ let db = state.store.as_ref();
+ let merchant_id_str = merchant_id.get_string_repr();
+ let profile_id_str = profile_id.get_string_repr();
+ let txn_type = &storage::enums::TransactionType::from(updated_mca.connector_type);
+
+ let mut connectors = get_merchant_default_config(db, merchant_id_str, txn_type).await?;
+
+ let choice = routing_types::RoutableConnectorChoice {
+ choice_kind: routing_types::RoutableChoiceKind::FullStruct,
+ connector: api_models::enums::RoutableConnectors::from_str(
+ &updated_mca.connector_name.to_string(),
+ )
+ .change_context(errors::ApiErrorResponse::IncorrectConnectorNameGiven)
+ .attach_printable(format!(
+ "Unable to parse RoutableConnectors from connector: {}",
+ updated_mca.connector_name
+ ))?,
+ merchant_connector_id: Some(updated_mca.get_id().clone()),
+ };
+
+ if updated_mca.disabled.unwrap_or(false)
+ || updated_mca.status == api_models::enums::ConnectorStatus::Inactive
+ {
+ // remove if disabled
+ connectors.retain(|c| c.merchant_connector_id != Some(updated_mca.get_id().clone()));
+ } else {
+ // ensure it is present if enabled
+ if !connectors.contains(&choice) {
+ connectors.push(choice);
+ }
+ }
+
+ // update merchant config
+ update_merchant_default_config(db, merchant_id_str, connectors.clone(), txn_type).await?;
+
+ // update profile config
+ update_merchant_default_config(db, profile_id_str, connectors, txn_type).await?;
+
+ Ok(())
+}