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(router): make customer_id optional when billing and shipping address is passed in payments create, update (#2762)

Prasunna Soppae40a29351c7a
diff --git a/crates/diesel_models/src/address.rs b/crates/diesel_models/src/address.rs
index e67f37c904..03dedfd60d 100644
--- a/crates/diesel_models/src/address.rs
+++ b/crates/diesel_models/src/address.rs
@@ -19,7 +19,7 @@ pub struct AddressNew {
pub last_name: Option<Encryption>,
pub phone_number: Option<Encryption>,
pub country_code: Option<String>,
- pub customer_id: String,
+ pub customer_id: Option<String>,
pub merchant_id: String,
pub payment_id: Option<String>,
pub created_at: PrimitiveDateTime,
@@ -45,7 +45,7 @@ pub struct Address {
pub country_code: Option<String>,
pub created_at: PrimitiveDateTime,
pub modified_at: PrimitiveDateTime,
- pub customer_id: String,
+ pub customer_id: Option<String>,
pub merchant_id: String,
pub payment_id: Option<String>,
pub updated_by: String,
diff --git a/crates/diesel_models/src/schema.rs b/crates/diesel_models/src/schema.rs
index 02abfb842b..e214fa364d 100644
--- a/crates/diesel_models/src/schema.rs
+++ b/crates/diesel_models/src/schema.rs
@@ -24,7 +24,7 @@ diesel::table! {
created_at -> Timestamp,
modified_at -> Timestamp,
#[max_length = 64]
- customer_id -> Varchar,
+ customer_id -> Nullable<Varchar>,
#[max_length = 64]
merchant_id -> Varchar,
#[max_length = 64]
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index af67d30ec6..f42e498538 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -221,8 +221,6 @@ pub async fn create_or_update_address_for_payment_by_request(
None => match req_address {
Some(address) => {
// generate a new address here
- let customer_id = customer_id.get_required_value("customer_id")?;
-
let address_details = address.address.clone().unwrap_or_default();
Some(
db.insert_address_for_payments(
@@ -282,7 +280,6 @@ pub async fn create_or_find_address_for_payment_by_request(
None => match req_address {
Some(address) => {
// generate a new address here
- let customer_id = customer_id.get_required_value("customer_id")?;
let address_details = address.address.clone().unwrap_or_default();
Some(
@@ -317,7 +314,7 @@ pub async fn get_domain_address_for_payments(
address_details: api_models::payments::AddressDetails,
address: &api_models::payments::Address,
merchant_id: &str,
- customer_id: &str,
+ customer_id: Option<&String>,
payment_id: &str,
key: &[u8],
storage_scheme: enums::MerchantStorageScheme,
@@ -332,7 +329,7 @@ pub async fn get_domain_address_for_payments(
.async_lift(|inner| types::encrypt_optional(inner, key))
.await?,
country_code: address.phone.as_ref().and_then(|a| a.country_code.clone()),
- customer_id: customer_id.to_string(),
+ customer_id: customer_id.cloned(),
merchant_id: merchant_id.to_string(),
address_id: generate_id(consts::ID_LENGTH, "add"),
city: address_details.city,
@@ -763,25 +760,14 @@ fn validate_new_mandate_request(
}
pub fn validate_customer_id_mandatory_cases(
- has_shipping: bool,
- has_billing: bool,
has_setup_future_usage: bool,
customer_id: &Option<String>,
) -> RouterResult<()> {
- match (
- has_shipping,
- has_billing,
- has_setup_future_usage,
- customer_id,
- ) {
- (true, _, _, None) | (_, true, _, None) | (_, _, true, None) => {
- Err(errors::ApiErrorResponse::PreconditionFailed {
- message: "customer_id is mandatory when shipping or billing \
- address is given or when setup_future_usage is given"
- .to_string(),
- })
- .into_report()
- }
+ match (has_setup_future_usage, customer_id) {
+ (true, None) => Err(errors::ApiErrorResponse::PreconditionFailed {
+ message: "customer_id is mandatory when setup_future_usage is given".to_string(),
+ })
+ .into_report(),
_ => Ok(()),
}
}
diff --git a/crates/router/src/core/payments/operations/payment_approve.rs b/crates/router/src/core/payments/operations/payment_approve.rs
index a1d50a9049..16bb84f69d 100644
--- a/crates/router/src/core/payments/operations/payment_approve.rs
+++ b/crates/router/src/core/payments/operations/payment_approve.rs
@@ -130,8 +130,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
amount = payment_attempt.amount.into();
helpers::validate_customer_id_mandatory_cases(
- request.shipping.is_some(),
- request.billing.is_some(),
request.setup_future_usage.is_some(),
&payment_intent
.customer_id
diff --git a/crates/router/src/core/payments/operations/payment_complete_authorize.rs b/crates/router/src/core/payments/operations/payment_complete_authorize.rs
index 0e357f0873..a2f5292a37 100644
--- a/crates/router/src/core/payments/operations/payment_complete_authorize.rs
+++ b/crates/router/src/core/payments/operations/payment_complete_authorize.rs
@@ -139,8 +139,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
amount = payment_attempt.amount.into();
helpers::validate_customer_id_mandatory_cases(
- request.shipping.is_some(),
- request.billing.is_some(),
request.setup_future_usage.is_some(),
&payment_intent
.customer_id
diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs
index 9e1f12b6bc..8842963990 100644
--- a/crates/router/src/core/payments/operations/payment_confirm.rs
+++ b/crates/router/src/core/payments/operations/payment_confirm.rs
@@ -284,8 +284,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
amount = payment_attempt.amount.into();
helpers::validate_customer_id_mandatory_cases(
- request.shipping.is_some(),
- request.billing.is_some(),
request.setup_future_usage.is_some(),
&payment_intent
.customer_id
diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs
index 2d31f82aeb..f3b777534b 100644
--- a/crates/router/src/core/payments/operations/payment_create.rs
+++ b/crates/router/src/core/payments/operations/payment_create.rs
@@ -538,8 +538,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen
)?;
helpers::validate_customer_id_mandatory_cases(
- request.shipping.is_some(),
- request.billing.is_some(),
request.setup_future_usage.is_some(),
&request
.customer
diff --git a/crates/router/src/core/payments/operations/payment_update.rs b/crates/router/src/core/payments/operations/payment_update.rs
index a77ede0e6f..d0b17b5d46 100644
--- a/crates/router/src/core/payments/operations/payment_update.rs
+++ b/crates/router/src/core/payments/operations/payment_update.rs
@@ -146,8 +146,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
if request.confirm.unwrap_or(false) {
helpers::validate_customer_id_mandatory_cases(
- request.shipping.is_some(),
- request.billing.is_some(),
request.setup_future_usage.is_some(),
&payment_intent
.customer_id
diff --git a/crates/router/src/db/address.rs b/crates/router/src/db/address.rs
index 20f7bdb912..9244fc022d 100644
--- a/crates/router/src/db/address.rs
+++ b/crates/router/src/db/address.rs
@@ -763,7 +763,8 @@ impl AddressInterface for MockDb {
.await
.iter_mut()
.find(|address| {
- address.customer_id == customer_id && address.merchant_id == merchant_id
+ address.customer_id == Some(customer_id.to_string())
+ && address.merchant_id == merchant_id
})
.map(|a| {
let address_updated =
diff --git a/crates/router/src/types/domain/address.rs b/crates/router/src/types/domain/address.rs
index 008cead1eb..ddf9c2152e 100644
--- a/crates/router/src/types/domain/address.rs
+++ b/crates/router/src/types/domain/address.rs
@@ -35,7 +35,7 @@ pub struct Address {
#[serde(skip_serializing)]
#[serde(with = "custom_serde::iso8601")]
pub modified_at: PrimitiveDateTime,
- pub customer_id: String,
+ pub customer_id: Option<String>,
pub merchant_id: String,
pub payment_id: Option<String>,
pub updated_by: String,
diff --git a/crates/router/src/utils.rs b/crates/router/src/utils.rs
index 84a75d397e..386bd02ae9 100644
--- a/crates/router/src/utils.rs
+++ b/crates/router/src/utils.rs
@@ -566,7 +566,7 @@ impl CustomerAddress for api_models::customers::CustomerRequest {
.async_lift(|inner| encrypt_optional(inner, key))
.await?,
country_code: self.phone_country_code.clone(),
- customer_id: customer_id.to_string(),
+ customer_id: Some(customer_id.to_string()),
merchant_id: merchant_id.to_string(),
address_id: generate_id(consts::ID_LENGTH, "add"),
payment_id: None,
diff --git a/migrations/2023-11-02-074243_make_customer_id_nullable_in_address_table/down.sql b/migrations/2023-11-02-074243_make_customer_id_nullable_in_address_table/down.sql
new file mode 100644
index 0000000000..b148e66d87
--- /dev/null
+++ b/migrations/2023-11-02-074243_make_customer_id_nullable_in_address_table/down.sql
@@ -0,0 +1,2 @@
+-- This file should undo anything in `up.sql`
+ALTER TABLE address ALTER COLUMN customer_id SET NOT NULL;
\ No newline at end of file
diff --git a/migrations/2023-11-02-074243_make_customer_id_nullable_in_address_table/up.sql b/migrations/2023-11-02-074243_make_customer_id_nullable_in_address_table/up.sql
new file mode 100644
index 0000000000..98826c41e7
--- /dev/null
+++ b/migrations/2023-11-02-074243_make_customer_id_nullable_in_address_table/up.sql
@@ -0,0 +1,2 @@
+-- Your SQL goes here
+ALTER TABLE address ALTER COLUMN customer_id DROP NOT NULL;
\ No newline at end of file