mirror of
				https://github.com/juspay/hyperswitch.git
				synced 2025-11-01 02:57:02 +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 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, | ||||
|  | ||||
| @ -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] | ||||
|  | ||||
| @ -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(), | ||||
|     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() | ||||
|         } | ||||
|         .into_report(), | ||||
|         _ => Ok(()), | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -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 = | ||||
|  | ||||
| @ -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, | ||||
|  | ||||
| @ -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, | ||||
|  | ||||
| @ -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
	 Prasunna Soppa
					Prasunna Soppa