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

chore: Conditional Audit Event of UCS only during Primary mode (#11065)

gopikrishna0007e18983c482e
diff --git a/crates/common_utils/src/consts.rs b/crates/common_utils/src/consts.rs
index 8702140362..ee3f881664 100644
--- a/crates/common_utils/src/consts.rs
+++ b/crates/common_utils/src/consts.rs
@@ -172,6 +172,9 @@ pub const X_SUB_FLOW_NAME: &str = "x-sub-flow";
/// Unified Connector Service Mode
pub const X_UNIFIED_CONNECTOR_SERVICE_MODE: &str = "x-shadow-mode";
+/// Config Override Header for UCS
+pub const X_CONFIG_OVERRIDE: &str = "x-config-override";
+
/// Chat Session ID
pub const X_CHAT_SESSION_ID: &str = "x-chat-session-id";
diff --git a/crates/external_services/src/grpc_client.rs b/crates/external_services/src/grpc_client.rs
index d23be13dbf..70ded8df6f 100644
--- a/crates/external_services/src/grpc_client.rs
+++ b/crates/external_services/src/grpc_client.rs
@@ -166,11 +166,20 @@ pub struct GrpcHeadersUcs {
merchant_reference_id: Option<ucs_types::UcsReferenceId>,
shadow_mode: Option<bool>,
+ /// Config override as JSON string to pass to UCS
+ config_override: Option<String>,
}
/// Type aliase for GrpcHeaders builder in initial stage
-pub type GrpcHeadersUcsBuilderInitial =
- GrpcHeadersUcsBuilder<((String,), (Option<RequestId>,), (), (), (), (Option<bool>,))>;
+pub type GrpcHeadersUcsBuilderInitial = GrpcHeadersUcsBuilder<(
+ (String,),
+ (Option<RequestId>,),
+ (),
+ (),
+ (),
+ (Option<bool>,),
+ (Option<String>,),
+)>;
/// Type aliase for GrpcHeaders builder in intermediate stage
pub type GrpcHeadersUcsBuilderFinal = GrpcHeadersUcsBuilder<(
(String,),
@@ -179,6 +188,7 @@ pub type GrpcHeadersUcsBuilderFinal = GrpcHeadersUcsBuilder<(
(Option<String>,),
(Option<ucs_types::UcsReferenceId>,),
(Option<bool>,),
+ (Option<String>,),
)>;
/// struct to represent set of Lineage ids
diff --git a/crates/external_services/src/grpc_client/unified_connector_service.rs b/crates/external_services/src/grpc_client/unified_connector_service.rs
index de03c7a83e..0df62f59b9 100644
--- a/crates/external_services/src/grpc_client/unified_connector_service.rs
+++ b/crates/external_services/src/grpc_client/unified_connector_service.rs
@@ -798,6 +798,17 @@ pub fn build_unified_connector_service_grpc_headers(
meta: ConnectorAuthMetadata,
grpc_headers: GrpcHeadersUcs,
) -> Result<MetadataMap, UnifiedConnectorServiceError> {
+ // Destructure grpc_headers to ensure all fields are handled
+ let GrpcHeadersUcs {
+ tenant_id,
+ request_id,
+ lineage_ids,
+ external_vault_proxy_metadata,
+ merchant_reference_id,
+ shadow_mode,
+ config_override,
+ } = grpc_headers;
+
let mut metadata = MetadataMap::new();
let parse =
|key: &str, value: &str| -> Result<MetadataValue<_>, UnifiedConnectorServiceError> {
@@ -850,26 +861,23 @@ pub fn build_unified_connector_service_grpc_headers(
parse(common_utils_consts::X_MERCHANT_ID, meta.merchant_id.peek())?,
);
- if let Some(external_vault_proxy_metadata) = grpc_headers.external_vault_proxy_metadata {
+ if let Some(external_vault_proxy_metadata) = external_vault_proxy_metadata {
metadata.append(
consts::UCS_HEADER_EXTERNAL_VAULT_METADATA,
parse("external_vault_metadata", &external_vault_proxy_metadata)?,
);
};
- let lineage_ids_str = grpc_headers
- .lineage_ids
- .get_url_encoded_string()
- .map_err(|err| {
- logger::error!(?err);
- UnifiedConnectorServiceError::HeaderInjectionFailed(consts::UCS_LINEAGE_IDS.to_string())
- })?;
+ let lineage_ids_str = lineage_ids.get_url_encoded_string().map_err(|err| {
+ logger::error!(?err);
+ UnifiedConnectorServiceError::HeaderInjectionFailed(consts::UCS_LINEAGE_IDS.to_string())
+ })?;
metadata.append(
consts::UCS_LINEAGE_IDS,
parse(consts::UCS_LINEAGE_IDS, &lineage_ids_str)?,
);
- if let Some(reference_id) = grpc_headers.merchant_reference_id {
+ if let Some(reference_id) = merchant_reference_id {
metadata.append(
consts::UCS_HEADER_REFERENCE_ID,
parse(
@@ -879,14 +887,14 @@ pub fn build_unified_connector_service_grpc_headers(
);
};
- if let Some(ref request_id) = grpc_headers.request_id {
+ if let Some(ref request_id) = request_id {
metadata.append(
common_utils_consts::X_REQUEST_ID,
parse(common_utils_consts::X_REQUEST_ID, request_id.as_str())?,
);
};
- if let Some(shadow_mode) = grpc_headers.shadow_mode {
+ if let Some(shadow_mode) = shadow_mode {
metadata.append(
common_utils_consts::X_UNIFIED_CONNECTOR_SERVICE_MODE,
parse(
@@ -896,14 +904,20 @@ pub fn build_unified_connector_service_grpc_headers(
);
}
- if let Err(err) = grpc_headers
- .tenant_id
+ if let Some(config_override) = config_override {
+ metadata.append(
+ common_utils_consts::X_CONFIG_OVERRIDE,
+ parse(common_utils_consts::X_CONFIG_OVERRIDE, &config_override)?,
+ );
+ }
+
+ if let Err(err) = tenant_id
.parse()
.map(|tenant_id| metadata.append(common_utils_consts::TENANT_HEADER, tenant_id))
{
logger::error!(
header_parse_error=?err,
- tenant_id=?grpc_headers.tenant_id,
+ tenant_id=?tenant_id,
"Failed to parse tenant_id header for UCS gRPC request: {}",
common_utils_consts::TENANT_HEADER
);
diff --git a/crates/router/src/core/payments/flows/authorize_flow.rs b/crates/router/src/core/payments/flows/authorize_flow.rs
index 073d9d761f..e74246ecb8 100644
--- a/crates/router/src/core/payments/flows/authorize_flow.rs
+++ b/crates/router/src/core/payments/flows/authorize_flow.rs
@@ -1470,6 +1470,7 @@ pub async fn call_unified_connector_service_pre_authenticate(
state,
payment_pre_authenticate_request,
headers_builder,
+ unified_connector_service_execution_mode,
|mut router_data, payment_pre_authenticate_request, grpc_headers| async move {
let response = client
.payment_pre_authenticate(
diff --git a/crates/router/src/core/payments/flows/complete_authorize_flow.rs b/crates/router/src/core/payments/flows/complete_authorize_flow.rs
index 1fef6d35cb..62e3d2ea7a 100644
--- a/crates/router/src/core/payments/flows/complete_authorize_flow.rs
+++ b/crates/router/src/core/payments/flows/complete_authorize_flow.rs
@@ -673,6 +673,7 @@ pub async fn call_unified_connector_service_authenticate(
state,
payment_authenticate_request,
headers_builder,
+ unified_connector_service_execution_mode,
|mut router_data, payment_authenticate_request, grpc_headers| async move {
let response = client
.payment_authenticate(
@@ -781,6 +782,7 @@ pub async fn call_unified_connector_service_post_authenticate(
state,
payment_post_authenticate_request,
headers_builder,
+ unified_connector_service_execution_mode,
|mut router_data, payment_post_authenticate_request, grpc_headers| async move {
let response = client
.payment_post_authenticate(
diff --git a/crates/router/src/core/payments/flows/external_proxy_flow.rs b/crates/router/src/core/payments/flows/external_proxy_flow.rs
index e6a78c7549..7b0feb0650 100644
--- a/crates/router/src/core/payments/flows/external_proxy_flow.rs
+++ b/crates/router/src/core/payments/flows/external_proxy_flow.rs
@@ -407,6 +407,7 @@ impl Feature<api::ExternalVaultProxy, types::ExternalVaultProxyPaymentsData>
state,
payment_authorize_request.clone(),
headers_builder,
+ unified_connector_service_execution_mode,
|mut router_data, payment_authorize_request, grpc_headers| async move {
let response = Box::pin(client
.payment_authorize(
diff --git a/crates/router/src/core/payments/gateway/access_token_gateway.rs b/crates/router/src/core/payments/gateway/access_token_gateway.rs
index aaab001869..10f4b1162e 100644
--- a/crates/router/src/core/payments/gateway/access_token_gateway.rs
+++ b/crates/router/src/core/payments/gateway/access_token_gateway.rs
@@ -125,6 +125,7 @@ where
state,
create_access_token_request,
header_payload,
+ unified_connector_service_execution_mode,
|mut router_data, create_access_token_request, grpc_headers| async move {
let response = client
.create_access_token(
diff --git a/crates/router/src/core/payments/gateway/authorize_gateway.rs b/crates/router/src/core/payments/gateway/authorize_gateway.rs
index 0b2cc00bb7..e1b80023a9 100644
--- a/crates/router/src/core/payments/gateway/authorize_gateway.rs
+++ b/crates/router/src/core/payments/gateway/authorize_gateway.rs
@@ -127,6 +127,7 @@ where
state,
payment_repeat_request,
grpc_headers,
+ unified_connector_service_execution_mode,
|mut router_data, payment_repeat_request, grpc_headers| async move {
logger::debug!("Calling UCS payment_repeat gRPC method");
let response = Box::pin(client.payment_repeat(
@@ -199,6 +200,7 @@ where
state,
granular_authorize_request,
grpc_headers,
+ unified_connector_service_execution_mode,
|mut router_data, granular_authorize_request, grpc_headers| async move {
let response = Box::pin(client.payment_authorize_granular(
granular_authorize_request,
diff --git a/crates/router/src/core/payments/gateway/cancel_gateway.rs b/crates/router/src/core/payments/gateway/cancel_gateway.rs
index 7d170180af..d0b411eb9d 100644
--- a/crates/router/src/core/payments/gateway/cancel_gateway.rs
+++ b/crates/router/src/core/payments/gateway/cancel_gateway.rs
@@ -108,6 +108,7 @@ where
state,
payment_void_request,
header_payload,
+ unified_connector_service_execution_mode,
|mut router_data, payment_void_request, grpc_headers| async move {
let response = client
.payment_cancel(payment_void_request, connector_auth_metadata, grpc_headers)
diff --git a/crates/router/src/core/payments/gateway/capture_gateway.rs b/crates/router/src/core/payments/gateway/capture_gateway.rs
index 21802e8d82..971b2075ae 100644
--- a/crates/router/src/core/payments/gateway/capture_gateway.rs
+++ b/crates/router/src/core/payments/gateway/capture_gateway.rs
@@ -105,6 +105,7 @@ where
state,
payment_capture_request,
header_payload,
+ unified_connector_service_execution_mode,
|mut router_data, payment_capture_request, grpc_headers| async move {
let response = client
.payment_capture(
diff --git a/crates/router/src/core/payments/gateway/complete_authorize_gateway.rs b/crates/router/src/core/payments/gateway/complete_authorize_gateway.rs
index feb41aad29..f24a4e5271 100644
--- a/crates/router/src/core/payments/gateway/complete_authorize_gateway.rs
+++ b/crates/router/src/core/payments/gateway/complete_authorize_gateway.rs
@@ -112,6 +112,7 @@ where
state,
granular_authorize_request,
header_payload,
+ unified_connector_service_execution_mode,
|mut router_data, granular_authorize_request, grpc_headers| async move {
let response = Box::pin(client.payment_authorize_granular(
granular_authorize_request,
diff --git a/crates/router/src/core/payments/gateway/create_customer_gateway.rs b/crates/router/src/core/payments/gateway/create_customer_gateway.rs
index 47059bcc1e..786c93180d 100644
--- a/crates/router/src/core/payments/gateway/create_customer_gateway.rs
+++ b/crates/router/src/core/payments/gateway/create_customer_gateway.rs
@@ -107,6 +107,7 @@ where
state,
create_connector_customer_request,
grpc_headers,
+ unified_connector_service_execution_mode,
|mut router_data, create_connector_customer_request, grpc_headers| async move {
let response = Box::pin(client.create_connector_customer(
create_connector_customer_request,
diff --git a/crates/router/src/core/payments/gateway/create_order_gateway.rs b/crates/router/src/core/payments/gateway/create_order_gateway.rs
index f78f8199d1..af5a1a70b5 100644
--- a/crates/router/src/core/payments/gateway/create_order_gateway.rs
+++ b/crates/router/src/core/payments/gateway/create_order_gateway.rs
@@ -110,6 +110,7 @@ where
state,
create_order_request,
header_payload,
+ unified_connector_service_execution_mode,
|mut router_data, create_order_request, grpc_headers| async move {
let response = Box::pin(client.payment_create_order(
create_order_request,
diff --git a/crates/router/src/core/payments/gateway/incremental_authorization_gateway.rs b/crates/router/src/core/payments/gateway/incremental_authorization_gateway.rs
index a328f881ba..465d77b38e 100644
--- a/crates/router/src/core/payments/gateway/incremental_authorization_gateway.rs
+++ b/crates/router/src/core/payments/gateway/incremental_authorization_gateway.rs
@@ -121,6 +121,7 @@ where
state,
incremental_authorization_request,
header_payload,
+ unified_connector_service_execution_mode,
|mut router_data, incremental_authorization_request, grpc_headers| async move {
let response = Box::pin(client.incremental_authorization(
incremental_authorization_request,
diff --git a/crates/router/src/core/payments/gateway/payment_method_token_create_gateway.rs b/crates/router/src/core/payments/gateway/payment_method_token_create_gateway.rs
index 64031f00f7..cbce196919 100644
--- a/crates/router/src/core/payments/gateway/payment_method_token_create_gateway.rs
+++ b/crates/router/src/core/payments/gateway/payment_method_token_create_gateway.rs
@@ -118,6 +118,7 @@ where
state,
pm_token_create_request,
header_payload,
+ unified_connector_service_execution_mode,
|mut router_data, pm_token_create_request, grpc_headers| async move {
let response = Box::pin(client.payment_method_token_create(
pm_token_create_request,
diff --git a/crates/router/src/core/payments/gateway/psync_gateway.rs b/crates/router/src/core/payments/gateway/psync_gateway.rs
index 499f2742aa..a389535aa0 100644
--- a/crates/router/src/core/payments/gateway/psync_gateway.rs
+++ b/crates/router/src/core/payments/gateway/psync_gateway.rs
@@ -137,6 +137,7 @@ where
state,
payment_get_request,
header_payload,
+ unified_connector_service_execution_mode,
|mut router_data, payment_get_request, grpc_headers| async move {
let response = client
.payment_get(payment_get_request, connector_auth_metadata, grpc_headers)
diff --git a/crates/router/src/core/payments/gateway/session_gateway.rs b/crates/router/src/core/payments/gateway/session_gateway.rs
index a9a0efadcb..4dadb4ce4d 100644
--- a/crates/router/src/core/payments/gateway/session_gateway.rs
+++ b/crates/router/src/core/payments/gateway/session_gateway.rs
@@ -111,6 +111,7 @@ where
state,
sdk_session_token_request,
header_payload,
+ unified_connector_service_execution_mode,
|mut router_data, sdk_session_token_request, grpc_headers| async move {
let response = Box::pin(client.sdk_session_token(
sdk_session_token_request,
diff --git a/crates/router/src/core/payments/gateway/session_token_gateway.rs b/crates/router/src/core/payments/gateway/session_token_gateway.rs
index 4abe8e89ca..a8335834ba 100644
--- a/crates/router/src/core/payments/gateway/session_token_gateway.rs
+++ b/crates/router/src/core/payments/gateway/session_token_gateway.rs
@@ -113,6 +113,7 @@ where
state,
authorize_session_token_request,
header_payload,
+ unified_connector_service_execution_mode,
|mut router_data, authorize_session_token_request, grpc_headers| async move {
let response = client
.payment_session_token_create(
diff --git a/crates/router/src/core/payments/gateway/setup_mandate.rs b/crates/router/src/core/payments/gateway/setup_mandate.rs
index 9a636f0141..52493f99e9 100644
--- a/crates/router/src/core/payments/gateway/setup_mandate.rs
+++ b/crates/router/src/core/payments/gateway/setup_mandate.rs
@@ -108,6 +108,7 @@ where
state,
setup_mandate_request,
header_payload,
+ unified_connector_service_execution_mode,
|mut router_data, setup_mandate_request, grpc_headers| async move {
let response = Box::pin(client.payment_setup_mandate_granular(
setup_mandate_request,
diff --git a/crates/router/src/core/unified_connector_service.rs b/crates/router/src/core/unified_connector_service.rs
index 189c5bda55..2978e74dfd 100644
--- a/crates/router/src/core/unified_connector_service.rs
+++ b/crates/router/src/core/unified_connector_service.rs
@@ -1857,6 +1857,7 @@ pub async fn ucs_logging_wrapper<T, F, Fut, Req, Resp, GrpcReq, GrpcResp, FlowOu
state: &SessionState,
grpc_request: GrpcReq,
grpc_header_builder: external_services::grpc_client::GrpcHeadersUcsBuilderFinal,
+ execution_mode: ExecutionMode,
handler: F,
) -> RouterResult<(RouterData<T, Req, Resp>, FlowOutput)>
where
@@ -1943,37 +1944,40 @@ where
}
};
- let mut connector_event = ConnectorEvent::new(
- state.tenant.tenant_id.clone(),
- connector_name,
- std::any::type_name::<T>(),
- grpc_request_body,
- "grpc://unified-connector-service".to_string(),
- Method::Post,
- payment_id,
- merchant_id,
- state.request_id.as_ref(),
- external_latency,
- refund_id,
- dispute_id,
- payout_id,
- status_code,
- );
-
- // Set response body based on status code
- if let Some(body) = response_body {
- match status_code {
- 400..=599 => {
- connector_event.set_error_response_body(&body);
- }
- _ => {
- connector_event.set_response_body(&body);
+ // Only emit connector event during primary mode
+ if let ExecutionMode::Primary = execution_mode {
+ let mut connector_event = ConnectorEvent::new(
+ state.tenant.tenant_id.clone(),
+ connector_name,
+ std::any::type_name::<T>(),
+ grpc_request_body,
+ "grpc://unified-connector-service".to_string(),
+ Method::Post,
+ payment_id,
+ merchant_id,
+ state.request_id.as_ref(),
+ external_latency,
+ refund_id,
+ dispute_id,
+ payout_id,
+ status_code,
+ );
+
+ // Set response body based on status code
+ if let Some(body) = response_body {
+ match status_code {
+ 400..=599 => {
+ connector_event.set_error_response_body(&body);
+ }
+ _ => {
+ connector_event.set_response_body(&body);
+ }
}
}
- }
- // Emit event
- state.event_handler.log_event(&connector_event);
+ // Emit event
+ state.event_handler.log_event(&connector_event);
+ }
// Set external latency on router data
router_result.map(|mut router_data| {
@@ -1992,6 +1996,7 @@ pub async fn ucs_logging_wrapper_granular<T, F, Fut, Req, Resp, GrpcReq, FlowOut
state: &SessionState,
grpc_request: GrpcReq,
grpc_header_builder: external_services::grpc_client::GrpcHeadersUcsBuilderFinal,
+ execution_mode: ExecutionMode,
handler: F,
) -> CustomResult<(RouterData<T, Req, Resp>, FlowOutput), UnifiedConnectorServiceError>
where
@@ -2082,37 +2087,40 @@ where
}
};
- let mut connector_event = ConnectorEvent::new(
- state.tenant.tenant_id.clone(),
- connector_name,
- std::any::type_name::<T>(),
- grpc_request_body,
- "grpc://unified-connector-service".to_string(),
- Method::Post,
- payment_id,
- merchant_id,
- state.request_id.as_ref(),
- external_latency,
- refund_id,
- dispute_id,
- payout_id,
- status_code,
- );
-
- // Set response body based on status code
- if let Some(body) = response_body {
- match status_code {
- 400..=599 => {
- connector_event.set_error_response_body(&body);
- }
- _ => {
- connector_event.set_response_body(&body);
+ // Only emit connector event during primary mode
+ if let ExecutionMode::Primary = execution_mode {
+ let mut connector_event = ConnectorEvent::new(
+ state.tenant.tenant_id.clone(),
+ connector_name,
+ std::any::type_name::<T>(),
+ grpc_request_body,
+ "grpc://unified-connector-service".to_string(),
+ Method::Post,
+ payment_id,
+ merchant_id,
+ state.request_id.as_ref(),
+ external_latency,
+ refund_id,
+ dispute_id,
+ payout_id,
+ status_code,
+ );
+
+ // Set response body based on status code
+ if let Some(body) = response_body {
+ match status_code {
+ 400..=599 => {
+ connector_event.set_error_response_body(&body);
+ }
+ _ => {
+ connector_event.set_response_body(&body);
+ }
}
}
- }
- // Emit event
- state.event_handler.log_event(&connector_event);
+ // Emit event
+ state.event_handler.log_event(&connector_event);
+ }
// Set external latency on router data
router_result.map(|mut router_data| {
@@ -2269,6 +2277,7 @@ pub async fn call_unified_connector_service_for_refund_execute(
state,
ucs_refund_request,
grpc_header_builder,
+ execution_mode,
|router_data, grpc_request, grpc_headers| async move {
// Call UCS payment_refund method
let response = ucs_client
@@ -2344,6 +2353,7 @@ pub async fn call_unified_connector_service_for_refund_sync(
state,
ucs_refund_sync_request,
grpc_header_builder,
+ execution_mode,
|router_data, grpc_request, grpc_headers| async move {
// Call UCS refund_sync method
let response = ucs_client
diff --git a/crates/router/src/routes/app.rs b/crates/router/src/routes/app.rs
index 82f754ab1d..b64de01c87 100644
--- a/crates/router/src/routes/app.rs
+++ b/crates/router/src/routes/app.rs
@@ -188,10 +188,23 @@ impl SessionState {
ExecutionMode::Shadow => Some(true),
ExecutionMode::NotApplicable => None,
};
+ // For shadow mode, disable event publishing in UCS
+ let config_override = match unified_connector_service_execution_mode {
+ ExecutionMode::Shadow => Some(
+ serde_json::json!({
+ "events": {
+ "enabled": false
+ }
+ })
+ .to_string(),
+ ),
+ _ => None,
+ };
GrpcHeadersUcs::builder()
.tenant_id(tenant_id)
.request_id(request_id)
.shadow_mode(shadow_mode)
+ .config_override(config_override)
}
#[cfg(all(feature = "revenue_recovery", feature = "v2"))]
pub fn get_recovery_grpc_headers(&self) -> GrpcRecoveryHeaders {