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(kv): add payout_id to payout_attempt lookup for correct KV partition key resolution (#12613)

Hrithikeshdcf9f166aaeb
diff --git a/crates/diesel_models/src/query/payout_attempt.rs b/crates/diesel_models/src/query/payout_attempt.rs
index 28de010675..a0b2015840 100644
--- a/crates/diesel_models/src/query/payout_attempt.rs
+++ b/crates/diesel_models/src/query/payout_attempt.rs
@@ -83,6 +83,22 @@ impl PayoutAttempt {
.await
}
+ pub async fn find_by_merchant_id_payout_id_payout_attempt_id(
+ conn: &PgPooledConn,
+ merchant_id: &common_utils::id_type::MerchantId,
+ payout_id: &common_utils::id_type::PayoutId,
+ payout_attempt_id: &str,
+ ) -> StorageResult<Self> {
+ generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
+ conn,
+ dsl::merchant_id
+ .eq(merchant_id.to_owned())
+ .and(dsl::payout_id.eq(payout_id.to_owned()))
+ .and(dsl::payout_attempt_id.eq(payout_attempt_id.to_owned())),
+ )
+ .await
+ }
+
pub async fn find_by_merchant_id_payout_attempt_id(
conn: &PgPooledConn,
merchant_id: &common_utils::id_type::MerchantId,
diff --git a/crates/hyperswitch_domain_models/src/payouts/payout_attempt.rs b/crates/hyperswitch_domain_models/src/payouts/payout_attempt.rs
index abc5096a5f..5abe9fc09c 100644
--- a/crates/hyperswitch_domain_models/src/payouts/payout_attempt.rs
+++ b/crates/hyperswitch_domain_models/src/payouts/payout_attempt.rs
@@ -35,6 +35,14 @@ pub trait PayoutAttemptInterface {
_storage_scheme: MerchantStorageScheme,
) -> error_stack::Result<PayoutAttempt, Self::Error>;
+ async fn find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
+ &self,
+ _merchant_id: &id_type::MerchantId,
+ _payout_id: &id_type::PayoutId,
+ _payout_attempt_id: &str,
+ _storage_scheme: MerchantStorageScheme,
+ ) -> error_stack::Result<PayoutAttempt, Self::Error>;
+
async fn find_payout_attempt_by_merchant_id_connector_payout_id(
&self,
_merchant_id: &id_type::MerchantId,
diff --git a/crates/router/src/core/payout_link.rs b/crates/router/src/core/payout_link.rs
index 2155d1133a..f0bf66d22e 100644
--- a/crates/router/src/core/payout_link.rs
+++ b/crates/router/src/core/payout_link.rs
@@ -55,8 +55,9 @@ pub async fn initiate_payout_link(
.await
.to_not_found_response(errors::ApiErrorResponse::PayoutNotFound)?;
let payout_attempt = db
- .find_payout_attempt_by_merchant_id_payout_attempt_id(
+ .find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
merchant_id,
+ &req.payout_id,
&get_payout_attempt_id(payout.payout_id.get_string_repr(), payout.attempt_count),
platform.get_processor().get_account().storage_scheme,
)
diff --git a/crates/router/src/core/payouts.rs b/crates/router/src/core/payouts.rs
index 1c50d131e7..0db8a4cc16 100644
--- a/crates/router/src/core/payouts.rs
+++ b/crates/router/src/core/payouts.rs
@@ -849,8 +849,9 @@ pub async fn payouts_list_core(
for payout in payouts {
match db
- .find_payout_attempt_by_merchant_id_payout_attempt_id(
+ .find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
merchant_id,
+ &payout.payout_id,
&utils::get_payout_attempt_id(
payout.payout_id.get_string_repr(),
payout.attempt_count,
@@ -3365,8 +3366,9 @@ pub async fn make_payout_data(
utils::get_payout_attempt_id(payouts.payout_id.get_string_repr(), payouts.attempt_count);
let mut payout_attempt = db
- .find_payout_attempt_by_merchant_id_payout_attempt_id(
+ .find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
merchant_id,
+ &payouts.payout_id,
&payout_attempt_id,
platform.get_processor().get_account().storage_scheme,
)
@@ -3902,8 +3904,9 @@ pub async fn payouts_manual_update_core(
let payout_attempt = state
.store
- .find_payout_attempt_by_merchant_id_payout_attempt_id(
+ .find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
&merchant_id,
+ &payout_id,
&payout_attempt_id,
merchant_account.storage_scheme,
)
diff --git a/crates/router/src/db/kafka_store.rs b/crates/router/src/db/kafka_store.rs
index 1d822ab2e7..cad17bbb58 100644
--- a/crates/router/src/db/kafka_store.rs
+++ b/crates/router/src/db/kafka_store.rs
@@ -2530,6 +2530,23 @@ impl PayoutAttemptInterface for KafkaStore {
.await
}
+ async fn find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
+ &self,
+ merchant_id: &id_type::MerchantId,
+ payout_id: &id_type::PayoutId,
+ payout_attempt_id: &str,
+ storage_scheme: MerchantStorageScheme,
+ ) -> CustomResult<storage::PayoutAttempt, errors::StorageError> {
+ self.diesel_store
+ .find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
+ merchant_id,
+ payout_id,
+ payout_attempt_id,
+ storage_scheme,
+ )
+ .await
+ }
+
async fn find_payout_attempt_by_merchant_id_connector_payout_id(
&self,
merchant_id: &id_type::MerchantId,
diff --git a/crates/storage_impl/src/mock_db/payout_attempt.rs b/crates/storage_impl/src/mock_db/payout_attempt.rs
index fbd45d6d1f..c330c3595a 100644
--- a/crates/storage_impl/src/mock_db/payout_attempt.rs
+++ b/crates/storage_impl/src/mock_db/payout_attempt.rs
@@ -44,6 +44,17 @@ impl PayoutAttemptInterface for MockDb {
Err(StorageError::MockDbError)?
}
+ async fn find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
+ &self,
+ _merchant_id: &common_utils::id_type::MerchantId,
+ _payout_id: &common_utils::id_type::PayoutId,
+ _payout_attempt_id: &str,
+ _storage_scheme: storage_enums::MerchantStorageScheme,
+ ) -> CustomResult<PayoutAttempt, StorageError> {
+ // TODO: Implement function for `MockDb`
+ Err(StorageError::MockDbError)?
+ }
+
async fn find_payout_attempt_by_merchant_id_connector_payout_id(
&self,
_merchant_id: &common_utils::id_type::MerchantId,
diff --git a/crates/storage_impl/src/payouts/payout_attempt.rs b/crates/storage_impl/src/payouts/payout_attempt.rs
index 2f8361a812..0886cf492a 100644
--- a/crates/storage_impl/src/payouts/payout_attempt.rs
+++ b/crates/storage_impl/src/payouts/payout_attempt.rs
@@ -315,6 +315,64 @@ impl<T: DatabaseStore> PayoutAttemptInterface for KVRouterStore<T> {
}
}
+ #[instrument(skip_all)]
+ async fn find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
+ &self,
+ merchant_id: &common_utils::id_type::MerchantId,
+ payout_id: &common_utils::id_type::PayoutId,
+ payout_attempt_id: &str,
+ storage_scheme: MerchantStorageScheme,
+ ) -> error_stack::Result<PayoutAttempt, errors::StorageError> {
+ let storage_scheme = Box::pin(decide_storage_scheme::<_, DieselPayoutAttempt>(
+ self,
+ storage_scheme,
+ Op::Find,
+ ))
+ .await;
+ match storage_scheme {
+ MerchantStorageScheme::PostgresOnly => {
+ self.router_store
+ .find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
+ merchant_id,
+ payout_id,
+ payout_attempt_id,
+ storage_scheme,
+ )
+ .await
+ }
+ MerchantStorageScheme::RedisKv => {
+ let key = PartitionKey::MerchantIdPayoutId {
+ merchant_id,
+ payout_id,
+ };
+ // TODO: Move all key generation logic to a common function
+ let field = format!("poa_{}", payout_attempt_id);
+ Box::pin(utils::try_redis_get_else_try_database_get(
+ async {
+ Box::pin(kv_wrapper(
+ self,
+ KvOperation::<DieselPayoutAttempt>::HGet(&field),
+ key,
+ ))
+ .await?
+ .try_into_hget()
+ },
+ || async {
+ self.router_store
+ .find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
+ merchant_id,
+ payout_id,
+ payout_attempt_id,
+ storage_scheme,
+ )
+ .await
+ },
+ ))
+ .await
+ }
+ }
+ }
+
#[instrument(skip_all)]
async fn find_payout_attempt_by_merchant_id_connector_payout_id(
&self,
@@ -478,6 +536,29 @@ impl<T: DatabaseStore> PayoutAttemptInterface for crate::RouterStore<T> {
})
}
+ #[instrument(skip_all)]
+ async fn find_payout_attempt_by_merchant_id_payout_id_payout_attempt_id(
+ &self,
+ merchant_id: &common_utils::id_type::MerchantId,
+ payout_id: &common_utils::id_type::PayoutId,
+ payout_attempt_id: &str,
+ _storage_scheme: MerchantStorageScheme,
+ ) -> error_stack::Result<PayoutAttempt, errors::StorageError> {
+ let conn = pg_connection_read(self).await?;
+ DieselPayoutAttempt::find_by_merchant_id_payout_id_payout_attempt_id(
+ &conn,
+ merchant_id,
+ payout_id,
+ payout_attempt_id,
+ )
+ .await
+ .map(PayoutAttempt::from_storage_model)
+ .map_err(|er| {
+ let new_err = diesel_error_to_data_error(*er.current_context());
+ er.change_context(new_err)
+ })
+ }
+
#[instrument(skip_all)]
async fn find_payout_attempt_by_merchant_id_connector_payout_id(
&self,