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(payment-methods): add support for session create api and customer pml for guest checkout (#10334)

Prasunna Soppa1164cd8214c7
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index bbb9438a9f..f42acd7542 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -19320,14 +19320,12 @@
},
"PaymentMethodSessionRequest": {
"type": "object",
- "required": [
- "customer_id"
- ],
"properties": {
"customer_id": {
"type": "string",
"description": "The customer id for which the payment methods session is to be created",
- "example": "cus_y3oqhf46pyzuxjbcn2giaqnb44"
+ "example": "cus_y3oqhf46pyzuxjbcn2giaqnb44",
+ "nullable": true
},
"billing": {
"allOf": [
@@ -19377,7 +19375,6 @@
"type": "object",
"required": [
"id",
- "customer_id",
"expires_at",
"client_secret"
],
@@ -19389,7 +19386,8 @@
"customer_id": {
"type": "string",
"description": "The customer id for which the payment methods session is to be created",
- "example": "12345_cus_01926c58bc6e77c09e809964e72af8c8"
+ "example": "12345_cus_01926c58bc6e77c09e809964e72af8c8",
+ "nullable": true
},
"billing": {
"allOf": [
diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rs
index 01a5a6514b..9b94900077 100644
--- a/crates/api_models/src/payment_methods.rs
+++ b/crates/api_models/src/payment_methods.rs
@@ -3217,8 +3217,8 @@ impl From<&Card> for MigrateCardDetail {
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, ToSchema)]
pub struct PaymentMethodSessionRequest {
/// The customer id for which the payment methods session is to be created
- #[schema(value_type = String, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")]
- pub customer_id: id_type::GlobalCustomerId,
+ #[schema(value_type = Option<String>, example = "cus_y3oqhf46pyzuxjbcn2giaqnb44")]
+ pub customer_id: Option<id_type::GlobalCustomerId>,
/// The billing address details of the customer. This will also be used for any new payment methods added during the session
#[schema(value_type = Option<Address>)]
@@ -3313,8 +3313,8 @@ pub struct PaymentMethodSessionResponse {
pub id: id_type::GlobalPaymentMethodSessionId,
/// The customer id for which the payment methods session is to be created
- #[schema(value_type = String, example = "12345_cus_01926c58bc6e77c09e809964e72af8c8")]
- pub customer_id: id_type::GlobalCustomerId,
+ #[schema(value_type = Option<String>, example = "12345_cus_01926c58bc6e77c09e809964e72af8c8")]
+ pub customer_id: Option<id_type::GlobalCustomerId>,
/// The billing address details of the customer. This will also be used for any new payment methods added during the session
#[schema(value_type = Option<Address>)]
diff --git a/crates/diesel_models/src/payment_methods_session.rs b/crates/diesel_models/src/payment_methods_session.rs
index b91daa5473..05a20ffd19 100644
--- a/crates/diesel_models/src/payment_methods_session.rs
+++ b/crates/diesel_models/src/payment_methods_session.rs
@@ -4,7 +4,7 @@ use common_utils::pii;
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct PaymentMethodSession {
pub id: common_utils::id_type::GlobalPaymentMethodSessionId,
- pub customer_id: common_utils::id_type::GlobalCustomerId,
+ pub customer_id: Option<common_utils::id_type::GlobalCustomerId>,
pub billing: Option<common_utils::encryption::Encryption>,
pub psp_tokenization: Option<common_types::payment_methods::PspTokenization>,
pub network_tokenization: Option<common_types::payment_methods::NetworkTokenization>,
diff --git a/crates/hyperswitch_domain_models/src/payment_methods.rs b/crates/hyperswitch_domain_models/src/payment_methods.rs
index ad86ab72f5..bdf3dab705 100644
--- a/crates/hyperswitch_domain_models/src/payment_methods.rs
+++ b/crates/hyperswitch_domain_models/src/payment_methods.rs
@@ -733,7 +733,7 @@ impl super::behaviour::Conversion for PaymentMethod {
#[derive(Clone, Debug, router_derive::ToEncryption)]
pub struct PaymentMethodSession {
pub id: id_type::GlobalPaymentMethodSessionId,
- pub customer_id: id_type::GlobalCustomerId,
+ pub customer_id: Option<id_type::GlobalCustomerId>,
#[encrypt(ty = Value)]
pub billing: Option<Encryptable<Address>>,
pub return_url: Option<common_utils::types::Url>,
diff --git a/crates/router/src/core/payment_methods.rs b/crates/router/src/core/payment_methods.rs
index befb7debbe..0e99e477f7 100644
--- a/crates/router/src/core/payment_methods.rs
+++ b/crates/router/src/core/payment_methods.rs
@@ -1622,9 +1622,12 @@ pub async fn list_payment_methods_for_session(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("error when fetching merchant connector accounts")?;
- let customer_payment_methods =
- list_customer_payment_methods_core(&state, &platform, &payment_method_session.customer_id)
- .await?;
+ let customer_payment_methods = match payment_method_session.customer_id {
+ Some(customer_id) => {
+ list_customer_payment_methods_core(&state, &platform, &customer_id).await?
+ }
+ None => Vec::new(),
+ };
let response =
hyperswitch_domain_models::merchant_connector_account::FlattenedPaymentMethodsEnabled::from_payment_connectors_list(payment_connector_accounts)
@@ -3213,13 +3216,15 @@ pub async fn payment_methods_session_create(
let db = state.store.as_ref();
let key_manager_state = &(&state).into();
- db.find_customer_by_global_id(
- &request.customer_id,
- platform.get_processor().get_key_store(),
- platform.get_processor().get_account().storage_scheme,
- )
- .await
- .to_not_found_response(errors::ApiErrorResponse::CustomerNotFound)?;
+ if let (Some(customer_id)) = &request.customer_id {
+ db.find_customer_by_global_id(
+ customer_id,
+ platform.get_processor().get_key_store(),
+ platform.get_processor().get_account().storage_scheme,
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::CustomerNotFound)?;
+ }
let payment_methods_session_id =
id_type::GlobalPaymentMethodSessionId::generate(&state.conf.cell_information.id)
@@ -3470,7 +3475,7 @@ fn construct_zero_auth_payments_request(
payment_method_data: confirm_request.payment_method_data.clone(),
payment_method_type: confirm_request.payment_method_type,
payment_method_subtype: confirm_request.payment_method_subtype,
- customer_id: Some(payment_method_session.customer_id.clone()),
+ customer_id: payment_method_session.customer_id.clone(),
customer_present: Some(enums::PresenceOfCustomerDuringPayment::Present),
setup_future_usage: Some(common_enums::FutureUsage::OffSession),
payment_method_id: Some(payment_method.id.clone()),
@@ -3572,7 +3577,10 @@ pub async fn payment_methods_session_confirm(
})
.or_else(|| payment_method_session_billing.clone());
- let customer_id = payment_method_session.customer_id.clone();
+ let customer_id = payment_method_session
+ .customer_id
+ .clone()
+ .get_required_value("customer_id")?;
let create_payment_method_request = get_payment_method_create_request(
request