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(customers): add modular customer support for v1 payments (#11716)
Shankar Singh Cd26c9db9f490
diff --git a/crates/common_utils/src/id_type/global_id/customer.rs b/crates/common_utils/src/id_type/global_id/customer.rsindex b1119e147e..5f2e51dd4f 100644--- a/crates/common_utils/src/id_type/global_id/customer.rs+++ b/crates/common_utils/src/id_type/global_id/customer.rs@@ -11,7 +11,34 @@ Example: `cell1_cus_uu1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p`"// Database related implementations so that this field can be used directly in the database tablescrate::impl_queryable_id_type!(GlobalCustomerId);-crate::impl_to_sql_from_sql_global_id_type!(GlobalCustomerId);++// NOTE: We intentionally do NOT use `impl_to_sql_from_sql_global_id_type!` here because that+// macro generates a strict `FromSql` that validates GlobalId format. During the V1/V2 coexistence+// period, V1-created rows may store a V1-format CustomerId (e.g. `cus_xxx`).+// We use `new_unchecked` to accept any string value.+impl<DB> diesel::serialize::ToSql<diesel::sql_types::Text, DB> for GlobalCustomerId+where+ DB: diesel::backend::Backend,+ super::GlobalId: diesel::serialize::ToSql<diesel::sql_types::Text, DB>,+{+ fn to_sql<'b>(+ &'b self,+ out: &mut diesel::serialize::Output<'b, '_, DB>,+ ) -> diesel::serialize::Result {+ self.0.to_sql(out)+ }+}++impl<DB> diesel::deserialize::FromSql<diesel::sql_types::Text, DB> for GlobalCustomerId+where+ DB: diesel::backend::Backend,+ String: diesel::deserialize::FromSql<diesel::sql_types::Text, DB>,+{+ fn from_sql(value: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {+ let string_val = String::from_sql(value)?;+ Ok(Self::new_unchecked(string_val))+ }+}impl GlobalCustomerId {/// Get string representation of the iddiff --git a/crates/common_utils/src/id_type/global_id/payment_methods.rs b/crates/common_utils/src/id_type/global_id/payment_methods.rsindex cb7099a8b5..550e279c59 100644--- a/crates/common_utils/src/id_type/global_id/payment_methods.rs+++ b/crates/common_utils/src/id_type/global_id/payment_methods.rs@@ -110,6 +110,15 @@ impl GlobalPaymentMethodId {.change_context(GlobalPaymentMethodIdError::ConstructionError)?;Ok(Self(id))}++ /// Create a new GlobalPaymentMethodId without validation+ /// WARNING: This should only be used when the input is already trusted/validated,+ /// such as when reading a V1 payment method id from the database+ pub fn new_unchecked(input_string: String) -> Self {+ let alphanumeric_id = crate::id_type::AlphaNumericId::new_unchecked(input_string);+ let length_id = crate::id_type::LengthId::new_unchecked(alphanumeric_id);+ Self(GlobalId(length_id))+ }}impl<DB> diesel::Queryable<diesel::sql_types::Text, DB> for GlobalPaymentMethodId@@ -139,11 +148,11 @@ whereimpl<DB> diesel::deserialize::FromSql<diesel::sql_types::Text, DB> for GlobalPaymentMethodIdwhereDB: diesel::backend::Backend,- GlobalId: diesel::deserialize::FromSql<diesel::sql_types::Text, DB>,+ String: diesel::deserialize::FromSql<diesel::sql_types::Text, DB>,{fn from_sql(value: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {- let global_id = GlobalId::from_sql(value)?;- Ok(Self(global_id))+ let string_val = String::from_sql(value)?;+ Ok(Self::new_unchecked(string_val))}}diff --git a/crates/diesel_models/src/customers.rs b/crates/diesel_models/src/customers.rsindex f69f81cb4e..ea8a676d82 100644--- a/crates/diesel_models/src/customers.rs+++ b/crates/diesel_models/src/customers.rs@@ -34,6 +34,7 @@ pub struct CustomerNew {pub created_by: Option<String>,pub last_modified_by: Option<String>,pub document_details: Option<Encryption>,+ pub id: Option<common_utils::id_type::CustomerId>,}#[cfg(feature = "v1")]@@ -66,6 +67,7 @@ impl From<CustomerNew> for Customer {document_details: customer_new.document_details,created_by: customer_new.created_by,last_modified_by: customer_new.last_modified_by,+ id: customer_new.id,}}}@@ -90,15 +92,15 @@ pub struct CustomerNew {pub updated_by: Option<String>,pub version: ApiVersion,pub tax_registration_id: Option<Encryption>,+ pub created_by: Option<String>,+ pub last_modified_by: Option<String>,+ pub document_details: Option<Encryption>,+ pub id: common_utils::id_type::GlobalCustomerId,pub merchant_reference_id: Option<common_utils::id_type::CustomerId>,pub default_billing_address: Option<Encryption>,pub default_shipping_address: Option<Encryption>,pub status: DeleteStatus,- pub id: common_utils::id_type::GlobalCustomerId,- pub created_by: Option<String>,- pub last_modified_by: Option<String>,pub customer_id: Option<common_utils::id_type::GlobalCustomerId>,- pub document_details: Option<Encryption>,}#[cfg(feature = "v2")]@@ -164,6 +166,7 @@ pub struct Customer {pub created_by: Option<String>,pub last_modified_by: Option<String>,pub document_details: Option<Encryption>,+ pub id: Option<common_utils::id_type::CustomerId>,}#[cfg(feature = "v2")]@@ -189,12 +192,12 @@ pub struct Customer {pub created_by: Option<String>,pub last_modified_by: Option<String>,pub document_details: Option<Encryption>,+ pub id: common_utils::id_type::GlobalCustomerId,pub merchant_reference_id: Option<common_utils::id_type::CustomerId>,pub default_billing_address: Option<Encryption>,pub default_shipping_address: Option<Encryption>,#[diesel(deserialize_as = RequiredFromNullableWithDefault<DeleteStatus>)]pub status: DeleteStatus,- pub id: common_utils::id_type::GlobalCustomerId,pub customer_id: Option<common_utils::id_type::GlobalCustomerId>,}diff --git a/crates/diesel_models/src/schema.rs b/crates/diesel_models/src/schema.rsindex a2202739ac..52976cd9d3 100644--- a/crates/diesel_models/src/schema.rs+++ b/crates/diesel_models/src/schema.rs@@ -440,6 +440,8 @@ diesel::table! {#[max_length = 255]last_modified_by -> Nullable<Varchar>,document_details -> Nullable<Bytea>,+ #[max_length = 64]+ id -> Nullable<Varchar>,}}diff --git a/crates/diesel_models/src/schema_v2.rs b/crates/diesel_models/src/schema_v2.rsindex 35ebbc8209..191ab4b83c 100644--- a/crates/diesel_models/src/schema_v2.rs+++ b/crates/diesel_models/src/schema_v2.rs@@ -445,13 +445,13 @@ diesel::table! {last_modified_by -> Nullable<Varchar>,document_details -> Nullable<Bytea>,#[max_length = 64]+ id -> Varchar,+ #[max_length = 64]merchant_reference_id -> Nullable<Varchar>,default_billing_address -> Nullable<Bytea>,default_shipping_address -> Nullable<Bytea>,status -> Nullable<DeleteStatus>,#[max_length = 64]- id -> Varchar,- #[max_length = 64]customer_id -> Nullable<Varchar>,}}diff --git a/crates/hyperswitch_domain_models/src/customer.rs b/crates/hyperswitch_domain_models/src/customer.rsindex c9007d0cf5..ae0788ddc0 100644--- a/crates/hyperswitch_domain_models/src/customer.rs+++ b/crates/hyperswitch_domain_models/src/customer.rs@@ -160,7 +160,7 @@ impl behaviour::Conversion for Customer {type NewDstType = diesel_models::customers::CustomerNew;async fn convert(self) -> CustomResult<Self::DstType, ValidationError> {Ok(diesel_models::customers::Customer {- customer_id: self.customer_id,+ customer_id: self.customer_id.clone(),merchant_id: self.merchant_id,name: self.name.map(Encryption::from),email: self.email.map(Encryption::from),@@ -181,6 +181,7 @@ impl behaviour::Conversion for Customer {last_modified_by: self.last_modified_by.map(|last_modified_by| last_modified_by.to_string()),+ id: Some(self.customer_id),})}@@ -271,6 +272,7 @@ impl behaviour::Conversion for Customer {async fn construct_new(self) -> CustomResult<Self::NewDstType, ValidationError> {let now = date_time::now();Ok(diesel_models::customers::CustomerNew {+ id: Some(self.customer_id.clone()),customer_id: self.customer_id,merchant_id: self.merchant_id,name: self.name.map(Encryption::from),diff --git a/crates/router/src/routes/customers.rs b/crates/router/src/routes/customers.rsindex 4b815da86f..6dd293c915 100644--- a/crates/router/src/routes/customers.rs+++ b/crates/router/src/routes/customers.rs@@ -150,13 +150,13 @@ pub async fn customers_retrieve(pub async fn customers_retrieve(state: web::Data<AppState>,req: HttpRequest,- path: web::Path<id_type::GlobalCustomerId>,+ path: web::Path<String>,) -> HttpResponse {use crate::services::authentication::sdk_or_api_or_client_auth;let flow = Flow::CustomersRetrieve;- let id = path.into_inner();+ let id = id_type::GlobalCustomerId::new_unchecked(path.into_inner());let v2_client_auth = auth::V2ClientAuth(common_utils::types::authentication::ResourceId::Customer(id.clone()),@@ -392,11 +392,11 @@ pub async fn customers_update(pub async fn customers_update(state: web::Data<AppState>,req: HttpRequest,- path: web::Path<id_type::GlobalCustomerId>,+ path: web::Path<String>,json_payload: web::Json<customers::CustomerUpdateRequest>,) -> HttpResponse {let flow = Flow::CustomersUpdate;- let id = path.into_inner();+ let id = id_type::GlobalCustomerId::new_unchecked(path.into_inner());let request = json_payload.into_inner();let request_internal = customers::CustomerUpdateRequestInternal { id, request };@@ -435,10 +435,10 @@ pub async fn customers_update(pub async fn customers_delete(state: web::Data<AppState>,req: HttpRequest,- path: web::Path<id_type::GlobalCustomerId>,+ path: web::Path<String>,) -> impl Responder {let flow = Flow::CustomersDelete;- let id = path.into_inner();+ let id = id_type::GlobalCustomerId::new_unchecked(path.into_inner());Box::pin(api::server_wrap(flow,diff --git a/migrations/2026-04-02-000001_add_id_column_to_customers/down.sql b/migrations/2026-04-02-000001_add_id_column_to_customers/down.sqlnew file mode 100644index 0000000000..baab219e74--- /dev/null+++ b/migrations/2026-04-02-000001_add_id_column_to_customers/down.sql@@ -0,0 +1,2 @@+ALTER TABLE customers+DROP COLUMN IF EXISTS id;diff --git a/migrations/2026-04-02-000001_add_id_column_to_customers/up.sql b/migrations/2026-04-02-000001_add_id_column_to_customers/up.sqlnew file mode 100644index 0000000000..9f78737f86--- /dev/null+++ b/migrations/2026-04-02-000001_add_id_column_to_customers/up.sql@@ -0,0 +1,2 @@+ALTER TABLE customers+ADD COLUMN IF NOT EXISTS id VARCHAR(64);diff --git a/migrations/2026-04-02-000002_backfill_id_for_v1_customers/down.sql b/migrations/2026-04-02-000002_backfill_id_for_v1_customers/down.sqlnew file mode 100644index 0000000000..5d6941b74e--- /dev/null+++ b/migrations/2026-04-02-000002_backfill_id_for_v1_customers/down.sql@@ -0,0 +1,3 @@+-- Revert: clear the id column for all V1-created rows.+-- V2-created rows (version = 'v2') always populate id on insert and are left untouched.+UPDATE customers SET id = NULL WHERE version = 'v1';diff --git a/migrations/2026-04-02-000002_backfill_id_for_v1_customers/up.sql b/migrations/2026-04-02-000002_backfill_id_for_v1_customers/up.sqlnew file mode 100644index 0000000000..64668382bc--- /dev/null+++ b/migrations/2026-04-02-000002_backfill_id_for_v1_customers/up.sql@@ -0,0 +1,6 @@+-- Populate the id column for existing V1-created customer rows where id is NULL.+-- During the V1/V2 coexistence period, the V2 binary looks up customers by the+-- id column (WHERE id = $1), so V1-created customers need id populated with the+-- same value as customer_id.+-- V2-created rows already have id populated on insert, so they are unaffected.+UPDATE customers SET id = customer_id WHERE id IS NULL AND version = 'v1';diff --git a/v2_compatible_migrations/2024-08-28-081747_recreate_ids_for_v2/down.sql b/v2_compatible_migrations/2024-08-28-081747_recreate_ids_for_v2/down.sqlindex 87b61f80da..3639104861 100644--- a/v2_compatible_migrations/2024-08-28-081747_recreate_ids_for_v2/down.sql+++ b/v2_compatible_migrations/2024-08-28-081747_recreate_ids_for_v2/down.sql@@ -1,9 +1,7 @@-- This file contains queries to create the `id` column as a `SERIAL` column instead of `VARCHAR` column for tables that already have it.-- This is to revert the `id` columns to the previous state.-ALTER TABLE customers DROP COLUMN IF EXISTS id;--+-- Note: customers `id` column is reverted via v1 migrations (migrations/2026-04-02-000001_add_id_column_to_customers)ALTER TABLE payment_intent DROP COLUMN IF EXISTS id;@@ -14,4 +12,3 @@ ALTER TABLE payment_attempt DROP COLUMN IF EXISTS id;------------------------ Payment Methods -----------------------ALTER TABLE payment_methods DROP COLUMN IF EXISTS id;-diff --git a/v2_compatible_migrations/2024-08-28-081747_recreate_ids_for_v2/up.sql b/v2_compatible_migrations/2024-08-28-081747_recreate_ids_for_v2/up.sqlindex 9ac4410616..6f25df1b2e 100644--- a/v2_compatible_migrations/2024-08-28-081747_recreate_ids_for_v2/up.sql+++ b/v2_compatible_migrations/2024-08-28-081747_recreate_ids_for_v2/up.sql@@ -2,11 +2,7 @@-- It must be ensured that the deployed version of the application does not include the `id` column in any of its queries.-- Drop the id column as this will be used later as the primary key with a different type------------------------- Customers --------------------------ALTER TABLE customers-ADD COLUMN IF NOT EXISTS id VARCHAR(64);+-- Note: customers `id` column is added via v1 migrations (migrations/2026-04-02-000001_add_id_column_to_customers)------------------------ Payment Intent -----------------------@@ -25,4 +21,3 @@ ADD COLUMN IF NOT EXISTS id VARCHAR(64);ALTER TABLE payment_methodsADD COLUMN IF NOT EXISTS id VARCHAR(64);-