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): merchant account delete does not delete the merchant_key_store (#2367)

Hudson C. Dalprá35f7ce0f4d9e
diff --git a/crates/diesel_models/src/query/merchant_key_store.rs b/crates/diesel_models/src/query/merchant_key_store.rs
index 02a31a1f39..27ec3be9fc 100644
--- a/crates/diesel_models/src/query/merchant_key_store.rs
+++ b/crates/diesel_models/src/query/merchant_key_store.rs
@@ -27,4 +27,16 @@ impl MerchantKeyStore {
)
.await
}
+
+ #[instrument(skip(conn))]
+ pub async fn delete_by_merchant_id(
+ conn: &PgPooledConn,
+ merchant_id: &str,
+ ) -> StorageResult<bool> {
+ generics::generic_delete::<<Self as HasTable>::Table, _>(
+ conn,
+ dsl::merchant_id.eq(merchant_id.to_owned()),
+ )
+ .await
+ }
}
diff --git a/crates/router/src/core/admin.rs b/crates/router/src/core/admin.rs
index fab416c431..dabe22da62 100644
--- a/crates/router/src/core/admin.rs
+++ b/crates/router/src/core/admin.rs
@@ -465,11 +465,19 @@ pub async fn merchant_account_delete(
state: AppState,
merchant_id: String,
) -> RouterResponse<api::MerchantAccountDeleteResponse> {
+ let mut is_deleted = false;
let db = state.store.as_ref();
- let is_deleted = db
+ let is_merchant_account_deleted = db
.delete_merchant_account_by_merchant_id(&merchant_id)
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
+ if is_merchant_account_deleted {
+ let is_merchant_key_store_deleted = db
+ .delete_merchant_key_store_by_merchant_id(&merchant_id)
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
+ is_deleted = is_merchant_account_deleted && is_merchant_key_store_deleted;
+ }
let response = api::MerchantAccountDeleteResponse {
merchant_id,
deleted: is_deleted,
diff --git a/crates/router/src/db/merchant_key_store.rs b/crates/router/src/db/merchant_key_store.rs
index 8cb93fc369..970e2b7703 100644
--- a/crates/router/src/db/merchant_key_store.rs
+++ b/crates/router/src/db/merchant_key_store.rs
@@ -1,7 +1,7 @@
use error_stack::{IntoReport, ResultExt};
use masking::Secret;
#[cfg(feature = "accounts_cache")]
-use storage_impl::redis::cache::ACCOUNTS_CACHE;
+use storage_impl::redis::cache::{CacheKind, ACCOUNTS_CACHE};
use crate::{
connection,
@@ -27,6 +27,11 @@ pub trait MerchantKeyStoreInterface {
merchant_id: &str,
key: &Secret<Vec<u8>>,
) -> CustomResult<domain::MerchantKeyStore, errors::StorageError>;
+
+ async fn delete_merchant_key_store_by_merchant_id(
+ &self,
+ merchant_id: &str,
+ ) -> CustomResult<bool, errors::StorageError>;
}
#[async_trait::async_trait]
@@ -66,6 +71,7 @@ impl MerchantKeyStoreInterface for Store {
.map_err(Into::into)
.into_report()
};
+
#[cfg(not(feature = "accounts_cache"))]
{
fetch_func()
@@ -90,6 +96,38 @@ impl MerchantKeyStoreInterface for Store {
.change_context(errors::StorageError::DecryptionError)
}
}
+
+ async fn delete_merchant_key_store_by_merchant_id(
+ &self,
+ merchant_id: &str,
+ ) -> CustomResult<bool, errors::StorageError> {
+ let delete_func = || async {
+ let conn = connection::pg_connection_write(self).await?;
+ diesel_models::merchant_key_store::MerchantKeyStore::delete_by_merchant_id(
+ &conn,
+ merchant_id,
+ )
+ .await
+ .map_err(Into::into)
+ .into_report()
+ };
+
+ #[cfg(not(feature = "accounts_cache"))]
+ {
+ delete_func().await
+ }
+
+ #[cfg(feature = "accounts_cache")]
+ {
+ let key_store_cache_key = format!("merchant_key_store_{}", merchant_id);
+ super::cache::publish_and_redact(
+ self,
+ CacheKind::Accounts(key_store_cache_key.into()),
+ delete_func,
+ )
+ .await
+ }
+ }
}
#[async_trait::async_trait]
@@ -140,6 +178,22 @@ impl MerchantKeyStoreInterface for MockDb {
.await
.change_context(errors::StorageError::DecryptionError)
}
+
+ async fn delete_merchant_key_store_by_merchant_id(
+ &self,
+ merchant_id: &str,
+ ) -> CustomResult<bool, errors::StorageError> {
+ let mut merchant_key_stores = self.merchant_key_store.lock().await;
+ let index = merchant_key_stores
+ .iter()
+ .position(|mks| mks.merchant_id == merchant_id)
+ .ok_or(errors::StorageError::ValueNotFound(format!(
+ "No merchant key store found for merchant_id = {}",
+ merchant_id
+ )))?;
+ merchant_key_stores.remove(index);
+ Ok(true)
+ }
}
#[cfg(test)]