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(payments): Avoid unnecessary database call for 'on-session' payments (#11038)

Amisha Prabhat1de62ea5e564
diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rs
index 01a535ba9d..36c9577b7c 100644
--- a/crates/router/src/core/payments/operations/payment_response.rs
+++ b/crates/router/src/core/payments/operations/payment_response.rs
@@ -1876,36 +1876,61 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
payment_data.payment_attempt.merchant_connector_id.clone()
{
// check if the mandate has not already been set to active
- if !mandate_details.payments
+ let is_active_mandate = mandate_details
+ .payments
.as_ref()
.and_then(|payments| payments.0.get(&mca_id))
- .map(|payment_mandate_reference_record| payment_mandate_reference_record.connector_mandate_status == Some(common_enums::ConnectorMandateStatus::Active))
- .unwrap_or(false)
- {
+ .is_some_and(|record| {
+ record.connector_mandate_status
+ == Some(common_enums::ConnectorMandateStatus::Active)
+ });
- let (connector_mandate_id, mandate_metadata,connector_mandate_request_reference_id) = payment_data.payment_attempt.connector_mandate_detail.clone()
- .map(|cmr| (cmr.connector_mandate_id, cmr.mandate_metadata,cmr.connector_mandate_request_reference_id))
- .unwrap_or((None, None,None));
- // Update the connector mandate details with the payment attempt connector mandate id
- let connector_mandate_details =
- tokenization::update_connector_mandate_details(
- Some(mandate_details),
- payment_data.payment_attempt.payment_method_type,
- Some(
- payment_data
- .payment_attempt
- .net_amount
- .get_total_amount()
- .get_amount_as_i64(),
- ),
- payment_data.payment_attempt.currency,
- payment_data.payment_attempt.merchant_connector_id.clone(),
- connector_mandate_id,
- mandate_metadata,
- connector_mandate_request_reference_id
- )?;
- // Update the payment method table with the active mandate record
- payment_methods::cards::update_payment_method_connector_mandate_details(
+ let is_off_session =
+ payment_data.payment_intent.setup_future_usage
+ == Some(common_enums::FutureUsage::OffSession);
+
+ if !is_active_mandate && is_off_session {
+ let (
+ connector_mandate_id,
+ mandate_metadata,
+ connector_mandate_request_reference_id,
+ ) = payment_data
+ .payment_attempt
+ .connector_mandate_detail
+ .clone()
+ .map(|cmr| {
+ (
+ cmr.connector_mandate_id,
+ cmr.mandate_metadata,
+ cmr.connector_mandate_request_reference_id,
+ )
+ })
+ .unwrap_or((None, None, None));
+ // Update the connector mandate details with the payment attempt connector mandate id
+ let connector_mandate_details =
+ tokenization::update_connector_mandate_details(
+ Some(mandate_details),
+ payment_data
+ .payment_attempt
+ .payment_method_type,
+ Some(
+ payment_data
+ .payment_attempt
+ .net_amount
+ .get_total_amount()
+ .get_amount_as_i64(),
+ ),
+ payment_data.payment_attempt.currency,
+ payment_data
+ .payment_attempt
+ .merchant_connector_id
+ .clone(),
+ connector_mandate_id,
+ mandate_metadata,
+ connector_mandate_request_reference_id,
+ )?;
+ // Update the payment method table with the active mandate record
+ payment_methods::cards::update_payment_method_connector_mandate_details(
processor.get_key_store(),
&*state.store,
payment_method,
@@ -1915,7 +1940,7 @@ async fn payment_response_update_tracker<F: Clone, T: types::Capturable>(
.await
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to update payment method in db")?;
- }
+ }
}
}