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

refactor(payment_attempt): standardize lookup and update methods to use payment_id + processor_merchant_id + attempt_id (#12549)

Hrithikesh68e5b754a7bd
diff --git a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
index 6a57af6448..c4d942352c 100644
--- a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
+++ b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
@@ -109,16 +109,6 @@ pub trait PaymentAttemptInterface {
storage_scheme: storage_enums::MerchantStorageScheme,
) -> error_stack::Result<PaymentAttempt, Self::Error>;
- #[cfg(feature = "v1")]
- async fn find_payment_attempt_by_connector_transaction_id_payment_id_processor_merchant_id(
- &self,
- connector_transaction_id: &ConnectorTransactionId,
- payment_id: &id_type::PaymentId,
- processor_merchant_id: &id_type::MerchantId,
- storage_scheme: storage_enums::MerchantStorageScheme,
- merchant_key_store: &MerchantKeyStore,
- ) -> error_stack::Result<PaymentAttempt, Self::Error>;
-
#[cfg(feature = "v1")]
async fn find_payment_attempt_last_successful_attempt_by_payment_id_processor_merchant_id(
&self,
diff --git a/crates/router/src/core/disputes.rs b/crates/router/src/core/disputes.rs
index 599fedfff0..12f57d1139 100644
--- a/crates/router/src/core/disputes.rs
+++ b/crates/router/src/core/disputes.rs
@@ -85,9 +85,10 @@ pub async fn retrieve_dispute(
core_utils::validate_profile_id_from_auth_layer(profile_id.clone(), &dispute)?;
let payment_attempt = db
- .find_payment_attempt_by_attempt_id_processor_merchant_id(
- &dispute.attempt_id,
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
+ &dispute.payment_id,
platform.get_processor().get_account().get_id(),
+ &dispute.attempt_id,
platform.get_processor().get_account().storage_scheme,
platform.get_processor().get_key_store(),
)
@@ -330,9 +331,10 @@ pub async fn accept_dispute(
.change_context(errors::ApiErrorResponse::PaymentNotFound)?;
let payment_attempt = db
- .find_payment_attempt_by_attempt_id_processor_merchant_id(
- &dispute.attempt_id,
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
+ &dispute.payment_id,
processor.get_account().get_id(),
+ &dispute.attempt_id,
processor.get_account().storage_scheme,
processor.get_key_store(),
)
@@ -454,9 +456,10 @@ pub async fn submit_evidence(
.change_context(errors::ApiErrorResponse::PaymentNotFound)?;
let payment_attempt = db
- .find_payment_attempt_by_attempt_id_processor_merchant_id(
- &dispute.attempt_id,
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
+ &dispute.payment_id,
processor.get_account().get_id(),
+ &dispute.attempt_id,
processor.get_account().storage_scheme,
processor.get_key_store(),
)
diff --git a/crates/router/src/core/files/helpers.rs b/crates/router/src/core/files/helpers.rs
index d1265e1860..e490d4243b 100644
--- a/crates/router/src/core/files/helpers.rs
+++ b/crates/router/src/core/files/helpers.rs
@@ -330,9 +330,10 @@ pub async fn upload_and_get_provider_provider_file_id_profile_id(
let payment_attempt = state
.store
- .find_payment_attempt_by_attempt_id_processor_merchant_id(
- &dispute.attempt_id,
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
+ &dispute.payment_id,
processor.get_account().get_id(),
+ &dispute.attempt_id,
processor.get_account().storage_scheme,
processor.get_key_store(),
)
diff --git a/crates/router/src/core/fraud_check.rs b/crates/router/src/core/fraud_check.rs
index 6d75db152e..72214e2823 100644
--- a/crates/router/src/core/fraud_check.rs
+++ b/crates/router/src/core/fraud_check.rs
@@ -822,9 +822,10 @@ pub async fn make_fulfillment_api_call(
req: frm_core_types::FrmFulfillmentRequest,
) -> RouterResponse<frm_types::FraudCheckResponseData> {
let payment_attempt = db
- .find_payment_attempt_by_attempt_id_processor_merchant_id(
- &payment_intent.active_attempt.get_id(),
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
+ &payment_intent.payment_id,
platform.get_processor().get_account().get_id(),
+ &payment_intent.active_attempt.get_id(),
platform.get_processor().get_account().storage_scheme,
platform.get_processor().get_key_store(),
)
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index e9846c849b..83eaa7ed58 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -4267,9 +4267,10 @@ impl PaymentRedirectFlow for PaymentAuthenticateCompleteAuthorize {
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;
let payment_attempt = state
.store
- .find_payment_attempt_by_attempt_id_processor_merchant_id(
- &payment_intent.active_attempt.get_id(),
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
+ &payment_intent.payment_id,
platform.get_processor().get_account().get_id(),
+ &payment_intent.active_attempt.get_id(),
platform.get_processor().get_account().storage_scheme,
platform.get_processor().get_key_store(),
)
diff --git a/crates/router/src/core/refunds.rs b/crates/router/src/core/refunds.rs
index 527f327947..77cee546f1 100644
--- a/crates/router/src/core/refunds.rs
+++ b/crates/router/src/core/refunds.rs
@@ -740,10 +740,10 @@ pub async fn refund_retrieve_core(
.to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?;
let payment_attempt = db
- .find_payment_attempt_by_connector_transaction_id_payment_id_processor_merchant_id(
- &refund.connector_transaction_id,
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
payment_id,
processor_merchant_id,
+ &refund.attempt_id,
platform.get_processor().get_account().storage_scheme,
platform.get_processor().get_key_store(),
)
@@ -2136,10 +2136,10 @@ pub async fn trigger_refund_execute_workflow(
match (&refund.sent_to_gateway, &refund.refund_status) {
(false, enums::RefundStatus::Pending) => {
let payment_attempt = db
- .find_payment_attempt_by_connector_transaction_id_payment_id_processor_merchant_id(
- &refund.connector_transaction_id,
- &refund_core.payment_id,
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
+ &refund.payment_id,
processor_merchant_id,
+ &refund.attempt_id,
processor_storage_scheme,
platform.get_processor().get_key_store(),
)
diff --git a/crates/router/src/db/kafka_store.rs b/crates/router/src/db/kafka_store.rs
index 91183b76b6..e31e0f092f 100644
--- a/crates/router/src/db/kafka_store.rs
+++ b/crates/router/src/db/kafka_store.rs
@@ -1689,26 +1689,6 @@ impl PaymentAttemptInterface for KafkaStore {
Ok(attempt)
}
- #[cfg(feature = "v1")]
- async fn find_payment_attempt_by_connector_transaction_id_payment_id_processor_merchant_id(
- &self,
- connector_transaction_id: &common_utils::types::ConnectorTransactionId,
- payment_id: &id_type::PaymentId,
- processor_merchant_id: &id_type::MerchantId,
- storage_scheme: MerchantStorageScheme,
- key_store: &domain::MerchantKeyStore,
- ) -> CustomResult<storage::PaymentAttempt, errors::StorageError> {
- self.diesel_store
- .find_payment_attempt_by_connector_transaction_id_payment_id_processor_merchant_id(
- connector_transaction_id,
- payment_id,
- processor_merchant_id,
- storage_scheme,
- key_store,
- )
- .await
- }
-
#[cfg(feature = "v1")]
async fn find_payment_attempt_by_processor_merchant_id_connector_txn_id(
&self,
diff --git a/crates/router/src/utils.rs b/crates/router/src/utils.rs
index ff0051bd87..b5536c8b85 100644
--- a/crates/router/src/utils.rs
+++ b/crates/router/src/utils.rs
@@ -257,9 +257,10 @@ pub async fn find_payment_intent_from_refund_id_type(
.to_not_found_response(errors::ApiErrorResponse::RefundNotFound)?,
};
let attempt = db
- .find_payment_attempt_by_attempt_id_processor_merchant_id(
- &refund.attempt_id,
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
+ &refund.payment_id,
platform.get_processor().get_account().get_id(),
+ &refund.attempt_id,
platform.get_processor().get_account().storage_scheme,
platform.get_processor().get_key_store(),
)
@@ -380,9 +381,10 @@ pub async fn get_mca_from_payment_intent(
#[cfg(feature = "v1")]
let payment_attempt = db
- .find_payment_attempt_by_attempt_id_processor_merchant_id(
- &payment_intent.active_attempt.get_id(),
+ .find_payment_attempt_by_payment_id_processor_merchant_id_attempt_id(
+ &payment_intent.payment_id,
platform.get_processor().get_account().get_id(),
+ &payment_intent.active_attempt.get_id(),
platform.get_processor().get_account().storage_scheme,
platform.get_processor().get_key_store(),
)
diff --git a/crates/storage_impl/src/mock_db/payment_attempt.rs b/crates/storage_impl/src/mock_db/payment_attempt.rs
index 484383e88a..7c4db326f7 100644
--- a/crates/storage_impl/src/mock_db/payment_attempt.rs
+++ b/crates/storage_impl/src/mock_db/payment_attempt.rs
@@ -320,19 +320,6 @@ impl PaymentAttemptInterface for MockDb {
Err(StorageError::MockDbError)?
}
- #[cfg(feature = "v1")]
- async fn find_payment_attempt_by_connector_transaction_id_payment_id_processor_merchant_id(
- &self,
- _connector_transaction_id: &common_utils::types::ConnectorTransactionId,
- _payment_id: &common_utils::id_type::PaymentId,
- _processor_merchant_id: &common_utils::id_type::MerchantId,
- _storage_scheme: storage_enums::MerchantStorageScheme,
- _merchant_key_store: &MerchantKeyStore,
- ) -> CustomResult<PaymentAttempt, StorageError> {
- // [#172]: Implement function for `MockDb`
- Err(StorageError::MockDbError)?
- }
-
#[cfg(feature = "v1")]
// safety: only used for testing
#[allow(clippy::unwrap_used)]
diff --git a/crates/storage_impl/src/payments/payment_attempt.rs b/crates/storage_impl/src/payments/payment_attempt.rs
index 0c3562f0b9..87c162aec0 100644
--- a/crates/storage_impl/src/payments/payment_attempt.rs
+++ b/crates/storage_impl/src/payments/payment_attempt.rs
@@ -1,6 +1,6 @@
-use common_utils::{errors::CustomResult, ext_traits::AsyncExt};
#[cfg(feature = "v1")]
-use common_utils::{fallback_reverse_lookup_not_found, types::ConnectorTransactionId};
+use common_utils::fallback_reverse_lookup_not_found;
+use common_utils::{errors::CustomResult, ext_traits::AsyncExt};
use diesel_models::{
enums::{
MandateAmountData as DieselMandateAmountData, MandateDataType as DieselMandateType,
@@ -177,44 +177,6 @@ impl<T: DatabaseStore> PaymentAttemptInterface for RouterStore<T> {
.change_context(errors::StorageError::DecryptionError)
}
- #[cfg(feature = "v1")]
- #[instrument(skip_all)]
- async fn find_payment_attempt_by_connector_transaction_id_payment_id_processor_merchant_id(
- &self,
- connector_transaction_id: &ConnectorTransactionId,
- payment_id: &common_utils::id_type::PaymentId,
- processor_merchant_id: &common_utils::id_type::MerchantId,
- _storage_scheme: MerchantStorageScheme,
- merchant_key_store: &MerchantKeyStore,
- ) -> CustomResult<PaymentAttempt, errors::StorageError> {
- let conn = pg_connection_read(self).await?;
- let key_manager_state = self
- .get_keymanager_state()
- .attach_printable("Missing KeyManagerState")?;
- DieselPaymentAttempt::find_by_connector_transaction_id_payment_id_processor_merchant_id(
- &conn,
- connector_transaction_id,
- payment_id,
- processor_merchant_id,
- )
- .await
- .map_err(|er| {
- let new_err = diesel_error_to_data_error(*er.current_context());
- er.change_context(new_err)
- })
- .async_map(|diesel_payment_attempt| async {
- PaymentAttempt::convert_back(
- key_manager_state,
- diesel_payment_attempt,
- merchant_key_store.key.get_inner(),
- merchant_key_store.merchant_id.clone().into(),
- )
- .await
- .change_context(errors::StorageError::DecryptionError)
- })
- .await?
- }
-
#[cfg(feature = "v1")]
#[instrument(skip_all)]
async fn find_payment_attempt_last_successful_attempt_by_payment_id_processor_merchant_id(
@@ -1208,81 +1170,6 @@ impl<T: DatabaseStore> PaymentAttemptInterface for KVRouterStore<T> {
.await
}
- #[cfg(feature = "v1")]
- #[instrument(skip_all)]
- async fn find_payment_attempt_by_connector_transaction_id_payment_id_processor_merchant_id(
- &self,
- connector_transaction_id: &ConnectorTransactionId,
- payment_id: &common_utils::id_type::PaymentId,
- processor_merchant_id: &common_utils::id_type::MerchantId,
- storage_scheme: MerchantStorageScheme,
- merchant_key_store: &MerchantKeyStore,
- ) -> error_stack::Result<PaymentAttempt, errors::StorageError> {
- let storage_scheme = Box::pin(decide_storage_scheme::<_, DieselPaymentAttempt>(
- self,
- storage_scheme,
- Op::Find,
- ))
- .await;
- match storage_scheme {
- MerchantStorageScheme::PostgresOnly => self
- .router_store
- .find_payment_attempt_by_connector_transaction_id_payment_id_processor_merchant_id(
- connector_transaction_id,
- payment_id,
- processor_merchant_id,
- storage_scheme,
- merchant_key_store,
- )
- .await,
- MerchantStorageScheme::RedisKv => {
- // We assume that PaymentAttempt <=> PaymentIntent is a one-to-one relation for now
- let lookup_id = format!(
- "pa_conn_trans_{}_{}",
- processor_merchant_id.get_string_repr(),
- connector_transaction_id.get_id()
- );
- let lookup = fallback_reverse_lookup_not_found!(
- self.get_lookup_by_lookup_id(&lookup_id, storage_scheme)
- .await,
- self.router_store
- .find_payment_attempt_by_connector_transaction_id_payment_id_processor_merchant_id(
- connector_transaction_id,
- payment_id,
- processor_merchant_id,
- storage_scheme,
- merchant_key_store,
- )
- .await
- );
-
- let key = PartitionKey::CombinationKey {
- combination: &lookup.pk_id,
- };
- let key_manager_state = self
- .get_keymanager_state()
- .attach_printable("Missing KeyManagerState")?;
-
- Box::pin(try_redis_get_else_try_database_get(
- async {
- let diesel_payment_attempt = Box::pin(kv_wrapper(self, KvOperation::<DieselPaymentAttempt>::HGet(&lookup.sk_id), key)).await?.try_into_hget()?;
- PaymentAttempt::convert_back(
- key_manager_state,
- diesel_payment_attempt,
- merchant_key_store.key.get_inner(),
- processor_merchant_id.clone().into(),
- )
- .await
- .change_context(redis_interface::errors::RedisError::UnknownResult)
- .attach_printable("Error while constructing domain model")
- },
- || async {self.router_store.find_payment_attempt_by_connector_transaction_id_payment_id_processor_merchant_id(connector_transaction_id, payment_id, processor_merchant_id, storage_scheme, merchant_key_store).await},
- ))
- .await
- }
- }
- }
-
#[cfg(feature = "v1")]
#[instrument(skip_all)]
async fn find_payment_attempt_last_successful_attempt_by_payment_id_processor_merchant_id(