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

fix(router): reuse prefetched trackers in redirect flows (#15053)

Hrithikeshc60d11f49c9b
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index 4344a269f2..24503d67e5 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -5131,6 +5131,7 @@ pub trait PaymentRedirectFlow: Sync {
connector_action: CallConnectorAction,
connector: String,
payment_id: id_type::PaymentId,
+ payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
) -> RouterResult<Self::PaymentFlowResponse>;
#[cfg(feature = "v2")]
@@ -5192,6 +5193,32 @@ pub trait PaymentRedirectFlow: Sync {
field_name: "payment_id".into(),
})?;
+ let (payment_intent, payment_attempt) =
+ operations::payment_status::get_payment_intent_payment_attempt(
+ &state,
+ &req.resource_id,
+ &platform.get_processor().get_processor_merchant_id(),
+ platform.get_processor().get_key_store(),
+ platform.get_processor().get_storage_scheme(),
+ )
+ .await?;
+ // All redirect URLs name the payment connector, including the external
+ // authentication return URL. Verify it against the active attempt.
+ if payment_attempt.connector.as_deref() != Some(connector.as_str()) {
+ return Err(errors::ApiErrorResponse::PreconditionFailed {
+ message: "The connector in the redirect URL does not match the active payment attempt"
+ .to_string(),
+ })
+ .attach_printable(format!(
+ "Redirect connector {connector} does not match active payment attempt connector {:?}",
+ payment_attempt.connector
+ ));
+ }
+ let payment_pre_fetched_info = Some(operations::PaymentPreFetchedInformation {
+ payment_intent,
+ payment_attempt,
+ });
+
#[cfg(feature = "v2")]
//TODO: Will get the global payment id from the resource id, we need to handle this in the further flow
let resource_id: id_type::PaymentId = todo!();
@@ -5224,6 +5251,7 @@ pub trait PaymentRedirectFlow: Sync {
flow_type,
connector.clone(),
resource_id.clone(),
+ payment_pre_fetched_info,
)
.await?;
@@ -5274,6 +5302,7 @@ impl PaymentRedirectFlow for PaymentRedirectCompleteAuthorize {
connector_action: CallConnectorAction,
_connector: String,
_payment_id: id_type::PaymentId,
+ payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
) -> RouterResult<Self::PaymentFlowResponse> {
let payment_confirm_req = api::PaymentsRequest {
payment_id: Some(req.resource_id.clone()),
@@ -5319,7 +5348,7 @@ impl PaymentRedirectFlow for PaymentRedirectCompleteAuthorize {
None,
None,
HeaderPayload::default(),
- None,
+ payment_pre_fetched_info,
))
.await?;
let payments_response = match response {
@@ -5482,6 +5511,7 @@ impl PaymentRedirectFlow for PaymentRedirectSync {
connector_action: CallConnectorAction,
_connector: String,
_payment_id: id_type::PaymentId,
+ payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
) -> RouterResult<Self::PaymentFlowResponse> {
let payment_sync_req = api::PaymentsRetrieveRequest {
resource_id: req.resource_id,
@@ -5513,7 +5543,7 @@ impl PaymentRedirectFlow for PaymentRedirectSync {
None,
None,
HeaderPayload::default(),
- None,
+ payment_pre_fetched_info,
),
)
.await?;
@@ -5794,28 +5824,25 @@ impl PaymentRedirectFlow for PaymentAuthenticateCompleteAuthorize {
connector_action: CallConnectorAction,
connector: String,
payment_id: id_type::PaymentId,
+ payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
) -> RouterResult<Self::PaymentFlowResponse> {
- let payment_intent = state
- .store
- .find_payment_intent_by_payment_id_processor_merchant_id(
- &payment_id,
- &platform.get_processor().get_processor_merchant_id(),
- platform.get_processor().get_key_store(),
- platform.get_processor().get_storage_scheme(),
- )
- .await
- .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;
- let payment_attempt = state
- .store
- .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
- &payment_intent.payment_id,
- &platform.get_processor().get_processor_merchant_id(),
- &payment_intent.active_attempt.get_id(),
- platform.get_processor().get_storage_scheme(),
- platform.get_processor().get_key_store(),
- )
- .await
- .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;
+ let (payment_intent, payment_attempt) = match payment_pre_fetched_info {
+ Some(info) => (info.payment_intent, info.payment_attempt),
+ None => {
+ operations::payment_status::get_payment_intent_payment_attempt(
+ state,
+ &api::PaymentIdType::PaymentIntentId(payment_id.clone()),
+ &platform.get_processor().get_processor_merchant_id(),
+ platform.get_processor().get_key_store(),
+ platform.get_processor().get_storage_scheme(),
+ )
+ .await?
+ }
+ };
+ let payment_pre_fetched_info = Some(operations::PaymentPreFetchedInformation {
+ payment_intent: payment_intent.clone(),
+ payment_attempt: payment_attempt.clone(),
+ });
let authentication_id = payment_attempt
.authentication_id
.ok_or(errors::ApiErrorResponse::InternalServerError)
@@ -5998,7 +6025,7 @@ impl PaymentRedirectFlow for PaymentAuthenticateCompleteAuthorize {
None,
None,
HeaderPayload::with_source(enums::PaymentSource::ExternalAuthenticator),
- None,
+ payment_pre_fetched_info,
))
.await?
} else {
@@ -6021,7 +6048,7 @@ impl PaymentRedirectFlow for PaymentAuthenticateCompleteAuthorize {
None,
None,
HeaderPayload::with_source(enums::PaymentSource::ExternalAuthenticator),
- None,
+ payment_pre_fetched_info,
))
.await?
}
@@ -6056,7 +6083,7 @@ impl PaymentRedirectFlow for PaymentAuthenticateCompleteAuthorize {
None,
None,
HeaderPayload::default(),
- None,
+ payment_pre_fetched_info,
),
)
.await?
diff --git a/crates/router/src/core/payments/operations/payment_complete_authorize.rs b/crates/router/src/core/payments/operations/payment_complete_authorize.rs
index 5c98f7425f..d8c959d576 100644
--- a/crates/router/src/core/payments/operations/payment_complete_authorize.rs
+++ b/crates/router/src/core/payments/operations/payment_complete_authorize.rs
@@ -55,7 +55,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
_header_payload: &hyperswitch_domain_models::payments::HeaderPayload,
payment_method_fetch_data: operations::PaymentMethodFetchData,
_dimensions: &dimension_state::DimensionsWithProcessorAndProviderMerchantId,
- _payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
+ payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
) -> RouterResult<operations::GetTrackerResponse<'a, F, api::PaymentsRequest, PaymentData<F>>>
{
let operations::PaymentMethodFetchData {
@@ -63,6 +63,10 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
payment_method_info: prefetched_payment_method_info,
..
} = payment_method_fetch_data;
+ let (pre_fetched_intent, pre_fetched_attempt) = match payment_pre_fetched_info {
+ Some(info) => (Some(info.payment_intent), Some(info.payment_attempt)),
+ None => (None, None),
+ };
let db = &*state.store;
let processor_merchant_id = platform.get_processor().get_processor_merchant_id();
@@ -73,7 +77,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
.get_payment_intent_id()
.change_context(errors::ApiErrorResponse::PaymentNotFound)?;
- payment_intent = match prefetched_payment_intent {
+ payment_intent = match prefetched_payment_intent.or(pre_fetched_intent) {
Some(payment_intent) => payment_intent,
None => db
.find_payment_intent_by_payment_id_processor_merchant_id(
@@ -116,16 +120,19 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
let recurring_details = request.recurring_details.clone();
- payment_attempt = db
- .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
- &payment_intent.payment_id,
- &processor_merchant_id,
- &payment_intent.active_attempt.get_id(),
- storage_scheme,
- platform.get_processor().get_key_store(),
- )
- .await
- .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;
+ payment_attempt = match pre_fetched_attempt {
+ Some(attempt) => attempt,
+ None => db
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
+ &payment_intent.payment_id,
+ &processor_merchant_id,
+ &payment_intent.active_attempt.get_id(),
+ storage_scheme,
+ platform.get_processor().get_key_store(),
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?,
+ };
let mandate_type = m_helpers::get_mandate_type(
request.mandate_data.clone(),
diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs
index 6d5b11eb9e..3d2d7d3cfb 100644
--- a/crates/router/src/core/payments/operations/payment_confirm.rs
+++ b/crates/router/src/core/payments/operations/payment_confirm.rs
@@ -87,7 +87,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
header_payload: &hyperswitch_domain_models::payments::HeaderPayload,
payment_method_fetch_data: operations::PaymentMethodFetchData,
dimensions: &dimension_state::DimensionsWithProcessorAndProviderMerchantId,
- _payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
+ payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
) -> RouterResult<operations::GetTrackerResponse<'a, F, api::PaymentsRequest, PaymentData<F>>>
{
let operations::PaymentMethodFetchData {
@@ -96,6 +96,10 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
payment_method_with_raw_data,
token_data: prefetched_token_data,
} = payment_method_fetch_data;
+ let (pre_fetched_intent, pre_fetched_attempt) = match payment_pre_fetched_info {
+ Some(info) => (Some(info.payment_intent), Some(info.payment_attempt)),
+ None => (None, None),
+ };
let processor_merchant_id = platform.get_processor().get_processor_merchant_id();
let storage_scheme = platform.get_processor().get_storage_scheme();
let (currency, amount);
@@ -109,7 +113,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
let m_merchant_id = processor_merchant_id.clone();
// Parallel calls - level 0
- let mut payment_intent = match prefetched_payment_intent {
+ let mut payment_intent = match prefetched_payment_intent.or(pre_fetched_intent) {
Some(payment_intent) => payment_intent,
None => store
.find_payment_intent_by_payment_id_processor_merchant_id(
@@ -233,21 +237,27 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
let m_merchant_id = processor_merchant_id.clone();
let merchant_key_store = platform.get_processor().get_key_store().clone();
- let payment_attempt_fut = tokio::spawn(
- async move {
- store
- .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
- &m_payment_id,
- &m_merchant_id,
- attempt_id.as_str(),
- storage_scheme,
- &merchant_key_store,
- )
- .map(|x| x.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound))
- .await
- }
- .instrument(tracing::debug_span!("payment_attempt")),
- );
+ let payment_attempt_fut =
+ tokio::spawn(
+ async move {
+ match pre_fetched_attempt {
+ Some(attempt) => Ok(attempt),
+ None => store
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
+ &m_payment_id,
+ &m_merchant_id,
+ attempt_id.as_str(),
+ storage_scheme,
+ &merchant_key_store,
+ )
+ .map(|x| {
+ x.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)
+ })
+ .await,
+ }
+ }
+ .instrument(tracing::debug_span!("payment_attempt")),
+ );
let m_merchant_id = processor_merchant_id.clone();
let m_request_shipping = request.shipping.clone();
diff --git a/crates/router/src/core/payments/operations/payment_status.rs b/crates/router/src/core/payments/operations/payment_status.rs
index e0809c20bc..f52e6846ee 100644
--- a/crates/router/src/core/payments/operations/payment_status.rs
+++ b/crates/router/src/core/payments/operations/payment_status.rs
@@ -239,7 +239,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRetrieve
_header_payload: &hyperswitch_domain_models::payments::HeaderPayload,
_payment_method_fetch_data: operations::PaymentMethodFetchData,
dimensions: &dimension_state::DimensionsWithProcessorAndProviderMerchantId,
- _payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
+ payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
) -> RouterResult<
operations::GetTrackerResponse<'a, F, api::PaymentsRetrieveRequest, PaymentData<F>>,
> {
@@ -251,6 +251,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRetrieve
self,
platform.get_processor().get_storage_scheme(),
dimensions,
+ payment_pre_fetched_info,
))
.await
}
@@ -269,6 +270,7 @@ async fn get_tracker_for_sync<
_operation: Op,
_storage_scheme: enums::MerchantStorageScheme,
_dimensions: &dimension_state::DimensionsWithProcessorAndProviderMerchantId,
+ _payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
) -> RouterResult<operations::GetTrackerResponse<'a, F, api::PaymentsRetrieveRequest, PaymentData<F>>>
{
todo!()
@@ -288,18 +290,27 @@ async fn get_tracker_for_sync<
operation: Op,
storage_scheme: domain::ProcessorStorageScheme,
dimensions: &dimension_state::DimensionsWithProcessorAndProviderMerchantId,
+ payment_pre_fetched_info: Option<operations::PaymentPreFetchedInformation>,
) -> RouterResult<operations::GetTrackerResponse<'a, F, api::PaymentsRetrieveRequest, PaymentData<F>>>
{
let (payment_intent, mut payment_attempt, currency, amount);
- (payment_intent, payment_attempt) = get_payment_intent_payment_attempt(
- state,
- payment_id,
- &platform.get_processor().get_processor_merchant_id(),
- platform.get_processor().get_key_store(),
- storage_scheme,
- )
- .await?;
+ (payment_intent, payment_attempt) = match payment_pre_fetched_info {
+ Some(pre_fetched_info) => (
+ pre_fetched_info.payment_intent,
+ pre_fetched_info.payment_attempt,
+ ),
+ None => {
+ get_payment_intent_payment_attempt(
+ state,
+ payment_id,
+ &platform.get_processor().get_processor_merchant_id(),
+ platform.get_processor().get_key_store(),
+ storage_scheme,
+ )
+ .await?
+ }
+ };
helpers::authenticate_client_secret(request.client_secret.as_ref(), &payment_intent)?;