mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-11-01 19:42:27 +08:00
refactor(core): accept customer data in customer object (#1447)
Co-authored-by: Abhishek Marrivagu <68317979+Abhicodes-crypto@users.noreply.github.com>
This commit is contained in:
@ -96,7 +96,7 @@ pub async fn get_address_for_payment_request(
|
||||
req_address: Option<&api::Address>,
|
||||
address_id: Option<&str>,
|
||||
merchant_id: &str,
|
||||
customer_id: &Option<String>,
|
||||
customer_id: Option<&String>,
|
||||
) -> CustomResult<Option<domain::Address>, errors::ApiErrorResponse> {
|
||||
let key = types::get_merchant_enc_key(db, merchant_id.to_string())
|
||||
.await
|
||||
@ -179,7 +179,7 @@ pub async fn get_address_for_payment_request(
|
||||
}
|
||||
None => {
|
||||
// generate a new address here
|
||||
let customer_id = customer_id.as_deref().get_required_value("customer_id")?;
|
||||
let customer_id = customer_id.get_required_value("customer_id")?;
|
||||
|
||||
let address_details = address.address.clone().unwrap_or_default();
|
||||
Some(
|
||||
@ -460,7 +460,7 @@ fn validate_new_mandate_request(
|
||||
is_confirm_operation: bool,
|
||||
) -> RouterResult<()> {
|
||||
// We need not check for customer_id in the confirm request if it is already passed
|
||||
//in create request
|
||||
// in create request
|
||||
|
||||
fp_utils::when(!is_confirm_operation && req.customer_id.is_none(), || {
|
||||
Err(report!(errors::ApiErrorResponse::PreconditionFailed {
|
||||
@ -810,6 +810,109 @@ pub async fn get_customer_from_details<F: Clone>(
|
||||
}
|
||||
}
|
||||
|
||||
// Checks if the inner values of two options are not equal and throws appropriate error
|
||||
fn validate_options_for_inequality<T: PartialEq>(
|
||||
first_option: Option<&T>,
|
||||
second_option: Option<&T>,
|
||||
field_name: &str,
|
||||
) -> Result<(), errors::ApiErrorResponse> {
|
||||
fp_utils::when(
|
||||
first_option
|
||||
.zip(second_option)
|
||||
.map(|(value1, value2)| value1 != value2)
|
||||
.unwrap_or(false),
|
||||
|| {
|
||||
Err(errors::ApiErrorResponse::PreconditionFailed {
|
||||
message: format!("The field name `{field_name}` sent in both places is ambiguous"),
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// Checks if the customer details are passed in both places
|
||||
// If so, raise an error
|
||||
pub fn validate_customer_details_in_request(
|
||||
request: &api_models::payments::PaymentsRequest,
|
||||
) -> Result<(), errors::ApiErrorResponse> {
|
||||
if let Some(customer_details) = request.customer.as_ref() {
|
||||
validate_options_for_inequality(
|
||||
request.customer_id.as_ref(),
|
||||
Some(&customer_details.id),
|
||||
"customer_id",
|
||||
)?;
|
||||
|
||||
validate_options_for_inequality(
|
||||
request.email.as_ref(),
|
||||
customer_details.email.as_ref(),
|
||||
"email",
|
||||
)?;
|
||||
|
||||
validate_options_for_inequality(
|
||||
request.name.as_ref(),
|
||||
customer_details.name.as_ref(),
|
||||
"name",
|
||||
)?;
|
||||
|
||||
validate_options_for_inequality(
|
||||
request.phone.as_ref(),
|
||||
customer_details.phone.as_ref(),
|
||||
"phone",
|
||||
)?;
|
||||
|
||||
validate_options_for_inequality(
|
||||
request.phone_country_code.as_ref(),
|
||||
customer_details.phone_country_code.as_ref(),
|
||||
"phone_country_code",
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the customer details from customer field if present
|
||||
/// or from the individual fields in `PaymentsRequest`
|
||||
pub fn get_customer_details_from_request(
|
||||
request: &api_models::payments::PaymentsRequest,
|
||||
) -> CustomerDetails {
|
||||
let customer_id = request
|
||||
.customer
|
||||
.as_ref()
|
||||
.map(|customer_details| customer_details.id.clone())
|
||||
.or(request.customer_id.clone());
|
||||
|
||||
let customer_name = request
|
||||
.customer
|
||||
.as_ref()
|
||||
.and_then(|customer_details| customer_details.name.clone())
|
||||
.or(request.name.clone());
|
||||
|
||||
let customer_email = request
|
||||
.customer
|
||||
.as_ref()
|
||||
.and_then(|customer_details| customer_details.email.clone())
|
||||
.or(request.email.clone());
|
||||
|
||||
let customer_phone = request
|
||||
.customer
|
||||
.as_ref()
|
||||
.and_then(|customer_details| customer_details.phone.clone())
|
||||
.or(request.phone.clone());
|
||||
|
||||
let customer_phone_code = request
|
||||
.customer
|
||||
.as_ref()
|
||||
.and_then(|customer_details| customer_details.phone_country_code.clone())
|
||||
.or(request.phone_country_code.clone());
|
||||
|
||||
CustomerDetails {
|
||||
customer_id,
|
||||
name: customer_name,
|
||||
email: customer_email,
|
||||
phone: customer_phone,
|
||||
phone_country_code: customer_phone_code,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_connector_default(
|
||||
_state: &AppState,
|
||||
request_connector: Option<serde_json::Value>,
|
||||
|
||||
@ -80,7 +80,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsCancelRequest>
|
||||
None,
|
||||
payment_intent.shipping_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
let billing_address = helpers::get_address_for_payment_request(
|
||||
@ -88,7 +88,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsCancelRequest>
|
||||
None,
|
||||
payment_intent.billing_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
|
||||
@ -99,7 +99,7 @@ impl<F: Send + Clone> GetTracker<F, payments::PaymentData<F>, api::PaymentsCaptu
|
||||
None,
|
||||
payment_intent.shipping_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
@ -108,7 +108,7 @@ impl<F: Send + Clone> GetTracker<F, payments::PaymentData<F>, api::PaymentsCaptu
|
||||
None,
|
||||
payment_intent.billing_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
|
||||
@ -135,7 +135,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Co
|
||||
request.shipping.as_ref(),
|
||||
payment_intent.shipping_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
let billing_address = helpers::get_address_for_payment_request(
|
||||
@ -143,7 +143,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Co
|
||||
request.billing.as_ref(),
|
||||
payment_intent.billing_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
|
||||
@ -122,6 +122,8 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
field_name: "browser_info",
|
||||
})?;
|
||||
|
||||
let customer_details = helpers::get_customer_details_from_request(request);
|
||||
|
||||
let token = token.or_else(|| payment_attempt.payment_token.clone());
|
||||
|
||||
helpers::validate_pm_or_token_given(
|
||||
@ -159,7 +161,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
&payment_intent
|
||||
.customer_id
|
||||
.clone()
|
||||
.or_else(|| request.customer_id.clone()),
|
||||
.or_else(|| customer_details.customer_id.clone()),
|
||||
)?;
|
||||
|
||||
let shipping_address = helpers::get_address_for_payment_request(
|
||||
@ -167,7 +169,10 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
request.shipping.as_ref(),
|
||||
payment_intent.shipping_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent
|
||||
.customer_id
|
||||
.as_ref()
|
||||
.or(customer_details.customer_id.as_ref()),
|
||||
)
|
||||
.await?;
|
||||
let billing_address = helpers::get_address_for_payment_request(
|
||||
@ -175,7 +180,10 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
request.billing.as_ref(),
|
||||
payment_intent.billing_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent
|
||||
.customer_id
|
||||
.as_ref()
|
||||
.or(customer_details.customer_id.as_ref()),
|
||||
)
|
||||
.await?;
|
||||
|
||||
@ -255,13 +263,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
ephemeral_key: None,
|
||||
redirect_response: None,
|
||||
},
|
||||
Some(CustomerDetails {
|
||||
customer_id: request.customer_id.clone(),
|
||||
name: request.name.clone(),
|
||||
email: request.email.clone(),
|
||||
phone: request.phone.clone(),
|
||||
phone_country_code: request.phone_country_code.clone(),
|
||||
}),
|
||||
Some(customer_details),
|
||||
))
|
||||
}
|
||||
}
|
||||
@ -472,6 +474,9 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentConfir
|
||||
{
|
||||
Err(errors::ApiErrorResponse::NotSupported { message: "order_details cannot be present both inside and outside metadata in payments request".to_string() })?
|
||||
}
|
||||
|
||||
helpers::validate_customer_details_in_request(request)?;
|
||||
|
||||
let given_payment_id = match &request.payment_id {
|
||||
Some(id_type) => Some(
|
||||
id_type
|
||||
|
||||
@ -74,12 +74,14 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
)
|
||||
.await?;
|
||||
|
||||
let customer_details = helpers::get_customer_details_from_request(request);
|
||||
|
||||
let shipping_address = helpers::get_address_for_payment_request(
|
||||
db,
|
||||
request.shipping.as_ref(),
|
||||
None,
|
||||
merchant_id,
|
||||
&request.customer_id,
|
||||
customer_details.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
@ -88,7 +90,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
request.billing.as_ref(),
|
||||
None,
|
||||
merchant_id,
|
||||
&request.customer_id,
|
||||
customer_details.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
@ -256,13 +258,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
ephemeral_key,
|
||||
redirect_response: None,
|
||||
},
|
||||
Some(CustomerDetails {
|
||||
customer_id: request.customer_id.clone(),
|
||||
name: request.name.clone(),
|
||||
email: request.email.clone(),
|
||||
phone: request.phone.clone(),
|
||||
phone_country_code: request.phone_country_code.clone(),
|
||||
}),
|
||||
Some(customer_details),
|
||||
))
|
||||
}
|
||||
}
|
||||
@ -423,6 +419,9 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentCreate
|
||||
{
|
||||
Err(errors::ApiErrorResponse::NotSupported { message: "order_details cannot be present both inside and outside metadata in payments request".to_string() })?
|
||||
}
|
||||
|
||||
helpers::validate_customer_details_in_request(request)?;
|
||||
|
||||
let given_payment_id = match &request.payment_id {
|
||||
Some(id_type) => Some(
|
||||
id_type
|
||||
|
||||
@ -87,7 +87,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsSessionRequest>
|
||||
None,
|
||||
payment_intent.shipping_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
@ -96,7 +96,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsSessionRequest>
|
||||
None,
|
||||
payment_intent.billing_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
|
||||
@ -81,7 +81,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsStartRequest> f
|
||||
None,
|
||||
payment_intent.shipping_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
let billing_address = helpers::get_address_for_payment_request(
|
||||
@ -89,7 +89,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsStartRequest> f
|
||||
None,
|
||||
payment_intent.billing_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent.customer_id.as_ref(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
|
||||
@ -107,6 +107,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
};
|
||||
|
||||
payment_attempt.payment_method = payment_method_type.or(payment_attempt.payment_method);
|
||||
let customer_details = helpers::get_customer_details_from_request(request);
|
||||
|
||||
let amount = request
|
||||
.amount
|
||||
@ -120,7 +121,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
&payment_intent
|
||||
.customer_id
|
||||
.clone()
|
||||
.or_else(|| request.customer_id.clone()),
|
||||
.or_else(|| customer_details.customer_id.clone()),
|
||||
)?;
|
||||
}
|
||||
|
||||
@ -129,7 +130,10 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
request.shipping.as_ref(),
|
||||
payment_intent.shipping_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent
|
||||
.customer_id
|
||||
.as_ref()
|
||||
.or(customer_details.customer_id.as_ref()),
|
||||
)
|
||||
.await?;
|
||||
let billing_address = helpers::get_address_for_payment_request(
|
||||
@ -137,7 +141,10 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
request.billing.as_ref(),
|
||||
payment_intent.billing_address_id.as_deref(),
|
||||
merchant_id,
|
||||
&payment_intent.customer_id,
|
||||
payment_intent
|
||||
.customer_id
|
||||
.as_ref()
|
||||
.or(customer_details.customer_id.as_ref()),
|
||||
)
|
||||
.await?;
|
||||
|
||||
@ -281,6 +288,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
.clone()
|
||||
.map(ForeignInto::foreign_into)),
|
||||
});
|
||||
|
||||
Ok((
|
||||
next_operation,
|
||||
PaymentData {
|
||||
@ -312,13 +320,7 @@ impl<F: Send + Clone> GetTracker<F, PaymentData<F>, api::PaymentsRequest> for Pa
|
||||
ephemeral_key: None,
|
||||
redirect_response: None,
|
||||
},
|
||||
Some(CustomerDetails {
|
||||
customer_id: request.customer_id.clone(),
|
||||
name: request.name.clone(),
|
||||
email: request.email.clone(),
|
||||
phone: request.phone.clone(),
|
||||
phone_country_code: request.phone_country_code.clone(),
|
||||
}),
|
||||
Some(customer_details),
|
||||
))
|
||||
}
|
||||
}
|
||||
@ -526,6 +528,9 @@ impl<F: Send + Clone> ValidateRequest<F, api::PaymentsRequest> for PaymentUpdate
|
||||
{
|
||||
Err(errors::ApiErrorResponse::NotSupported { message: "order_details cannot be present both inside and outside metadata in payments request".to_string() })?
|
||||
}
|
||||
|
||||
helpers::validate_customer_details_in_request(request)?;
|
||||
|
||||
let given_payment_id = match &request.payment_id {
|
||||
Some(id_type) => Some(
|
||||
id_type
|
||||
|
||||
@ -241,6 +241,7 @@ Never share your secret api keys. Keep them guarded and secure.
|
||||
api_models::mandates::MandateResponse,
|
||||
api_models::mandates::MandateCardDetails,
|
||||
api_models::ephemeral_key::EphemeralKeyCreateResponse,
|
||||
api_models::payments::CustomerDetails,
|
||||
crate::types::api::admin::MerchantAccountResponse,
|
||||
crate::types::api::admin::MerchantConnectorId,
|
||||
crate::types::api::admin::MerchantDetails,
|
||||
|
||||
Reference in New Issue
Block a user