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(payment_methods): prevent orphan payment_methods row on recurring/MIT charge via connector mandate (#13417)

Shankar Singh Cc3881f3d571a
diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs
index 64d142cbef..841dee5f36 100644
--- a/crates/router/src/core/payments/operations/payment_confirm.rs
+++ b/crates/router/src/core/payments/operations/payment_confirm.rs
@@ -599,7 +599,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
request.mandate_data.clone(),
request.off_session,
payment_intent.setup_future_usage,
- request.customer_acceptance.clone(),
+ customer_acceptance.clone(),
request.payment_token.clone(),
payment_attempt.payment_method.or(request.payment_method),
)
diff --git a/crates/router/src/core/payments/tokenization.rs b/crates/router/src/core/payments/tokenization.rs
index 426fee2dd7..9bb4dd1f53 100644
--- a/crates/router/src/core/payments/tokenization.rs
+++ b/crates/router/src/core/payments/tokenization.rs
@@ -286,7 +286,37 @@ where
_ => (None, None, None),
};
- let pm_id = if customer_acceptance.is_some() {
+ let pm_id = if let Some(existing_pm) = payment_method_info.clone().filter(|_| {
+ matches!(
+ save_payment_method_data.request.get_payment_method_data(),
+ domain::PaymentMethodData::MandatePayment
+ | domain::PaymentMethodData::CardToken(_)
+ )
+ }) {
+ // Recharge of an already-saved payment method where the request
+ // carries no fresh raw card data — either a recurring/MIT charge via
+ // an established connector mandate (PaymentMethodData::MandatePayment)
+ // or a repeat-customer confirm that only supplies a saved-card token +
+ // CVC (PaymentMethodData::CardToken; card_token always references an
+ // existing saved method, there is no "new card" form of it). There is
+ // no new card to save in either case; update the existing row's
+ // last-used timestamp instead of falling through to the "unknown
+ // payment method" branches below, which would otherwise mint a
+ // locker-less orphan payment_methods row (no card => no locker call
+ // => no dedup => throwaway locker_id).
+ payment_methods::cards::update_last_used_at(
+ &existing_pm,
+ state,
+ platform.get_provider().get_account().storage_scheme,
+ platform.get_provider().get_key_store(),
+ )
+ .await
+ .map_err(|e| {
+ logger::error!("Failed to update last used at: {:?}", e);
+ })
+ .ok();
+ Some(existing_pm.get_id().clone())
+ } else if customer_acceptance.is_some() {
let payment_method_data =
save_payment_method_data.request.get_payment_method_data();
let payment_method_create_request =