mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-01 19:42:27 +08:00
fix(router): make customer_id optional when billing and shipping address is passed in payments create, update (#2762)
This commit is contained in:
@ -19,7 +19,7 @@ pub struct AddressNew {
|
|||||||
pub last_name: Option<Encryption>,
|
pub last_name: Option<Encryption>,
|
||||||
pub phone_number: Option<Encryption>,
|
pub phone_number: Option<Encryption>,
|
||||||
pub country_code: Option<String>,
|
pub country_code: Option<String>,
|
||||||
pub customer_id: String,
|
pub customer_id: Option<String>,
|
||||||
pub merchant_id: String,
|
pub merchant_id: String,
|
||||||
pub payment_id: Option<String>,
|
pub payment_id: Option<String>,
|
||||||
pub created_at: PrimitiveDateTime,
|
pub created_at: PrimitiveDateTime,
|
||||||
@ -45,7 +45,7 @@ pub struct Address {
|
|||||||
pub country_code: Option<String>,
|
pub country_code: Option<String>,
|
||||||
pub created_at: PrimitiveDateTime,
|
pub created_at: PrimitiveDateTime,
|
||||||
pub modified_at: PrimitiveDateTime,
|
pub modified_at: PrimitiveDateTime,
|
||||||
pub customer_id: String,
|
pub customer_id: Option<String>,
|
||||||
pub merchant_id: String,
|
pub merchant_id: String,
|
||||||
pub payment_id: Option<String>,
|
pub payment_id: Option<String>,
|
||||||
pub updated_by: String,
|
pub updated_by: String,
|
||||||
|
|||||||
@ -24,7 +24,7 @@ diesel::table! {
|
|||||||
created_at -> Timestamp,
|
created_at -> Timestamp,
|
||||||
modified_at -> Timestamp,
|
modified_at -> Timestamp,
|
||||||
#[max_length = 64]
|
#[max_length = 64]
|
||||||
customer_id -> Varchar,
|
customer_id -> Nullable<Varchar>,
|
||||||
#[max_length = 64]
|
#[max_length = 64]
|
||||||
merchant_id -> Varchar,
|
merchant_id -> Varchar,
|
||||||
#[max_length = 64]
|
#[max_length = 64]
|
||||||
|
|||||||
@ -221,8 +221,6 @@ pub async fn create_or_update_address_for_payment_by_request(
|
|||||||
None => match req_address {
|
None => match req_address {
|
||||||
Some(address) => {
|
Some(address) => {
|
||||||
// generate a new address here
|
// generate a new address here
|
||||||
let customer_id = customer_id.get_required_value("customer_id")?;
|
|
||||||
|
|
||||||
let address_details = address.address.clone().unwrap_or_default();
|
let address_details = address.address.clone().unwrap_or_default();
|
||||||
Some(
|
Some(
|
||||||
db.insert_address_for_payments(
|
db.insert_address_for_payments(
|
||||||
@ -282,7 +280,6 @@ pub async fn create_or_find_address_for_payment_by_request(
|
|||||||
None => match req_address {
|
None => match req_address {
|
||||||
Some(address) => {
|
Some(address) => {
|
||||||
// generate a new address here
|
// generate a new address here
|
||||||
let customer_id = customer_id.get_required_value("customer_id")?;
|
|
||||||
|
|
||||||
let address_details = address.address.clone().unwrap_or_default();
|
let address_details = address.address.clone().unwrap_or_default();
|
||||||
Some(
|
Some(
|
||||||
@ -317,7 +314,7 @@ pub async fn get_domain_address_for_payments(
|
|||||||
address_details: api_models::payments::AddressDetails,
|
address_details: api_models::payments::AddressDetails,
|
||||||
address: &api_models::payments::Address,
|
address: &api_models::payments::Address,
|
||||||
merchant_id: &str,
|
merchant_id: &str,
|
||||||
customer_id: &str,
|
customer_id: Option<&String>,
|
||||||
payment_id: &str,
|
payment_id: &str,
|
||||||
key: &[u8],
|
key: &[u8],
|
||||||
storage_scheme: enums::MerchantStorageScheme,
|
storage_scheme: enums::MerchantStorageScheme,
|
||||||
@ -332,7 +329,7 @@ pub async fn get_domain_address_for_payments(
|
|||||||
.async_lift(|inner| types::encrypt_optional(inner, key))
|
.async_lift(|inner| types::encrypt_optional(inner, key))
|
||||||
.await?,
|
.await?,
|
||||||
country_code: address.phone.as_ref().and_then(|a| a.country_code.clone()),
|
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(),
|
merchant_id: merchant_id.to_string(),
|
||||||
address_id: generate_id(consts::ID_LENGTH, "add"),
|
address_id: generate_id(consts::ID_LENGTH, "add"),
|
||||||
city: address_details.city,
|
city: address_details.city,
|
||||||
@ -763,25 +760,14 @@ fn validate_new_mandate_request(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn validate_customer_id_mandatory_cases(
|
pub fn validate_customer_id_mandatory_cases(
|
||||||
has_shipping: bool,
|
|
||||||
has_billing: bool,
|
|
||||||
has_setup_future_usage: bool,
|
has_setup_future_usage: bool,
|
||||||
customer_id: &Option<String>,
|
customer_id: &Option<String>,
|
||||||
) -> RouterResult<()> {
|
) -> RouterResult<()> {
|
||||||
match (
|
match (has_setup_future_usage, customer_id) {
|
||||||
has_shipping,
|
(true, None) => Err(errors::ApiErrorResponse::PreconditionFailed {
|
||||||
has_billing,
|
message: "customer_id is mandatory when setup_future_usage is given".to_string(),
|
||||||
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()
|
.into_report(),
|
||||||
}
|
|
||||||
_ => Ok(()),
|
_ => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -130,8 +130,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
|
|||||||
amount = payment_attempt.amount.into();
|
amount = payment_attempt.amount.into();
|
||||||
|
|
||||||
helpers::validate_customer_id_mandatory_cases(
|
helpers::validate_customer_id_mandatory_cases(
|
||||||
request.shipping.is_some(),
|
|
||||||
request.billing.is_some(),
|
|
||||||
request.setup_future_usage.is_some(),
|
request.setup_future_usage.is_some(),
|
||||||
&payment_intent
|
&payment_intent
|
||||||
.customer_id
|
.customer_id
|
||||||
|
|||||||
@ -139,8 +139,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
|
|||||||
amount = payment_attempt.amount.into();
|
amount = payment_attempt.amount.into();
|
||||||
|
|
||||||
helpers::validate_customer_id_mandatory_cases(
|
helpers::validate_customer_id_mandatory_cases(
|
||||||
request.shipping.is_some(),
|
|
||||||
request.billing.is_some(),
|
|
||||||
request.setup_future_usage.is_some(),
|
request.setup_future_usage.is_some(),
|
||||||
&payment_intent
|
&payment_intent
|
||||||
.customer_id
|
.customer_id
|
||||||
|
|||||||
@ -284,8 +284,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
|
|||||||
amount = payment_attempt.amount.into();
|
amount = payment_attempt.amount.into();
|
||||||
|
|
||||||
helpers::validate_customer_id_mandatory_cases(
|
helpers::validate_customer_id_mandatory_cases(
|
||||||
request.shipping.is_some(),
|
|
||||||
request.billing.is_some(),
|
|
||||||
request.setup_future_usage.is_some(),
|
request.setup_future_usage.is_some(),
|
||||||
&payment_intent
|
&payment_intent
|
||||||
.customer_id
|
.customer_id
|
||||||
|
|||||||
@ -538,8 +538,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve> ValidateRequest<F, api::Paymen
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
helpers::validate_customer_id_mandatory_cases(
|
helpers::validate_customer_id_mandatory_cases(
|
||||||
request.shipping.is_some(),
|
|
||||||
request.billing.is_some(),
|
|
||||||
request.setup_future_usage.is_some(),
|
request.setup_future_usage.is_some(),
|
||||||
&request
|
&request
|
||||||
.customer
|
.customer
|
||||||
|
|||||||
@ -146,8 +146,6 @@ impl<F: Send + Clone, Ctx: PaymentMethodRetrieve>
|
|||||||
|
|
||||||
if request.confirm.unwrap_or(false) {
|
if request.confirm.unwrap_or(false) {
|
||||||
helpers::validate_customer_id_mandatory_cases(
|
helpers::validate_customer_id_mandatory_cases(
|
||||||
request.shipping.is_some(),
|
|
||||||
request.billing.is_some(),
|
|
||||||
request.setup_future_usage.is_some(),
|
request.setup_future_usage.is_some(),
|
||||||
&payment_intent
|
&payment_intent
|
||||||
.customer_id
|
.customer_id
|
||||||
|
|||||||
@ -763,7 +763,8 @@ impl AddressInterface for MockDb {
|
|||||||
.await
|
.await
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.find(|address| {
|
.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| {
|
.map(|a| {
|
||||||
let address_updated =
|
let address_updated =
|
||||||
|
|||||||
@ -35,7 +35,7 @@ pub struct Address {
|
|||||||
#[serde(skip_serializing)]
|
#[serde(skip_serializing)]
|
||||||
#[serde(with = "custom_serde::iso8601")]
|
#[serde(with = "custom_serde::iso8601")]
|
||||||
pub modified_at: PrimitiveDateTime,
|
pub modified_at: PrimitiveDateTime,
|
||||||
pub customer_id: String,
|
pub customer_id: Option<String>,
|
||||||
pub merchant_id: String,
|
pub merchant_id: String,
|
||||||
pub payment_id: Option<String>,
|
pub payment_id: Option<String>,
|
||||||
pub updated_by: String,
|
pub updated_by: String,
|
||||||
|
|||||||
@ -566,7 +566,7 @@ impl CustomerAddress for api_models::customers::CustomerRequest {
|
|||||||
.async_lift(|inner| encrypt_optional(inner, key))
|
.async_lift(|inner| encrypt_optional(inner, key))
|
||||||
.await?,
|
.await?,
|
||||||
country_code: self.phone_country_code.clone(),
|
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(),
|
merchant_id: merchant_id.to_string(),
|
||||||
address_id: generate_id(consts::ID_LENGTH, "add"),
|
address_id: generate_id(consts::ID_LENGTH, "add"),
|
||||||
payment_id: None,
|
payment_id: None,
|
||||||
|
|||||||
@ -0,0 +1,2 @@
|
|||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
ALTER TABLE address ALTER COLUMN customer_id SET NOT NULL;
|
||||||
@ -0,0 +1,2 @@
|
|||||||
|
-- Your SQL goes here
|
||||||
|
ALTER TABLE address ALTER COLUMN customer_id DROP NOT NULL;
|
||||||
Reference in New Issue
Block a user