Skip to content

Commit

Permalink
Merge branch 'main' into add_ctx_generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Chethan-rao authored Oct 4, 2023
2 parents ed6fd5a + 4225238 commit 5357b83
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 145 deletions.
109 changes: 8 additions & 101 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions crates/diesel_models/src/query/merchant_key_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
10 changes: 8 additions & 2 deletions crates/router/src/connector/stax/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct StaxPaymentsRequest {
is_refundable: bool,
pre_auth: bool,
meta: StaxPaymentsRequestMetaData,
idempotency_id: Option<String>,
}

impl TryFrom<&types::PaymentsAuthorizeRouterData> for StaxPaymentsRequest {
Expand Down Expand Up @@ -51,6 +52,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for StaxPaymentsRequest {
Err(errors::ConnectorError::InvalidWalletToken)?
}
}),
idempotency_id: Some(item.connector_request_reference_id.clone()),
})
}
api::PaymentMethodData::BankDebit(
Expand All @@ -69,6 +71,7 @@ impl TryFrom<&types::PaymentsAuthorizeRouterData> for StaxPaymentsRequest {
Err(errors::ConnectorError::InvalidWalletToken)?
}
}),
idempotency_id: Some(item.connector_request_reference_id.clone()),
})
}
api::PaymentMethodData::BankDebit(_)
Expand Down Expand Up @@ -282,6 +285,7 @@ pub struct StaxPaymentsResponse {
child_captures: Vec<StaxChildCapture>,
#[serde(rename = "type")]
payment_response_type: StaxPaymentResponseTypes,
idempotency_id: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -323,12 +327,14 @@ impl<F, T>
Ok(Self {
status,
response: Ok(types::PaymentsResponseData::TransactionResponse {
resource_id: types::ResponseId::ConnectorTransactionId(item.response.id),
resource_id: types::ResponseId::ConnectorTransactionId(item.response.id.clone()),
redirection_data: None,
mandate_reference: None,
connector_metadata,
network_txn_id: None,
connector_response_reference_id: None,
connector_response_reference_id: Some(
item.response.idempotency_id.unwrap_or(item.response.id),
),
}),
..item.data
})
Expand Down
10 changes: 9 additions & 1 deletion crates/router/src/core/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
56 changes: 55 additions & 1 deletion crates/router/src/db/merchant_key_store.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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]
Expand Down Expand Up @@ -66,6 +71,7 @@ impl MerchantKeyStoreInterface for Store {
.map_err(Into::into)
.into_report()
};

#[cfg(not(feature = "accounts_cache"))]
{
fetch_func()
Expand All @@ -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]
Expand Down Expand Up @@ -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)]
Expand Down
Loading

0 comments on commit 5357b83

Please sign in to comment.