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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
fix(connector): [Stripe] route split refunds to the connected account when the charge id is unavailable (#14575)
Tushar Shuklad33837069ba0
diff --git a/crates/hyperswitch_connectors/src/connectors/stripe.rs b/crates/hyperswitch_connectors/src/connectors/stripe.rsindex 43462ac468..d0e8e1fa34 100644--- a/crates/hyperswitch_connectors/src/connectors/stripe.rs+++ b/crates/hyperswitch_connectors/src/connectors/stripe.rs@@ -1892,6 +1892,9 @@ impl ConnectorIntegration<Execute, RefundsData, RefundsResponseData> for Stripereq.request.currency,)?;let request_body = match req.request.split_refunds.as_ref() {+ // `ChargeRefundRequest` falls back to `payment_intent` when the charge id is unknown;+ // the `Stripe-Account` header added in `get_headers` is what routes either shape to+ // the connected account, which is what makes the refund resolvable at all.Some(SplitRefundsRequest::StripeSplitRefund(_)) => RequestContent::FormUrlEncoded(Box::new(stripe::ChargeRefundRequest::try_from(req)?),),diff --git a/crates/hyperswitch_connectors/src/connectors/stripe/transformers.rs b/crates/hyperswitch_connectors/src/connectors/stripe/transformers.rsindex 0bfb622a09..283df6cf4d 100644--- a/crates/hyperswitch_connectors/src/connectors/stripe/transformers.rs+++ b/crates/hyperswitch_connectors/src/connectors/stripe/transformers.rs@@ -4338,7 +4338,11 @@ impl<F> TryFrom<(&RefundsRouterData<F>, MinorUnit)> for RefundRequest {#[derive(Debug, Serialize)]pub struct ChargeRefundRequest {- pub charge: String,+ /// Stripe's Create Refund API accepts either identifier. `charge` is used when it is known,+ /// otherwise the refund is issued against `payment_intent` - which keeps `reverse_transfer`+ /// and `refund_application_fee` on the request either way.+ pub charge: Option<String>,+ pub payment_intent: Option<String>,pub refund_application_fee: Option<bool>,pub reverse_transfer: Option<bool>,pub amount: Option<MinorUnit>, //amount in cents, hence passed as integer@@ -4368,8 +4372,14 @@ impl<F> TryFrom<&RefundsRouterData<F>> for ChargeRefundRequest {}) => (Some(*revert_platform_fee), Some(*revert_transfer)),};+ let charge = stripe_refund.charge_id.clone();+ let payment_intent = charge+ .is_none()+ .then(|| item.request.connector_transaction_id.clone());+Ok(Self {- charge: stripe_refund.charge_id.clone(),+ charge,+ payment_intent,refund_application_fee,reverse_transfer,amount: Some(amount),diff --git a/crates/hyperswitch_domain_models/src/router_request_types.rs b/crates/hyperswitch_domain_models/src/router_request_types.rsindex 1f438412d3..316fe6e56c 100644--- a/crates/hyperswitch_domain_models/src/router_request_types.rs+++ b/crates/hyperswitch_domain_models/src/router_request_types.rs@@ -1468,7 +1468,10 @@ pub enum SplitRefundsRequest {#[derive(Debug, serde::Deserialize, serde::Serialize, Clone)]pub struct StripeSplitRefund {- pub charge_id: String,+ /// `None` when the Stripe charge id could not be resolved from the payment attempt. The+ /// refund is then issued against the payment intent instead, and the Connect routing is+ /// still derived from `transfer_account_id` and `charge_type`.+ pub charge_id: Option<String>,pub transfer_account_id: String,pub charge_type: api_models::enums::PaymentChargeType,pub options: ChargeRefundsOptions,diff --git a/crates/router/src/core/unified_connector_service/transformers.rs b/crates/router/src/core/unified_connector_service/transformers.rsindex 37da377a79..2b6d2c3544 100644--- a/crates/router/src/core/unified_connector_service/transformers.rs+++ b/crates/router/src/core/unified_connector_service/transformers.rs@@ -10653,7 +10653,11 @@ impl ForeignFrom<&router_request_types::StripeSplitRefund>{fn foreign_from(stripe: &router_request_types::StripeSplitRefund) -> Self {Self {- charge_id: stripe.charge_id.clone(),+ // `charge_id` is a plain proto3 string, so prost leaves an empty value off the wire+ // entirely - which is how an unresolved charge id reaches connector-service as an+ // absent field. Requires the connector-service side to read it as optional+ // (juspay/connector-service `optional string charge_id`), so that deploys first.+ charge_id: stripe.charge_id.clone().unwrap_or_default(),transfer_account_id: stripe.transfer_account_id.clone(),charge_type: payments_grpc::PaymentChargeType::foreign_from(&stripe.charge_type).into(),options: Some(payments_grpc::ChargeRefundsOptions::foreign_from(diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rsindex 2fbd1e2789..c9a34952a2 100644--- a/crates/router/src/core/utils.rs+++ b/crates/router/src/core/utils.rs@@ -936,25 +936,44 @@ pub fn get_split_refunds((_, _) => (None, None),};- if let Some(charge_id) = charge_id_option {- let options = refunds_validator::validate_stripe_charge_refund(- charge_type_option,- &split_refund_input.refund_request,- )?;-- Ok(Some(- router_request_types::SplitRefundsRequest::StripeSplitRefund(- router_request_types::StripeSplitRefund {- charge_id,- charge_type: stripe_payment.charge_type.clone(),- transfer_account_id: stripe_payment.transfer_account_id.clone(),- options,- },- ),- ))- } else {- Ok(None)+ // Destination charges are created on the platform account, so a refund issued+ // against the payment intent already reaches the right account with no split refund+ // data; only direct charges need it, because only they carry the `Stripe-Account`+ // header. Keeping the old `None` for destination also keeps the merchant-facing+ // validation below off a path that used to work without `split_refunds`.+ if charge_id_option.is_none()+ && !matches!(+ stripe_payment.charge_type,+ api_models::enums::PaymentChargeType::Stripe(+ api_models::enums::StripeChargeType::Direct+ )+ )+ {+ return Ok(None);}++ let options = refunds_validator::validate_stripe_charge_refund(+ charge_type_option,+ &split_refund_input.refund_request,+ )?;++ // The charge id is only known once charge data has been persisted on the attempt,+ // which does not happen for payments that reach a terminal state without a PSync.+ // The Connect routing information lives on the payment intent and is always+ // present, so the refund must still be built as a split refund - issued against+ // the payment intent rather than the charge when the charge id is unknown.+ // Returning `None` here instead would silently downgrade the refund to the+ // non-Connect shape and send it to the platform account.+ Ok(Some(+ router_request_types::SplitRefundsRequest::StripeSplitRefund(+ router_request_types::StripeSplitRefund {+ charge_id: charge_id_option,+ charge_type: stripe_payment.charge_type.clone(),+ transfer_account_id: stripe_payment.transfer_account_id.clone(),+ options,+ },+ ),+ ))}Some(common_types::payments::SplitPaymentsRequest::AdyenSplitPayment(_)) => {match &split_refund_input.payment_charges {