Skip to content

Commit e40a293

Browse files
authored
fix(router): make customer_id optional when billing and shipping address is passed in payments create, update (#2762)
1 parent c0a5e7b commit e40a293

File tree

13 files changed

+18
-37
lines changed

13 files changed

+18
-37
lines changed

crates/diesel_models/src/address.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct AddressNew {
1919
pub last_name: Option<Encryption>,
2020
pub phone_number: Option<Encryption>,
2121
pub country_code: Option<String>,
22-
pub customer_id: String,
22+
pub customer_id: Option<String>,
2323
pub merchant_id: String,
2424
pub payment_id: Option<String>,
2525
pub created_at: PrimitiveDateTime,
@@ -45,7 +45,7 @@ pub struct Address {
4545
pub country_code: Option<String>,
4646
pub created_at: PrimitiveDateTime,
4747
pub modified_at: PrimitiveDateTime,
48-
pub customer_id: String,
48+
pub customer_id: Option<String>,
4949
pub merchant_id: String,
5050
pub payment_id: Option<String>,
5151
pub updated_by: String,

crates/diesel_models/src/schema.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ diesel::table! {
2424
created_at -> Timestamp,
2525
modified_at -> Timestamp,
2626
#[max_length = 64]
27-
customer_id -> Varchar,
27+
customer_id -> Nullable<Varchar>,
2828
#[max_length = 64]
2929
merchant_id -> Varchar,
3030
#[max_length = 64]

crates/router/src/core/payments/helpers.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ pub async fn create_or_update_address_for_payment_by_request(
221221
None => match req_address {
222222
Some(address) => {
223223
// generate a new address here
224-
let customer_id = customer_id.get_required_value("customer_id")?;
225-
226224
let address_details = address.address.clone().unwrap_or_default();
227225
Some(
228226
db.insert_address_for_payments(
@@ -282,7 +280,6 @@ pub async fn create_or_find_address_for_payment_by_request(
282280
None => match req_address {
283281
Some(address) => {
284282
// generate a new address here
285-
let customer_id = customer_id.get_required_value("customer_id")?;
286283

287284
let address_details = address.address.clone().unwrap_or_default();
288285
Some(
@@ -317,7 +314,7 @@ pub async fn get_domain_address_for_payments(
317314
address_details: api_models::payments::AddressDetails,
318315
address: &api_models::payments::Address,
319316
merchant_id: &str,
320-
customer_id: &str,
317+
customer_id: Option<&String>,
321318
payment_id: &str,
322319
key: &[u8],
323320
storage_scheme: enums::MerchantStorageScheme,
@@ -332,7 +329,7 @@ pub async fn get_domain_address_for_payments(
332329
.async_lift(|inner| types::encrypt_optional(inner, key))
333330
.await?,
334331
country_code: address.phone.as_ref().and_then(|a| a.country_code.clone()),
335-
customer_id: customer_id.to_string(),
332+
customer_id: customer_id.cloned(),
336333
merchant_id: merchant_id.to_string(),
337334
address_id: generate_id(consts::ID_LENGTH, "add"),
338335
city: address_details.city,
@@ -763,25 +760,14 @@ fn validate_new_mandate_request(
763760
}
764761

765762
pub fn validate_customer_id_mandatory_cases(
766-
has_shipping: bool,
767-
has_billing: bool,
768763
has_setup_future_usage: bool,
769764
customer_id: &Option<String>,
770765
) -> RouterResult<()> {
771-
match (
772-
has_shipping,
773-
has_billing,
774-
has_setup_future_usage,
775-
customer_id,
776-
) {
777-
(true, _, _, None) | (_, true, _, None) | (_, _, true, None) => {
778-
Err(errors::ApiErrorResponse::PreconditionFailed {
779-
message: "customer_id is mandatory when shipping or billing \
780-
address is given or when setup_future_usage is given"
781-
.to_string(),
782-
})
783-
.into_report()
784-
}
766+
match (has_setup_future_usage, customer_id) {
767+
(true, None) => Err(errors::ApiErrorResponse::PreconditionFailed {
768+
message: "customer_id is mandatory when setup_future_usage is given".to_string(),
769+
})
770+
.into_report(),
785771
_ => Ok(()),
786772
}
787773
}

crates/router/src/core/payments/operations/payment_approve.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
130130
amount = payment_attempt.amount.into();
131131

132132
helpers::validate_customer_id_mandatory_cases(
133-
request.shipping.is_some(),
134-
request.billing.is_some(),
135133
request.setup_future_usage.is_some(),
136134
&payment_intent
137135
.customer_id

crates/router/src/core/payments/operations/payment_complete_authorize.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
139139
amount = payment_attempt.amount.into();
140140

141141
helpers::validate_customer_id_mandatory_cases(
142-
request.shipping.is_some(),
143-
request.billing.is_some(),
144142
request.setup_future_usage.is_some(),
145143
&payment_intent
146144
.customer_id

crates/router/src/core/payments/operations/payment_confirm.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
284284
amount = payment_attempt.amount.into();
285285

286286
helpers::validate_customer_id_mandatory_cases(
287-
request.shipping.is_some(),
288-
request.billing.is_some(),
289287
request.setup_future_usage.is_some(),
290288
&payment_intent
291289
.customer_id

crates/router/src/core/payments/operations/payment_create.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen
538538
)?;
539539

540540
helpers::validate_customer_id_mandatory_cases(
541-
request.shipping.is_some(),
542-
request.billing.is_some(),
543541
request.setup_future_usage.is_some(),
544542
&request
545543
.customer

crates/router/src/core/payments/operations/payment_update.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
146146

147147
if request.confirm.unwrap_or(false) {
148148
helpers::validate_customer_id_mandatory_cases(
149-
request.shipping.is_some(),
150-
request.billing.is_some(),
151149
request.setup_future_usage.is_some(),
152150
&payment_intent
153151
.customer_id

crates/router/src/db/address.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,8 @@ impl AddressInterface for MockDb {
763763
.await
764764
.iter_mut()
765765
.find(|address| {
766-
address.customer_id == customer_id && address.merchant_id == merchant_id
766+
address.customer_id == Some(customer_id.to_string())
767+
&& address.merchant_id == merchant_id
767768
})
768769
.map(|a| {
769770
let address_updated =

crates/router/src/types/domain/address.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub struct Address {
3535
#[serde(skip_serializing)]
3636
#[serde(with = "custom_serde::iso8601")]
3737
pub modified_at: PrimitiveDateTime,
38-
pub customer_id: String,
38+
pub customer_id: Option<String>,
3939
pub merchant_id: String,
4040
pub payment_id: Option<String>,
4141
pub updated_by: String,

0 commit comments

Comments
 (0)