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

feat(core): Make customer id optional for guest customer flows (#11138)

Sayak Bhattacharyac3824fe88238
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.json
index 928155dd7c..20d3970701 100644
--- a/api-reference/v1/openapi_spec_v1.json
+++ b/api-reference/v1/openapi_spec_v1.json
@@ -16627,14 +16627,12 @@
"CustomerDetails": {
"type": "object",
"description": "Passing this object creates a new customer or attaches an existing customer to the payment",
- "required": [
- "id"
- ],
"properties": {
"id": {
"type": "string",
"description": "The identifier for the customer.",
"example": "cus_y3oqhf46pyzuxjbcn2giaqnb44",
+ "nullable": true,
"maxLength": 64,
"minLength": 1
},
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index b1b4c5e225..37e79a2f8a 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -10613,14 +10613,12 @@
"CustomerDetails": {
"type": "object",
"description": "Passing this object creates a new customer or attaches an existing customer to the payment",
- "required": [
- "id"
- ],
"properties": {
"id": {
"type": "string",
"description": "The identifier for the customer.",
"example": "cus_y3oqhf46pyzuxjbcn2giaqnb44",
+ "nullable": true,
"maxLength": 64,
"minLength": 1
},
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs
index c4b7dd6ab7..bab7d2c5fc 100644
--- a/crates/api_models/src/payments.rs
+++ b/crates/api_models/src/payments.rs
@@ -124,9 +124,9 @@ pub struct BankCodeResponse {
#[smithy(namespace = "com.hyperswitch.smithy.types")]
pub struct CustomerDetails {
/// The identifier for the customer.
- #[schema(value_type = String, max_length = 64, min_length = 1, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")]
- #[smithy(value_type = "String")]
- pub id: id_type::CustomerId,
+ #[schema(value_type = Option<String>, max_length = 64, min_length = 1, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")]
+ #[smithy(value_type = "Option<String>")]
+ pub id: Option<id_type::CustomerId>,
/// The customer's name
#[schema(max_length = 255, value_type = Option<String>, example = "John Doe")]
@@ -1596,9 +1596,11 @@ impl PaymentsRequest {
/// First check the id for `customer.id`
/// If not present, check for `customer_id` at the root level
pub fn get_customer_id(&self) -> Option<&id_type::CustomerId> {
- self.customer_id
- .as_ref()
- .or(self.customer.as_ref().map(|customer| &customer.id))
+ self.customer_id.as_ref().or_else(|| {
+ self.customer
+ .as_ref()
+ .and_then(|customer| customer.id.as_ref())
+ })
}
pub fn validate_and_get_request_extended_authorization(
@@ -1627,7 +1629,7 @@ impl PaymentsRequest {
}) = self.customer.as_ref()
{
let invalid_fields = [
- are_optional_values_invalid(self.customer_id.as_ref(), Some(id))
+ are_optional_values_invalid(self.customer_id.as_ref(), id.as_ref())
.then_some("customer_id and customer.id"),
are_optional_values_invalid(self.email.as_ref(), email.as_ref())
.then_some("email and customer.email"),
@@ -1764,7 +1766,7 @@ mod payments_request_test {
let customer_id = generate_customer_id_of_default_length();
let customer_object = CustomerDetails {
- id: customer_id.clone(),
+ id: Some(customer_id.clone()),
name: None,
email: None,
phone: None,
@@ -1789,7 +1791,7 @@ mod payments_request_test {
let another_customer_id = generate_customer_id_of_default_length();
let customer_object = CustomerDetails {
- id: customer_id.clone(),
+ id: Some(customer_id.clone()),
name: None,
email: None,
phone: None,
@@ -9058,9 +9060,9 @@ impl From<&PaymentsRequest> for MandateValidationFields {
customer_id: req
.customer
.as_ref()
- .map(|customer_details| &customer_details.id)
+ .and_then(|customer_details| customer_details.id.as_ref())
.or(req.customer_id.as_ref())
- .map(ToOwned::to_owned),
+ .cloned(),
mandate_data: req.mandate_data.clone(),
setup_future_usage: req.setup_future_usage,
off_session: req.off_session,
diff --git a/crates/api_models/src/payouts.rs b/crates/api_models/src/payouts.rs
index 8c2a45304a..7597742521 100644
--- a/crates/api_models/src/payouts.rs
+++ b/crates/api_models/src/payouts.rs
@@ -204,9 +204,10 @@ pub struct PayoutCreateRequest {
impl PayoutCreateRequest {
pub fn get_customer_id(&self) -> Option<&id_type::CustomerId> {
- self.customer_id
+ self.customer_id.as_ref().or(self
+ .customer
.as_ref()
- .or(self.customer.as_ref().map(|customer| &customer.id))
+ .and_then(|customer| customer.id.as_ref()))
}
}
diff --git a/crates/hyperswitch_domain_models/src/bulk_tokenization.rs b/crates/hyperswitch_domain_models/src/bulk_tokenization.rs
index cc4a070c1e..524d5ad772 100644
--- a/crates/hyperswitch_domain_models/src/bulk_tokenization.rs
+++ b/crates/hyperswitch_domain_models/src/bulk_tokenization.rs
@@ -104,7 +104,7 @@ pub struct CardNetworkTokenizeRecord {
impl ForeignFrom<&CardNetworkTokenizeRecord> for payments_api::CustomerDetails {
fn foreign_from(record: &CardNetworkTokenizeRecord) -> Self {
Self {
- id: record.customer_id.clone(),
+ id: Some(record.customer_id.clone()),
name: record.customer_name.clone(),
email: record.customer_email.clone(),
phone: record.customer_phone.clone(),
@@ -219,7 +219,7 @@ impl ForeignTryFrom<CustomerDetails> for payments_api::CustomerDetails {
type Error = error_stack::Report<errors::ValidationError>;
fn foreign_try_from(customer: CustomerDetails) -> Result<Self, Self::Error> {
Ok(Self {
- id: customer.customer_id.get_required_value("customer_id")?,
+ id: Some(customer.customer_id.get_required_value("customer_id")?),
name: customer.name,
email: customer.email,
phone: customer.phone,
@@ -272,7 +272,7 @@ impl ForeignFrom<payment_methods_api::TokenizeDataRequest> for TokenizeDataReque
impl ForeignFrom<payments_api::CustomerDetails> for CustomerDetails {
fn foreign_from(req: payments_api::CustomerDetails) -> Self {
Self {
- customer_id: Some(req.id),
+ customer_id: req.id,
name: req.name,
email: req.email,
phone: req.phone,
diff --git a/crates/payment_methods/src/client.rs b/crates/payment_methods/src/client.rs
index 428e94561e..96603794f3 100644
--- a/crates/payment_methods/src/client.rs
+++ b/crates/payment_methods/src/client.rs
@@ -57,7 +57,7 @@ impl<'a> PaymentMethodClient<'a> {
}
}
-impl<'a> MicroserviceClient for PaymentMethodClient<'a> {
+impl MicroserviceClient for PaymentMethodClient<'_> {
fn base_url(&self) -> &url::Url {
self.base_url.as_ref()
}
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index 1bd71f49b7..066de2ca90 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -5276,14 +5276,21 @@ pub async fn execute_card_tokenization(
let domain_card = optional_card
.get_required_value("card")
.change_context(errors::ApiErrorResponse::InternalServerError)?;
+ let customer_id =
+ customer
+ .id
+ .as_ref()
+ .ok_or(errors::ApiErrorResponse::MissingRequiredField {
+ field_name: "customer_id",
+ })?;
let network_token_details = executor
- .tokenize_card(&customer.id, &domain_card, optional_cvc)
+ .tokenize_card(customer_id, &domain_card, optional_cvc)
.await?;
let builder = builder.set_token_details(&network_token_details);
// Store card and token in locker
let store_card_and_token_resp = executor
- .store_card_and_token_in_locker(&network_token_details, &domain_card, &customer.id)
+ .store_card_and_token_in_locker(&network_token_details, &domain_card, customer_id)
.await?;
let builder = builder.set_stored_card_response(&store_card_and_token_resp);
let builder = builder.set_stored_token_response(&store_card_and_token_resp);
@@ -5294,7 +5301,7 @@ pub async fn execute_card_tokenization(
&store_card_and_token_resp,
&network_token_details,
&domain_card,
- &customer.id,
+ customer_id,
)
.await?;
let builder = builder.set_payment_method_response(&payment_method);
@@ -5320,10 +5327,18 @@ pub async fn execute_payment_method_tokenization(
.await?;
let builder = builder.set_validate_result(&customer);
+ let customer_id =
+ customer
+ .id
+ .as_ref()
+ .ok_or(errors::ApiErrorResponse::MissingRequiredField {
+ field_name: "customer_id",
+ })?;
+
// Fetch card from locker
let card_details = get_card_from_locker(
executor.state,
- &customer.id,
+ customer_id,
executor.merchant_account.get_id(),
&locker_id,
)
@@ -5346,7 +5361,7 @@ pub async fn execute_payment_method_tokenization(
let (optional_card, optional_cvc) = builder.get_optional_card_and_cvc();
let domain_card = optional_card.get_required_value("card")?;
let network_token_details = executor
- .tokenize_card(&customer.id, &domain_card, optional_cvc)
+ .tokenize_card(customer_id, &domain_card, optional_cvc)
.await?;
let builder = builder.set_token_details(&network_token_details);
@@ -5354,7 +5369,7 @@ pub async fn execute_payment_method_tokenization(
let store_token_resp = executor
.store_network_token_in_locker(
&network_token_details,
- &customer.id,
+ customer_id,
card_details.name_on_card.clone(),
card_details.nick_name.clone().map(Secret::new),
)
diff --git a/crates/router/src/core/payment_methods/tokenize/card_executor.rs b/crates/router/src/core/payment_methods/tokenize/card_executor.rs
index 7b4114d1d2..5b8b2f7c5f 100644
--- a/crates/router/src/core/payment_methods/tokenize/card_executor.rs
+++ b/crates/router/src/core/payment_methods/tokenize/card_executor.rs
@@ -357,7 +357,7 @@ impl CardNetworkTokenizeExecutor<'_, domain::TokenizeCardRequest> {
// If found, send back CustomerDetails from DB
|optional_customer| {
Ok(optional_customer.map(|customer| api::CustomerDetails {
- id: customer.customer_id.clone(),
+ id: Some(customer.customer_id.clone()),
name: customer.name.clone().map(|name| name.into_inner()),
email: customer.email.clone().map(Email::from),
phone: customer.phone.clone().map(|phone| phone.into_inner()),
@@ -457,7 +457,7 @@ impl CardNetworkTokenizeExecutor<'_, domain::TokenizeCardRequest> {
})?;
Ok(api::CustomerDetails {
- id: new_customer_id,
+ id: Some(new_customer_id),
name: self.customer.name.clone(),
email: self.customer.email.clone(),
phone: self.customer.phone.clone(),
diff --git a/crates/router/src/core/payment_methods/tokenize/payment_method_executor.rs b/crates/router/src/core/payment_methods/tokenize/payment_method_executor.rs
index 777c675f36..f104a9fdf4 100644
--- a/crates/router/src/core/payment_methods/tokenize/payment_method_executor.rs
+++ b/crates/router/src/core/payment_methods/tokenize/payment_method_executor.rs
@@ -367,7 +367,7 @@ impl CardNetworkTokenizeExecutor<'_, domain::TokenizePaymentMethodRequest> {
.change_context(errors::ApiErrorResponse::InternalServerError)?;
let customer_details = api::CustomerDetails {
- id: customer.customer_id.clone(),
+ id: Some(customer.customer_id.clone()),
name: customer.name.clone().map(|name| name.into_inner()),
email: customer.email.clone().map(Email::from),
phone: customer.phone.clone().map(|phone| phone.into_inner()),
diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs
index b48238032c..dc8abb8de4 100644
--- a/crates/router/src/core/payments/operations/payment_create.rs
+++ b/crates/router/src/core/payments/operations/payment_create.rs
@@ -1044,8 +1044,7 @@ impl<F: Send + Clone + Sync> ValidateRequest<F, api::PaymentsRequest, PaymentDat
request.customer_id.as_ref().or(request
.customer
.as_ref()
- .map(|customer| customer.id.clone())
- .as_ref()),
+ .and_then(|customer| customer.id.as_ref())),
)?;
}
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index c1ed5f8f96..4a4aca2e2e 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -6334,7 +6334,7 @@ impl ForeignFrom<payments::FraudCheck> for FrmMessage {
impl ForeignFrom<CustomerDetails> for router_request_types::CustomerDetails {
fn foreign_from(customer: CustomerDetails) -> Self {
Self {
- customer_id: Some(customer.id),
+ customer_id: customer.id,
name: customer.name,
email: customer.email,
phone: customer.phone,
diff --git a/crates/router/src/core/unified_authentication_service.rs b/crates/router/src/core/unified_authentication_service.rs
index e391bc7e31..86d6bb2575 100644
--- a/crates/router/src/core/unified_authentication_service.rs
+++ b/crates/router/src/core/unified_authentication_service.rs
@@ -873,7 +873,7 @@ pub async fn authentication_create_core(
acquirer_details,
new_authentication.profile_acquirer_id,
customer_details,
- req.customer_details.map(|details| details.id.clone()),
+ req.customer_details.and_then(|details| details.id.clone()),
))?;
Ok(hyperswitch_domain_models::api::ApplicationResponse::Json(
@@ -928,7 +928,7 @@ impl
.ok_or(ApiErrorResponse::InternalServerError)
.attach_printable("Customer id not found in authentication create request")?;
Some(CustomerDetails {
- id: customer_id.clone(),
+ id: Some(customer_id.clone()),
name: details.name,
email: details.email,
phone: details.phone,
diff --git a/crates/router/src/routes/payments.rs b/crates/router/src/routes/payments.rs
index bc11637f4d..09c68ed9fb 100644
--- a/crates/router/src/routes/payments.rs
+++ b/crates/router/src/routes/payments.rs
@@ -2586,11 +2586,11 @@ impl GetLockingInput for payment_types::PaymentsRequest {
api_identifier: api_identifier.clone(),
override_lock_retries: None,
};
- if let Some(customer_id) = self
- .customer_id
- .as_ref()
- .or(self.customer.as_ref().map(|customer| &customer.id))
- {
+ if let Some(customer_id) = self.customer_id.as_ref().or_else(|| {
+ self.customer
+ .as_ref()
+ .and_then(|customer| customer.id.as_ref())
+ }) {
api_locking::LockAction::HoldMultiple {
inputs: vec![
intent_id_locking_input,