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(core): Add all_keys_required in Capture, Refund and RSync Flows (#10178)

Anurag6f582dc0c4a9
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.json
index 563127dac5..93d21e1502 100644
--- a/api-reference/v1/openapi_spec_v1.json
+++ b/api-reference/v1/openapi_spec_v1.json
@@ -26202,6 +26202,11 @@
}
],
"nullable": true
+ },
+ "all_keys_required": {
+ "type": "boolean",
+ "description": "If true, returns stringified connector raw response body",
+ "nullable": true
}
}
},
@@ -33443,6 +33448,11 @@
}
],
"nullable": true
+ },
+ "all_keys_required": {
+ "type": "boolean",
+ "description": "If true, returns stringified connector raw response body",
+ "nullable": true
}
},
"additionalProperties": false
@@ -33554,6 +33564,11 @@
"type": "string",
"description": "Error message received from the issuer in case of failed refunds",
"nullable": true
+ },
+ "raw_connector_response": {
+ "type": "string",
+ "description": "Contains whole connector response",
+ "nullable": true
}
}
},
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index 771e80c6f6..acdae459ff 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -19800,6 +19800,11 @@
"description": "The Amount to be captured/ debited from the user's payment method. If not passed the full amount will be captured.",
"example": 6540,
"nullable": true
+ },
+ "return_raw_connector_response": {
+ "type": "boolean",
+ "description": "If true, returns stringified connector raw response body",
+ "nullable": true
}
}
},
@@ -24206,6 +24211,11 @@
"type": "string",
"description": "The reference id of the connector for the refund",
"nullable": true
+ },
+ "raw_connector_response": {
+ "type": "string",
+ "description": "Contains raw connector response",
+ "nullable": true
}
}
},
@@ -24291,6 +24301,11 @@
}
],
"nullable": true
+ },
+ "return_raw_connector_response": {
+ "type": "boolean",
+ "description": "If true, returns stringified connector raw response body",
+ "nullable": true
}
},
"additionalProperties": false
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs
index 17124b047c..bb21eb9aee 100644
--- a/crates/api_models/src/payments.rs
+++ b/crates/api_models/src/payments.rs
@@ -6032,6 +6032,8 @@ pub struct PaymentsCaptureRequest {
#[schema(value_type = Option<MerchantConnectorDetailsWrap>, deprecated)]
#[smithy(value_type = "Option<MerchantConnectorDetailsWrap>")]
pub merchant_connector_details: Option<admin::MerchantConnectorDetailsWrap>,
+ /// If true, returns stringified connector raw response body
+ pub all_keys_required: Option<bool>,
}
#[cfg(feature = "v2")]
@@ -6040,6 +6042,8 @@ pub struct PaymentsCaptureRequest {
/// The Amount to be captured/ debited from the user's payment method. If not passed the full amount will be captured.
#[schema(value_type = Option<i64>, example = 6540)]
pub amount_to_capture: Option<MinorUnit>,
+ /// If true, returns stringified connector raw response body
+ pub return_raw_connector_response: Option<bool>,
}
#[cfg(feature = "v2")]
diff --git a/crates/api_models/src/refunds.rs b/crates/api_models/src/refunds.rs
index a851c01949..eddfb733eb 100644
--- a/crates/api_models/src/refunds.rs
+++ b/crates/api_models/src/refunds.rs
@@ -70,6 +70,9 @@ pub struct RefundRequest {
#[schema(value_type = Option<SplitRefund>)]
#[smithy(value_type = "Option<SplitRefund>")]
pub split_refunds: Option<common_types::refunds::SplitRefund>,
+
+ /// If true, returns stringified connector raw response body
+ pub all_keys_required: Option<bool>,
}
#[cfg(feature = "v2")]
@@ -117,18 +120,23 @@ pub struct RefundsCreateRequest {
/// Merchant connector details used to make payments.
#[schema(value_type = Option<MerchantConnectorAuthDetails>)]
pub merchant_connector_details: Option<common_types::domain::MerchantConnectorAuthDetails>,
+
+ /// If true, returns stringified connector raw response body
+ pub return_raw_connector_response: Option<bool>,
}
#[cfg(feature = "v1")]
#[derive(Default, Debug, Clone, Deserialize)]
pub struct RefundsRetrieveBody {
pub force_sync: Option<bool>,
+ pub all_keys_required: Option<bool>,
}
#[cfg(feature = "v2")]
#[derive(Default, Debug, Clone, Deserialize)]
pub struct RefundsRetrieveBody {
pub force_sync: Option<bool>,
+ pub return_raw_connector_response: Option<bool>,
}
#[cfg(feature = "v2")]
@@ -139,6 +147,9 @@ pub struct RefundsRetrievePayload {
/// Merchant connector details used to make payments.
pub merchant_connector_details: Option<common_types::domain::MerchantConnectorAuthDetails>,
+
+ /// If true, returns stringified connector raw response body
+ pub return_raw_connector_response: Option<bool>,
}
#[cfg(feature = "v1")]
@@ -158,6 +169,9 @@ pub struct RefundsRetrieveRequest {
/// Merchant connector details used to make payments.
pub merchant_connector_details: Option<admin::MerchantConnectorDetailsWrap>,
+
+ /// If true, returns stringified connector raw response body
+ pub all_keys_required: Option<bool>,
}
#[cfg(feature = "v2")]
@@ -178,6 +192,9 @@ pub struct RefundsRetrieveRequest {
/// Merchant connector details used to make payments.
#[schema(value_type = Option<MerchantConnectorAuthDetails>)]
pub merchant_connector_details: Option<common_types::domain::MerchantConnectorAuthDetails>,
+
+ /// If true, returns stringified connector raw response body
+ pub return_raw_connector_response: Option<bool>,
}
#[derive(Default, Debug, ToSchema, Clone, Deserialize, Serialize, SmithyModel)]
@@ -331,6 +348,9 @@ pub struct RefundResponse {
/// Error message received from the issuer in case of failed refunds
#[smithy(value_type = "Option<String>")]
pub issuer_error_message: Option<String>,
+ /// Contains whole connector response
+ #[schema(value_type = Option<String>)]
+ pub raw_connector_response: Option<masking::Secret<String>>,
}
#[cfg(feature = "v1")]
@@ -389,6 +409,9 @@ pub struct RefundResponse {
pub merchant_connector_id: Option<common_utils::id_type::MerchantConnectorAccountId>,
/// The reference id of the connector for the refund
pub connector_refund_reference_id: Option<String>,
+ /// Contains raw connector response
+ #[schema(value_type = Option<String>)]
+ pub raw_connector_response: Option<masking::Secret<String>>,
}
#[cfg(feature = "v2")]
diff --git a/crates/router/src/compatibility/stripe/refunds.rs b/crates/router/src/compatibility/stripe/refunds.rs
index f44f5ac8f3..0152eebdc1 100644
--- a/crates/router/src/compatibility/stripe/refunds.rs
+++ b/crates/router/src/compatibility/stripe/refunds.rs
@@ -130,6 +130,7 @@ pub async fn refund_retrieve(
refund_id: path.into_inner(),
force_sync: Some(true),
merchant_connector_details: None,
+ all_keys_required: None,
};
let flow = Flow::RefundsRetrieveForceSync;
diff --git a/crates/router/src/core/fraud_check/operation/fraud_check_post.rs b/crates/router/src/core/fraud_check/operation/fraud_check_post.rs
index 118e9c1de4..1d427a163b 100644
--- a/crates/router/src/core/fraud_check/operation/fraud_check_post.rs
+++ b/crates/router/src/core/fraud_check/operation/fraud_check_post.rs
@@ -309,6 +309,7 @@ where
statement_descriptor_suffix: None,
statement_descriptor_prefix: None,
merchant_connector_details: None,
+ all_keys_required: None,
};
let capture_response = Box::pin(payments::payments_core::<
Capture,
diff --git a/crates/router/src/core/payments/flows/capture_flow.rs b/crates/router/src/core/payments/flows/capture_flow.rs
index bfe4f78857..f8738ccd04 100644
--- a/crates/router/src/core/payments/flows/capture_flow.rs
+++ b/crates/router/src/core/payments/flows/capture_flow.rs
@@ -105,7 +105,7 @@ impl Feature<api::Capture, types::PaymentsCaptureData>
connector_request: Option<services::Request>,
_business_profile: &domain::Profile,
_header_payload: hyperswitch_domain_models::payments::HeaderPayload,
- _return_raw_connector_response: Option<bool>,
+ return_raw_connector_response: Option<bool>,
) -> RouterResult<Self> {
let connector_integration: services::BoxedPaymentConnectorIntegrationInterface<
api::Capture,
@@ -119,7 +119,7 @@ impl Feature<api::Capture, types::PaymentsCaptureData>
&self,
call_connector_action,
connector_request,
- None,
+ return_raw_connector_response,
)
.await
.to_payment_failed_response()?;
diff --git a/crates/router/src/core/refunds.rs b/crates/router/src/core/refunds.rs
index 51f9d4cbec..f5742ce317 100644
--- a/crates/router/src/core/refunds.rs
+++ b/crates/router/src/core/refunds.rs
@@ -145,7 +145,8 @@ pub async fn trigger_refund_to_gateway(
payment_intent: &storage::PaymentIntent,
creds_identifier: Option<String>,
split_refunds: Option<SplitRefundsRequest>,
-) -> RouterResult<diesel_refund::Refund> {
+ all_keys_required: Option<bool>,
+) -> RouterResult<(diesel_refund::Refund, Option<masking::Secret<String>>)> {
let routed_through = payment_attempt
.connector
.clone()
@@ -269,6 +270,7 @@ pub async fn trigger_refund_to_gateway(
merchant_context,
refund,
router_data,
+ all_keys_required,
))
.await?
}
@@ -280,6 +282,7 @@ pub async fn trigger_refund_to_gateway(
refund,
router_data,
merchant_connector_account.clone(),
+ all_keys_required,
))
.await?
}
@@ -428,7 +431,7 @@ pub async fn trigger_refund_to_gateway(
.await
.map_err(|error| logger::warn!(refunds_outgoing_webhook_error=?error))
.ok();
- Ok(response)
+ Ok((response, router_data_res.raw_connector_response))
}
// ============================================================================
@@ -442,6 +445,7 @@ async fn execute_refund_execute_via_direct(
merchant_context: &domain::MerchantContext,
refund: &diesel_refund::Refund,
router_data: types::RefundExecuteRouterData,
+ all_keys_required: Option<bool>,
) -> RouterResult<types::RefundExecuteRouterData> {
let storage_scheme = merchant_context.get_merchant_account().storage_scheme;
@@ -457,7 +461,7 @@ async fn execute_refund_execute_via_direct(
&router_data,
payments::CallConnectorAction::Trigger,
None,
- None,
+ all_keys_required,
)
.await;
@@ -537,6 +541,7 @@ async fn execute_refund_execute_via_direct_with_ucs_shadow(
refund: &diesel_refund::Refund,
router_data: types::RefundExecuteRouterData,
merchant_connector_account: MerchantConnectorAccountType,
+ all_keys_required: Option<bool>,
) -> RouterResult<types::RefundExecuteRouterData> {
// Execute Direct connector (PRIMARY)
let direct_result = Box::pin(execute_refund_execute_via_direct(
@@ -545,6 +550,7 @@ async fn execute_refund_execute_via_direct_with_ucs_shadow(
merchant_context,
refund,
router_data.clone(),
+ all_keys_required,
))
.await;
@@ -650,7 +656,7 @@ pub async fn refund_retrieve_core(
profile_id: Option<common_utils::id_type::ProfileId>,
request: refunds::RefundsRetrieveRequest,
refund: diesel_refund::Refund,
-) -> RouterResult<diesel_refund::Refund> {
+) -> RouterResult<(diesel_refund::Refund, Option<masking::Secret<String>>)> {
let db = &*state.store;
let merchant_id = merchant_context.get_merchant_account().get_id();
core_utils::validate_profile_id_from_auth_layer(profile_id, &refund)?;
@@ -716,7 +722,11 @@ pub async fn refund_retrieve_core(
..refund
};
- let response = if should_call_refund(&refund, request.force_sync.unwrap_or(false)) {
+ let (response, raw_connector_response) = if should_call_refund(
+ &refund,
+ request.force_sync.unwrap_or(false),
+ request.all_keys_required.unwrap_or(false),
+ ) {
Box::pin(sync_refund_with_gateway(
&state,
&merchant_context,
@@ -725,23 +735,29 @@ pub async fn refund_retrieve_core(
&refund,
creds_identifier,
split_refunds_req,
+ request.all_keys_required,
))
.await
} else {
- Ok(refund)
+ Ok((refund, None))
}?;
- Ok(response)
+ Ok((response, raw_connector_response))
}
-fn should_call_refund(refund: &diesel_models::refund::Refund, force_sync: bool) -> bool {
+fn should_call_refund(
+ refund: &diesel_models::refund::Refund,
+ force_sync: bool,
+ all_keys_required: bool,
+) -> bool {
// This implies, we cannot perform a refund sync & `the connector_refund_id`
// doesn't exist
let predicate1 = refund.connector_refund_id.is_some();
- // This allows refund sync at connector level if force_sync is enabled, or
+ // This allows refund sync at connector level if all_keys_required or force_sync is enabled, or
// checks if the refund has failed
- let predicate2 = force_sync
+ let predicate2 = all_keys_required
+ || force_sync
|| !matches!(
refund.refund_status,
diesel_models::enums::RefundStatus::Failure
@@ -761,7 +777,8 @@ pub async fn sync_refund_with_gateway(
refund: &diesel_refund::Refund,
creds_identifier: Option<String>,
split_refunds: Option<SplitRefundsRequest>,
-) -> RouterResult<diesel_refund::Refund> {
+ all_keys_required: Option<bool>,
+) -> RouterResult<(diesel_refund::Refund, Option<masking::Secret<String>>)> {
let connector_id = refund.connector.to_string();
let connector: api::ConnectorData = api::ConnectorData::get_connector_by_name(
&state.conf.connectors,
@@ -870,6 +887,7 @@ pub async fn sync_refund_with_gateway(
state,
&connector,
router_data,
+ all_keys_required,
))
.await?
}
@@ -984,7 +1002,7 @@ pub async fn sync_refund_with_gateway(
.await
.map_err(|error| logger::warn!(refunds_outgoing_webhook_error=?error))
.ok();
- Ok(response)
+ Ok((response, router_data_res.raw_connector_response))
}
// ============================================================================
@@ -996,6 +1014,7 @@ async fn execute_refund_sync_via_direct(
state: &SessionState,
connector: &api::ConnectorData,
router_data: types::RefundSyncRouterData,
+ all_keys_required: Option<bool>,
) -> RouterResult<types::RefundSyncRouterData> {
let connector_integration: services::BoxedRefundConnectorIntegrationInterface<
api::RSync,
@@ -1009,7 +1028,7 @@ async fn execute_refund_sync_via_direct(
&router_data,
payments::CallConnectorAction::Trigger,
None,
- None,
+ all_keys_required,
)
.await;
@@ -1044,6 +1063,7 @@ async fn execute_refund_sync_via_direct_with_ucs_shadow(
state,
connector,
router_data.clone(),
+ None,
))
.await;
@@ -1255,7 +1275,7 @@ pub async fn validate_and_create_refund(
processor_refund_data: None,
};
- let refund = match db
+ let (refund, raw_connector_response) = match db
.insert_refund(
refund_create_req,
merchant_context.get_merchant_account().storage_scheme,
@@ -1272,6 +1292,7 @@ pub async fn validate_and_create_refund(
payment_intent,
creds_identifier,
split_refunds,
+ req.all_keys_required,
))
.await?
}
@@ -1305,7 +1326,7 @@ pub async fn validate_and_create_refund(
..refund
};
- Ok(refund.foreign_into())
+ Ok((refund, raw_connector_response).foreign_into())
}
// ********************************************** Refund list **********************************************
@@ -1402,6 +1423,7 @@ pub async fn refund_retrieve_core_with_internal_reference_id(
refund_id: refund.refund_id.clone(),
force_sync,
merchant_connector_details: None,
+ all_keys_required: None,
};
Box::pin(refund_retrieve_core(
@@ -1412,6 +1434,7 @@ pub async fn refund_retrieve_core_with_internal_reference_id(
refund,
))
.await
+ .map(|(refund, _)| refund)
}
#[instrument(skip_all)]
@@ -1420,7 +1443,7 @@ pub async fn refund_retrieve_core_with_refund_id(
merchant_context: domain::MerchantContext,
profile_id: Option<common_utils::id_type::ProfileId>,
request: refunds::RefundsRetrieveRequest,
-) -> RouterResult<diesel_refund::Refund> {
+) -> RouterResult<(diesel_refund::Refund, Option<masking::Secret<String>>)> {
let refund_id = request.refund_id.clone();
let db = &*state.store;
let merchant_id = merchant_context.get_merchant_account().get_id();
@@ -1604,6 +1627,36 @@ impl ForeignFrom<diesel_refund::Refund> for api::RefundResponse {
unified_message: refund.unified_message,
issuer_error_code: refund.issuer_error_code,
issuer_error_message: refund.issuer_error_message,
+ raw_connector_response: None,
+ }
+ }
+}
+
+impl ForeignFrom<(diesel_refund::Refund, Option<masking::Secret<String>>)> for api::RefundResponse {
+ fn foreign_from(item: (diesel_refund::Refund, Option<masking::Secret<String>>)) -> Self {
+ let (refund, raw_connector_response) = item;
+
+ Self {
+ payment_id: refund.payment_id,
+ refund_id: refund.refund_id,
+ amount: refund.refund_amount,
+ currency: refund.currency.to_string(),
+ reason: refund.refund_reason,
+ status: refund.refund_status.foreign_into(),
+ profile_id: refund.profile_id,
+ metadata: refund.metadata,
+ error_message: refund.refund_error_message,
+ error_code: refund.refund_error_code,
+ created_at: Some(refund.created_at),
+ updated_at: Some(refund.modified_at),
+ connector: refund.connector,
+ merchant_connector_id: refund.merchant_connector_id,
+ split_refunds: refund.split_refunds,
+ unified_code: refund.unified_code,
+ unified_message: refund.unified_message,
+ issuer_error_code: refund.issuer_error_code,
+ issuer_error_message: refund.issuer_error_message,
+ raw_connector_response,
}
}
}
@@ -1621,7 +1674,8 @@ pub async fn schedule_refund_execution(
payment_intent: &storage::PaymentIntent,
creds_identifier: Option<String>,
split_refunds: Option<SplitRefundsRequest>,
-) -> RouterResult<diesel_refund::Refund> {
+ all_keys_required: Option<bool>,
+) -> RouterResult<(diesel_refund::Refund, Option<masking::Secret<String>>)> {
// refunds::RefundResponse> {
let db = &*state.store;
let runner = storage::ProcessTrackerRunner::RefundWorkflowRouter;
@@ -1646,7 +1700,7 @@ pub async fn schedule_refund_execution(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable_lazy(|| format!("Failed while pushing refund execute task to scheduler, refund_id: {}", refund.refund_id))?;
- Ok(refund)
+ Ok((refund, None))
}
api_models::refunds::RefundType::Instant => {
let update_refund = Box::pin(trigger_refund_to_gateway(
@@ -1657,11 +1711,12 @@ pub async fn schedule_refund_execution(
payment_intent,
creds_identifier,
split_refunds,
+ all_keys_required,
))
.await;
match update_refund {
- Ok(updated_refund_data) => {
+ Ok((updated_refund_data, raw_connector_response)) => {
add_refund_sync_task(db, &updated_refund_data, runner)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
@@ -1669,7 +1724,7 @@ pub async fn schedule_refund_execution(
"Failed while pushing refund sync task in scheduler: refund_id: {}",
refund.refund_id
))?;
- Ok(updated_refund_data)
+ Ok((updated_refund_data, raw_connector_response))
}
Err(err) => Err(err),
}
@@ -1685,19 +1740,19 @@ pub async fn schedule_refund_execution(
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable_lazy(|| format!("Failed while pushing refund sync task in scheduler: refund_id: {}", refund.refund_id))?;
- Ok(refund)
+ Ok((refund, None))
}
api_models::refunds::RefundType::Instant => {
// [#255]: This is not possible in schedule_refund_execution as it will always be scheduled
// sync_refund_with_gateway(data, &refund).await
- Ok(refund)
+ Ok((refund, None))
}
}
}
}
}
// [#255]: This is not allowed to be otherwise or all
- _ => Ok(refund),
+ _ => Ok((refund, None)),
}?;
Ok(result)
}
@@ -1907,7 +1962,7 @@ pub async fn trigger_refund_execute_workflow(
})?;
//trigger refund request to gateway
- let updated_refund = Box::pin(trigger_refund_to_gateway(
+ let (updated_refund, _) = Box::pin(trigger_refund_to_gateway(
state,
&refund,
&merchant_context,
@@ -1915,6 +1970,7 @@ pub async fn trigger_refund_execute_workflow(
&payment_intent,
None,
split_refunds,
+ None,
))
.await?;
add_refund_sync_task(
diff --git a/crates/router/src/core/refunds_v2.rs b/crates/router/src/core/refunds_v2.rs
index 146b80b031..673c4dbb67 100644
--- a/crates/router/src/core/refunds_v2.rs
+++ b/crates/router/src/core/refunds_v2.rs
@@ -122,7 +122,8 @@ pub async fn trigger_refund_to_gateway(
merchant_context: &domain::MerchantContext,
payment_attempt: &storage::PaymentAttempt,
payment_intent: &storage::PaymentIntent,
-) -> errors::RouterResult<diesel_refund::Refund> {
+ return_raw_connector_response: Option<bool>,
+) -> errors::RouterResult<(diesel_refund::Refund, Option<masking::Secret<String>>)> {
let db = &*state.store;
let mca_id = payment_attempt.get_attempt_merchant_connector_account_id()?;
@@ -190,6 +191,7 @@ pub async fn trigger_refund_to_gateway(
&connector,
add_access_token_result,
router_data,
+ return_raw_connector_response,
))
.await;
@@ -220,9 +222,13 @@ pub async fn trigger_refund_to_gateway(
})?,
None => refund.to_owned(),
};
+ let raw_connector_response = connector_response
+ .as_ref()
+ .ok()
+ .and_then(|data| data.raw_connector_response.clone());
// Implement outgoing webhooks here
connector_response.to_refund_failed_response()?;
- Ok(response)
+ Ok((response, raw_connector_response))
}
#[allow(clippy::too_many_arguments)]
@@ -234,7 +240,8 @@ pub async fn internal_trigger_refund_to_gateway(
payment_attempt: &storage::PaymentAttempt,
payment_intent: &storage::PaymentIntent,
merchant_connector_details: common_types::domain::MerchantConnectorAuthDetails,
-) -> errors::RouterResult<diesel_refund::Refund> {
+ return_raw_connector_response: Option<bool>,
+) -> errors::RouterResult<(diesel_refund::Refund, Option<masking::Secret<String>>)> {
let storage_scheme = merchant_context.get_merchant_account().storage_scheme;
let routed_through = payment_attempt
@@ -294,6 +301,7 @@ pub async fn internal_trigger_refund_to_gateway(
&connector,
add_access_token_result,
router_data,
+ return_raw_connector_response,
))
.await;
@@ -324,9 +332,13 @@ pub async fn internal_trigger_refund_to_gateway(
})?,
None => refund.to_owned(),
};
+ let raw_connector_response = connector_response
+ .as_ref()
+ .ok()
+ .and_then(|data| data.raw_connector_response.clone());
// Implement outgoing webhooks here
connector_response.to_refund_failed_response()?;
- Ok(response)
+ Ok((response, raw_connector_response))
}
async fn call_connector_service<F>(
@@ -334,6 +346,7 @@ async fn call_connector_service<F>(
connector: &api::ConnectorData,
add_access_token_result: types::AddAccessTokenResult,
router_data: RouterData<F, types::RefundsData, types::RefundsResponseData>,
+ return_raw_connector_response: Option<bool>,
) -> Result<
RouterData<F, types::RefundsData, types::RefundsResponseData>,
error_stack::Report<errors::ConnectorError>,
@@ -359,7 +372,7 @@ where
&router_data,
payments::CallConnectorAction::Trigger,
None,
- None,
+ return_raw_connector_response,
)
.await
} else {
@@ -641,7 +654,7 @@ pub async fn refund_retrieve_core_with_refund_id(
.await
.to_not_found_response(errors::ApiErrorResponse::RefundNotFound)?;
- let response = Box::pin(refund_retrieve_core(
+ let (response, raw_connector_response) = Box::pin(refund_retrieve_core(
state.clone(),
merchant_context,
Some(profile_id),
@@ -660,7 +673,7 @@ pub async fn refund_retrieve_core(
profile_id: Option<id_type::ProfileId>,
request: refunds::RefundsRetrieveRequest,
refund: diesel_refund::Refund,
-) -> errors::RouterResult<diesel_refund::Refund> {
+) -> errors::RouterResult<(diesel_refund::Refund, Option<masking::Secret<String>>)> {
let db = &*state.store;
let key_manager_state = &(&state).into();
@@ -712,7 +725,11 @@ pub async fn refund_retrieve_core(
..refund
};
- let response = if should_call_refund(&refund, request.force_sync.unwrap_or(false)) {
+ let (response, raw_connector_response) = if should_call_refund(
+ &refund,
+ request.force_sync.unwrap_or(false),
+ request.return_raw_connector_response.unwrap_or(false),
+ ) {
if state.conf.merchant_id_auth.merchant_id_auth_enabled {
let merchant_connector_details = match request.merchant_connector_details {
Some(details) => details,
@@ -729,6 +746,7 @@ pub async fn refund_retrieve_core(
&payment_intent,
&refund,
merchant_connector_details,
+ request.return_raw_connector_response,
))
.await
} else {
@@ -738,23 +756,29 @@ pub async fn refund_retrieve_core(
&payment_attempt,
&payment_intent,
&refund,
+ request.return_raw_connector_response,
))
.await
}
} else {
- Ok(refund)
+ Ok((refund, None))
}?;
- Ok(response)
+ Ok((response, raw_connector_response))
}
-fn should_call_refund(refund: &diesel_models::refund::Refund, force_sync: bool) -> bool {
+fn should_call_refund(
+ refund: &diesel_models::refund::Refund,
+ force_sync: bool,
+ return_raw_connector_response: bool,
+) -> bool {
// This implies, we cannot perform a refund sync & `the connector_refund_id`
// doesn't exist
let predicate1 = refund.connector_refund_id.is_some();
// This allows refund sync at connector level if force_sync is enabled, or
// checks if the refund has failed
- let predicate2 = force_sync
+ let predicate2 = return_raw_connector_response
+ || force_sync
|| !matches!(
refund.refund_status,
diesel_models::enums::RefundStatus::Failure
@@ -772,7 +796,8 @@ pub async fn sync_refund_with_gateway(
payment_attempt: &storage::PaymentAttempt,
payment_intent: &storage::PaymentIntent,
refund: &diesel_refund::Refund,
-) -> errors::RouterResult<diesel_refund::Refund> {
+ return_raw_connector_response: Option<bool>,
+) -> errors::RouterResult<(diesel_refund::Refund, Option<masking::Secret<String>>)> {
let db = &*state.store;
let connector_id = refund.connector.to_string();
@@ -834,6 +859,7 @@ pub async fn sync_refund_with_gateway(
&connector,
add_access_token_result,
router_data,
+ return_raw_connector_response,
))
.await
.to_refund_failed_response()?;
@@ -841,7 +867,7 @@ pub async fn sync_refund_with_gateway(
let connector_response = perform_integrity_check(connector_response);
let refund_update =
- build_refund_update_for_rsync(&connector, merchant_context, connector_response);
+ build_refund_update_for_rsync(&connector, merchant_context, connector_response.clone());
let response = state
.store
@@ -860,7 +886,7 @@ pub async fn sync_refund_with_gateway(
})?;
// Implement outgoing webhook here
- Ok(response)
+ Ok((response, connector_response.raw_connector_response))
}
#[allow(clippy::too_many_arguments)]
@@ -872,7 +898,8 @@ pub async fn internal_sync_refund_with_gateway(
payment_intent: &storage::PaymentIntent,
refund: &diesel_refund::Refund,
merchant_connector_details: common_types::domain::MerchantConnectorAuthDetails,
-) -> errors::RouterResult<diesel_refund::Refund> {
+ return_raw_connector_response: Option<bool>,
+) -> errors::RouterResult<(diesel_refund::Refund, Option<masking::Secret<String>>)> {
let connector_enum = merchant_connector_details.connector_name;
let connector: api::ConnectorData = api::ConnectorData::get_connector_by_name(
@@ -917,6 +944,7 @@ pub async fn internal_sync_refund_with_gateway(
&connector,
add_access_token_result,
router_data,
+ return_raw_connector_response,
))
.await
.to_refund_failed_response()?;
@@ -924,7 +952,7 @@ pub async fn internal_sync_refund_with_gateway(
let connector_response = perform_integrity_check(connector_response);
let refund_update =
- build_refund_update_for_rsync(&connector, merchant_context, connector_response);
+ build_refund_update_for_rsync(&connector, merchant_context, connector_response.clone());
let response = state
.store
@@ -943,7 +971,7 @@ pub async fn internal_sync_refund_with_gateway(
})?;
// Implement outgoing webhook here
- Ok(response)
+ Ok((response, connector_response.raw_connector_response))
}
pub fn build_refund_update_for_rsync(
@@ -1175,7 +1203,7 @@ pub async fn validate_and_create_refund(
processor_refund_data: None,
};
- let refund = match db
+ let (refund, raw_connector_response) = match db
.insert_refund(
refund_create_req,
merchant_context.get_merchant_account().storage_scheme,
@@ -1191,6 +1219,7 @@ pub async fn validate_and_create_refund(
payment_attempt,
payment_intent,
merchant_connector_details,
+ req.return_raw_connector_response,
))
.await?
}
@@ -1223,7 +1252,10 @@ pub async fn validate_and_create_refund(
..refund
};
- api::RefundResponse::foreign_try_from(refund)
+ let mut refund_response: api::RefundResponse = api::RefundResponse::foreign_try_from(refund)?;
+ refund_response.raw_connector_response = raw_connector_response;
+
+ Ok(refund_response)
}
impl ForeignTryFrom<diesel_refund::Refund> for api::RefundResponse {
@@ -1266,6 +1298,7 @@ impl ForeignTryFrom<diesel_refund::Refund> for api::RefundResponse {
message: refund.refund_error_message.unwrap_or_default(),
}),
connector_refund_reference_id: None,
+ raw_connector_response: None,
})
}
}
@@ -1282,7 +1315,8 @@ pub async fn schedule_refund_execution(
payment_attempt: &storage::PaymentAttempt,
payment_intent: &storage::PaymentIntent,
merchant_connector_details: Option<common_types::domain::MerchantConnectorAuthDetails>,
-) -> errors::RouterResult<diesel_refund::Refund> {
+ return_raw_connector_response: Option<bool>,
+) -> errors::RouterResult<(diesel_refund::Refund, Option<masking::Secret<String>>)> {
let db = &*state.store;
let runner = storage::ProcessTrackerRunner::RefundWorkflowRouter;
let task = "EXECUTE_REFUND";
@@ -1306,7 +1340,7 @@ pub async fn schedule_refund_execution(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable_lazy(|| format!("Failed while pushing refund execute task to scheduler, refund_id: {}", refund.id.get_string_repr()))?;
- Ok(refund)
+ Ok((refund, None))
}
api_models::refunds::RefundType::Instant => {
let update_refund =
@@ -1329,6 +1363,7 @@ pub async fn schedule_refund_execution(
payment_attempt,
payment_intent,
merchant_connector_details,
+ return_raw_connector_response,
))
.await
} else {
@@ -1338,12 +1373,13 @@ pub async fn schedule_refund_execution(
merchant_context,
payment_attempt,
payment_intent,
+ return_raw_connector_response,
))
.await
};
match update_refund {
- Ok(updated_refund_data) => {
+ Ok((updated_refund_data, raw_connector_response)) => {
add_refund_sync_task(db, &updated_refund_data, runner)
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
@@ -1351,7 +1387,7 @@ pub async fn schedule_refund_execution(
"Failed while pushing refund sync task in scheduler: refund_id: {}",
refund.id.get_string_repr()
))?;
- Ok(updated_refund_data)
+ Ok((updated_refund_data, raw_connector_response))
}
Err(err) => Err(err),
}
@@ -1367,19 +1403,19 @@ pub async fn schedule_refund_execution(
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable_lazy(|| format!("Failed while pushing refund sync task in scheduler: refund_id: {}", refund.id.get_string_repr()))?;
- Ok(refund)
+ Ok((refund, None))
}
api_models::refunds::RefundType::Instant => {
// [#255]: This is not possible in schedule_refund_execution as it will always be scheduled
// sync_refund_with_gateway(data, &refund).await
- Ok(refund)
+ Ok((refund, None))
}
}
}
}
}
// [#255]: This is not allowed to be otherwise or all
- _ => Ok(refund),
+ _ => Ok((refund, None)),
}?;
Ok(result)
}
diff --git a/crates/router/src/core/webhooks/incoming.rs b/crates/router/src/core/webhooks/incoming.rs
index cecb869573..b61e85fd8f 100644
--- a/crates/router/src/core/webhooks/incoming.rs
+++ b/crates/router/src/core/webhooks/incoming.rs
@@ -1811,10 +1811,12 @@ async fn refunds_incoming_webhook_flow(
refund_id: refund_id.to_owned(),
force_sync: Some(true),
merchant_connector_details: None,
+ all_keys_required: None,
},
))
.await
.attach_printable_lazy(|| format!("Failed while updating refund: refund_id: {refund_id}"))?
+ .0
};
let event_type: Option<enums::EventType> = updated_refund.refund_status.into();
diff --git a/crates/router/src/routes/refunds.rs b/crates/router/src/routes/refunds.rs
index 99d860f4db..ba14e68ff0 100644
--- a/crates/router/src/routes/refunds.rs
+++ b/crates/router/src/routes/refunds.rs
@@ -155,6 +155,7 @@ pub async fn refunds_retrieve(
refund_id: path.into_inner(),
force_sync: query_params.force_sync,
merchant_connector_details: None,
+ all_keys_required: query_params.all_keys_required,
};
let flow = match query_params.force_sync {
Some(true) => Flow::RefundsRetrieveForceSync,
@@ -207,6 +208,7 @@ pub async fn refunds_retrieve(
refund_id: path.into_inner(),
force_sync: query_params.force_sync,
merchant_connector_details: None,
+ return_raw_connector_response: query_params.return_raw_connector_response,
};
let flow = match query_params.force_sync {
Some(true) => Flow::RefundsRetrieveForceSync,
@@ -265,6 +267,7 @@ pub async fn refunds_retrieve_with_gateway_creds(
refund_id: path.into_inner(),
force_sync: payload.force_sync,
merchant_connector_details: payload.merchant_connector_details.clone(),
+ return_raw_connector_response: payload.return_raw_connector_response,
};
let auth_type = if state.conf.merchant_id_auth.merchant_id_auth_enabled {
diff --git a/crates/router/src/services/api.rs b/crates/router/src/services/api.rs
index 5ad640b2ed..33dfa5d53d 100644
--- a/crates/router/src/services/api.rs
+++ b/crates/router/src/services/api.rs
@@ -820,7 +820,19 @@ impl Authenticate for api_models::payments::PaymentsRetrieveRequest {
}
impl Authenticate for api_models::payments::PaymentsCancelRequest {}
impl Authenticate for api_models::payments::PaymentsCancelPostCaptureRequest {}
-impl Authenticate for api_models::payments::PaymentsCaptureRequest {}
+impl Authenticate for api_models::payments::PaymentsCaptureRequest {
+ #[cfg(feature = "v2")]
+ fn should_return_raw_response(&self) -> Option<bool> {
+ self.return_raw_connector_response
+ }
+
+ #[cfg(feature = "v1")]
+ fn should_return_raw_response(&self) -> Option<bool> {
+ // In v1, this maps to `all_keys_required` to retain backward compatibility.
+ // The equivalent field in v2 is `return_raw_connector_response`.
+ self.all_keys_required
+ }
+}
impl Authenticate for api_models::payments::PaymentsIncrementalAuthorizationRequest {}
impl Authenticate for api_models::payments::PaymentsExtendAuthorizationRequest {}
impl Authenticate for api_models::payments::PaymentsStartRequest {}
diff --git a/crates/router/src/workflows/outgoing_webhook_retry.rs b/crates/router/src/workflows/outgoing_webhook_retry.rs
index 5ba5e86540..564578891a 100644
--- a/crates/router/src/workflows/outgoing_webhook_retry.rs
+++ b/crates/router/src/workflows/outgoing_webhook_retry.rs
@@ -457,9 +457,10 @@ async fn get_outgoing_webhook_content_and_event_type(
refund_id,
force_sync: Some(false),
merchant_connector_details: None,
+ all_keys_required: None,
};
- let refund = Box::pin(refund_retrieve_core_with_refund_id(
+ let (refund, _) = Box::pin(refund_retrieve_core_with_refund_id(
state,
merchant_context.clone(),
None,