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(payments): Create context s2s (#14172)

Prasunna Soppacdfa01f50296
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index 25ba5843de..861a98a788 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -12,13 +12,13 @@ pub mod client_session;
#[cfg(feature = "retry")]
pub mod retry;
pub mod routing;
+#[cfg(feature = "v1")]
+pub mod server_integration;
#[cfg(feature = "v2")]
pub mod session_operation;
pub mod tokenization;
pub mod transformers;
pub mod types;
-#[cfg(feature = "v1")]
-pub mod update_context;
pub mod vault_session;
#[cfg(feature = "olap")]
use std::collections::HashMap;
diff --git a/crates/router/src/core/payments/update_context.rs b/crates/router/src/core/payments/server_integration.rs
similarity index 89%
rename from crates/router/src/core/payments/update_context.rs
rename to crates/router/src/core/payments/server_integration.rs
index fb65ddcfee..2c5e47158a 100644
--- a/crates/router/src/core/payments/update_context.rs
+++ b/crates/router/src/core/payments/server_integration.rs
@@ -1,14 +1,15 @@
-//! Server-integration enrichment for payments responses.
+//! Shared plumbing for the server-integration (`X-Integration-Type: server`) response shape.
//!
-//! A caller that sends `X-Integration-Type: server` gets the payment response it always got,
-//! plus the two artifacts its checkout would otherwise fetch in separate calls: the combined
-//! payment-method list and the wallet session tokens. Client integrations, and callers that
-//! send no header at all, are unaffected.
+//! Both the create-intent and update-intent flows hand the caller the two artifacts its checkout
+//! would otherwise fetch in separate calls: the combined payment-method list and the wallet
+//! session tokens. This module owns the header parsing and the concurrent fetch of those two
+//! sections, and attaches them to the response. The two routes decide when to run it: a create
+//! or an update that opted in with the header.
//!
-//! This module adds no business logic. It calls the two existing cores — the same ones behind
-//! `POST /payments/session_tokens` and `GET /payments/{id}/client` — and attaches their results
-//! to the response. Both reads run after the write they depend on, and concurrently with each
-//! other, since neither reads the other's output.
+//! No business logic lives here. It calls the two existing cores — the same ones behind
+//! `POST /payments/session_tokens` and `GET /payments/{id}/client` — and reports each outcome.
+//! Both reads run after the write they depend on, and concurrently with each other, since neither
+//! reads the other's output.
use api_models::{
payment_methods as payment_methods_api,
@@ -128,11 +129,16 @@ fn timed_out(section: &str) -> error_stack::Report<errors::ApiErrorResponse> {
))
}
-/// Attaches the payment-method list and wallet session tokens to a payments response.
+/// Attaches the wallet session tokens and the combined payment-method list to a payments
+/// response, fetching both concurrently.
///
-/// Best-effort by design: the payment write has already committed by the time this runs, so a
-/// failing section reports its own error inline and the response still succeeds. Turning a
-/// section failure into a 5xx would hide a committed state change from the caller.
+/// Shared by create intent and update intent: the payment is committed by the time either route
+/// calls this, so neither needs anything the other does not.
+///
+/// Best-effort by design: a failing section reports its own error inline rather than failing the
+/// whole response. Turning a section failure into a 5xx would hide a committed state change from
+/// the caller. That is why this is a `join` and not a `try_join`: one section's failure must not
+/// cancel the other.
#[instrument(skip_all, fields(payment_id))]
pub async fn attach_server_context(
state: SessionState,
diff --git a/crates/router/src/routes/payments.rs b/crates/router/src/routes/payments.rs
index d971eff44d..c999be47cb 100644
--- a/crates/router/src/routes/payments.rs
+++ b/crates/router/src/routes/payments.rs
@@ -29,7 +29,7 @@ use super::app::ReqState;
#[cfg(feature = "v2")]
use crate::core::payment_method_balance;
#[cfg(feature = "v1")]
-use crate::core::payments::update_context;
+use crate::core::payments::server_integration;
#[cfg(feature = "v2")]
use crate::core::revenue_recovery::api as recovery;
#[cfg(feature = "v1")]
@@ -134,7 +134,12 @@ pub async fn payments_create(
),
};
- let integration_type = update_context::integration_type_from_headers(req.headers());
+ let integration_type = server_integration::integration_type_from_headers(req.headers());
+
+ // Merchant-only auth here, so the header alone opts in. A create-and-confirm has no use for
+ // the enrichment, and the session core rejects the statuses it lands in.
+ let is_create_and_confirm = payload.confirm.is_some_and(|confirm| confirm);
+ let enrich = integration_type.is_server() && !is_create_and_confirm;
Box::pin(api::server_wrap(
flow,
@@ -145,8 +150,8 @@ pub async fn payments_create(
let header_payload = header_payload.clone();
async move {
let merchant_integration_type =
- update_context::merchant_integration_type(&state, &auth.platform).await;
- update_context::validate_integration_type(
+ server_integration::merchant_integration_type(&state, &auth.platform).await;
+ server_integration::validate_integration_type(
integration_type,
merchant_integration_type,
)?;
@@ -160,12 +165,27 @@ pub async fn payments_create(
} else {
None
};
+
+ let profile_id = auth.profile.map(|profile| profile.get_id().clone());
+
+ // Only the enrichment path needs these afterwards. A client create — the vast
+ // majority — clones nothing.
+ let enrichment_inputs = enrich.then(|| {
+ (
+ state.clone(),
+ req_state.clone(),
+ auth.platform.clone(),
+ profile_id.clone(),
+ header_payload.clone(),
+ )
+ });
+
let result = Box::pin(authorize_verify_select::<_>(
payments::PaymentCreate,
state,
req_state,
auth.platform,
- auth.profile.map(|profile| profile.get_id().clone()),
+ profile_id,
header_payload,
req,
api::AuthFlow::Client,
@@ -176,7 +196,44 @@ pub async fn payments_create(
record_payment_confirm(&result, start.elapsed(), context);
}
- result
+ let response = result?;
+
+ // Invoked directly, not through `server_wrap`: both flows map to
+ // `ApiIdentifier::Payments`, so a nested wrap would deadlock on the lock this
+ // request already holds.
+ let enrich_payment = |mut payment: payment_types::PaymentsResponse| async {
+ if let Some((state, req_state, platform, profile_id, header_payload)) =
+ enrichment_inputs
+ {
+ let id = payment.payment_id.clone();
+ Box::pin(server_integration::attach_server_context(
+ state,
+ req_state,
+ platform,
+ profile_id,
+ &id,
+ header_payload,
+ &mut payment,
+ ))
+ .await;
+ }
+ payment
+ };
+
+ // `enrich_payment` is a no-op when `enrichment_inputs` is `None`, so the opt-in
+ // decision lives in exactly one place rather than being re-tested here.
+ match response {
+ services::ApplicationResponse::JsonWithHeaders((payment, headers)) => {
+ Ok(services::ApplicationResponse::JsonWithHeaders((
+ enrich_payment(payment).await,
+ headers,
+ )))
+ }
+ services::ApplicationResponse::Json(payment) => Ok(
+ services::ApplicationResponse::Json(enrich_payment(payment).await),
+ ),
+ response => Ok(response),
+ }
}
},
auth_type,
@@ -938,7 +995,7 @@ pub async fn payments_update(
}
};
- let integration_type = update_context::integration_type_from_headers(req.headers());
+ let integration_type = server_integration::integration_type_from_headers(req.headers());
// Gated on merchant auth too: this route also accepts publishable-key + client-secret, and
// the enrichment runs as `AuthFlow::Merchant`, so a client caller must not opt in by header.
@@ -953,8 +1010,8 @@ pub async fn payments_update(
let header_payload = header_payload.clone();
async move {
let merchant_integration_type =
- update_context::merchant_integration_type(&state, &auth.platform).await;
- update_context::validate_integration_type(
+ server_integration::merchant_integration_type(&state, &auth.platform).await;
+ server_integration::validate_integration_type(
integration_type,
merchant_integration_type,
)?;
@@ -1004,7 +1061,7 @@ pub async fn payments_update(
enrichment_inputs
{
let id = payment.payment_id.clone();
- Box::pin(update_context::attach_server_context(
+ Box::pin(server_integration::attach_server_context(
state,
req_state,
platform,