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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
feat(payment_methods): inactivate payment methods sharing an auxiliary fingerprint (#14854)
Shankar Singh Cb6f71b02d663
diff --git a/crates/diesel_models/src/query/payment_method.rs b/crates/diesel_models/src/query/payment_method.rsindex c4a1c84926..7695e6ae82 100644--- a/crates/diesel_models/src/query/payment_method.rs+++ b/crates/diesel_models/src/query/payment_method.rs@@ -335,6 +335,27 @@ impl PaymentMethod {.await}+ pub async fn find_by_auxiliary_fingerprint_id_and_status(+ conn: &DatabaseConnectionWithContext<'_>,+ customer_id: &common_utils::id_type::GlobalCustomerId,+ merchant_id: &common_utils::id_type::MerchantId,+ auxiliary_fingerprint_id: &str,+ status: storage_enums::PaymentMethodStatus,+ ) -> StorageResult<Vec<Self>> {+ generics::generic_filter::<<Self as HasTable>::Table, _, _, _>(+ conn,+ dsl::customer_id+ .eq(customer_id.to_owned())+ .and(dsl::merchant_id.eq(merchant_id.to_owned()))+ .and(dsl::auxiliary_fingerprint_id.eq(auxiliary_fingerprint_id.to_owned()))+ .and(dsl::status.eq(status)),+ None,+ None,+ Some(dsl::created_at.desc()),+ )+ .await+ }+pub async fn find_by_global_customer_id(conn: &DatabaseConnectionWithContext<'_>,customer_id: &common_utils::id_type::GlobalCustomerId,diff --git a/crates/hyperswitch_domain_models/src/payment_methods.rs b/crates/hyperswitch_domain_models/src/payment_methods.rsindex 9e31a9da8a..a6f08c39da 100644--- a/crates/hyperswitch_domain_models/src/payment_methods.rs+++ b/crates/hyperswitch_domain_models/src/payment_methods.rs@@ -660,6 +660,16 @@ pub trait PaymentMethodInterface {fingerprint_id: &str,) -> CustomResult<PaymentMethod, Self::Error>;+ #[cfg(feature = "v2")]+ async fn find_payment_methods_by_auxiliary_fingerprint_id_and_status(+ &self,+ key_store: &MerchantKeyStore,+ customer_id: &id_type::GlobalCustomerId,+ merchant_id: &id_type::MerchantId,+ auxiliary_fingerprint_id: &str,+ status: common_enums::PaymentMethodStatus,+ ) -> CustomResult<Vec<PaymentMethod>, Self::Error>;+#[cfg(feature = "v1")]async fn delete_payment_method_by_merchant_id_payment_method_id(&self,diff --git a/crates/router/src/core/payment_methods.rs b/crates/router/src/core/payment_methods.rsindex d641382ac1..b6f725558e 100644--- a/crates/router/src/core/payment_methods.rs+++ b/crates/router/src/core/payment_methods.rs@@ -6275,12 +6275,11 @@ pub async fn retrieve_payment_method(when(matches!(payment_method.status,- common_enums::PaymentMethodStatus::Inactive- | common_enums::PaymentMethodStatus::Redacted+ common_enums::PaymentMethodStatus::Redacted),|| {Err(report!(errors::ApiErrorResponse::PaymentMethodNotFound)- .attach_printable("Payment method is inactive or redacted"))+ .attach_printable("Payment method is redacted"))},)?;@@ -7022,9 +7021,69 @@ pub async fn update_payment_method_status_internal(.change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Failed to update payment method in db")?;+ if status == enums::PaymentMethodStatus::Active {+ inactivate_payment_methods_with_same_auxiliary_fingerprint(+ state,+ key_store,+ storage_scheme,+ &updated_pm,+ initiator,+ )+ .await?;+ }+Ok(updated_pm)}+#[cfg(feature = "v2")]+#[instrument(skip_all)]+async fn inactivate_payment_methods_with_same_auxiliary_fingerprint(+ state: &SessionState,+ key_store: &domain::MerchantKeyStore,+ storage_scheme: enums::MerchantStorageScheme,+ activated_payment_method: &domain::PaymentMethod,+ initiator: Option<&domain::Initiator>,+) -> RouterResult<()> {+ if let (Some(customer_id), Some(auxiliary_fingerprint_id)) = (+ activated_payment_method.customer_id.as_ref(),+ activated_payment_method.auxiliary_fingerprint_id.as_deref(),+ ) {+ let db = &*state.store;++ let superseded_payment_methods = db+ .find_payment_methods_by_auxiliary_fingerprint_id_and_status(+ key_store,+ customer_id,+ &activated_payment_method.merchant_id,+ auxiliary_fingerprint_id,+ enums::PaymentMethodStatus::Active,+ )+ .await+ .change_context(errors::ApiErrorResponse::InternalServerError)+ .attach_printable("Failed to find payment methods with same auxiliary fingerprint")?+ .into_iter()+ .filter(|payment_method| payment_method.id != activated_payment_method.id);++ for payment_method in superseded_payment_methods {+ let pm_update = storage::PaymentMethodUpdate::StatusUpdate {+ status: Some(enums::PaymentMethodStatus::Inactive),+ last_modified_by: initiator+ .and_then(|initiator| initiator.to_created_by())+ .map(|last_modified_by| last_modified_by.to_string()),+ };++ db.update_payment_method(key_store, payment_method, pm_update, storage_scheme, None)+ .await+ .change_context(errors::ApiErrorResponse::InternalServerError)+ .attach_printable(+ "Failed to inactivate payment method with same auxiliary fingerprint",+ )?;+ }+ }++ Ok(())+}+#[cfg(feature = "v2")]#[instrument(skip_all)]pub async fn update_payment_method(@@ -7067,7 +7126,8 @@ pub async fn update_payment_method_core() -> RouterResult<(api::PaymentMethodResponse, domain::PaymentMethod)> {// `PayThenVault` defers vaulting to the acknowledgement, so this update is what writes the// card to the vault and the database. Both update endpoints reach this point.- let volatile_payment_method = if request.status == Some(enums::PaymentMethodStatus::Active)+ let is_activation = request.status == Some(enums::PaymentMethodStatus::Active);+ let volatile_payment_method = if is_activation&& resolve_payment_method_integration_type(state, platform).await== pm_types::PaymentMethodIntegrationType::PayThenVault{@@ -7177,6 +7237,17 @@ pub async fn update_payment_method_core(}}+ if is_activation {+ inactivate_payment_methods_with_same_auxiliary_fingerprint(+ state,+ platform.get_provider().get_key_store(),+ platform.get_provider().get_account().storage_scheme,+ &payment_method,+ platform.get_initiator(),+ )+ .await?;+ }+Ok((response, payment_method))}}@@ -7250,11 +7321,6 @@ pub async fn delete_payment_method_core(.clone().get_required_value("GlobalCustomerId")?;- when(- payment_method.status == enums::PaymentMethodStatus::Inactive,- || Err(errors::ApiErrorResponse::PaymentMethodNotFound),- )?;-when(payment_method.status == enums::PaymentMethodStatus::Redacted,|| Err(errors::ApiErrorResponse::PaymentMethodRedacted),diff --git a/crates/router/src/db/kafka_store.rs b/crates/router/src/db/kafka_store.rsindex d937760fd4..7184c3ae3e 100644--- a/crates/router/src/db/kafka_store.rs+++ b/crates/router/src/db/kafka_store.rs@@ -2644,6 +2644,26 @@ impl PaymentMethodInterface for KafkaStore {.find_payment_method_by_fingerprint_id(key_store, fingerprint_id).await}++ #[cfg(feature = "v2")]+ async fn find_payment_methods_by_auxiliary_fingerprint_id_and_status(+ &self,+ key_store: &domain::MerchantKeyStore,+ customer_id: &id_type::GlobalCustomerId,+ merchant_id: &id_type::MerchantId,+ auxiliary_fingerprint_id: &str,+ status: common_enums::PaymentMethodStatus,+ ) -> CustomResult<Vec<domain::PaymentMethod>, errors::StorageError> {+ self.diesel_store+ .find_payment_methods_by_auxiliary_fingerprint_id_and_status(+ key_store,+ customer_id,+ merchant_id,+ auxiliary_fingerprint_id,+ status,+ )+ .await+ }}#[cfg(not(feature = "payouts"))]diff --git a/crates/storage_impl/src/payment_method.rs b/crates/storage_impl/src/payment_method.rsindex 8bb920e4f1..b43ab59c46 100644--- a/crates/storage_impl/src/payment_method.rs+++ b/crates/storage_impl/src/payment_method.rs@@ -508,6 +508,26 @@ impl<T: DatabaseStore> PaymentMethodInterface for KVRouterStore<T> {.find_payment_method_by_fingerprint_id(key_store, fingerprint_id).await}++ #[cfg(feature = "v2")]+ async fn find_payment_methods_by_auxiliary_fingerprint_id_and_status(+ &self,+ key_store: &MerchantKeyStore,+ customer_id: &id_type::GlobalCustomerId,+ merchant_id: &id_type::MerchantId,+ auxiliary_fingerprint_id: &str,+ status: common_enums::PaymentMethodStatus,+ ) -> CustomResult<Vec<DomainPaymentMethod>, errors::StorageError> {+ self.router_store+ .find_payment_methods_by_auxiliary_fingerprint_id_and_status(+ key_store,+ customer_id,+ merchant_id,+ auxiliary_fingerprint_id,+ status,+ )+ .await+ }}#[async_trait::async_trait]@@ -894,6 +914,29 @@ impl<T: DatabaseStore> PaymentMethodInterface for RouterStore<T> {).await}++ #[cfg(feature = "v2")]+ async fn find_payment_methods_by_auxiliary_fingerprint_id_and_status(+ &self,+ key_store: &MerchantKeyStore,+ customer_id: &id_type::GlobalCustomerId,+ merchant_id: &id_type::MerchantId,+ auxiliary_fingerprint_id: &str,+ status: common_enums::PaymentMethodStatus,+ ) -> CustomResult<Vec<DomainPaymentMethod>, errors::StorageError> {+ let conn = pg_connection_read(self).await?;+ Box::pin(self.find_resources_new(+ key_store,+ PaymentMethod::find_by_auxiliary_fingerprint_id_and_status(+ &conn,+ customer_id,+ merchant_id,+ auxiliary_fingerprint_id,+ status,+ ),+ ))+ .await+ }}#[async_trait::async_trait]@@ -1253,6 +1296,27 @@ impl PaymentMethodInterface for MockDb {).await}++ #[cfg(feature = "v2")]+ async fn find_payment_methods_by_auxiliary_fingerprint_id_and_status(+ &self,+ key_store: &MerchantKeyStore,+ customer_id: &id_type::GlobalCustomerId,+ merchant_id: &id_type::MerchantId,+ auxiliary_fingerprint_id: &str,+ status: common_enums::PaymentMethodStatus,+ ) -> CustomResult<Vec<DomainPaymentMethod>, errors::StorageError> {+ let payment_methods = self.payment_methods.lock().await;+ let find_pm_by = |pm: &&PaymentMethod| {+ pm.customer_id.as_ref() == Some(customer_id)+ && pm.merchant_id == *merchant_id+ && pm.auxiliary_fingerprint_id.as_deref() == Some(auxiliary_fingerprint_id)+ && pm.status == status+ };+ let error_message = "cannot find payment method".to_string();+ self.get_resources_new(key_store, payment_methods, find_pm_by, error_message)+ .await+ }}#[cfg(feature = "v2")]diff --git a/v2_compatible_migrations/2026-10-06-000001_add_payment_methods_auxiliary_fingerprint_id_index/down.sql b/v2_compatible_migrations/2026-10-06-000001_add_payment_methods_auxiliary_fingerprint_id_index/down.sqlnew file mode 100644index 0000000000..f069a71b47--- /dev/null+++ b/v2_compatible_migrations/2026-10-06-000001_add_payment_methods_auxiliary_fingerprint_id_index/down.sql@@ -0,0 +1 @@+DROP INDEX CONCURRENTLY IF EXISTS payment_methods_customer_id_merchant_id_auxiliary_fingerprint_id_index;diff --git a/v2_compatible_migrations/2026-10-06-000001_add_payment_methods_auxiliary_fingerprint_id_index/metadata.toml b/v2_compatible_migrations/2026-10-06-000001_add_payment_methods_auxiliary_fingerprint_id_index/metadata.tomlnew file mode 100644index 0000000000..79e9221c1f--- /dev/null+++ b/v2_compatible_migrations/2026-10-06-000001_add_payment_methods_auxiliary_fingerprint_id_index/metadata.toml@@ -0,0 +1 @@+run_in_transaction = falsediff --git a/v2_compatible_migrations/2026-10-06-000001_add_payment_methods_auxiliary_fingerprint_id_index/up.sql b/v2_compatible_migrations/2026-10-06-000001_add_payment_methods_auxiliary_fingerprint_id_index/up.sqlnew file mode 100644index 0000000000..1500257a05--- /dev/null+++ b/v2_compatible_migrations/2026-10-06-000001_add_payment_methods_auxiliary_fingerprint_id_index/up.sql@@ -0,0 +1,3 @@+CREATE INDEX CONCURRENTLY IF NOT EXISTS payment_methods_customer_id_merchant_id_auxiliary_fingerprint_id_index+ON payment_methods (customer_id, merchant_id, auxiliary_fingerprint_id)+WHERE auxiliary_fingerprint_id IS NOT NULL;