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): add storage type to PaymentMethodSession create call (#11245)

Gaurav Rawat30650bf1a5ca
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index ad3cad2062..86594a6496 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -20405,7 +20405,8 @@
"required": [
"payment_method_type",
"customer_id",
- "payment_method_data"
+ "payment_method_data",
+ "storage_type"
],
"properties": {
"payment_method_type": {
@@ -20459,12 +20460,7 @@
"nullable": true
},
"storage_type": {
- "allOf": [
- {
- "$ref": "#/components/schemas/StorageType"
- }
- ],
- "nullable": true
+ "$ref": "#/components/schemas/StorageType"
}
},
"additionalProperties": false
@@ -21154,6 +21150,7 @@
"merchant_id",
"customer_id",
"payment_method_type",
+ "storage_type",
"network_transaction_id",
"raw_payment_method_data"
],
@@ -21231,12 +21228,7 @@
"nullable": true
},
"storage_type": {
- "allOf": [
- {
- "$ref": "#/components/schemas/StorageType"
- }
- ],
- "nullable": true
+ "$ref": "#/components/schemas/StorageType"
},
"card_cvc_token_storage": {
"allOf": [
@@ -21435,6 +21427,9 @@
},
"PaymentMethodSessionRequest": {
"type": "object",
+ "required": [
+ "storage_type"
+ ],
"properties": {
"customer_id": {
"type": "string",
@@ -21483,6 +21478,9 @@
"tokenization_data": {
"description": "Contains data to be passed on to tokenization service ( if present ) to create token_id for given JSON data",
"nullable": true
+ },
+ "storage_type": {
+ "$ref": "#/components/schemas/StorageType"
}
}
},
diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rs
index e27810fa0b..5ba2d81d8d 100644
--- a/crates/api_models/src/payment_methods.rs
+++ b/crates/api_models/src/payment_methods.rs
@@ -148,8 +148,8 @@ pub struct PaymentMethodCreate {
pub network_tokenization: Option<common_types::payment_methods::NetworkTokenization>,
/// The storage type for the payment method
- #[schema(value_type = Option<StorageType>)]
- pub storage_type: Option<common_enums::StorageType>,
+ #[schema(value_type = StorageType)]
+ pub storage_type: common_enums::StorageType,
}
#[cfg(feature = "v2")]
@@ -1220,8 +1220,8 @@ pub struct PaymentMethodResponse {
pub network_token: Option<NetworkTokenResponse>,
/// The storage type for the payment method
- #[schema(value_type = Option<StorageType>)]
- pub storage_type: Option<common_enums::StorageType>,
+ #[schema(value_type = StorageType)]
+ pub storage_type: common_enums::StorageType,
/// Card CVC token storage details
pub card_cvc_token_storage: Option<CardCVCTokenStorageDetails>,
@@ -3581,6 +3581,10 @@ pub struct PaymentMethodSessionRequest {
/// Contains data to be passed on to tokenization service ( if present ) to create token_id for given JSON data
#[schema(value_type = Option<serde_json::Value>)]
pub tokenization_data: Option<pii::SecretSerdeValue>,
+
+ /// The storage type for the payment method
+ #[schema(value_type = StorageType)]
+ pub storage_type: common_enums::StorageType,
}
#[cfg(feature = "v2")]
diff --git a/crates/diesel_models/src/payment_methods_session.rs b/crates/diesel_models/src/payment_methods_session.rs
index 349012a6c1..06155a150d 100644
--- a/crates/diesel_models/src/payment_methods_session.rs
+++ b/crates/diesel_models/src/payment_methods_session.rs
@@ -16,6 +16,7 @@ pub struct PaymentMethodSession {
Option<Vec<common_types::payment_methods::AssociatedPaymentMethods>>,
pub associated_payment: Option<common_utils::id_type::GlobalPaymentId>,
pub associated_token_id: Option<common_utils::id_type::GlobalTokenId>,
+ pub storage_type: common_enums::StorageType,
}
#[cfg(feature = "v2")]
@@ -33,6 +34,7 @@ impl PaymentMethodSession {
associated_payment_methods,
associated_payment,
associated_token_id,
+ storage_type,
} = self;
Self {
@@ -47,6 +49,7 @@ impl PaymentMethodSession {
associated_payment_methods,
associated_payment,
associated_token_id,
+ storage_type,
}
}
}
diff --git a/crates/hyperswitch_domain_models/src/payment_methods.rs b/crates/hyperswitch_domain_models/src/payment_methods.rs
index 4c325156ae..6ccdc5b680 100644
--- a/crates/hyperswitch_domain_models/src/payment_methods.rs
+++ b/crates/hyperswitch_domain_models/src/payment_methods.rs
@@ -795,6 +795,7 @@ pub struct PaymentMethodSession {
Option<Vec<common_types::payment_methods::AssociatedPaymentMethods>>,
pub associated_payment: Option<id_type::GlobalPaymentId>,
pub associated_token_id: Option<id_type::GlobalTokenId>,
+ pub storage_type: common_enums::StorageType,
}
#[cfg(feature = "v2")]
@@ -815,6 +816,7 @@ impl super::behaviour::Conversion for PaymentMethodSession {
associated_payment: self.associated_payment,
return_url: self.return_url,
associated_token_id: self.associated_token_id,
+ storage_type: self.storage_type,
})
}
@@ -869,6 +871,7 @@ impl super::behaviour::Conversion for PaymentMethodSession {
associated_payment: storage_model.associated_payment,
return_url: storage_model.return_url,
associated_token_id: storage_model.associated_token_id,
+ storage_type: storage_model.storage_type,
})
}
.await
@@ -890,6 +893,7 @@ impl super::behaviour::Conversion for PaymentMethodSession {
associated_payment: self.associated_payment,
return_url: self.return_url,
associated_token_id: self.associated_token_id,
+ storage_type: self.storage_type,
})
}
}
@@ -1082,6 +1086,7 @@ impl PaymentMethodSession {
associated_payment_methods,
associated_payment,
associated_token_id,
+ storage_type,
} = self;
Self {
id,
@@ -1097,6 +1102,7 @@ impl PaymentMethodSession {
.or(associated_payment_methods),
associated_payment,
associated_token_id,
+ storage_type,
}
}
}
diff --git a/crates/router/src/core/payment_methods.rs b/crates/router/src/core/payment_methods.rs
index add9fa4189..e1b3f616b1 100644
--- a/crates/router/src/core/payment_methods.rs
+++ b/crates/router/src/core/payment_methods.rs
@@ -790,7 +790,7 @@ pub(crate) fn get_payment_method_create_request(
customer_id: Option<id_type::GlobalCustomerId>,
billing_address: Option<&api_models::payments::Address>,
payment_method_session: Option<&domain::payment_methods::PaymentMethodSession>,
- storage_type: Option<common_enums::StorageType>,
+ storage_type: common_enums::StorageType,
) -> RouterResult<payment_methods::PaymentMethodCreate> {
match payment_method_data {
api_models::payments::PaymentMethodData::Card(card) => {
@@ -1062,10 +1062,10 @@ pub async fn create_payment_method_core(
profile: &domain::Profile,
) -> RouterResult<(api::PaymentMethodResponse, domain::PaymentMethod)> {
match req.storage_type {
- Some(common_enums::StorageType::Volatile) => {
+ common_enums::StorageType::Volatile => {
create_volatile_payment_method_core(state, _request_state, req, platform, profile).await
}
- Some(common_enums::StorageType::Persistent) | None => {
+ common_enums::StorageType::Persistent => {
create_persistent_payment_method_core(state, _request_state, req, platform, profile)
.await
}
@@ -2258,7 +2258,7 @@ pub async fn payment_method_intent_create(
let resp = pm_transforms::generate_payment_method_response(
&payment_method,
&None,
- None,
+ common_enums::StorageType::Persistent,
None,
Some(customer_id),
None,
@@ -3742,7 +3742,7 @@ pub async fn retrieve_payment_method(
transformers::generate_payment_method_response(
&payment_method,
&single_use_token_in_cache,
- Some(storage_type),
+ storage_type,
card_cvc_expiry.map(|time| {
payment_methods::CardCVCTokenStorageDetails::generate_expiry_timestamp(time)
}),
@@ -4387,6 +4387,7 @@ pub async fn payment_methods_session_create(
associated_payment_methods: None,
associated_payment: None,
associated_token_id: None,
+ storage_type: request.storage_type,
};
db.insert_payment_methods_session(
@@ -4403,7 +4404,7 @@ pub async fn payment_methods_session_create(
client_secret.secret,
None,
None,
- None,
+ Some(request.storage_type),
None,
None,
);
@@ -4818,6 +4819,11 @@ pub async fn payment_methods_session_confirm(
})
.or_else(|| payment_method_session_billing.clone());
+ let storage_type = get_storage_type_for_payment_method_create(
+ request.storage_type,
+ payment_method_session.storage_type,
+ )?;
+
let create_payment_method_request = get_payment_method_create_request(
request
.payment_method_data
@@ -4829,7 +4835,7 @@ pub async fn payment_methods_session_confirm(
payment_method_session.customer_id.clone(),
unified_billing_address.as_ref(),
Some(&payment_method_session),
- request.storage_type,
+ storage_type,
)
.attach_printable("Failed to create payment method request")?;
@@ -4847,9 +4853,7 @@ pub async fn payment_methods_session_confirm(
let token_data = get_pm_list_token_data(
request.payment_method_type,
&payment_method,
- create_payment_method_request
- .storage_type
- .unwrap_or(enums::StorageType::Persistent),
+ create_payment_method_request.storage_type,
)?;
// insert the token data into redis
@@ -4953,7 +4957,7 @@ pub async fn payment_methods_session_confirm(
Secret::new("CLIENT_SECRET_REDACTED".to_string()),
payments_response,
(tokenization_response.flatten()),
- payment_method_response.storage_type,
+ Some(payment_method_response.storage_type),
payment_method_response.card_cvc_token_storage,
None,
);
@@ -4963,6 +4967,25 @@ pub async fn payment_methods_session_confirm(
))
}
+#[cfg(feature = "v2")]
+pub fn get_storage_type_for_payment_method_create(
+ request_storage_type: Option<enums::StorageType>,
+ session_storage_type: enums::StorageType,
+) -> RouterResult<enums::StorageType> {
+ match session_storage_type {
+ // If volatile return Volatile
+ enums::StorageType::Volatile => Ok(enums::StorageType::Volatile),
+
+ // If persistent, validate that request_storage_type exists
+ enums::StorageType::Persistent => request_storage_type.ok_or(
+ errors::ApiErrorResponse::MissingRequiredField {
+ field_name: "storage_type",
+ }
+ .into(),
+ ),
+ }
+}
+
#[cfg(feature = "v2")]
impl pm_types::SavedPMLPaymentsInfo {
pub async fn form_payments_info(
@@ -5561,7 +5584,7 @@ impl<'a> pm_types::PaymentMethodUpdateHandler<'a> {
let response = pm_transforms::generate_payment_method_response(
&self.payment_method,
&None,
- Some(common_enums::StorageType::Persistent),
+ common_enums::StorageType::Persistent,
card_cvc_token_details,
self.payment_method.customer_id.clone(),
None,
diff --git a/crates/router/src/core/payment_methods/transformers.rs b/crates/router/src/core/payment_methods/transformers.rs
index 1b7a14ddab..6bea2bfee5 100644
--- a/crates/router/src/core/payment_methods/transformers.rs
+++ b/crates/router/src/core/payment_methods/transformers.rs
@@ -577,7 +577,7 @@ pub fn generate_pm_vaulting_req_from_update_request(
pub fn generate_payment_method_response(
payment_method: &domain::PaymentMethod,
single_use_token: &Option<payment_method_data::SingleUsePaymentMethodToken>,
- storage_type: Option<common_enums::StorageType>,
+ storage_type: common_enums::StorageType,
card_cvc_token_storage: Option<api_models::payment_methods::CardCVCTokenStorageDetails>,
customer_id: Option<id_type::GlobalCustomerId>,
raw_payment_method_data: Option<api_models::payment_methods::RawPaymentMethodData>,
diff --git a/crates/router/src/core/payments/operations/external_vault_proxy_payment_intent.rs b/crates/router/src/core/payments/operations/external_vault_proxy_payment_intent.rs
index 4362a5bf54..05a20f4638 100644
--- a/crates/router/src/core/payments/operations/external_vault_proxy_payment_intent.rs
+++ b/crates/router/src/core/payments/operations/external_vault_proxy_payment_intent.rs
@@ -383,7 +383,7 @@ impl<F: Clone + Send + Sync> Domain<F, ExternalVaultProxyPaymentsRequest, Paymen
billing,
psp_tokenization: None,
network_tokenization: None,
- storage_type: None, //this field is currently not being used in storing payment methods via external vault
+ storage_type: common_enums::StorageType::Persistent, //this field is currently not being used in storing payment methods via external vault
};
let (_pm_response, payment_method) = Box::pin(payment_methods::create_payment_method_core(
diff --git a/crates/router/src/core/payments/operations/payment_confirm_intent.rs b/crates/router/src/core/payments/operations/payment_confirm_intent.rs
index 8a794ab806..18a99d8903 100644
--- a/crates/router/src/core/payments/operations/payment_confirm_intent.rs
+++ b/crates/router/src/core/payments/operations/payment_confirm_intent.rs
@@ -641,7 +641,7 @@ impl<F: Clone + Send + Sync> Domain<F, PaymentsConfirmIntentRequest, PaymentConf
billing: None,
psp_tokenization: None,
network_tokenization: None,
- storage_type: Some(common_enums::StorageType::Persistent), //since customer acceptance is present, we always store it persistently
+ storage_type: common_enums::StorageType::Persistent, //since customer acceptance is present, we always store it persistently
};
let (_pm_response, payment_method) =