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

fix(payment_methods): align recurring_enabled and requires_cvv in modular PM list with legacy DB flow (#12520)

Shankar Singh C1127ba9779de
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index bac1ad12e7..528fe56d17 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -22590,14 +22590,6 @@
],
"nullable": true
},
- "bank": {
- "allOf": [
- {
- "$ref": "#/components/schemas/MaskedBankDetails"
- }
- ],
- "nullable": true
- },
"created": {
"type": "string",
"format": "date-time",
@@ -22635,6 +22627,19 @@
}
],
"nullable": true
+ },
+ "connector_tokens": {
+ "type": "array",
+ "items": {
+ "$ref": "#/components/schemas/ConnectorTokenDetails"
+ },
+ "description": "The connector token details if available",
+ "nullable": true
+ },
+ "network_transaction_id": {
+ "type": "string",
+ "description": "The network transaction ID provided by the card network during a Customer Initiated Transaction (CIT)\nwhen `setup_future_usage` is set to `off_session`.",
+ "nullable": true
}
}
},
diff --git a/crates/api_models/src/payment_methods.rs b/crates/api_models/src/payment_methods.rs
index b95d51f856..135a7bf353 100644
--- a/crates/api_models/src/payment_methods.rs
+++ b/crates/api_models/src/payment_methods.rs
@@ -3400,10 +3400,6 @@ pub struct PaymentMethodResponseItem {
/// PaymentMethod Data from locker
pub payment_method_data: Option<PaymentMethodListData>,
- /// Masked bank details from PM auth services
- #[schema(example = json!({"mask": "0000"}))]
- pub bank: Option<MaskedBankDetails>,
-
/// A timestamp (ISO 8601 code) that determines when the payment method was created
#[schema(value_type = PrimitiveDateTime, example = "2023-01-18T11:04:09.922Z")]
#[serde(with = "common_utils::custom_serde::iso8601")]
@@ -3428,6 +3424,14 @@ pub struct PaymentMethodResponseItem {
///The network token details for the payment method
pub network_tokenization: Option<NetworkTokenResponse>,
+
+ /// The connector token details if available
+ pub connector_tokens: Option<Vec<ConnectorTokenDetails>>,
+
+ /// The network transaction ID provided by the card network during a Customer Initiated Transaction (CIT)
+ /// when `setup_future_usage` is set to `off_session`.
+ #[schema(value_type = Option<String>)]
+ pub network_transaction_id: Option<hyperswitch_masking::Secret<String>>,
}
#[cfg(feature = "v2")]
diff --git a/crates/payment_methods/src/types.rs b/crates/payment_methods/src/types.rs
index 177a184a64..be57ba71fe 100644
--- a/crates/payment_methods/src/types.rs
+++ b/crates/payment_methods/src/types.rs
@@ -36,6 +36,8 @@ pub struct PaymentMethodResponseItemV1 {
pub billing: Option<api_models::payments::Address>,
pub network_tokenization: Option<NetworkTokenResponse>,
pub psp_tokenization_enabled: bool,
+ pub connector_tokens: Option<Vec<ConnectorTokenDetails>>,
+ pub network_transaction_id: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
diff --git a/crates/router/src/core/payment_methods/client.rs b/crates/router/src/core/payment_methods/client.rs
index b7f16cc742..37d3ae373e 100644
--- a/crates/router/src/core/payment_methods/client.rs
+++ b/crates/router/src/core/payment_methods/client.rs
@@ -187,23 +187,61 @@ impl CustomerPaymentMethodsFetcher for ModularCustomerPaymentMethodsFetcher {
)
.await;
+ // Fetch all MCAs for the merchant so we can check whether the connector that
+ // issued a mandate token is still active for this profile — mirroring the
+ // `get_mca_status` check performed in the legacy DB flow.
+ let merchant_connector_accounts = state
+ .store
+ .find_merchant_connector_account_by_merchant_id_and_disabled_list(
+ &merchant_id,
+ true,
+ platform.get_provider().get_key_store(),
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::MerchantConnectorAccountNotFound {
+ id: merchant_id.get_string_repr().to_owned(),
+ })?;
+
+ // Pre-compute the set of MCA IDs that are active for this profile
+ let active_mca_ids: std::collections::HashSet<id_type::MerchantConnectorAccountId> =
+ merchant_connector_accounts
+ .iter()
+ .filter(|mca| {
+ mca.disabled.is_some_and(|disabled| !disabled)
+ && mca.profile_id == self.profile_id
+ })
+ .map(|mca| mca.get_id())
+ .collect();
+
let mut customer_payment_methods = Vec::with_capacity(items.len());
for pm in items {
- // Compute requires_cvv using the same logic as the legacy API
- // (using psp_tokenization_enabled in place of connector_mandate_details
- // and network_tokenization in place of network_transaction_id).
+ // Replicate `get_mca_status` from the legacy DB flow:
+ // - agnostic MIT: network_transaction_id is present and the feature is enabled
+ // - mandate match: at least one Active connector token belongs to an active MCA
+ // for this profile (equivalent to MCA id present in connector_mandate_details)
+ let has_active_connector_token = pm.connector_tokens.as_ref().is_some_and(|tokens| {
+ tokens.iter().any(|t| {
+ t.status == common_enums::ConnectorTokenStatus::Active
+ && active_mca_ids.contains(&t.connector_id)
+ })
+ });
+ let agnostic_mit =
+ self.is_connector_agnostic_mit_enabled && pm.network_transaction_id.is_some();
+ let recurring_enabled = agnostic_mit || has_active_connector_token;
+
+ // requires_cvv mirrors the legacy logic (cards.rs list_customer_payment_method):
+ // CVV is skipped when this is an off-session payment and the PM has a usable
+ // mandate or network transaction ID.
let requires_cvv = if self.is_connector_agnostic_mit_enabled {
requires_cvv_base
&& !(self.off_session_payment_flag
- && (pm.psp_tokenization_enabled || pm.network_tokenization.is_some()))
+ && (pm.connector_tokens.is_some() || pm.network_transaction_id.is_some()))
} else {
- requires_cvv_base && !(self.off_session_payment_flag && pm.psp_tokenization_enabled)
+ requires_cvv_base
+ && !(self.off_session_payment_flag && pm.connector_tokens.is_some())
};
- // recurring_enabled is inferred from psp_tokenization_enabled.
- let recurring_enabled = Some(pm.psp_tokenization_enabled);
-
let payment_token = Self::store_payment_token_in_redis(
state,
pm.payment_method_type,
@@ -215,17 +253,22 @@ impl CustomerPaymentMethodsFetcher for ModularCustomerPaymentMethodsFetcher {
// Build the client-facing response item.
let payment_method_data = pm.payment_method_data.and_then(|d| d.into());
- customer_payment_methods.push(CustomerPaymentMethodForClient {
- payment_token,
- payment_method: pm.payment_method_type,
- payment_method_type: Some(pm.payment_method_subtype),
- default_payment_method_set: pm.is_default,
- requires_cvv,
- recurring_enabled,
- created: Some(pm.created),
- last_used_at: Some(pm.last_used_at),
- payment_method_data,
- });
+ // Mirror the legacy `list_customer_payment_method` filter: skip PMs where
+ // CVV is not required (off-session + has tokens → CVV skipped) AND no
+ // active mandate exists. Such PMs are unusable and should not be surfaced.
+ if requires_cvv || recurring_enabled {
+ customer_payment_methods.push(CustomerPaymentMethodForClient {
+ payment_token,
+ payment_method: pm.payment_method_type,
+ payment_method_type: Some(pm.payment_method_subtype),
+ default_payment_method_set: pm.is_default,
+ requires_cvv,
+ recurring_enabled: Some(recurring_enabled),
+ created: Some(pm.created),
+ last_used_at: Some(pm.last_used_at),
+ payment_method_data,
+ });
+ }
}
Ok(customer_payment_methods)
diff --git a/crates/router/src/core/payment_methods/transformers.rs b/crates/router/src/core/payment_methods/transformers.rs
index 04ac2982f0..d6f88d25a3 100644
--- a/crates/router/src/core/payment_methods/transformers.rs
+++ b/crates/router/src/core/payment_methods/transformers.rs
@@ -1156,8 +1156,29 @@ impl
payment_method_data: pmd,
});
- // TODO: check how we can get this field
- let recurring_enabled = Some(true);
+ let recurring_enabled = item.connector_mandate_details.as_ref().and_then(|details| {
+ details.payments.as_ref().map(|payments| {
+ payments.values().any(|connector_token_reference| {
+ connector_token_reference.connector_token_status
+ == api_enums::ConnectorTokenStatus::Active
+ })
+ })
+ });
+
+ let connector_tokens = item
+ .connector_mandate_details
+ .as_ref()
+ .and_then(|details| details.payments.clone())
+ .map(|payment_details| {
+ payment_details
+ .0
+ .into_iter()
+ .map(transformers::ForeignFrom::foreign_from)
+ .collect::<Vec<_>>()
+ })
+ .and_then(|tokens| (!tokens.is_empty()).then_some(tokens));
+
+ let network_transaction_id = item.network_transaction_id.clone().map(Secret::new);
let is_default = default_payment_method_id.is_some()
&& default_payment_method_id == Some(item.id.clone());
@@ -1176,11 +1197,12 @@ impl
last_used_at: item.last_used_at,
recurring_enabled,
payment_method_data,
- bank: None,
requires_cvv: true,
is_default,
billing: payment_method_billing,
network_tokenization: network_token_resp,
+ connector_tokens,
+ network_transaction_id,
})
}
}