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): deduplicate modular saved wallets (#14951)

Shankar Singh C076a191e51dc
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index cd3ebf5d2b..775947fb45 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -6421,14 +6421,22 @@ pub async fn do_list_customer_pm_fetch_customer_if_not_passed(
/// Filters customer payment methods to keep only the latest Apple Pay and Google Pay method
/// when multiple DPANs exist. All other payment methods are returned unchanged.
#[cfg(feature = "v1")]
-fn filter_latest_wallet_methods(
- payment_methods: Vec<api::CustomerPaymentMethod>,
-) -> Vec<api::CustomerPaymentMethod> {
+pub(super) fn filter_latest_wallet_methods<T: Clone>(
+ payment_methods: Vec<T>,
+ wallet_details: impl Fn(
+ &T,
+ ) -> (
+ api_enums::PaymentMethod,
+ Option<api_enums::PaymentMethodType>,
+ Option<time::PrimitiveDateTime>,
+ ),
+) -> Vec<T> {
let (wallet_methods, other_methods): (Vec<_>, Vec<_>) =
payment_methods.into_iter().partition(|pm| {
- pm.payment_method == api_enums::PaymentMethod::Wallet
- && (pm.payment_method_type == Some(api_enums::PaymentMethodType::ApplePay)
- || pm.payment_method_type == Some(api_enums::PaymentMethodType::GooglePay))
+ let (payment_method, payment_method_type, _) = wallet_details(pm);
+ payment_method == api_enums::PaymentMethod::Wallet
+ && (payment_method_type == Some(api_enums::PaymentMethodType::ApplePay)
+ || payment_method_type == Some(api_enums::PaymentMethodType::GooglePay))
});
if wallet_methods.is_empty() {
@@ -6443,13 +6451,13 @@ fn filter_latest_wallet_methods(
] {
let type_methods: Vec<_> = wallet_methods
.iter()
- .filter(|pm| pm.payment_method_type == Some(wallet_type))
+ .filter(|pm| wallet_details(pm).1 == Some(wallet_type))
.cloned()
.collect();
if type_methods.len() > 1 {
let mut sorted = type_methods;
- sorted.sort_by_key(|b| std::cmp::Reverse(b.last_used_at));
+ sorted.sort_by_key(|pm| std::cmp::Reverse(wallet_details(pm).2));
if let Some(latest) = sorted.into_iter().next() {
result.push(latest);
}
@@ -6764,7 +6772,9 @@ pub async fn list_customer_payment_method(
}
// // Filter to keep only the latest Apple Pay method if duplicates exist
- let filtered_customer_pms = filter_latest_wallet_methods(customer_pms);
+ let filtered_customer_pms = filter_latest_wallet_methods(customer_pms, |pm| {
+ (pm.payment_method, pm.payment_method_type, pm.last_used_at)
+ });
let mut response = api::CustomerPaymentMethodsListResponse {
customer_payment_methods: filtered_customer_pms,
diff --git a/crates/router/src/core/payment_methods/client.rs b/crates/router/src/core/payment_methods/client.rs
index 2c2f69ce8a..8c1b484550 100644
--- a/crates/router/src/core/payment_methods/client.rs
+++ b/crates/router/src/core/payment_methods/client.rs
@@ -369,10 +369,18 @@ impl CustomerPaymentMethodsFetcher for ModularCustomerPaymentMethodsFetcher {
}
}
- Ok(customer_payment_methods)
+ Ok(filter_latest_wallet_methods(customer_payment_methods))
}
}
+fn filter_latest_wallet_methods(
+ payment_methods: Vec<CustomerPaymentMethodForClient>,
+) -> Vec<CustomerPaymentMethodForClient> {
+ cards::filter_latest_wallet_methods(payment_methods, |pm| {
+ (pm.payment_method, pm.payment_method_type, pm.last_used_at)
+ })
+}
+
// ---------------------------------------------------------------------------
// Internal context struct
// ---------------------------------------------------------------------------