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: Added new headers for comparison service HS<>UCS (#10770)

Amitsingh Tanwar35c15e9922a1
diff --git a/crates/common_utils/src/consts.rs b/crates/common_utils/src/consts.rs
index 27c894aa60..8702140362 100644
--- a/crates/common_utils/src/consts.rs
+++ b/crates/common_utils/src/consts.rs
@@ -166,6 +166,9 @@ pub const X_FLOW_NAME: &str = "x-flow";
/// Connector name
pub const X_CONNECTOR_NAME: &str = "x-connector";
+/// Sub-flow name
+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";
diff --git a/crates/hyperswitch_interfaces/src/helpers.rs b/crates/hyperswitch_interfaces/src/helpers.rs
index c74f47d03b..0d0d5b0b12 100644
--- a/crates/hyperswitch_interfaces/src/helpers.rs
+++ b/crates/hyperswitch_interfaces/src/helpers.rs
@@ -1,4 +1,7 @@
-use common_utils::{errors as common_utils_errors, request};
+use common_utils::{
+ consts::{X_CONNECTOR_NAME, X_SUB_FLOW_NAME},
+ errors as common_utils_errors, request,
+};
use error_stack::ResultExt;
use hyperswitch_domain_models::router_data;
use masking;
@@ -44,6 +47,9 @@ where
{
logger::info!("Simulating UCS call for shadow mode comparison");
+ let connector_name = hyperswitch_router_data.connector.clone();
+ let sub_flow_name = api_client::get_flow_name::<F>().ok();
+
let [hyperswitch_data, unified_connector_service_data] = [
(hyperswitch_router_data, "hyperswitch"),
(unified_connector_service_router_data, "ucs"),
@@ -67,6 +73,8 @@ where
state,
comparison_data,
comparison_service_config,
+ connector_name,
+ sub_flow_name,
request_id,
)
.await
@@ -81,6 +89,8 @@ pub async fn send_comparison_data(
state: &dyn api_client::ApiClientWrapper,
comparison_data: ComparisonData,
comparison_service_config: types::ComparisonServiceConfig,
+ connector_name: String,
+ sub_flow_name: Option<String>,
request_id: Option<String>,
) -> common_utils_errors::CustomResult<(), errors::HttpClientError> {
let mut request = request::RequestBuilder::new()
@@ -91,6 +101,12 @@ pub async fn send_comparison_data(
.set_body(request::RequestContent::Json(Box::new(comparison_data)))
.build();
+ request.add_header(X_CONNECTOR_NAME, masking::Maskable::Normal(connector_name));
+
+ if let Some(sub_flow_name) = sub_flow_name.filter(|name| !name.is_empty()) {
+ request.add_header(X_SUB_FLOW_NAME, masking::Maskable::Normal(sub_flow_name));
+ }
+
if let Some(req_id) = request_id {
request.add_header(consts::X_REQUEST_ID, masking::Maskable::Normal(req_id));
}
diff --git a/crates/router/src/core/refunds.rs b/crates/router/src/core/refunds.rs
index 24ad62b2af..272bb39e3d 100644
--- a/crates/router/src/core/refunds.rs
+++ b/crates/router/src/core/refunds.rs
@@ -81,7 +81,7 @@ pub async fn refund_create_core(
current_flow: "refund".into(),
field_name: "status".into(),
current_value: payment_intent.status.to_string(),
- states: "succeeded, partially_captured".to_string()
+ states: "succeeded, partially_captured".to_string(),
})
.attach_printable("unable to refund for a unsuccessful payment intent"))
},
@@ -98,7 +98,7 @@ pub async fn refund_create_core(
utils::when(amount <= MinorUnit::new(0), || {
Err(report!(errors::ApiErrorResponse::InvalidDataFormat {
field_name: "amount".to_string(),
- expected_format: "positive integer".to_string()
+ expected_format: "positive integer".to_string(),
})
.attach_printable("amount less than or equal to zero"))
})?;
@@ -108,9 +108,8 @@ pub async fn refund_create_core(
&req.payment_id,
merchant_id,
platform.get_processor().get_account().storage_scheme,
- platform.get_processor().get_key_store(),
- )
- .await
+ platform.get_processor().get_key_store()
+ ).await
.to_not_found_response(errors::ApiErrorResponse::SuccessfulPaymentNotFound)?;
let creds_identifier = req
@@ -377,7 +376,7 @@ pub async fn trigger_refund_to_gateway(
(
"merchant_id",
platform.get_processor().get_account().get_id().clone()
- ),
+ )
),
);
diesel_refund::RefundUpdate::ErrorUpdate {
@@ -404,7 +403,7 @@ pub async fn trigger_refund_to_gateway(
"connector",
connector.connector_name.to_string(),
)),
- )
+ );
}
let (connector_refund_id, processor_refund_data) =
ConnectorTransactionId::form_id_and_data(response.connector_refund_id);
@@ -594,47 +593,48 @@ async fn execute_refund_execute_via_direct_with_ucs_shadow(
let direct_router_data_for_comparison = direct_result.as_ref().ok().cloned();
tokio::spawn(
- async move {
- let ucs_result =
- unified_connector_service::call_unified_connector_service_for_refund_execute(
- &ucs_state,
- &ucs_platform,
- ucs_router_data,
- ExecutionMode::Shadow,
- merchant_connector_account,
- )
- .await;
-
- match (ucs_result, direct_router_data_for_comparison) {
- (Ok(ucs_router_data), Some(direct_router_data)) => {
- unified_connector_service::serialize_router_data_and_send_to_comparison_service(
- &ucs_state,
- direct_router_data,
- ucs_router_data,
- )
- .await
- .inspect_err(|e| {
- router_env::logger::debug!(
- "Shadow UCS refund execute comparison failed: {:?}",
- e
- );
- })
- .ok();
- }
- (Err(e), _) => {
- router_env::logger::debug!(
- "Skipping refund execute comparison - UCS shadow execute failed: {:?}",
- e
- );
- }
- (_, None) => {
- router_env::logger::debug!(
- "Skipping refund execute comparison - direct execute failed"
- );
+ (
+ async move {
+ let ucs_result =
+ unified_connector_service::call_unified_connector_service_for_refund_execute(
+ &ucs_state,
+ &ucs_platform,
+ ucs_router_data,
+ ExecutionMode::Shadow,
+ merchant_connector_account
+ ).await;
+
+ match (ucs_result, direct_router_data_for_comparison) {
+ (Ok(ucs_router_data), Some(direct_router_data)) => {
+ Box::pin(
+ unified_connector_service::serialize_router_data_and_send_to_comparison_service(
+ &ucs_state,
+ direct_router_data,
+ ucs_router_data
+ )
+ ).await
+ .inspect_err(|e| {
+ router_env::logger::debug!(
+ "Shadow UCS refund execute comparison failed: {:?}",
+ e
+ );
+ })
+ .ok();
+ }
+ (Err(e), _) => {
+ router_env::logger::debug!(
+ "Skipping refund execute comparison - UCS shadow execute failed: {:?}",
+ e
+ );
+ }
+ (_, None) => {
+ router_env::logger::debug!(
+ "Skipping refund execute comparison - direct execute failed"
+ );
+ }
}
}
- }
- .instrument(tracing::Span::current()),
+ ).instrument(tracing::Span::current())
);
// Return PRIMARY result (Direct connector response)
@@ -752,7 +752,7 @@ pub async fn refund_retrieve_core(
..refund
};
- let (response, raw_connector_response) = if should_call_refund(
+ let (response, raw_connector_response) = (if should_call_refund(
&refund,
request.force_sync.unwrap_or(false),
request.all_keys_required.unwrap_or(false),
@@ -770,7 +770,7 @@ pub async fn refund_retrieve_core(
.await
} else {
Ok((refund, None))
- }?;
+ })?;
Ok((response, raw_connector_response))
}
@@ -975,7 +975,7 @@ pub async fn sync_refund_with_gateway(
(
"merchant_id",
platform.get_processor().get_account().get_id().clone()
- ),
+ )
),
);
let (refund_connector_transaction_id, processor_refund_data) = err
@@ -1113,43 +1113,47 @@ async fn execute_refund_sync_via_direct_with_ucs_shadow(
let direct_router_data_for_comparison = direct_result.as_ref().ok().cloned();
tokio::spawn(
- async move {
- let ucs_result =
- unified_connector_service::call_unified_connector_service_for_refund_sync(
- &state,
- &platform,
- router_data,
- ExecutionMode::Shadow,
- merchant_connector_account,
- )
- .await;
-
- match (ucs_result, direct_router_data_for_comparison) {
- (Ok(ucs_router_data), Some(direct_router_data)) => {
- unified_connector_service::serialize_router_data_and_send_to_comparison_service(
- &state,
- direct_router_data,
- ucs_router_data,
- )
- .await
- .inspect_err(|e| {
- router_env::logger::debug!("Shadow UCS sync comparison failed: {:?}", e);
- })
- .ok();
- }
- (Err(_), _) => {
- router_env::logger::debug!(
- "Skipping refund sync comparison - UCS shadow sync failed"
- );
- }
- (_, None) => {
- router_env::logger::debug!(
- "Skipping refund sync comparison - direct sync failed"
- );
+ (
+ async move {
+ let ucs_result =
+ unified_connector_service::call_unified_connector_service_for_refund_sync(
+ &state,
+ &platform,
+ router_data,
+ ExecutionMode::Shadow,
+ merchant_connector_account
+ ).await;
+
+ match (ucs_result, direct_router_data_for_comparison) {
+ (Ok(ucs_router_data), Some(direct_router_data)) => {
+ Box::pin(
+ unified_connector_service::serialize_router_data_and_send_to_comparison_service(
+ &state,
+ direct_router_data,
+ ucs_router_data
+ )
+ ).await
+ .inspect_err(|e| {
+ router_env::logger::debug!(
+ "Shadow UCS sync comparison failed: {:?}",
+ e
+ );
+ })
+ .ok();
+ }
+ (Err(_), _) => {
+ router_env::logger::debug!(
+ "Skipping refund sync comparison - UCS shadow sync failed"
+ );
+ }
+ (_, None) => {
+ router_env::logger::debug!(
+ "Skipping refund sync comparison - direct sync failed"
+ );
+ }
}
}
- }
- .instrument(tracing::Span::current()),
+ ).instrument(tracing::Span::current())
);
direct_result
@@ -1231,15 +1235,18 @@ pub async fn validate_and_create_refund(
utils::when(predicate.unwrap_or(false), || {
Err(report!(errors::ApiErrorResponse::InvalidDataFormat {
field_name: "merchant_id".to_string(),
- expected_format: "merchant_id from merchant account".to_string()
+ expected_format: "merchant_id from merchant account".to_string(),
})
.attach_printable("invalid merchant_id in request"))
})?;
- let connector_transaction_id = payment_attempt.clone().connector_transaction_id.ok_or_else(|| {
- report!(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Transaction in invalid. Missing field \"connector_transaction_id\" in payment_attempt.")
- })?;
+ let connector_transaction_id = payment_attempt
+ .clone()
+ .connector_transaction_id.ok_or_else(|| {
+ report!(errors::ApiErrorResponse::InternalServerError).attach_printable(
+ "Transaction in invalid. Missing field \"connector_transaction_id\" in payment_attempt."
+ )
+ })?;
let all_refunds = db
.find_refund_by_merchant_id_connector_transaction_id(
@@ -1258,7 +1265,7 @@ pub async fn validate_and_create_refund(
field_name: "created_at".to_string(),
expected_format: format!(
"created_at not older than {} days",
- state.conf.refund.max_age,
+ state.conf.refund.max_age
),
})?;
@@ -1733,17 +1740,26 @@ pub async fn schedule_refund_execution(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to find the process id")?;
- let result = match refund.refund_status {
+ let result = (match refund.refund_status {
enums::RefundStatus::Pending | enums::RefundStatus::ManualReview => {
match (refund.sent_to_gateway, refund_process) {
(false, None) => {
// Execute the refund task based on refund_type
match refund_type {
api_models::refunds::RefundType::Scheduled => {
- add_refund_execute_task(db, &refund, runner, state.conf.application_source)
- .await
+ add_refund_execute_task(
+ db,
+ &refund,
+ runner,
+ state.conf.application_source
+ ).await
.change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable_lazy(|| format!("Failed while pushing refund execute task to scheduler, refund_id: {}", refund.refund_id))?;
+ .attach_printable_lazy(||
+ format!(
+ "Failed while pushing refund execute task to scheduler, refund_id: {}",
+ refund.refund_id
+ )
+ )?;
Ok((refund, None))
}
@@ -1762,13 +1778,21 @@ pub async fn schedule_refund_execution(
match update_refund {
Ok((updated_refund_data, raw_connector_response)) => {
- add_refund_sync_task(db, &updated_refund_data, runner, state.conf.application_source)
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable_lazy(|| format!(
- "Failed while pushing refund sync task in scheduler: refund_id: {}",
- refund.refund_id
- ))?;
+ add_refund_sync_task(
+ db,
+ &updated_refund_data,
+ runner,
+ state.conf.application_source
+ ).await
+ .change_context(
+ errors::ApiErrorResponse::InternalServerError
+ )
+ .attach_printable_lazy(||
+ format!(
+ "Failed while pushing refund sync task in scheduler: refund_id: {}",
+ refund.refund_id
+ )
+ )?;
Ok((updated_refund_data, raw_connector_response))
}
Err(err) => Err(err),
@@ -1781,10 +1805,19 @@ pub async fn schedule_refund_execution(
//[#300]: return refund status response
match refund_type {
api_models::refunds::RefundType::Scheduled => {
- add_refund_sync_task(db, &refund, runner, state.conf.application_source)
- .await
+ add_refund_sync_task(
+ db,
+ &refund,
+ runner,
+ state.conf.application_source
+ ).await
.change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable_lazy(|| format!("Failed while pushing refund sync task in scheduler: refund_id: {}", refund.refund_id))?;
+ .attach_printable_lazy(||
+ format!(
+ "Failed while pushing refund sync task in scheduler: refund_id: {}",
+ refund.refund_id
+ )
+ )?;
Ok((refund, None))
}
api_models::refunds::RefundType::Instant => {
@@ -1798,7 +1831,7 @@ pub async fn schedule_refund_execution(
}
// [#255]: This is not allowed to be otherwise or all
_ => Ok((refund, None)),
- }?;
+ })?;
Ok(result)
}
@@ -1858,7 +1891,7 @@ pub async fn sync_refund_with_gateway_workflow(
refund_tracker.clone(),
business_status::COMPLETED_BY_PT,
)
- .await?
+ .await?;
}
_ => {
_ = retry_refund_sync_task(
@@ -2033,7 +2066,7 @@ pub async fn trigger_refund_execute_workflow(
)
.await?;
}
- };
+ }
Ok(())
}
diff --git a/crates/router/src/core/unified_connector_service.rs b/crates/router/src/core/unified_connector_service.rs
index a200527a64..70817002af 100644
--- a/crates/router/src/core/unified_connector_service.rs
+++ b/crates/router/src/core/unified_connector_service.rs
@@ -10,7 +10,7 @@ use common_enums::{
#[cfg(feature = "v2")]
use common_utils::consts::BASE64_ENGINE;
use common_utils::{
- consts::X_FLOW_NAME,
+ consts::{X_CONNECTOR_NAME, X_FLOW_NAME, X_SUB_FLOW_NAME},
errors::CustomResult,
ext_traits::ValueExt,
id_type,
@@ -1731,6 +1731,9 @@ where
{
logger::info!("Simulating UCS call for shadow mode comparison");
+ let connector_name = hyperswitch_router_data.connector.clone();
+ let sub_flow_name = get_flow_name::<F>().ok();
+
let [hyperswitch_data, unified_connector_service_data] = [
(hyperswitch_router_data, "hyperswitch"),
(unified_connector_service_router_data, "ucs"),
@@ -1750,7 +1753,7 @@ where
hyperswitch_data,
unified_connector_service_data,
};
- let _ = send_comparison_data(state, comparison_data)
+ let _ = send_comparison_data(state, comparison_data, connector_name, sub_flow_name)
.await
.map_err(|e| {
logger::debug!("Failed to send comparison data: {:?}", e);
@@ -1762,6 +1765,8 @@ where
pub async fn send_comparison_data(
state: &SessionState,
comparison_data: ComparisonData,
+ connector_name: String,
+ sub_flow_name: Option<String>,
) -> RouterResult<()> {
// Check if comparison service is enabled
let comparison_config = match state.conf.comparison_service.as_ref() {
@@ -1781,6 +1786,13 @@ pub async fn send_comparison_data(
.header(X_FLOW_NAME, "router-data")
.set_body(RequestContent::Json(Box::new(comparison_data)))
.build();
+
+ request.add_header(X_CONNECTOR_NAME, masking::Maskable::Normal(connector_name));
+
+ if let Some(sub_flow_name) = sub_flow_name.filter(|name| !name.is_empty()) {
+ request.add_header(X_SUB_FLOW_NAME, masking::Maskable::Normal(sub_flow_name));
+ }
+
if let Some(req_id) = &state.request_id {
request.add_header(X_REQUEST_ID, masking::Maskable::Normal(req_id.to_string()));
}