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 cvc storage support in update saved payment method session (#12487)

Prasunna Soppa87c1ec6bca85
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index 4100e62582..77a184933c 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -22899,14 +22899,12 @@
},
{
"type": "object",
- "required": [
- "payment_method_token"
- ],
"properties": {
"payment_method_token": {
"type": "string",
- "description": "The payment method token associated with the payment method session",
- "example": "token_9wcXDRVkfEtLEsSnYKgQ"
+ "description": "The payment method token associated with the payment method session.\nIf not provided, a new token will be generated.",
+ "example": "token_9wcXDRVkfEtLEsSnYKgQ",
+ "nullable": true
}
}
}
diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rs
index 135a7bf353..3f516bfe67 100644
--- a/crates/api_models/src/payment_methods.rs
+++ b/crates/api_models/src/payment_methods.rs
@@ -4438,9 +4438,10 @@ pub struct PaymentMethodsSessionUpdateRequest {
#[cfg(feature = "v2")]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, ToSchema)]
pub struct PaymentMethodSessionUpdateSavedPaymentMethod {
- /// The payment method token associated with the payment method session
- #[schema(value_type = String, example = "token_9wcXDRVkfEtLEsSnYKgQ")]
- pub payment_method_token: String,
+ /// The payment method token associated with the payment method session.
+ /// If not provided, a new token will be generated.
+ #[schema(value_type = Option<String>, example = "token_9wcXDRVkfEtLEsSnYKgQ")]
+ pub payment_method_token: Option<String>,
/// The update request for the payment method update
#[serde(flatten)]
pub payment_method_update_request: PaymentMethodUpdate,
diff --git a/crates/router/src/core/payment_methods.rs b/crates/router/src/core/payment_methods.rs
index 110cc826c1..a06ebf5875 100644
--- a/crates/router/src/core/payment_methods.rs
+++ b/crates/router/src/core/payment_methods.rs
@@ -4128,6 +4128,13 @@ pub async fn get_external_vault_token(
),
})?
}
+ storage::PaymentTokenData::TemporaryCardToken(_) => {
+ Err(errors::ApiErrorResponse::NotImplemented {
+ message: errors::NotImplementedMessage::Reason(
+ "TemporaryCardToken does not have a vaulted payment method".to_string(),
+ ),
+ })?
+ }
};
let payment_method = db
@@ -5444,7 +5451,8 @@ pub async fn resolve_storage_type_from_token(
// Any token kind other than PermanentCard is invalid for retrieving a saved payment method.
// Only PermanentCard is currently supported; all other enum variants should be removed.
storage::PaymentTokenData::TemporaryGeneric(_)
- | storage::PaymentTokenData::AuthBankDebit(_) => {
+ | storage::PaymentTokenData::AuthBankDebit(_)
+ | storage::PaymentTokenData::TemporaryCardToken(_) => {
Err(report!(errors::ApiErrorResponse::PaymentMethodNotFound))
}
}
@@ -6010,7 +6018,7 @@ pub async fn payment_methods_session_create(
) -> RouterResponse<payment_methods::PaymentMethodSessionResponse> {
let db = state.store.as_ref();
let key_manager_state = &(&state).into();
- let provider = platform.get_provider();
+ let _provider = platform.get_provider();
let customer = if let Some(customer_id) = &request.customer_id {
let customer = db
@@ -6264,6 +6272,84 @@ pub async fn payment_methods_session_retrieve(
Ok(services::ApplicationResponse::Json(response))
}
+/// Stores `card_cvc` and `card_holder_name` as a `TemporaryCardToken` in Redis under `token`.
+#[cfg(feature = "v2")]
+async fn store_cvc_and_card_holder_name_as_payment_token_in_redis(
+ state: &SessionState,
+ token: &str,
+ card_cvc: Option<Secret<String>>,
+ card_holder_name: Option<Secret<String>>,
+) -> RouterResult<()> {
+ let redis_token_data =
+ storage::PaymentTokenData::temporary_card_token(card_cvc, card_holder_name);
+ let intent_fulfillment_time = common_utils::consts::DEFAULT_INTENT_FULFILLMENT_TIME;
+ pm_routes::ParentPaymentMethodToken::create_key_for_token(&token.to_string())
+ .insert(intent_fulfillment_time, redis_token_data, state)
+ .await?;
+ Ok(())
+}
+
+/// Extracts `(card_cvc, card_holder_name)` from a `PaymentMethodUpdateData`, returning
+/// `(None, None)` for non-card variants.
+#[cfg(feature = "v2")]
+fn extract_card_cvc_and_holder_name(
+ payment_method_data: Option<&payment_methods::PaymentMethodUpdateData>,
+) -> (Option<Secret<String>>, Option<Secret<String>>) {
+ match payment_method_data {
+ Some(payment_methods::PaymentMethodUpdateData::Card(card_update)) => (
+ card_update.card_cvc.clone(),
+ card_update.card_holder_name.clone(),
+ ),
+ _ => (None, None),
+ }
+}
+
+/// Updates the associated payment methods list in the session DB record.
+#[cfg(feature = "v2")]
+async fn update_associated_payment_methods_in_session(
+ db: &dyn StorageInterface,
+ key_store: &domain::MerchantKeyStore,
+ payment_method_session_id: &id_type::GlobalPaymentMethodSessionId,
+ payment_method_session: hyperswitch_domain_models::payment_methods::PaymentMethodSession,
+ associated_payment_methods: Option<
+ Vec<common_types::payment_methods::AssociatedPaymentMethods>,
+ >,
+) -> RouterResult<hyperswitch_domain_models::payment_methods::PaymentMethodSession> {
+ let update = hyperswitch_domain_models::payment_methods::PaymentMethodsSessionUpdateEnum::UpdateAssociatedPaymentMethods {
+ associated_payment_methods,
+ };
+ db.update_payment_method_session(
+ key_store,
+ payment_method_session_id,
+ update,
+ payment_method_session,
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to update payment method session with associated payment methods")
+}
+
+/// Moves `token` to the front of the existing associated payment methods list.
+/// Entries with a different token are preserved in their original order after position 0.
+#[cfg(feature = "v2")]
+fn reorder_token_to_front(
+ existing: Option<Vec<common_types::payment_methods::AssociatedPaymentMethods>>,
+ token: &str,
+) -> Option<Vec<common_types::payment_methods::AssociatedPaymentMethods>> {
+ existing.map(|mut tokens| {
+ tokens.retain(|pm| match &pm.payment_method_token {
+ common_types::payment_methods::AssociatedPaymentMethodTokenType::PaymentMethodSessionToken(t) => t != token,
+ });
+ tokens.insert(
+ 0,
+ common_types::payment_methods::AssociatedPaymentMethods {
+ payment_method_token: common_types::payment_methods::AssociatedPaymentMethodTokenType::PaymentMethodSessionToken(token.to_owned()),
+ },
+ );
+ tokens
+ })
+}
+
#[cfg(feature = "v2")]
pub async fn payment_methods_session_update_payment_method(
state: SessionState,
@@ -6286,85 +6372,149 @@ pub async fn payment_methods_session_update_payment_method(
})
.attach_printable("Failed to retrieve payment methods session from db")?;
- // Get the associated_pm_token_details for the payment_method_token from the request
- payment_method_session
- .associated_payment_methods
- .as_ref()
- .and_then(|payment_methods| {
- payment_methods.iter().find(|pm| match &pm.payment_method_token {
- common_types::payment_methods::AssociatedPaymentMethodTokenType::PaymentMethodSessionToken(token) => token == &request.payment_method_token
- })
- })
- .ok_or(errors::ApiErrorResponse::GenericNotFoundError {
- message: "No associated payment method found in the session".to_string(),
- })?;
+ // Extract card CVC and holder name from the update request (if card update)
+ let (card_cvc, card_holder_name) = extract_card_cvc_and_holder_name(
+ request
+ .payment_method_update_request
+ .payment_method_data
+ .as_ref(),
+ );
- let payment_method_id = utils::retrieve_payment_method_id_from_payment_method_token_data(
- &state,
- request.payment_method_token.clone(),
- )
- .await
- .attach_printable("Failed to retrieve payment method id from payment method token data")?;
+ // If only CVC/holder name are provided (no payment_method_token), this is a
+ // temporary card token flow: store CVC in Redis and return a new token.
+ // If a token is provided, re-use it (update the CVC stored under that token).
+ let has_cvc_and_card_holder_name_only_data = card_cvc.is_some() || card_holder_name.is_some();
- // Insert the token as the first element in the associated payment methods list
- let mut tokens = payment_method_session
- .associated_payment_methods
- .clone()
- .map(|tokens| tokens.into_iter().filter(|token| match &token.payment_method_token {
- common_types::payment_methods::AssociatedPaymentMethodTokenType::PaymentMethodSessionToken(token) => token != &request.payment_method_token
- }).collect::<Vec<_>>());
+ match request.payment_method_token.clone() {
+ None if has_cvc_and_card_holder_name_only_data => {
+ // Generate a new token, store CVC + card_holder_name in Redis
+ let parent_payment_method_token = generate_id(consts::ID_LENGTH, "token");
- tokens.as_mut().map(|tokens| tokens.insert(0, common_types::payment_methods::AssociatedPaymentMethods {
- payment_method_token: common_types::payment_methods::AssociatedPaymentMethodTokenType::PaymentMethodSessionToken(request.payment_method_token.clone()),
- }));
+ store_cvc_and_card_holder_name_as_payment_token_in_redis(
+ &state,
+ &parent_payment_method_token,
+ card_cvc,
+ card_holder_name,
+ )
+ .await?;
- // Update payment method session with associated payment methods
- let update_payment_method_session = hyperswitch_domain_models::payment_methods::PaymentMethodsSessionUpdateEnum::UpdateAssociatedPaymentMethods {
- associated_payment_methods: tokens
- };
+ let associated_payment_methods = Some(vec![
+ common_types::payment_methods::AssociatedPaymentMethods {
+ payment_method_token: common_types::payment_methods::AssociatedPaymentMethodTokenType::PaymentMethodSessionToken(
+ parent_payment_method_token,
+ ),
+ },
+ ]);
- let updated_payment_method_session = db
- .update_payment_method_session(
- platform.get_provider().get_key_store(),
- &payment_method_session_id,
- update_payment_method_session,
- payment_method_session,
- )
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable(
- "Failed to update payment method session with associated payment methods",
- )?;
+ let updated_payment_method_session = update_associated_payment_methods_in_session(
+ db,
+ platform.get_provider().get_key_store(),
+ &payment_method_session_id,
+ payment_method_session,
+ associated_payment_methods,
+ )
+ .await?;
- let update_request =
- DomainPaymentMethodUpdate::from(request.payment_method_update_request.clone());
+ let response = transformers::generate_payment_method_session_response(
+ updated_payment_method_session.clone(),
+ Secret::new("CLIENT_SECRET_REDACTED".to_string()),
+ None,
+ None,
+ None,
+ updated_payment_method_session.storage_type,
+ None,
+ None,
+ None,
+ );
- let (update_response, _updated_payment_method) = Box::pin(update_payment_method_core(
- &state,
- &platform,
- &profile,
- update_request,
- &payment_method_id,
- None,
- None,
- ))
- .await
- .attach_printable("Failed to update saved payment method")?;
+ Ok(services::ApplicationResponse::Json(response))
+ }
+ token_opt => {
+ // Existing saved PM flow: token must be present
+ let pm_token = token_opt.ok_or(errors::ApiErrorResponse::MissingRequiredField {
+ field_name: "payment_method_token",
+ })?;
- let response = transformers::generate_payment_method_session_response(
- updated_payment_method_session.clone(),
- Secret::new("CLIENT_SECRET_REDACTED".to_string()),
- None, // sdk_authorization is not returned for non-create flows
- None, // TODO: send associated payments response based on the expandable param
- None,
- updated_payment_method_session.storage_type,
- update_response.card_cvc_token_storage,
- update_response.payment_method_data.clone(),
- None,
- None,
- );
+ // Validate token is associated with this session
+ payment_method_session
+ .associated_payment_methods
+ .as_ref()
+ .and_then(|payment_methods| {
+ payment_methods.iter().find(|pm| match &pm.payment_method_token {
+ common_types::payment_methods::AssociatedPaymentMethodTokenType::PaymentMethodSessionToken(token) => token == &pm_token
+ })
+ })
+ .ok_or(errors::ApiErrorResponse::GenericNotFoundError {
+ message: "No associated payment method found in the session".to_string(),
+ })?;
- Ok(services::ApplicationResponse::Json(response))
+ // If CVC is present, refresh the TemporaryCardToken in Redis under the same token
+ if has_cvc_and_card_holder_name_only_data {
+ store_cvc_and_card_holder_name_as_payment_token_in_redis(
+ &state,
+ &pm_token,
+ card_cvc,
+ card_holder_name,
+ )
+ .await?;
+ }
+
+ let payment_method_id =
+ utils::retrieve_payment_method_id_from_payment_method_token_data(
+ &state,
+ pm_token.clone(),
+ )
+ .await
+ .attach_printable(
+ "Failed to retrieve payment method id from payment method token data",
+ )?;
+
+ // Re-order the token to the front of the associated payment methods list
+ let tokens = reorder_token_to_front(
+ payment_method_session.associated_payment_methods.clone(),
+ &pm_token,
+ );
+
+ let updated_payment_method_session = update_associated_payment_methods_in_session(
+ db,
+ platform.get_provider().get_key_store(),
+ &payment_method_session_id,
+ payment_method_session,
+ tokens,
+ )
+ .await?;
+
+ let update_request =
+ DomainPaymentMethodUpdate::from(request.payment_method_update_request);
+
+ let (update_response, _updated_payment_method) = Box::pin(update_payment_method_core(
+ &state,
+ &platform,
+ &profile,
+ update_request,
+ &payment_method_id,
+ None,
+ None,
+ ))
+ .await
+ .attach_printable("Failed to update saved payment method")?;
+
+ let response = transformers::generate_payment_method_session_response(
+ updated_payment_method_session.clone(),
+ Secret::new("CLIENT_SECRET_REDACTED".to_string()),
+ None, // sdk_authorization is not returned for non-create flows
+ None, // TODO: send associated payments response based on the expandable param
+ None,
+ updated_payment_method_session.storage_type,
+ update_response.card_cvc_token_storage,
+ update_response.payment_method_data.clone(),
+ None,
+ None,
+ );
+
+ Ok(services::ApplicationResponse::Json(response))
+ }
+ }
}
#[cfg(feature = "v2")]
diff --git a/crates/router/src/core/payment_methods/vault.rs b/crates/router/src/core/payment_methods/vault.rs
index 379fe4b718..c719ecd3a2 100644
--- a/crates/router/src/core/payment_methods/vault.rs
+++ b/crates/router/src/core/payment_methods/vault.rs
@@ -2249,6 +2249,13 @@ pub async fn retrieve_payment_method_from_vault_using_payment_token(
),
})?
}
+ storage::PaymentTokenData::TemporaryCardToken(_) => {
+ Err(errors::ApiErrorResponse::NotImplemented {
+ message: errors::NotImplementedMessage::Reason(
+ "TemporaryCardToken does not have a vaulted payment method".to_string(),
+ ),
+ })?
+ }
};
let db = &*state.store;
diff --git a/crates/router/src/types/storage/payment_method.rs b/crates/router/src/types/storage/payment_method.rs
index 42622702f2..6f445df845 100644
--- a/crates/router/src/types/storage/payment_method.rs
+++ b/crates/router/src/types/storage/payment_method.rs
@@ -78,6 +78,13 @@ pub enum PaymentTokenData {
BankDebit(BankDebitTokenData),
}
+#[cfg(feature = "v2")]
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
+pub struct TemporaryCardTokenData {
+ pub card_cvc: Option<hyperswitch_masking::Secret<String>>,
+ pub card_holder_name: Option<hyperswitch_masking::Secret<String>>,
+}
+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
#[cfg(feature = "v2")]
@@ -86,6 +93,9 @@ pub enum PaymentTokenData {
PermanentCard(CardTokenData),
AuthBankDebit(payment_methods::BankAccountTokenData),
BankDebit(BankDebitTokenData),
+ /// Temporary token that carries only CVC + card holder name for the repeat CIT
+ /// (session confirm) flow. The card number/expiry come from the internal PM service.
+ TemporaryCardToken(TemporaryCardTokenData),
}
impl PaymentTokenData {
@@ -123,6 +133,17 @@ impl PaymentTokenData {
Self::TemporaryGeneric(GenericTokenData { token })
}
+ #[cfg(feature = "v2")]
+ pub fn temporary_card_token(
+ card_cvc: Option<hyperswitch_masking::Secret<String>>,
+ card_holder_name: Option<hyperswitch_masking::Secret<String>>,
+ ) -> Self {
+ Self::TemporaryCardToken(TemporaryCardTokenData {
+ card_cvc,
+ card_holder_name,
+ })
+ }
+
#[cfg(feature = "v2")]
pub fn bank_debit(
payment_method_id: common_utils::id_type::GlobalPaymentMethodId,