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(platform): platform refund query update (#11649)
Ankit Kumar Guptab0885ef81ad3
diff --git a/crates/diesel_models/src/query/refund.rs b/crates/diesel_models/src/query/refund.rsindex b963f2f553..86c7701f43 100644--- a/crates/diesel_models/src/query/refund.rs+++ b/crates/diesel_models/src/query/refund.rs@@ -20,6 +20,10 @@ impl RefundNew {#[cfg(feature = "v1")]impl Refund {pub async fn update(self, conn: &PgPooledConn, refund: RefundUpdate) -> StorageResult<Self> {+ let processor_merchant_id = self+ .processor_merchant_id+ .clone()+ .unwrap_or_else(|| self.merchant_id.clone());match generics::generic_update_with_unique_predicate_get_result::<<Self as HasTable>::Table,_,@@ -29,7 +33,39 @@ impl Refund {conn,dsl::refund_id.eq(self.refund_id.to_owned())- .and(dsl::merchant_id.eq(self.merchant_id.to_owned())),+ .and(dsl::processor_merchant_id.eq(processor_merchant_id)),+ RefundUpdateInternal::from(refund),+ )+ .await+ {+ Err(error) => match error.current_context() {+ errors::DatabaseError::NoFieldsToUpdate => Ok(self),+ _ => Err(error),+ },+ result => result,+ }+ }++ // Fallback function for stagger release - updates by merchant_id when processor_merchant_id is NULL+ pub async fn update_by_merchant_id(+ self,+ conn: &PgPooledConn,+ refund: RefundUpdate,+ ) -> StorageResult<Self> {+ let processor_merchant_id = self+ .processor_merchant_id+ .clone()+ .unwrap_or_else(|| self.merchant_id.clone());+ match generics::generic_update_with_unique_predicate_get_result::<+ <Self as HasTable>::Table,+ _,+ _,+ _,+ >(+ conn,+ dsl::refund_id+ .eq(self.refund_id.to_owned())+ .and(dsl::merchant_id.eq(processor_merchant_id)),RefundUpdateInternal::from(refund),).await@@ -43,53 +79,100 @@ impl Refund {}// This is required to be changed for KV.+ pub async fn find_by_processor_merchant_id_refund_id(+ conn: &PgPooledConn,+ processor_merchant_id: &common_utils::id_type::MerchantId,+ refund_id: &str,+ ) -> StorageResult<Self> {+ generics::generic_find_one::<<Self as HasTable>::Table, _, _>(+ conn,+ dsl::processor_merchant_id+ .eq(processor_merchant_id.to_owned())+ .and(dsl::refund_id.eq(refund_id.to_owned())),+ )+ .await+ }++ // Fallback function for stagger release - queries by merchant_id when processor_merchant_id is NULLpub async fn find_by_merchant_id_refund_id(conn: &PgPooledConn,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_id: &str,) -> StorageResult<Self> {generics::generic_find_one::<<Self as HasTable>::Table, _, _>(conn,dsl::merchant_id- .eq(merchant_id.to_owned())+ .eq(processor_merchant_id.to_owned()).and(dsl::refund_id.eq(refund_id.to_owned())),).await}+ pub async fn find_by_processor_merchant_id_connector_refund_id_connector(+ conn: &PgPooledConn,+ processor_merchant_id: &common_utils::id_type::MerchantId,+ connector_refund_id: &str,+ connector: &str,+ ) -> StorageResult<Self> {+ generics::generic_find_one::<<Self as HasTable>::Table, _, _>(+ conn,+ dsl::processor_merchant_id+ .eq(processor_merchant_id.to_owned())+ .and(dsl::connector_refund_id.eq(connector_refund_id.to_owned()))+ .and(dsl::connector.eq(connector.to_owned())),+ )+ .await+ }++ // Fallback function for stagger release - queries by merchant_id when processor_merchant_id is NULLpub async fn find_by_merchant_id_connector_refund_id_connector(conn: &PgPooledConn,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,connector_refund_id: &str,connector: &str,) -> StorageResult<Self> {generics::generic_find_one::<<Self as HasTable>::Table, _, _>(conn,dsl::merchant_id- .eq(merchant_id.to_owned())+ .eq(processor_merchant_id.to_owned()).and(dsl::connector_refund_id.eq(connector_refund_id.to_owned())).and(dsl::connector.eq(connector.to_owned())),).await}+ pub async fn find_by_internal_reference_id_processor_merchant_id(+ conn: &PgPooledConn,+ internal_reference_id: &str,+ processor_merchant_id: &common_utils::id_type::MerchantId,+ ) -> StorageResult<Self> {+ generics::generic_find_one::<<Self as HasTable>::Table, _, _>(+ conn,+ dsl::processor_merchant_id+ .eq(processor_merchant_id.to_owned())+ .and(dsl::internal_reference_id.eq(internal_reference_id.to_owned())),+ )+ .await+ }++ // Fallback function for stagger release - queries by merchant_id when processor_merchant_id is NULLpub async fn find_by_internal_reference_id_merchant_id(conn: &PgPooledConn,internal_reference_id: &str,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,) -> StorageResult<Self> {generics::generic_find_one::<<Self as HasTable>::Table, _, _>(conn,dsl::merchant_id- .eq(merchant_id.to_owned())+ .eq(processor_merchant_id.to_owned()).and(dsl::internal_reference_id.eq(internal_reference_id.to_owned())),).await}- pub async fn find_by_merchant_id_connector_transaction_id(+ pub async fn find_by_processor_merchant_id_connector_transaction_id(conn: &PgPooledConn,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,connector_transaction_id: &str,) -> StorageResult<Vec<Self>> {generics::generic_filter::<@@ -99,8 +182,11 @@ impl Refund {_,>(conn,- dsl::merchant_id- .eq(merchant_id.to_owned())+ dsl::processor_merchant_id+ .eq(processor_merchant_id.to_owned())+ .or(dsl::processor_merchant_id+ .is_null()+ .and(dsl::merchant_id.eq(processor_merchant_id.to_owned()))).and(dsl::connector_transaction_id.eq(connector_transaction_id.to_owned())),None,None,@@ -109,10 +195,10 @@ impl Refund {.await}- pub async fn find_by_payment_id_merchant_id(+ pub async fn find_by_payment_id_processor_merchant_id(conn: &PgPooledConn,payment_id: &common_utils::id_type::PaymentId,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,) -> StorageResult<Vec<Self>> {generics::generic_filter::<<Self as HasTable>::Table,@@ -121,8 +207,11 @@ impl Refund {_,>(conn,- dsl::merchant_id- .eq(merchant_id.to_owned())+ dsl::processor_merchant_id+ .eq(processor_merchant_id.to_owned())+ .or(dsl::processor_merchant_id+ .is_null()+ .and(dsl::merchant_id.eq(processor_merchant_id.to_owned()))).and(dsl::payment_id.eq(payment_id.to_owned())),None,None,diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rsindex e340071d1c..a6a4461a33 100644--- a/crates/router/src/core/payments.rs+++ b/crates/router/src/core/payments.rs@@ -14416,7 +14416,7 @@ impl PaymentIntentStateMetadataExt {// Update payment_intent for the refund's payment_id// Calculate total_refunded_amount based on all succeeded refunds for that payment_idlet all_refunds_for_payment = db- .find_refund_by_payment_id_merchant_id(+ .find_refund_by_payment_id_processor_merchant_id(&payment_intent.payment_id,merchant_account.get_id(),merchant_account.storage_scheme,diff --git a/crates/router/src/core/payments/operations/payment_status.rs b/crates/router/src/core/payments/operations/payment_status.rsindex 3112c9e849..98aba9e7dd 100644--- a/crates/router/src/core/payments/operations/payment_status.rs+++ b/crates/router/src/core/payments/operations/payment_status.rs@@ -367,7 +367,7 @@ async fn get_tracker_for_sync<};let refunds = db- .find_refund_by_payment_id_merchant_id(+ .find_refund_by_payment_id_processor_merchant_id(&payment_id,platform.get_processor().get_account().get_id(),storage_scheme,diff --git a/crates/router/src/core/refunds.rs b/crates/router/src/core/refunds.rsindex 74e824a19d..c9ed9b6ba3 100644--- a/crates/router/src/core/refunds.rs+++ b/crates/router/src/core/refunds.rs@@ -1265,7 +1265,7 @@ pub async fn refund_update_core() -> RouterResponse<refunds::RefundResponse> {let db = state.store.as_ref();let refund = db- .find_refund_by_merchant_id_refund_id(+ .find_refund_by_processor_merchant_id_refund_id(platform.get_processor().get_account().get_id(),&req.refund_id,platform.get_processor().get_account().storage_scheme,@@ -1346,7 +1346,7 @@ pub async fn validate_and_create_refund(})?;let all_refunds = db- .find_refund_by_merchant_id_connector_transaction_id(+ .find_refund_by_processor_merchant_id_connector_transaction_id(platform.get_processor().get_account().get_id(),&connector_transaction_id,platform.get_processor().get_account().storage_scheme,@@ -1606,7 +1606,7 @@ pub async fn refund_retrieve_core_with_internal_reference_id(let merchant_id = platform.get_processor().get_account().get_id();let refund = db- .find_refund_by_internal_reference_id_merchant_id(+ .find_refund_by_internal_reference_id_processor_merchant_id(&refund_internal_request_id,merchant_id,platform.get_processor().get_account().storage_scheme,@@ -1647,7 +1647,7 @@ pub async fn refund_retrieve_core_with_refund_id(let merchant_id = platform.get_processor().get_account().get_id();let refund = db- .find_refund_by_merchant_id_refund_id(+ .find_refund_by_processor_merchant_id_refund_id(merchant_id,refund_id.as_str(),platform.get_processor().get_account().storage_scheme,@@ -1688,7 +1688,7 @@ pub async fn refund_manual_update(.attach_printable("Error while fetching the merchant_account by merchant_id")?;let refund = state.store- .find_refund_by_merchant_id_refund_id(+ .find_refund_by_processor_merchant_id_refund_id(merchant_account.get_id(),&req.refund_id,merchant_account.storage_scheme,@@ -2123,7 +2123,7 @@ pub async fn trigger_refund_execute_workflow(let processor_storage_scheme = platform.get_processor().get_account().storage_scheme;let refund = db- .find_refund_by_internal_reference_id_merchant_id(+ .find_refund_by_internal_reference_id_processor_merchant_id(&refund_core.refund_internal_reference_id,&refund_core.merchant_id,processor_storage_scheme,diff --git a/crates/router/src/core/refunds_v2.rs b/crates/router/src/core/refunds_v2.rsindex df1a03e08f..af0a8b9c7d 100644--- a/crates/router/src/core/refunds_v2.rs+++ b/crates/router/src/core/refunds_v2.rs@@ -1178,7 +1178,7 @@ pub async fn validate_and_create_refund(})?;let all_refunds = db- .find_refund_by_merchant_id_connector_transaction_id(+ .find_refund_by_processor_merchant_id_connector_transaction_id(platform.get_processor().get_account().get_id(),&connector_payment_id,platform.get_processor().get_account().storage_scheme,diff --git a/crates/router/src/core/webhooks/incoming.rs b/crates/router/src/core/webhooks/incoming.rsindex eabde53924..47e01b0a44 100644--- a/crates/router/src/core/webhooks/incoming.rs+++ b/crates/router/src/core/webhooks/incoming.rs@@ -2104,7 +2104,7 @@ async fn refunds_incoming_webhook_flow(let refund = match webhook_details.object_reference_id {webhooks::ObjectReferenceId::RefundId(refund_id_type) => match refund_id_type {webhooks::RefundIdType::RefundId(id) => db- .find_refund_by_merchant_id_refund_id(+ .find_refund_by_processor_merchant_id_refund_id(platform.get_processor().get_account().get_id(),&id,platform.get_processor().get_account().storage_scheme,@@ -2113,7 +2113,7 @@ async fn refunds_incoming_webhook_flow(.change_context(errors::ApiErrorResponse::WebhookResourceNotFound).attach_printable("Failed to fetch the refund")?,webhooks::RefundIdType::ConnectorRefundId(id) => db- .find_refund_by_merchant_id_connector_refund_id_connector(+ .find_refund_by_processor_merchant_id_connector_refund_id_connector(platform.get_processor().get_account().get_id(),&id,connector_name,diff --git a/crates/router/src/db/kafka_store.rs b/crates/router/src/db/kafka_store.rsindex c5bafb0507..702a82b401 100644--- a/crates/router/src/db/kafka_store.rs+++ b/crates/router/src/db/kafka_store.rs@@ -2753,56 +2753,64 @@ impl CaptureInterface for KafkaStore {#[async_trait::async_trait]impl RefundInterface for KafkaStore {#[cfg(feature = "v1")]- async fn find_refund_by_internal_reference_id_merchant_id(+ async fn find_refund_by_internal_reference_id_processor_merchant_id(&self,internal_reference_id: &str,- merchant_id: &id_type::MerchantId,+ processor_merchant_id: &id_type::MerchantId,storage_scheme: MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {self.diesel_store- .find_refund_by_internal_reference_id_merchant_id(+ .find_refund_by_internal_reference_id_processor_merchant_id(internal_reference_id,- merchant_id,+ processor_merchant_id,storage_scheme,).await}#[cfg(feature = "v1")]- async fn find_refund_by_payment_id_merchant_id(+ async fn find_refund_by_payment_id_processor_merchant_id(&self,payment_id: &id_type::PaymentId,- merchant_id: &id_type::MerchantId,+ processor_merchant_id: &id_type::MerchantId,storage_scheme: MerchantStorageScheme,) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError> {self.diesel_store- .find_refund_by_payment_id_merchant_id(payment_id, merchant_id, storage_scheme)+ .find_refund_by_payment_id_processor_merchant_id(+ payment_id,+ processor_merchant_id,+ storage_scheme,+ ).await}#[cfg(feature = "v1")]- async fn find_refund_by_merchant_id_refund_id(+ async fn find_refund_by_processor_merchant_id_refund_id(&self,- merchant_id: &id_type::MerchantId,+ processor_merchant_id: &id_type::MerchantId,refund_id: &str,storage_scheme: MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {self.diesel_store- .find_refund_by_merchant_id_refund_id(merchant_id, refund_id, storage_scheme)+ .find_refund_by_processor_merchant_id_refund_id(+ processor_merchant_id,+ refund_id,+ storage_scheme,+ ).await}#[cfg(feature = "v1")]- async fn find_refund_by_merchant_id_connector_refund_id_connector(+ async fn find_refund_by_processor_merchant_id_connector_refund_id_connector(&self,- merchant_id: &id_type::MerchantId,+ processor_merchant_id: &id_type::MerchantId,connector_refund_id: &str,connector: &str,storage_scheme: MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {self.diesel_store- .find_refund_by_merchant_id_connector_refund_id_connector(- merchant_id,+ .find_refund_by_processor_merchant_id_connector_refund_id_connector(+ processor_merchant_id,connector_refund_id,connector,storage_scheme,@@ -2831,15 +2839,15 @@ impl RefundInterface for KafkaStore {Ok(refund)}- async fn find_refund_by_merchant_id_connector_transaction_id(+ async fn find_refund_by_processor_merchant_id_connector_transaction_id(&self,- merchant_id: &id_type::MerchantId,+ processor_merchant_id: &id_type::MerchantId,connector_transaction_id: &str,storage_scheme: MerchantStorageScheme,) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError> {self.diesel_store- .find_refund_by_merchant_id_connector_transaction_id(- merchant_id,+ .find_refund_by_processor_merchant_id_connector_transaction_id(+ processor_merchant_id,connector_transaction_id,storage_scheme,)@@ -2877,7 +2885,7 @@ impl RefundInterface for KafkaStore {#[cfg(all(feature = "v1", feature = "olap"))]async fn filter_refund_by_constraints(&self,- merchant_id: &id_type::MerchantId,+ processor_merchant_id: &id_type::MerchantId,refund_details: &refunds::RefundListConstraints,storage_scheme: MerchantStorageScheme,limit: i64,@@ -2885,7 +2893,7 @@ impl RefundInterface for KafkaStore {) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError> {self.diesel_store.filter_refund_by_constraints(- merchant_id,+ processor_merchant_id,refund_details,storage_scheme,limit,@@ -2917,37 +2925,46 @@ impl RefundInterface for KafkaStore {#[cfg(all(feature = "v1", feature = "olap"))]async fn filter_refund_by_meta_constraints(&self,- merchant_id: &id_type::MerchantId,+ processor_merchant_id: &id_type::MerchantId,refund_details: &common_utils::types::TimeRange,storage_scheme: MerchantStorageScheme,) -> CustomResult<api_models::refunds::RefundListMetaData, errors::StorageError> {self.diesel_store- .filter_refund_by_meta_constraints(merchant_id, refund_details, storage_scheme)+ .filter_refund_by_meta_constraints(+ processor_merchant_id,+ refund_details,+ storage_scheme,+ ).await}#[cfg(all(feature = "v1", feature = "olap"))]async fn get_refund_status_with_count(&self,- merchant_id: &id_type::MerchantId,+ processor_merchant_id: &id_type::MerchantId,profile_id_list: Option<Vec<id_type::ProfileId>>,constraints: &common_utils::types::TimeRange,storage_scheme: MerchantStorageScheme,) -> CustomResult<Vec<(common_enums::RefundStatus, i64)>, errors::StorageError> {self.diesel_store- .get_refund_status_with_count(merchant_id, profile_id_list, constraints, storage_scheme)+ .get_refund_status_with_count(+ processor_merchant_id,+ profile_id_list,+ constraints,+ storage_scheme,+ ).await}#[cfg(all(feature = "v1", feature = "olap"))]async fn get_total_count_of_refunds(&self,- merchant_id: &id_type::MerchantId,+ processor_merchant_id: &id_type::MerchantId,refund_details: &refunds::RefundListConstraints,storage_scheme: MerchantStorageScheme,) -> CustomResult<i64, errors::StorageError> {self.diesel_store- .get_total_count_of_refunds(merchant_id, refund_details, storage_scheme)+ .get_total_count_of_refunds(processor_merchant_id, refund_details, storage_scheme).await}diff --git a/crates/router/src/db/refund.rs b/crates/router/src/db/refund.rsindex 5d1db3ac72..e17cafe75d 100644--- a/crates/router/src/db/refund.rs+++ b/crates/router/src/db/refund.rs@@ -18,33 +18,33 @@ const MAX_LIMIT: usize = 100;#[async_trait::async_trait]pub trait RefundInterface {#[cfg(feature = "v1")]- async fn find_refund_by_internal_reference_id_merchant_id(+ async fn find_refund_by_internal_reference_id_processor_merchant_id(&self,internal_reference_id: &str,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError>;#[cfg(feature = "v1")]- async fn find_refund_by_payment_id_merchant_id(+ async fn find_refund_by_payment_id_processor_merchant_id(&self,payment_id: &common_utils::id_type::PaymentId,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError>;#[cfg(feature = "v1")]- async fn find_refund_by_merchant_id_refund_id(+ async fn find_refund_by_processor_merchant_id_refund_id(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_id: &str,storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError>;#[cfg(feature = "v1")]- async fn find_refund_by_merchant_id_connector_refund_id_connector(+ async fn find_refund_by_processor_merchant_id_connector_refund_id_connector(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,connector_refund_id: &str,connector: &str,storage_scheme: enums::MerchantStorageScheme,@@ -57,9 +57,9 @@ pub trait RefundInterface {storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError>;- async fn find_refund_by_merchant_id_connector_transaction_id(+ async fn find_refund_by_processor_merchant_id_connector_transaction_id(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,connector_transaction_id: &str,storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError>;@@ -80,7 +80,7 @@ pub trait RefundInterface {#[cfg(all(feature = "v1", feature = "olap"))]async fn filter_refund_by_constraints(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &refunds::RefundListConstraints,storage_scheme: enums::MerchantStorageScheme,limit: i64,@@ -100,7 +100,7 @@ pub trait RefundInterface {#[cfg(all(feature = "v1", feature = "olap"))]async fn filter_refund_by_meta_constraints(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &common_utils::types::TimeRange,storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<api_models::refunds::RefundListMetaData, errors::StorageError>;@@ -108,7 +108,7 @@ pub trait RefundInterface {#[cfg(all(feature = "v1", feature = "olap"))]async fn get_refund_status_with_count(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,profile_id_list: Option<Vec<common_utils::id_type::ProfileId>>,constraints: &common_utils::types::TimeRange,storage_scheme: enums::MerchantStorageScheme,@@ -117,7 +117,7 @@ pub trait RefundInterface {#[cfg(all(feature = "v1", feature = "olap"))]async fn get_total_count_of_refunds(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &refunds::RefundListConstraints,storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<i64, errors::StorageError>;@@ -149,20 +149,42 @@ mod storage {impl RefundInterface for Store {#[cfg(feature = "v1")]#[instrument(skip_all)]- async fn find_refund_by_internal_reference_id_merchant_id(+ async fn find_refund_by_internal_reference_id_processor_merchant_id(&self,internal_reference_id: &str,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;- diesel_refund::Refund::find_by_internal_reference_id_merchant_id(- &conn,- internal_reference_id,- merchant_id,- )- .await- .map_err(|error| report!(errors::StorageError::from(error)))+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)+ let result =+ diesel_refund::Refund::find_by_internal_reference_id_processor_merchant_id(+ &conn,+ internal_reference_id,+ processor_merchant_id,+ )+ .await;++ match result {+ Ok(refund) => Ok(refund),+ Err(error) => {+ if matches!(+ error.current_context(),+ diesel_models::errors::DatabaseError::NotFound+ ) {+ diesel_refund::Refund::find_by_internal_reference_id_merchant_id(+ &conn,+ internal_reference_id,+ processor_merchant_id,+ )+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ } else {+ Err(report!(errors::StorageError::from(error)))+ }+ }+ }}#[instrument(skip_all)]@@ -178,16 +200,16 @@ mod storage {}#[instrument(skip_all)]- async fn find_refund_by_merchant_id_connector_transaction_id(+ async fn find_refund_by_processor_merchant_id_connector_transaction_id(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,connector_transaction_id: &str,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;- diesel_refund::Refund::find_by_merchant_id_connector_transaction_id(+ diesel_refund::Refund::find_by_processor_merchant_id_connector_transaction_id(&conn,- merchant_id,+ processor_merchant_id,connector_transaction_id,).await@@ -215,9 +237,25 @@ mod storage {_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {let conn = connection::pg_connection_write(self).await?;- this.update(&conn, refund)- .await- .map_err(|error| report!(errors::StorageError::from(error)))+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)+ let result = this.clone().update(&conn, refund.clone()).await;++ match result {+ Ok(refund) => Ok(refund),+ Err(error) => {+ if matches!(+ error.current_context(),+ diesel_models::errors::DatabaseError::NotFound+ ) {+ this.update_by_merchant_id(&conn, refund)+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ } else {+ Err(report!(errors::StorageError::from(error)))+ }+ }+ }}#[cfg(feature = "v2")]@@ -236,57 +274,109 @@ mod storage {#[cfg(feature = "v1")]#[instrument(skip_all)]- async fn find_refund_by_merchant_id_refund_id(+ async fn find_refund_by_processor_merchant_id_refund_id(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_id: &str,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;- diesel_refund::Refund::find_by_merchant_id_refund_id(&conn, merchant_id, refund_id)- .await- .map_err(|error| report!(errors::StorageError::from(error)))+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)+ let result = diesel_refund::Refund::find_by_processor_merchant_id_refund_id(+ &conn,+ processor_merchant_id,+ refund_id,+ )+ .await;++ match result {+ Ok(refund) => Ok(refund),+ Err(error) => {+ if matches!(+ error.current_context(),+ diesel_models::errors::DatabaseError::NotFound+ ) {+ diesel_refund::Refund::find_by_merchant_id_refund_id(+ &conn,+ processor_merchant_id,+ refund_id,+ )+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ } else {+ Err(report!(errors::StorageError::from(error)))+ }+ }+ }}#[cfg(feature = "v1")]#[instrument(skip_all)]- async fn find_refund_by_merchant_id_connector_refund_id_connector(+ async fn find_refund_by_processor_merchant_id_connector_refund_id_connector(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,connector_refund_id: &str,connector: &str,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;- diesel_refund::Refund::find_by_merchant_id_connector_refund_id_connector(- &conn,- merchant_id,- connector_refund_id,- connector,- )- .await- .map_err(|error| report!(errors::StorageError::from(error)))+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)+ let result =+ diesel_refund::Refund::find_by_processor_merchant_id_connector_refund_id_connector(+ &conn,+ processor_merchant_id,+ connector_refund_id,+ connector,+ )+ .await;++ match result {+ Ok(refund) => Ok(refund),+ Err(error) => {+ if matches!(+ error.current_context(),+ diesel_models::errors::DatabaseError::NotFound+ ) {+ diesel_refund::Refund::find_by_merchant_id_connector_refund_id_connector(+ &conn,+ processor_merchant_id,+ connector_refund_id,+ connector,+ )+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ } else {+ Err(report!(errors::StorageError::from(error)))+ }+ }+ }}#[cfg(feature = "v1")]#[instrument(skip_all)]- async fn find_refund_by_payment_id_merchant_id(+ async fn find_refund_by_payment_id_processor_merchant_id(&self,payment_id: &common_utils::id_type::PaymentId,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;- diesel_refund::Refund::find_by_payment_id_merchant_id(&conn, payment_id, merchant_id)- .await- .map_err(|error| report!(errors::StorageError::from(error)))+ diesel_refund::Refund::find_by_payment_id_processor_merchant_id(+ &conn,+ payment_id,+ processor_merchant_id,+ )+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))}#[cfg(all(feature = "v1", feature = "olap"))]#[instrument(skip_all)]async fn filter_refund_by_constraints(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &refunds::RefundListConstraints,_storage_scheme: enums::MerchantStorageScheme,limit: i64,@@ -295,7 +385,7 @@ mod storage {let conn = connection::pg_connection_read(self).await?;<diesel_models::refund::Refund as storage_types::RefundDbExt>::filter_by_constraints(&conn,- merchant_id,+ processor_merchant_id,refund_details,limit,offset,@@ -330,14 +420,14 @@ mod storage {#[instrument(skip_all)]async fn filter_refund_by_meta_constraints(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &api_models::payments::TimeRange,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<api_models::refunds::RefundListMetaData, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;<diesel_models::refund::Refund as storage_types::RefundDbExt>::filter_by_meta_constraints(&conn,- merchant_id,+ processor_merchant_id,refund_details,).await@@ -348,13 +438,13 @@ mod storage {#[instrument(skip_all)]async fn get_refund_status_with_count(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,profile_id_list: Option<Vec<common_utils::id_type::ProfileId>>,time_range: &api_models::payments::TimeRange,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<Vec<(common_enums::RefundStatus, i64)>, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;- <diesel_models::refund::Refund as storage_types::RefundDbExt>::get_refund_status_with_count(&conn, merchant_id,profile_id_list, time_range)+ <diesel_models::refund::Refund as storage_types::RefundDbExt>::get_refund_status_with_count(&conn, processor_merchant_id,profile_id_list, time_range).await.map_err(|error|report!(errors::StorageError::from(error)))}@@ -363,14 +453,14 @@ mod storage {#[instrument(skip_all)]async fn get_total_count_of_refunds(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &refunds::RefundListConstraints,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<i64, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;<diesel_models::refund::Refund as storage_types::RefundDbExt>::get_refunds_count(&conn,- merchant_id,+ processor_merchant_id,refund_details,).await@@ -424,21 +514,43 @@ mod storage {impl RefundInterface for Store {#[cfg(feature = "v1")]#[instrument(skip_all)]- async fn find_refund_by_internal_reference_id_merchant_id(+ async fn find_refund_by_internal_reference_id_processor_merchant_id(&self,internal_reference_id: &str,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)let database_call = || async {let conn = connection::pg_connection_read(self).await?;- diesel_refund::Refund::find_by_internal_reference_id_merchant_id(- &conn,- internal_reference_id,- merchant_id,- )- .await- .map_err(|error| report!(errors::StorageError::from(error)))+ let result =+ diesel_refund::Refund::find_by_internal_reference_id_processor_merchant_id(+ &conn,+ internal_reference_id,+ processor_merchant_id,+ )+ .await;++ match result {+ Ok(refund) => Ok(refund),+ Err(error) => {+ if matches!(+ error.current_context(),+ diesel_models::errors::DatabaseError::NotFound+ ) {+ diesel_refund::Refund::find_by_internal_reference_id_merchant_id(+ &conn,+ internal_reference_id,+ processor_merchant_id,+ )+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ } else {+ Err(report!(errors::StorageError::from(error)))+ }+ }+ }};let storage_scheme = Box::pin(decide_storage_scheme::<_, diesel_refund::Refund>(self,@@ -451,7 +563,7 @@ mod storage {enums::MerchantStorageScheme::RedisKv => {let lookup_id = format!("ref_inter_ref_{}_{internal_reference_id}",- merchant_id.get_string_repr()+ processor_merchant_id.get_string_repr());let lookup = fallback_reverse_lookup_not_found!(self.get_lookup_by_lookup_id(&lookup_id, storage_scheme)@@ -650,17 +762,17 @@ mod storage {#[cfg(feature = "v1")]#[instrument(skip_all)]- async fn find_refund_by_merchant_id_connector_transaction_id(+ async fn find_refund_by_processor_merchant_id_connector_transaction_id(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,connector_transaction_id: &str,storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError> {let database_call = || async {let conn = connection::pg_connection_read(self).await?;- diesel_refund::Refund::find_by_merchant_id_connector_transaction_id(+ diesel_refund::Refund::find_by_processor_merchant_id_connector_transaction_id(&conn,- merchant_id,+ processor_merchant_id,connector_transaction_id,).await@@ -677,7 +789,7 @@ mod storage {enums::MerchantStorageScheme::RedisKv => {let lookup_id = format!("pa_conn_trans_{}_{connector_transaction_id}",- merchant_id.get_string_repr()+ processor_merchant_id.get_string_repr());let lookup = fallback_reverse_lookup_not_found!(self.get_lookup_by_lookup_id(&lookup_id, storage_scheme)@@ -710,16 +822,16 @@ mod storage {#[cfg(feature = "v2")]#[instrument(skip_all)]- async fn find_refund_by_merchant_id_connector_transaction_id(+ async fn find_refund_by_processor_merchant_id_connector_transaction_id(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,connector_transaction_id: &str,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;diesel_refund::Refund::find_by_merchant_id_connector_transaction_id(&conn,- merchant_id,+ processor_merchant_id,connector_transaction_id,).await@@ -750,9 +862,25 @@ mod storage {match storage_scheme {enums::MerchantStorageScheme::PostgresOnly => {let conn = connection::pg_connection_write(self).await?;- this.update(&conn, refund)- .await- .map_err(|error| report!(errors::StorageError::from(error)))+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)+ let result = this.clone().update(&conn, refund.clone()).await;++ match result {+ Ok(refund) => Ok(refund),+ Err(error) => {+ if matches!(+ error.current_context(),+ diesel_models::errors::DatabaseError::NotFound+ ) {+ this.update_by_merchant_id(&conn, refund)+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ } else {+ Err(report!(errors::StorageError::from(error)))+ }+ }+ }}enums::MerchantStorageScheme::RedisKv => {let key_str = key.to_string();@@ -807,17 +935,42 @@ mod storage {#[cfg(feature = "v1")]#[instrument(skip_all)]- async fn find_refund_by_merchant_id_refund_id(+ async fn find_refund_by_processor_merchant_id_refund_id(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_id: &str,storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)let database_call = || async {let conn = connection::pg_connection_read(self).await?;- diesel_refund::Refund::find_by_merchant_id_refund_id(&conn, merchant_id, refund_id)- .await- .map_err(|error| report!(errors::StorageError::from(error)))+ let result = diesel_refund::Refund::find_by_processor_merchant_id_refund_id(+ &conn,+ processor_merchant_id,+ refund_id,+ )+ .await;++ match result {+ Ok(refund) => Ok(refund),+ Err(error) => {+ if matches!(+ error.current_context(),+ diesel_models::errors::DatabaseError::NotFound+ ) {+ diesel_refund::Refund::find_by_merchant_id_refund_id(+ &conn,+ processor_merchant_id,+ refund_id,+ )+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ } else {+ Err(report!(errors::StorageError::from(error)))+ }+ }+ }};let storage_scheme = Box::pin(decide_storage_scheme::<_, diesel_refund::Refund>(self,@@ -828,8 +981,10 @@ mod storage {match storage_scheme {enums::MerchantStorageScheme::PostgresOnly => database_call().await,enums::MerchantStorageScheme::RedisKv => {- let lookup_id =- format!("ref_ref_id_{}_{refund_id}", merchant_id.get_string_repr());+ let lookup_id = format!(+ "ref_ref_id_{}_{refund_id}",+ processor_merchant_id.get_string_repr()+ );let lookup = fallback_reverse_lookup_not_found!(self.get_lookup_by_lookup_id(&lookup_id, storage_scheme).await,@@ -858,23 +1013,46 @@ mod storage {#[cfg(feature = "v1")]#[instrument(skip_all)]- async fn find_refund_by_merchant_id_connector_refund_id_connector(+ async fn find_refund_by_processor_merchant_id_connector_refund_id_connector(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,connector_refund_id: &str,connector: &str,storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)let database_call = || async {let conn = connection::pg_connection_read(self).await?;- diesel_refund::Refund::find_by_merchant_id_connector_refund_id_connector(- &conn,- merchant_id,- connector_refund_id,- connector,- )- .await- .map_err(|error| report!(errors::StorageError::from(error)))+ let result =+ diesel_refund::Refund::find_by_processor_merchant_id_connector_refund_id_connector(+ &conn,+ processor_merchant_id,+ connector_refund_id,+ connector,+ )+ .await;++ match result {+ Ok(refund) => Ok(refund),+ Err(error) => {+ if matches!(+ error.current_context(),+ diesel_models::errors::DatabaseError::NotFound+ ) {+ diesel_refund::Refund::find_by_merchant_id_connector_refund_id_connector(+ &conn,+ processor_merchant_id,+ connector_refund_id,+ connector,+ )+ .await+ .map_err(|error| report!(errors::StorageError::from(error)))+ } else {+ Err(report!(errors::StorageError::from(error)))+ }+ }+ }};let storage_scheme = Box::pin(decide_storage_scheme::<_, diesel_refund::Refund>(self,@@ -887,7 +1065,7 @@ mod storage {enums::MerchantStorageScheme::RedisKv => {let lookup_id = format!("ref_connector_{}_{connector_refund_id}_{connector}",- merchant_id.get_string_repr()+ processor_merchant_id.get_string_repr());let lookup = fallback_reverse_lookup_not_found!(self.get_lookup_by_lookup_id(&lookup_id, storage_scheme)@@ -917,18 +1095,18 @@ mod storage {#[cfg(feature = "v1")]#[instrument(skip_all)]- async fn find_refund_by_payment_id_merchant_id(+ async fn find_refund_by_payment_id_processor_merchant_id(&self,payment_id: &common_utils::id_type::PaymentId,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError> {let database_call = || async {let conn = connection::pg_connection_read(self).await?;- diesel_refund::Refund::find_by_payment_id_merchant_id(+ diesel_refund::Refund::find_by_payment_id_processor_merchant_id(&conn,payment_id,- merchant_id,+ processor_merchant_id,).await.map_err(|error| report!(errors::StorageError::from(error)))@@ -943,7 +1121,7 @@ mod storage {enums::MerchantStorageScheme::PostgresOnly => database_call().await,enums::MerchantStorageScheme::RedisKv => {let key = PartitionKey::MerchantIdPaymentId {- merchant_id,+ merchant_id: processor_merchant_id,payment_id,};Box::pin(db_utils::try_redis_get_else_try_database_get(@@ -980,7 +1158,7 @@ mod storage {#[instrument(skip_all)]async fn filter_refund_by_constraints(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &refunds::RefundListConstraints,_storage_scheme: enums::MerchantStorageScheme,limit: i64,@@ -989,7 +1167,7 @@ mod storage {let conn = connection::pg_connection_read(self).await?;<diesel_models::refund::Refund as storage_types::RefundDbExt>::filter_by_constraints(&conn,- merchant_id,+ processor_merchant_id,refund_details,limit,offset,@@ -1024,12 +1202,12 @@ mod storage {#[instrument(skip_all)]async fn filter_refund_by_meta_constraints(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &common_utils::types::TimeRange,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<api_models::refunds::RefundListMetaData, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;- <diesel_models::refund::Refund as storage_types::RefundDbExt>::filter_by_meta_constraints(&conn, merchant_id, refund_details)+ <diesel_models::refund::Refund as storage_types::RefundDbExt>::filter_by_meta_constraints(&conn, processor_merchant_id, refund_details).await.map_err(|error|report!(errors::StorageError::from(error)))}@@ -1038,13 +1216,13 @@ mod storage {#[instrument(skip_all)]async fn get_refund_status_with_count(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,profile_id_list: Option<Vec<common_utils::id_type::ProfileId>>,constraints: &common_utils::types::TimeRange,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<Vec<(common_enums::RefundStatus, i64)>, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;- <diesel_models::refund::Refund as storage_types::RefundDbExt>::get_refund_status_with_count(&conn, merchant_id,profile_id_list, constraints)+ <diesel_models::refund::Refund as storage_types::RefundDbExt>::get_refund_status_with_count(&conn, processor_merchant_id,profile_id_list, constraints).await.map_err(|error| report!(errors::StorageError::from(error)))}@@ -1053,14 +1231,14 @@ mod storage {#[instrument(skip_all)]async fn get_total_count_of_refunds(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &refunds::RefundListConstraints,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<i64, errors::StorageError> {let conn = connection::pg_connection_read(self).await?;<diesel_models::refund::Refund as storage_types::RefundDbExt>::get_refunds_count(&conn,- merchant_id,+ processor_merchant_id,refund_details,).await@@ -1090,19 +1268,27 @@ mod storage {#[async_trait::async_trait]impl RefundInterface for MockDb {#[cfg(feature = "v1")]- async fn find_refund_by_internal_reference_id_merchant_id(+ async fn find_refund_by_internal_reference_id_processor_merchant_id(&self,internal_reference_id: &str,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {let refunds = self.refunds.lock().await;+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)refunds.iter().find(|refund| {- refund.merchant_id == *merchant_id+ refund.processor_merchant_id.as_ref() == Some(processor_merchant_id)&& refund.internal_reference_id == internal_reference_id})+ .or_else(|| {+ refunds.iter().find(|refund| {+ refund.merchant_id == *processor_merchant_id+ && refund.internal_reference_id == internal_reference_id+ })+ }).cloned().ok_or_else(|| {errors::StorageError::DatabaseError(DatabaseError::NotFound.into()).into()@@ -1214,9 +1400,9 @@ impl RefundInterface for MockDb {Ok(refund)}- async fn find_refund_by_merchant_id_connector_transaction_id(+ async fn find_refund_by_processor_merchant_id_connector_transaction_id(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,connector_transaction_id: &str,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError> {@@ -1224,8 +1410,10 @@ impl RefundInterface for MockDb {Ok(refunds.iter()- .take_while(|refund| {- refund.merchant_id == *merchant_id+ .filter(|refund| {+ (refund.processor_merchant_id.as_ref() == Some(processor_merchant_id)+ || (refund.processor_merchant_id.is_none()+ && refund.merchant_id == *processor_merchant_id))&& refund.get_connector_transaction_id() == connector_transaction_id}).cloned()@@ -1239,16 +1427,33 @@ impl RefundInterface for MockDb {refund: diesel_refund::RefundUpdate,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {- self.refunds- .lock()- .await- .iter_mut()- .find(|refund| this.refund_id == refund.refund_id)- .map(|r| {- let refund_updated =- diesel_refund::RefundUpdateInternal::from(refund).create_refund(r.clone());- *r = refund_updated.clone();- refund_updated+ let mut refunds = self.refunds.lock().await;+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)+ let processor_merchant_id = this+ .processor_merchant_id+ .clone()+ .unwrap_or_else(|| this.merchant_id.clone());+ let index = refunds+ .iter()+ .position(|r| {+ r.refund_id == this.refund_id+ && r.processor_merchant_id.as_ref() == Some(&processor_merchant_id)+ })+ .or_else(|| {+ refunds.iter().position(|r| {+ r.refund_id == this.refund_id && r.merchant_id == processor_merchant_id+ })+ });++ index+ .and_then(|idx| {+ refunds.get_mut(idx).map(|r| {+ let refund_updated =+ diesel_refund::RefundUpdateInternal::from(refund).create_refund(r.clone());+ *r = refund_updated.clone();+ refund_updated+ })}).ok_or_else(|| {errors::StorageError::ValueNotFound("cannot find refund to update".to_string())@@ -1281,17 +1486,26 @@ impl RefundInterface for MockDb {}#[cfg(feature = "v1")]- async fn find_refund_by_merchant_id_refund_id(+ async fn find_refund_by_processor_merchant_id_refund_id(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_id: &str,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {let refunds = self.refunds.lock().await;-+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)refunds.iter()- .find(|refund| refund.merchant_id == *merchant_id && refund.refund_id == refund_id)+ .find(|refund| {+ refund.processor_merchant_id.as_ref() == Some(processor_merchant_id)+ && refund.refund_id == refund_id+ })+ .or_else(|| {+ refunds.iter().find(|refund| {+ refund.merchant_id == *processor_merchant_id && refund.refund_id == refund_id+ })+ }).cloned().ok_or_else(|| {errors::StorageError::DatabaseError(DatabaseError::NotFound.into()).into()@@ -1299,25 +1513,36 @@ impl RefundInterface for MockDb {}#[cfg(feature = "v1")]- async fn find_refund_by_merchant_id_connector_refund_id_connector(+ async fn find_refund_by_processor_merchant_id_connector_refund_id_connector(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,connector_refund_id: &str,connector: &str,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<diesel_refund::Refund, errors::StorageError> {let refunds = self.refunds.lock().await;-+ // Stagger release fallback: first try processor_merchant_id, if not found fallback to merchant_id+ // For old records processor_merchant_id is NULL, so we use merchant_id (which has the same value)refunds.iter().find(|refund| {- refund.merchant_id == *merchant_id+ refund.processor_merchant_id.as_ref() == Some(processor_merchant_id)&& refund.get_optional_connector_refund_id().map(|refund_id| refund_id.as_str())== Some(connector_refund_id)&& refund.connector == connector})+ .or_else(|| {+ refunds.iter().find(|refund| {+ refund.merchant_id == *processor_merchant_id+ && refund+ .get_optional_connector_refund_id()+ .map(|refund_id| refund_id.as_str())+ == Some(connector_refund_id)+ && refund.connector == connector+ })+ }).cloned().ok_or_else(|| {errors::StorageError::DatabaseError(DatabaseError::NotFound.into()).into()@@ -1325,17 +1550,22 @@ impl RefundInterface for MockDb {}#[cfg(feature = "v1")]- async fn find_refund_by_payment_id_merchant_id(+ async fn find_refund_by_payment_id_processor_merchant_id(&self,payment_id: &common_utils::id_type::PaymentId,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<Vec<diesel_refund::Refund>, errors::StorageError> {let refunds = self.refunds.lock().await;Ok(refunds.iter()- .filter(|refund| refund.merchant_id == *merchant_id && refund.payment_id == *payment_id)+ .filter(|refund| {+ (refund.processor_merchant_id.as_ref() == Some(processor_merchant_id)+ || (refund.processor_merchant_id.is_none()+ && refund.merchant_id == *processor_merchant_id))+ && refund.payment_id == *payment_id+ }).cloned().collect::<Vec<_>>())}@@ -1360,7 +1590,7 @@ impl RefundInterface for MockDb {#[cfg(all(feature = "v1", feature = "olap"))]async fn filter_refund_by_constraints(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &refunds::RefundListConstraints,_storage_scheme: enums::MerchantStorageScheme,limit: i64,@@ -1406,7 +1636,11 @@ impl RefundInterface for MockDb {let refunds = self.refunds.lock().await;let filtered_refunds = refunds.iter()- .filter(|refund| refund.merchant_id == *merchant_id)+ .filter(|refund| {+ refund.processor_merchant_id.as_ref() == Some(processor_merchant_id)+ || (refund.processor_merchant_id.is_none()+ && refund.merchant_id == *processor_merchant_id)+ }).filter(|refund| {refund_details.payment_id@@ -1576,7 +1810,7 @@ impl RefundInterface for MockDb {#[cfg(all(feature = "v1", feature = "olap"))]async fn filter_refund_by_meta_constraints(&self,- _merchant_id: &common_utils::id_type::MerchantId,+ _processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &common_utils::types::TimeRange,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<api_models::refunds::RefundListMetaData, errors::StorageError> {@@ -1623,7 +1857,7 @@ impl RefundInterface for MockDb {#[cfg(all(feature = "v1", feature = "olap"))]async fn get_refund_status_with_count(&self,- _merchant_id: &common_utils::id_type::MerchantId,+ _processor_merchant_id: &common_utils::id_type::MerchantId,profile_id_list: Option<Vec<common_utils::id_type::ProfileId>>,time_range: &common_utils::types::TimeRange,_storage_scheme: enums::MerchantStorageScheme,@@ -1669,7 +1903,7 @@ impl RefundInterface for MockDb {#[cfg(all(feature = "v1", feature = "olap"))]async fn get_total_count_of_refunds(&self,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_details: &refunds::RefundListConstraints,_storage_scheme: enums::MerchantStorageScheme,) -> CustomResult<i64, errors::StorageError> {@@ -1713,7 +1947,11 @@ impl RefundInterface for MockDb {let refunds = self.refunds.lock().await;let filtered_refunds = refunds.iter()- .filter(|refund| refund.merchant_id == *merchant_id)+ .filter(|refund| {+ refund.processor_merchant_id.as_ref() == Some(processor_merchant_id)+ || (refund.processor_merchant_id.is_none()+ && refund.merchant_id == *processor_merchant_id)+ }).filter(|refund| {refund_details.payment_iddiff --git a/crates/router/src/routes/refunds.rs b/crates/router/src/routes/refunds.rsindex 7643c243e1..091857f46b 100644--- a/crates/router/src/routes/refunds.rs+++ b/crates/router/src/routes/refunds.rs@@ -66,12 +66,12 @@ pub async fn refunds_create(},auth::auth_type(&auth::HeaderAuth(auth::ApiKeyAuth {- allow_connected_scope_operation: false,+ allow_connected_scope_operation: true,allow_platform_self_operation: false,}),&auth::JWTAuth {permission: Permission::ProfileRefundWrite,- allow_connected: false,+ allow_connected: true,allow_platform: false,},req.headers(),@@ -175,12 +175,12 @@ pub async fn refunds_retrieve(},auth::auth_type(&auth::HeaderAuth(auth::ApiKeyAuth {- allow_connected_scope_operation: false,+ allow_connected_scope_operation: true,allow_platform_self_operation: false,}),&auth::JWTAuth {permission: Permission::ProfileRefundRead,- allow_connected: false,+ allow_connected: true,allow_platform: false,},req.headers(),@@ -333,7 +333,7 @@ pub async fn refunds_retrieve_with_body()},&auth::HeaderAuth(auth::ApiKeyAuth {- allow_connected_scope_operation: false,+ allow_connected_scope_operation: true,allow_platform_self_operation: false,}),api_locking::LockAction::NotApplicable,@@ -365,7 +365,7 @@ pub async fn refunds_update(refund_update_core(state, auth.platform, req)},&auth::HeaderAuth(auth::ApiKeyAuth {- allow_connected_scope_operation: false,+ allow_connected_scope_operation: true,allow_platform_self_operation: false,}),api_locking::LockAction::NotApplicable,@@ -433,7 +433,7 @@ pub async fn refunds_list(},auth::auth_type(&auth::HeaderAuth(auth::ApiKeyAuth {- allow_connected_scope_operation: false,+ allow_connected_scope_operation: true,allow_platform_self_operation: false,}),&auth::JWTAuth {@@ -512,7 +512,7 @@ pub async fn refunds_list_profile(},auth::auth_type(&auth::HeaderAuth(auth::ApiKeyAuth {- allow_connected_scope_operation: false,+ allow_connected_scope_operation: true,allow_platform_self_operation: false,}),&auth::JWTAuth {@@ -548,7 +548,7 @@ pub async fn refunds_filter_list(},auth::auth_type(&auth::HeaderAuth(auth::ApiKeyAuth {- allow_connected_scope_operation: false,+ allow_connected_scope_operation: true,allow_platform_self_operation: false,}),&auth::JWTAuth {@@ -580,7 +580,7 @@ pub async fn get_refunds_filters(state: web::Data<AppState>, req: HttpRequest) -},auth::auth_type(&auth::HeaderAuth(auth::ApiKeyAuth {- allow_connected_scope_operation: false,+ allow_connected_scope_operation: true,allow_platform_self_operation: false,}),&auth::JWTAuth {@@ -619,7 +619,7 @@ pub async fn get_refunds_filters_profile(},auth::auth_type(&auth::HeaderAuth(auth::ApiKeyAuth {- allow_connected_scope_operation: false,+ allow_connected_scope_operation: true,allow_platform_self_operation: false,}),&auth::JWTAuth {@@ -653,7 +653,7 @@ pub async fn get_refunds_aggregates(},auth::auth_type(&auth::HeaderAuth(auth::ApiKeyAuth {- allow_connected_scope_operation: false,+ allow_connected_scope_operation: true,allow_platform_self_operation: false,}),&auth::JWTAuth {@@ -715,7 +715,7 @@ pub async fn get_refunds_aggregate_profile(},auth::auth_type(&auth::HeaderAuth(auth::ApiKeyAuth {- allow_connected_scope_operation: false,+ allow_connected_scope_operation: true,allow_platform_self_operation: false,}),&auth::JWTAuth {diff --git a/crates/router/src/types/storage/refund.rs b/crates/router/src/types/storage/refund.rsindex defc8809ad..9f82b5517c 100644--- a/crates/router/src/types/storage/refund.rs+++ b/crates/router/src/types/storage/refund.rs@@ -22,7 +22,7 @@ pub trait RefundDbExt: Sized {#[cfg(feature = "v1")]async fn filter_by_constraints(conn: &PgPooledConn,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_list_details: &refunds::RefundListConstraints,limit: i64,offset: i64,@@ -40,21 +40,21 @@ pub trait RefundDbExt: Sized {#[cfg(feature = "v1")]async fn filter_by_meta_constraints(conn: &PgPooledConn,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_list_details: &common_utils::types::TimeRange,) -> CustomResult<api_models::refunds::RefundListMetaData, errors::DatabaseError>;#[cfg(feature = "v1")]async fn get_refunds_count(conn: &PgPooledConn,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_list_details: &refunds::RefundListConstraints,) -> CustomResult<i64, errors::DatabaseError>;#[cfg(feature = "v1")]async fn get_refund_status_with_count(conn: &PgPooledConn,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,profile_id_list: Option<Vec<common_utils::id_type::ProfileId>>,time_range: &common_utils::types::TimeRange,) -> CustomResult<Vec<(RefundStatus, i64)>, errors::DatabaseError>;@@ -72,13 +72,19 @@ impl RefundDbExt for Refund {#[cfg(feature = "v1")]async fn filter_by_constraints(conn: &PgPooledConn,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_list_details: &refunds::RefundListConstraints,limit: i64,offset: i64,) -> CustomResult<Vec<Self>, errors::DatabaseError> {let mut filter = <Self as HasTable>::table()- .filter(dsl::merchant_id.eq(merchant_id.to_owned()))+ .filter(+ dsl::processor_merchant_id+ .eq(processor_merchant_id.to_owned())+ .or(dsl::processor_merchant_id+ .is_null()+ .and(dsl::merchant_id.eq(processor_merchant_id.to_owned()))),+ ).order(dsl::modified_at.desc()).into_boxed();let mut search_by_pay_or_ref_id = false;@@ -260,7 +266,7 @@ impl RefundDbExt for Refund {#[cfg(feature = "v1")]async fn filter_by_meta_constraints(conn: &PgPooledConn,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_list_details: &common_utils::types::TimeRange,) -> CustomResult<api_models::refunds::RefundListMetaData, errors::DatabaseError> {let start_time = refund_list_details.start_time;@@ -270,7 +276,13 @@ impl RefundDbExt for Refund {.unwrap_or_else(common_utils::date_time::now);let filter = <Self as HasTable>::table()- .filter(dsl::merchant_id.eq(merchant_id.to_owned()))+ .filter(+ dsl::processor_merchant_id+ .eq(processor_merchant_id.to_owned())+ .or(dsl::processor_merchant_id+ .is_null()+ .and(dsl::merchant_id.eq(processor_merchant_id.to_owned()))),+ ).order(dsl::modified_at.desc()).filter(dsl::created_at.ge(start_time)).filter(dsl::created_at.le(end_time));@@ -316,12 +328,18 @@ impl RefundDbExt for Refund {#[cfg(feature = "v1")]async fn get_refunds_count(conn: &PgPooledConn,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,refund_list_details: &refunds::RefundListConstraints,) -> CustomResult<i64, errors::DatabaseError> {let mut filter = <Self as HasTable>::table().count()- .filter(dsl::merchant_id.eq(merchant_id.to_owned()))+ .filter(+ dsl::processor_merchant_id+ .eq(processor_merchant_id.to_owned())+ .or(dsl::processor_merchant_id+ .is_null()+ .and(dsl::merchant_id.eq(processor_merchant_id.to_owned()))),+ ).into_boxed();let mut search_by_pay_or_ref_id = false;@@ -473,14 +491,20 @@ impl RefundDbExt for Refund {#[cfg(feature = "v1")]async fn get_refund_status_with_count(conn: &PgPooledConn,- merchant_id: &common_utils::id_type::MerchantId,+ processor_merchant_id: &common_utils::id_type::MerchantId,profile_id_list: Option<Vec<common_utils::id_type::ProfileId>>,time_range: &common_utils::types::TimeRange,) -> CustomResult<Vec<(RefundStatus, i64)>, errors::DatabaseError> {let mut query = <Self as HasTable>::table().group_by(dsl::refund_status).select((dsl::refund_status, diesel::dsl::count_star()))- .filter(dsl::merchant_id.eq(merchant_id.to_owned()))+ .filter(+ dsl::processor_merchant_id+ .eq(processor_merchant_id.to_owned())+ .or(dsl::processor_merchant_id+ .is_null()+ .and(dsl::merchant_id.eq(processor_merchant_id.to_owned()))),+ ).into_boxed();if let Some(profile_id) = profile_id_list {diff --git a/crates/router/src/utils.rs b/crates/router/src/utils.rsindex cd61e43b8c..5969dffe34 100644--- a/crates/router/src/utils.rs+++ b/crates/router/src/utils.rs@@ -237,7 +237,7 @@ pub async fn find_payment_intent_from_refund_id_type(let db = &*state.store;let refund = match refund_id_type {webhooks::RefundIdType::RefundId(id) => db- .find_refund_by_merchant_id_refund_id(+ .find_refund_by_processor_merchant_id_refund_id(platform.get_processor().get_account().get_id(),&id,platform.get_processor().get_account().storage_scheme,@@ -245,7 +245,7 @@ pub async fn find_payment_intent_from_refund_id_type(.await.to_not_found_response(errors::ApiErrorResponse::RefundNotFound)?,webhooks::RefundIdType::ConnectorRefundId(id) => db- .find_refund_by_merchant_id_connector_refund_id_connector(+ .find_refund_by_processor_merchant_id_connector_refund_id_connector(platform.get_processor().get_account().get_id(),&id,connector_name,diff --git a/migrations/2026-03-31-123119_add_index_processor_merchant_id_refund_id_for_refund/down.sql b/migrations/2026-03-31-123119_add_index_processor_merchant_id_refund_id_for_refund/down.sqlnew file mode 100644index 0000000000..f95e0a93f0--- /dev/null+++ b/migrations/2026-03-31-123119_add_index_processor_merchant_id_refund_id_for_refund/down.sql@@ -0,0 +1,2 @@+-- This file should undo anything in `up.sql`+DROP INDEX CONCURRENTLY IF EXISTS refund_processor_merchant_id_refund_id_index;diff --git a/migrations/2026-03-31-123119_add_index_processor_merchant_id_refund_id_for_refund/metadata.toml b/migrations/2026-03-31-123119_add_index_processor_merchant_id_refund_id_for_refund/metadata.tomlnew file mode 100644index 0000000000..79e9221c1f--- /dev/null+++ b/migrations/2026-03-31-123119_add_index_processor_merchant_id_refund_id_for_refund/metadata.toml@@ -0,0 +1 @@+run_in_transaction = falsediff --git a/migrations/2026-03-31-123119_add_index_processor_merchant_id_refund_id_for_refund/up.sql b/migrations/2026-03-31-123119_add_index_processor_merchant_id_refund_id_for_refund/up.sqlnew file mode 100644index 0000000000..8f6af46360--- /dev/null+++ b/migrations/2026-03-31-123119_add_index_processor_merchant_id_refund_id_for_refund/up.sql@@ -0,0 +1,2 @@+-- Your SQL goes here+CREATE INDEX CONCURRENTLY IF NOT EXISTS refund_processor_merchant_id_refund_id_index ON refund (processor_merchant_id, refund_id);