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(payment-methods): fetch customer using merchant_id (#11261)

Kashif788c2e2805ca
diff --git a/crates/diesel_models/src/query/customers.rs b/crates/diesel_models/src/query/customers.rs
index 989bcdc9d9..e5a3d470dd 100644
--- a/crates/diesel_models/src/query/customers.rs
+++ b/crates/diesel_models/src/query/customers.rs
@@ -56,6 +56,21 @@ impl Customer {
generics::generic_find_by_id::<<Self as HasTable>::Table, _, _>(conn, id.to_owned()).await
}
+ #[cfg(feature = "v2")]
+ pub async fn find_by_global_id_merchant_id(
+ conn: &PgPooledConn,
+ id: &id_type::GlobalCustomerId,
+ merchant_id: &id_type::MerchantId,
+ ) -> StorageResult<Self> {
+ generics::generic_find_one::<<Self as HasTable>::Table, _, _>(
+ conn,
+ dsl::id
+ .eq(id.to_owned())
+ .and(dsl::merchant_id.eq(merchant_id.to_owned())),
+ )
+ .await
+ }
+
#[cfg(feature = "v1")]
pub async fn get_customer_count_by_merchant_id_and_constraints(
conn: &PgPooledConn,
diff --git a/crates/hyperswitch_domain_models/src/customer.rs b/crates/hyperswitch_domain_models/src/customer.rs
index e7e25b5d39..86dba393e0 100644
--- a/crates/hyperswitch_domain_models/src/customer.rs
+++ b/crates/hyperswitch_domain_models/src/customer.rs
@@ -784,6 +784,15 @@ where
key_store: &MerchantKeyStore,
storage_scheme: MerchantStorageScheme,
) -> CustomResult<Customer, Self::Error>;
+
+ #[cfg(feature = "v2")]
+ async fn find_customer_by_global_id_merchant_id(
+ &self,
+ id: &id_type::GlobalCustomerId,
+ merchant_id: &id_type::MerchantId,
+ key_store: &MerchantKeyStore,
+ storage_scheme: MerchantStorageScheme,
+ ) -> CustomResult<Customer, Self::Error>;
}
#[cfg(feature = "v1")]
diff --git a/crates/router/src/core/payment_methods.rs b/crates/router/src/core/payment_methods.rs
index 11cd3535e4..e4f3b5010d 100644
--- a/crates/router/src/core/payment_methods.rs
+++ b/crates/router/src/core/payment_methods.rs
@@ -1091,8 +1091,9 @@ pub async fn create_persistent_payment_method_core(
.get_required_value("customer_id")?;
let key_manager_state = &(state).into();
- db.find_customer_by_global_id(
+ db.find_customer_by_global_id_merchant_id(
&customer_id,
+ platform.get_provider().get_account().get_id(),
platform.get_provider().get_key_store(),
platform.get_provider().get_account().storage_scheme,
)
@@ -1173,8 +1174,9 @@ pub async fn create_volatile_payment_method_core(
let key_manager_state = &(state).into();
if let Some(ref customer_id) = customer_id {
- db.find_customer_by_global_id(
+ db.find_customer_by_global_id_merchant_id(
customer_id,
+ platform.get_provider().get_account().get_id(),
platform.get_provider().get_key_store(),
platform.get_provider().get_account().storage_scheme,
)
@@ -2198,8 +2200,9 @@ pub async fn payment_method_intent_create(
let customer_id = req.customer_id.to_owned();
let key_manager_state = &(state).into();
- db.find_customer_by_global_id(
+ db.find_customer_by_global_id_merchant_id(
&customer_id,
+ provider.get_account().get_id(),
provider.get_key_store(),
provider.get_account().storage_scheme,
)
@@ -4270,8 +4273,9 @@ pub async fn delete_payment_method_core(
)?;
let _customer = db
- .find_customer_by_global_id(
+ .find_customer_by_global_id_merchant_id(
customer_id,
+ platform.get_provider().get_account().get_id(),
platform.get_provider().get_key_store(),
platform.get_provider().get_account().storage_scheme,
)
@@ -4427,13 +4431,15 @@ pub async fn payment_methods_session_create(
let key_manager_state = &(&state).into();
if let (Some(customer_id)) = &request.customer_id {
- db.find_customer_by_global_id(
+ db.find_customer_by_global_id_merchant_id(
customer_id,
+ provider.get_account().get_id(),
provider.get_key_store(),
provider.get_account().storage_scheme,
)
.await
- .to_not_found_response(errors::ApiErrorResponse::CustomerNotFound)?;
+ .to_not_found_response(errors::ApiErrorResponse::CustomerNotFound)
+ .attach_printable("Customer not found for the payment method")?;
}
let payment_methods_session_id =
diff --git a/crates/router/src/db/kafka_store.rs b/crates/router/src/db/kafka_store.rs
index d5400c4e42..4201409688 100644
--- a/crates/router/src/db/kafka_store.rs
+++ b/crates/router/src/db/kafka_store.rs
@@ -532,6 +532,19 @@ impl CustomerInterface for KafkaStore {
.await
}
+ #[cfg(feature = "v2")]
+ async fn find_customer_by_global_id_merchant_id(
+ &self,
+ id: &id_type::GlobalCustomerId,
+ merchant_id: &id_type::MerchantId,
+ key_store: &domain::MerchantKeyStore,
+ storage_scheme: MerchantStorageScheme,
+ ) -> CustomResult<domain::Customer, errors::StorageError> {
+ self.diesel_store
+ .find_customer_by_global_id_merchant_id(id, merchant_id, key_store, storage_scheme)
+ .await
+ }
+
async fn insert_customer(
&self,
customer_data: domain::Customer,
diff --git a/crates/storage_impl/src/customers.rs b/crates/storage_impl/src/customers.rs
index e76baff0ec..dc018f2b46 100644
--- a/crates/storage_impl/src/customers.rs
+++ b/crates/storage_impl/src/customers.rs
@@ -423,6 +423,41 @@ impl<T: DatabaseStore> domain::CustomerInterface for kv_router_store::KVRouterSt
}
}
+ #[cfg(feature = "v2")]
+ #[instrument(skip_all)]
+ async fn find_customer_by_global_id_merchant_id(
+ &self,
+ id: &id_type::GlobalCustomerId,
+ merchant_id: &id_type::MerchantId,
+ key_store: &MerchantKeyStore,
+ storage_scheme: MerchantStorageScheme,
+ ) -> CustomResult<domain::Customer, StorageError> {
+ let conn = pg_connection_read(self).await?;
+ let result: domain::Customer = self
+ .find_resource_by_id(
+ key_store,
+ storage_scheme,
+ customers::Customer::find_by_global_id_merchant_id(&conn, id, merchant_id),
+ kv_router_store::FindResourceBy::Id(
+ format!("cust_{}", id.get_string_repr()),
+ PartitionKey::GlobalId {
+ id: id.get_string_repr(),
+ },
+ ),
+ )
+ .await?;
+
+ if result.merchant_id != *merchant_id {
+ Err(StorageError::ValueNotFound(
+ "db value not found".to_string(),
+ ))?
+ } else if result.status == common_enums::DeleteStatus::Redacted {
+ Err(StorageError::CustomerRedacted)?
+ } else {
+ Ok(result)
+ }
+ }
+
#[cfg(feature = "v2")]
#[instrument(skip_all)]
async fn update_customer_by_global_id(
@@ -752,6 +787,28 @@ impl<T: DatabaseStore> domain::CustomerInterface for RouterStore<T> {
_ => Ok(customer),
}
}
+
+ #[cfg(feature = "v2")]
+ #[instrument(skip_all)]
+ async fn find_customer_by_global_id_merchant_id(
+ &self,
+ id: &id_type::GlobalCustomerId,
+ merchant_id: &id_type::MerchantId,
+ key_store: &MerchantKeyStore,
+ _storage_scheme: MerchantStorageScheme,
+ ) -> CustomResult<domain::Customer, StorageError> {
+ let conn = pg_connection_read(self).await?;
+ let customer: domain::Customer = self
+ .call_database(
+ key_store,
+ customers::Customer::find_by_global_id_merchant_id(&conn, id, merchant_id),
+ )
+ .await?;
+ match customer.name {
+ Some(ref name) if name.peek() == pii::REDACTED => Err(StorageError::CustomerRedacted)?,
+ _ => Ok(customer),
+ }
+ }
}
#[async_trait::async_trait]
@@ -964,4 +1021,16 @@ impl domain::CustomerInterface for MockDb {
// [#172]: Implement function for `MockDb`
Err(StorageError::MockDbError)?
}
+
+ #[cfg(feature = "v2")]
+ async fn find_customer_by_global_id_merchant_id(
+ &self,
+ _id: &id_type::GlobalCustomerId,
+ _merchant_id: &id_type::MerchantId,
+ _key_store: &MerchantKeyStore,
+ _storage_scheme: MerchantStorageScheme,
+ ) -> CustomResult<domain::Customer, StorageError> {
+ // [#172]: Implement function for `MockDb`
+ Err(StorageError::MockDbError)?
+ }
}