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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
fix(payments): resolve payment_token to payment_method_id in modular service fetch (#12655)
Ayush Anandcdba3c16e93f
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rsindex 019edc099d..a7c64dd1d7 100644--- a/crates/router/src/core/payments.rs+++ b/crates/router/src/core/payments.rs@@ -140,9 +140,7 @@ use crate::{DimensionsWithProcessorAndProviderMerchantIdAndProfileId,},errors::{self, CustomResult, RouterResponse, RouterResult},- payment_methods::{- cards, network_tokenization, transformers as pm_transformers, utils as pm_utils,- },+ payment_methods::{cards, network_tokenization, transformers as pm_transformers},payments::helpers::{get_applepay_metadata, is_applepay_predecrypted_flow_supported,is_googlepay_predecrypted_flow_supported,@@ -9431,8 +9429,11 @@ impl PaymentEligibilityData {})}- /// Resolves a `payment_token` to raw card data for blocklist checks.- /// Uses modular PM service if enabled for the org, otherwise falls back to locker.+ /// Resolves a `payment_token` to raw card data for blocklist checks: modular+ /// organizations fetch from the PM Modular Service directly; legacy organizations+ /// resolve the Redis token to the payment method DB record, escalating to the+ /// modular service when the record itself is modular, and reading raw card data+ /// from the locker otherwise.async fn resolve_payment_token_to_method_data(state: &SessionState,platform: &domain::Platform,@@ -9440,29 +9441,36 @@ impl PaymentEligibilityData {payment_method_type: common_enums::PaymentMethod,profile_id: &id_type::ProfileId,) -> CustomResult<Option<domain::EligibilityPaymentMethodData>, errors::ApiErrorResponse> {- let pm_modular_dimensions = Dimensions::new().with_organization_id(- platform- .get_processor()- .get_account()- .organization_id- .clone(),- );-- let is_modular_flow = pm_utils::get_organization_eligibility_config_for_pm_modular_service(- state,- &pm_modular_dimensions,- )- .await;+ let dimensions = Dimensions::new()+ .with_processor_merchant_id(platform.get_processor().get_processor_merchant_id())+ .with_provider_merchant_id(platform.get_provider().get_provider_merchant_id());+ let feature_config = core_utils::get_feature_config(state, platform, &dimensions).await;- if is_modular_flow {- // Modular path: payment_token IS the payment_method_id in the modular service.+ if feature_config.is_payment_method_modular_allowed {logger::info!("Resolving payment token via PM Modular Service for eligibility check");+ // Permanent tokens carry the underlying payment_method_id; any other token+ // type (or a Redis miss) is treated as the payment_method_id itself.+ let payment_token = payment_token.peek();+ let payment_method_id = match helpers::retrieve_payment_token_data(+ state,+ payment_token.clone(),+ Some(payment_method_type),+ )+ .await+ {+ Ok(storage::PaymentTokenData::Permanent(card_token_data))+ | Ok(storage::PaymentTokenData::PermanentCard(card_token_data)) => card_token_data+ .payment_method_id+ .unwrap_or_else(|| payment_token.clone()),+ _ => payment_token.clone(),+ };+let pm_response = pm_transformers::fetch_payment_method_from_modular_service(state,platform,profile_id,- payment_token.clone().expose().as_str(),- None, // CVC token data is not passed in create api+ payment_method_id.as_str(),+ None, // CVC is not collected during the eligibility checktrue, // fetch raw card detail from the internal vault).await@@ -9473,40 +9481,75 @@ impl PaymentEligibilityData {.raw_payment_method_data.map(domain::EligibilityPaymentMethodData::from))} else {- // Legacy path: resolve via Redis token → DB → locker.- Self::resolve_payment_token_via_db(state, platform, payment_token, payment_method_type)- .await+ let token_data = helpers::retrieve_payment_token_data(+ state,+ payment_token.peek().clone(),+ Some(payment_method_type),+ )+ .await+ .change_context(errors::ApiErrorResponse::UnprocessableEntity {+ message: "Invalid or expired payment token".to_string(),+ })+ .attach_printable("Failed to retrieve payment token data from storage")?;++ let payment_method_record = helpers::retrieve_payment_method_from_db_with_token_data(+ state,+ platform.get_provider().get_key_store(),+ &token_data,+ platform.get_processor().get_account().storage_scheme,+ )+ .await+ .attach_printable("Failed to retrieve payment method from DB")?;++ match payment_method_record {+ Some(payment_method)+ if feature_config.should_use_modular_pm_path(+ Some(payment_method.version),+ payment_method.compatibility_updated_at,+ Some(payment_method.last_modified),+ ) =>+ {+ logger::debug!(+ "Payment method is modular, fetching payment method from PM Modular Service"+ );+ let pm_response = pm_transformers::fetch_payment_method_from_modular_service(+ state,+ platform,+ profile_id,+ payment_method.get_id(),+ None, // CVC is not collected during the eligibility check+ true, // fetch raw card detail from the internal vault+ )+ .await+ .change_context(errors::ApiErrorResponse::PaymentMethodNotFound)+ .attach_printable("Failed to fetch payment method from modular service")?;++ Ok(pm_response+ .raw_payment_method_data+ .map(domain::EligibilityPaymentMethodData::from))+ }+ payment_method_record => {+ Self::read_payment_method_data_from_locker(+ state,+ platform,+ &token_data,+ payment_method_record,+ )+ .await+ }+ }}}- /// Legacy token resolution: Redis → DB → locker.- async fn resolve_payment_token_via_db(+ /// Reads raw card data from the locker for permanent card tokens using the+ /// prefetched token data and payment method DB record.+ async fn read_payment_method_data_from_locker(state: &SessionState,platform: &domain::Platform,- payment_token: &Secret<String>,- payment_method_type: common_enums::PaymentMethod,+ token_data: &storage::PaymentTokenData,+ payment_method_record: Option<domain::PaymentMethod>,) -> CustomResult<Option<domain::EligibilityPaymentMethodData>, errors::ApiErrorResponse> {- let token_data = helpers::retrieve_payment_token_data(- state,- payment_token.clone().expose(),- Some(payment_method_type),- )- .await- .change_context(errors::ApiErrorResponse::UnprocessableEntity {- message: "Invalid or expired payment token".to_string(),- })- .attach_printable("Failed to retrieve payment token data from storage")?;-- let payment_method_record = helpers::retrieve_payment_method_from_db_with_token_data(- state,- platform.get_provider().get_key_store(),- &token_data,- platform.get_processor().get_account().storage_scheme,- )- .await- .attach_printable("Failed to retrieve payment method from DB")?;-- let payment_method_data = match &token_data {+ let payment_method_data = match token_data {storage::PaymentTokenData::PermanentCard(card_token) => {// Read full card data from the locker using the DB record.let pm_record = payment_method_record@@ -9545,17 +9588,12 @@ impl PaymentEligibilityData {))}// Wallet and other token types: no raw card data available via this path.- #[cfg(feature = "v1")]storage::PaymentTokenData::Temporary(_)| storage::PaymentTokenData::TemporaryGeneric(_)| storage::PaymentTokenData::Permanent(_)| storage::PaymentTokenData::AuthBankDebit(_)| storage::PaymentTokenData::WalletToken(_)| storage::PaymentTokenData::BankDebit(_) => None,- #[cfg(feature = "v2")]- storage::PaymentTokenData::TemporaryGeneric(_)- | storage::PaymentTokenData::AuthBankDebit(_)- | storage::PaymentTokenData::BankDebit(_) => None,};Ok(payment_method_data)