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
chore(payment_methods): add success, failure and decision logs to modular payment method flows (#13693)
Prasunna Soppa658cd9279124
diff --git a/crates/payment_methods/src/client/delete.rs b/crates/payment_methods/src/client/delete.rsindex 6a05e75c3f..9df17159c0 100644--- a/crates/payment_methods/src/client/delete.rs+++ b/crates/payment_methods/src/client/delete.rs@@ -5,8 +5,6 @@ use common_utils::request::Method;use hyperswitch_interfaces::micro_service::{MicroserviceClientError, MicroserviceClientErrorKind};use serde::Deserialize;-const DUMMY_PM_ID: &str = "pm_dummy";-/// V1-facing delete flow type.#[derive(Debug)]pub struct DeletePaymentMethod;@@ -57,9 +55,15 @@ impl TryFrom<&DeletePaymentMethodV1Request> for DeletePaymentMethodV2Request {impl TryFrom<DeletePaymentMethodV2Response> for DeletePaymentMethodResponse {type Error = MicroserviceClientError;- fn try_from(_: DeletePaymentMethodV2Response) -> Result<Self, Self::Error> {+ fn try_from(response: DeletePaymentMethodV2Response) -> Result<Self, Self::Error> {+ // Stub transform: the real id returned by the modular service is surfaced instead of+ // being discarded, but `deleted` is still hardcoded until the real models land.+ router_env::logger::warn!(+ payment_method_id=%response.id,+ "modular delete response handled by stub transform; deleted=true is hardcoded"+ );Ok(Self {- payment_method_id: DUMMY_PM_ID.to_string(),+ payment_method_id: response.id,deleted: Some(true),})}diff --git a/crates/payment_methods/src/client/list.rs b/crates/payment_methods/src/client/list.rsindex 20c8277e3b..08bd27bbab 100644--- a/crates/payment_methods/src/client/list.rs+++ b/crates/payment_methods/src/client/list.rs@@ -80,20 +80,40 @@ impl ListCustomerPaymentMethods {}if let Some(countries) = &qp.accepted_countries {- if let Ok(serialized) = serde_json::to_string(countries) {- params.push(("accepted_countries", serialized));+ match serde_json::to_string(countries) {+ Ok(serialized) => params.push(("accepted_countries", serialized)),+ // A dropped filter widens the list the PM service returns, so it must not+ // disappear silently.+ Err(err) => {+ router_env::logger::warn!(+ ?err,+ "failed to serialize accepted_countries filter; listing without it"+ );+ }}}if let Some(currencies) = &qp.accepted_currencies {- if let Ok(serialized) = serde_json::to_string(currencies) {- params.push(("accepted_currencies", serialized));+ match serde_json::to_string(currencies) {+ Ok(serialized) => params.push(("accepted_currencies", serialized)),+ Err(err) => {+ router_env::logger::warn!(+ ?err,+ "failed to serialize accepted_currencies filter; listing without it"+ );+ }}}if let Some(networks) = &qp.card_networks {- if let Ok(serialized) = serde_json::to_string(networks) {- params.push(("card_networks", serialized));+ match serde_json::to_string(networks) {+ Ok(serialized) => params.push(("card_networks", serialized)),+ Err(err) => {+ router_env::logger::warn!(+ ?err,+ "failed to serialize card_networks filter; listing without it"+ );+ }}}diff --git a/crates/router/src/core/payment_methods.rs b/crates/router/src/core/payment_methods.rsindex 513dfe6e2e..5da10a6194 100644--- a/crates/router/src/core/payment_methods.rs+++ b/crates/router/src/core/payment_methods.rs@@ -547,6 +547,12 @@ pub async fn payment_method_modular_forward_compat_action(.with_organization_id(organization_id.clone());let should_schedule_modular_forward_compat =utils::get_should_schedule_modular_forward_compat(state, &dimensions, customer_id).await;+ logger::debug!(+ should_schedule_modular_forward_compat,+ merchant_id=%merchant_id.get_string_repr(),+ organization_id=%organization_id.get_string_repr(),+ "resolved modular forward compat scheduling flag"+ );let organization_id = organization_id.clone();should_schedule_modular_forward_compat.then(|| {@@ -565,13 +571,22 @@ pub async fn payment_method_modular_forward_compat_action().await;- if let Err(err) = res {- logger::error!(- ?err,- payment_method_id=%payment_method.get_id(),- merchant_id=%payment_method.merchant_id.get_string_repr(),- "Failed to schedule modular forward compatibility PT after payment method DB write"- );+ match res {+ Err(err) => {+ logger::error!(+ ?err,+ payment_method_id=%payment_method.get_id(),+ merchant_id=%payment_method.merchant_id.get_string_repr(),+ "Failed to schedule modular forward compatibility PT after payment method DB write"+ );+ }+ Ok(_) => {+ logger::info!(+ payment_method_id=%payment_method.get_id(),+ merchant_id=%payment_method.merchant_id.get_string_repr(),+ "Scheduled modular forward compatibility PT after payment method DB write"+ );+ }}})})diff --git a/crates/router/src/core/payment_methods/transformers.rs b/crates/router/src/core/payment_methods/transformers.rsindex 64d5cd3637..a50cabd531 100644--- a/crates/router/src/core/payment_methods/transformers.rs+++ b/crates/router/src/core/payment_methods/transformers.rs@@ -1425,9 +1425,20 @@ pub async fn call_modular_payment_method_update();}result.map_err(|err| {- logger::error!(error=?err, "modular payment method update failed");+ logger::error!(+ error=?err,+ payment_method_id=%payment_method_id,+ merchant_id=%processor_merchant_id.get_string_repr(),+ profile_id=%profile_id.get_string_repr(),+ "modular payment method update failed"+ );::payment_methods::errors::ModularPaymentMethodError::UpdateFailed})?;+ logger::info!(+ payment_method_id=%payment_method_id,+ merchant_id=%processor_merchant_id.get_string_repr(),+ "modular payment method update succeeded"+ );Ok(())}@@ -2070,10 +2081,20 @@ pub async fn retrieve_pm_modular_service_call(}let pm_response = result.map_err(|err| {- logger::debug!("Error in retrieving payment method: {:?}", err);+ logger::error!(+ error=?err,+ merchant_id=%processor_merchant_id.get_string_repr(),+ profile_id=%profile_id.get_string_repr(),+ "modular payment method retrieve failed"+ );errors::ApiErrorResponse::InternalServerError}).attach_printable("Failed to retrieve payment method from modular service")?;+ logger::info!(+ payment_method_id=%pm_response.payment_method_id,+ merchant_id=%processor_merchant_id.get_string_repr(),+ "modular payment method retrieve succeeded"+ );Ok(pm_response)}@@ -2121,7 +2142,13 @@ pub async fn create_payment_method_in_modular_service(.await?;//Convert PMResponse to PaymentMethodWithRawData- let payment_method_with_raw_data = DomainPaymentMethodWrapper::try_from(pm_response)?;+ let payment_method_with_raw_data = DomainPaymentMethodWrapper::try_from(pm_response)+ .attach_printable("Failed to convert modular create response to domain payment method")?;+ logger::info!(+ payment_method_id=%payment_method_with_raw_data.0.get_id(),+ merchant_id=%processor_merchant_id.get_string_repr(),+ "modular payment method create succeeded"+ );Ok(payment_method_with_raw_data.0)}@@ -2162,7 +2189,15 @@ pub async fn create_proxy_card_payment_method_in_modular_service().await?;- let payment_method_with_raw_data = DomainPaymentMethodWrapper::try_from(pm_response)?;+ let payment_method_with_raw_data = DomainPaymentMethodWrapper::try_from(pm_response)+ .attach_printable(+ "Failed to convert modular proxy card create response to domain payment method",+ )?;+ logger::info!(+ payment_method_id=%payment_method_with_raw_data.0.get_id(),+ merchant_id=%processor_merchant_id.get_string_repr(),+ "modular proxy card payment method create succeeded"+ );Ok(payment_method_with_raw_data.0)}@@ -2218,15 +2253,43 @@ pub async fn get_permanent_pm_id_from_temporary_token(let response = http_client::send_request(&state.conf.proxy, request, None).await+ .inspect_err(|err| {+ logger::error!(+ error=?err,+ "hyperswitch vault token details call failed (transport)"+ );+ }).change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Failed to call hyperswitch vault token details endpoint")?;+ let status_code = response.status().as_u16();+ response+ .status()+ .is_success()+ .then_some(())+ .ok_or_else(|| {+ logger::error!(+ status_code,+ "hyperswitch vault token details endpoint returned non-success status"+ );+ error_stack::report!(errors::ApiErrorResponse::InternalServerError)+ })+ .attach_printable("Hyperswitch vault token details endpoint returned an error status")?;+let token_details = response.json::<VaultTokenDetailsResponse>().await+ .inspect_err(|err| {+ logger::error!(+ error=?err,+ status_code,+ "failed to parse hyperswitch vault token details response"+ );+ }).change_context(errors::ApiErrorResponse::InternalServerError).attach_printable("Failed to parse hyperswitch vault token details response")?;+ logger::info!("resolved permanent payment method id from temporary vault token");Ok(token_details.id)}@@ -2270,9 +2333,23 @@ pub async fn list_customer_payment_methods_from_modular_service(ListCustomerPaymentMethods::call(state, &client, request).await- .map(|resp| resp.0.customer_payment_methods)+ .map(|resp| {+ let payment_methods = resp.0.customer_payment_methods;+ logger::info!(+ merchant_id=%merchant_id.get_string_repr(),+ profile_id=%profile_id.get_string_repr(),+ payment_method_count=payment_methods.len(),+ "modular list customer payment methods succeeded"+ );+ payment_methods+ }).map_err(|err| {- logger::error!(error=?err, "modular list customer payment methods failed");+ logger::error!(+ error=?err,+ merchant_id=%merchant_id.get_string_repr(),+ profile_id=%profile_id.get_string_repr(),+ "modular list customer payment methods failed"+ );errors::ApiErrorResponse::InternalServerError}).attach_printable("Failed to list customer payment methods from modular service")@@ -2325,7 +2402,12 @@ pub async fn create_pm_modular_service_call(}let pm_response = result.map_err(|err| {- logger::debug!("Error in creating payment method: {:?}", err);+ logger::error!(+ error=?err,+ merchant_id=%merchant_id.get_string_repr(),+ profile_id=%profile_id.get_string_repr(),+ "modular payment method create failed"+ );errors::ApiErrorResponse::InternalServerError}).attach_printable("Failed to create payment method in modular service")?;diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rsindex 8ef2cbcd49..adf5c1cb76 100644--- a/crates/router/src/core/payments.rs+++ b/crates/router/src/core/payments.rs@@ -2805,6 +2805,19 @@ whereD: OperationSessionGetters<F> + Send + Sync,Op: Operation<F, R, Data = D> + Send + Sync,{+ // Log the inputs of the modular-vs-legacy decision so a wrong branch is diagnosable:+ // the feature flag, the payment method's version and the modular/legacy modification+ // timestamps are everything `should_use_modular_pm_path` looks at.+ let pm_decision_inputs = payment_data+ .get_payment_method_info()+ .map(|pm| (pm.version, pm.compatibility_updated_at, pm.last_modified));+ logger::info!(+ payment_id = ?payment_data.get_payment_attempt().payment_id,+ is_payment_method_modular_allowed = feature_config.is_payment_method_modular_allowed,+ payment_method_decision_inputs = ?pm_decision_inputs,+ "resolving modular vs legacy payment method update path"+ );+if payment_data.get_payment_method_info().is_some_and(|pm| {feature_config.should_use_modular_pm_path(Some(pm.version),@@ -2812,9 +2825,9 @@ whereSome(pm.last_modified),)}) {- logger::debug!(+ logger::info!(payment_id = ?payment_data.get_payment_attempt().payment_id,- "Modular merchant detected; calling update_modular_pm_and_mandate"+ "Modular payment method path selected; calling update_modular_pm_and_mandate");let domain_payment_method_data =@@ -2833,9 +2846,11 @@ where).await?;} else {- logger::debug!(+ // Reached when the flag is off, the timestamps favour legacy, or there is no+ // payment_method_info at all — the decision-inputs log above disambiguates.+ logger::info!(payment_id = ?payment_data.get_payment_attempt().payment_id,- "Non-modular merchant; calling save_pm_and_mandate"+ "Legacy payment method path selected; calling save_pm_and_mandate");operation.to_post_update_tracker()?diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rsindex 9f1d609da6..536e703189 100644--- a/crates/router/src/core/payments/operations/payment_confirm.rs+++ b/crates/router/src/core/payments/operations/payment_confirm.rs@@ -1235,9 +1235,15 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> forpayment_data.set_payment_method_info(Some(pm_info));}Err(err) => {+ // Non-fatal by design: the payment proceeds without a+ // saved payment method, so this error log is the only+ // trace that saving was attempted and failed.logger::error!(- "Error creating payment method in PM Modular service: {:?}",- err+ error=?err,+ payment_id=%payment_data.payment_intent.payment_id.get_string_repr(),+ merchant_id=%platform.get_processor().get_account().get_id().get_string_repr(),+ profile_id=%business_profile.get_id().get_string_repr(),+ "Error creating payment method in PM Modular service; continuing payment without saved payment method");}}@@ -1415,15 +1421,28 @@ impl<F: Clone + Send + Sync> Domain<F, api::PaymentsRequest, PaymentData<F>> for.await?}Some(payment_method) => {- logger::info!("Organization is not eligible for PM Modular Service, skipping fetch payment method.");+ // Reached when the modular flag is off OR the payment method's timestamps+ // favour the legacy copy — not only for non-eligible organizations.+ logger::info!(+ payment_method_id=%payment_method.get_id(),+ payment_method_version=?payment_method.version,+ compatibility_updated_at=?payment_method.compatibility_updated_at,+ last_modified=?payment_method.last_modified,+ "Legacy payment method path selected, skipping fetch from PM Modular Service."+ );operations::PaymentMethodFetchData::from_legacy(payment_method, token_data)}- None => operations::PaymentMethodFetchData {- payment_intent: None,- payment_method_info: None,- payment_method_with_raw_data: None,- token_data,- },+ None => {+ logger::debug!(+ "No stored payment method resolved from the request; skipping payment method fetch."+ );+ operations::PaymentMethodFetchData {+ payment_intent: None,+ payment_method_info: None,+ payment_method_with_raw_data: None,+ token_data,+ }+ }}};diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rsindex d4a0487abd..74b3913156 100644--- a/crates/router/src/core/payments/operations/payment_response.rs+++ b/crates/router/src/core/payments/operations/payment_response.rs@@ -365,25 +365,47 @@ where.await{Ok(_) => {- logger::info!("Successfully called modular payment method update");+ logger::info!(+ payment_method_id=%pm_id,+ "Successfully called modular payment method update"+ );}Err(err) => {+ // Non-fatal by design: the attempt still gets the pm_id below,+ // so this log is the only trace the modular update failed and+ // the payment method may be stale (missing connector token /+ // NTI / acknowledgement).logger::error!(- "Failed to call modular payment method update: {}",- err+ error=%err,+ payment_method_id=%pm_id,+ merchant_id=%payment_data.payment_attempt.processor_merchant_id.get_string_repr(),+ profile_id=%payment_data.payment_attempt.profile_id.get_string_repr(),+ "Failed to call modular payment method update; continuing with possibly stale payment method");}};payment_data.payment_attempt.payment_method_id = Some(pm_id.clone());} else {- logger::info!("No updates found for modular payment method update call");+ logger::info!(+ payment_method_id=%pm_id,+ "No updates found for modular payment method update call"+ );}}}- (_, _) => {- logger::info!("Payment method is not eligible for modular update");+ (is_volatile, payment_method_id) => {+ logger::info!(+ ?is_volatile,+ ?payment_method_id,+ "Payment method is not eligible for modular update (volatile, or no payment method attached)"+ );}}+ } else {+ logger::debug!(+ payment_method=?payment_data.payment_attempt.payment_method,+ "Payment method type is not eligible for modular update; skipping"+ );}Ok(())}@@ -932,14 +954,20 @@ impl<F: Send + Clone> PostUpdateTracker<F, PaymentData<F>, types::PaymentsAuthor.get_payment_method_info().map(|pm| pm.last_modified),) {- let _ = update_pm_connector_mandate_details(+ if let Err(err) = update_pm_connector_mandate_details(state,provider,initiator,payment_data,router_data,)- .await;+ .await+ {+ logger::error!(+ error=?err,+ "Failed to update legacy payment method connector mandate details"+ );+ }}Ok(())}@@ -1255,14 +1283,20 @@ impl<F: Clone> PostUpdateTracker<F, PaymentData<F>, types::PaymentsSyncData> for.get_payment_method_info().map(|pm| pm.last_modified),) {- let _ = update_pm_connector_mandate_details(+ if let Err(err) = update_pm_connector_mandate_details(state,provider,initiator,payment_data,router_data,)- .await;+ .await+ {+ logger::error!(+ error=?err,+ "Failed to update legacy payment method connector mandate details"+ );+ }}Ok(())}@@ -2108,14 +2142,20 @@ impl<F: Clone> PostUpdateTracker<F, PaymentData<F>, types::SetupMandateRequestDa.get_payment_method_info().map(|pm| pm.last_modified),) {- let _ = update_pm_connector_mandate_details(+ if let Err(err) = update_pm_connector_mandate_details(state,provider,initiator,payment_data,router_data,)- .await;+ .await+ {+ logger::error!(+ error=?err,+ "Failed to update legacy payment method connector mandate details"+ );+ }}Ok(())}@@ -2259,14 +2299,20 @@ impl<F: Clone> PostUpdateTracker<F, PaymentData<F>, types::CompleteAuthorizeData.get_payment_method_info().map(|pm| pm.last_modified),) {- let _ = update_pm_connector_mandate_details(+ if let Err(err) = update_pm_connector_mandate_details(state,provider,initiator,payment_data,router_data,)- .await;+ .await+ {+ logger::error!(+ error=?err,+ "Failed to update legacy payment method connector mandate details"+ );+ }}Ok(())}diff --git a/crates/router/src/core/payments/vault_session.rs b/crates/router/src/core/payments/vault_session.rsindex e057fb3b48..6f798a6d40 100644--- a/crates/router/src/core/payments/vault_session.rs+++ b/crates/router/src/core/payments/vault_session.rs@@ -160,7 +160,13 @@ async fn call_internal_pm_session_create_for_vault(let response = CreatePaymentMethodSession::call(state, &client, request).await.map_err(|err| {- router_env::logger::error!(?err, "Internal PM session create for vault failed");+ router_env::logger::error!(+ ?err,+ merchant_id=%merchant_id.get_string_repr(),+ profile_id=%profile_id.get_string_repr(),+ ?storage_type,+ "Internal PM session create for vault failed"+ );errors::ApiErrorResponse::InternalServerError}).attach_printable("Failed to create PM session via internal service for vault details")?;@@ -178,11 +184,23 @@ async fn call_internal_pm_session_create_for_vault(// Only return Some if at least one of the two parts is present.if internal_vault.is_none() && external_vault_details.is_none() {router_env::logger::warn!(+ merchant_id=%merchant_id.get_string_repr(),+ profile_id=%profile_id.get_string_repr(),+ ?storage_type,"Internal PM session create returned neither sdk_authorization nor external_vault_details");return Ok(None);}+ router_env::logger::info!(+ merchant_id=%merchant_id.get_string_repr(),+ profile_id=%profile_id.get_string_repr(),+ ?storage_type,+ has_internal_vault=internal_vault.is_some(),+ has_external_vault_details=external_vault_details.is_some(),+ "Internal PM session create for vault succeeded"+ );+Ok(Some(api::VaultDetails {internal_vault,external_vault_details,@@ -231,6 +249,12 @@ where(Some(_), Some(_)) => common_enums::StorageType::Persistent,_ => common_enums::StorageType::Volatile,};+ router_env::logger::info!(+ ?storage_type,+ setup_future_usage=?payment_data.get_payment_intent().setup_future_usage,+ has_customer=customer_id.is_some(),+ "Resolved storage type for PM vault session create"+ );// Use the resolved external vault profile (the platform merchant's profile in platform// flows) so the PM service operates under the merchant that actually holds the external@@ -243,14 +267,21 @@ where).await.unwrap_or_else(|err| {+ // Non-fatal by design: the payment continues without vault session details, which+ // means the SDK gets no vault session — this warn is the only trace of that.router_env::logger::warn!(?err,- "Failed to fetch vault details via internal PM session service"+ profile_id=%external_vault_profile.get_id().get_string_repr(),+ "Failed to fetch vault details via internal PM session service; continuing without vault session");None});payment_data.set_vault_session_details(vault_details);+ } else {+ router_env::logger::debug!(+ "PM modular service disabled for this organization; skipping vault session creation"+ );}Ok(())}diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rsindex 876e93e493..a381ebb86a 100644--- a/crates/router/src/core/utils.rs+++ b/crates/router/src/core/utils.rs@@ -118,6 +118,11 @@ pub async fn get_feature_config(let is_payment_method_modular_allowed =payment_methods::utils::get_should_call_pm_modular_service(state, &dimensions, None).await;+ router_env::logger::debug!(+ is_payment_method_modular_allowed,+ organization_id=%platform.get_processor().get_account().organization_id.get_string_repr(),+ "resolved PM modular service feature flag"+ );FeatureConfig {is_payment_method_modular_allowed,}diff --git a/crates/router/src/routes/payment_methods.rs b/crates/router/src/routes/payment_methods.rsindex a80bdd96d5..74928234ea 100644--- a/crates/router/src/routes/payment_methods.rs+++ b/crates/router/src/routes/payment_methods.rs@@ -2060,7 +2060,7 @@ pub async fn payment_method_session_update_saved_payment_method(}#[cfg(feature = "v2")]-#[instrument(skip_all, fields(flow = ?Flow::PaymentMethodSessionUpdateSavedPaymentMethod))]+#[instrument(skip_all, fields(flow = ?Flow::PaymentMethodSessionDeleteSavedPaymentMethod))]pub async fn payment_method_session_delete_saved_payment_method(state: web::Data<AppState>,req: HttpRequest,