From 52abda9bd17c6a4285df8a535f48e7a621fcabbe Mon Sep 17 00:00:00 2001 From: Shankar Singh C <83439957+ShankarSinghC@users.noreply.github.com> Date: Thu, 4 Jul 2024 15:57:27 +0530 Subject: [PATCH] fix(router): `override setup_future_usage` filed to on_session based on merchant config (#5195) --- crates/router/src/core/payments.rs | 23 +++----------- crates/router/src/core/payments/helpers.rs | 31 +++++++++++++++++-- .../operations/payment_complete_authorize.rs | 1 + .../payments/operations/payment_confirm.rs | 3 ++ .../payments/operations/payment_create.rs | 1 + .../payments/operations/payment_update.rs | 1 + 6 files changed, 38 insertions(+), 22 deletions(-) diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs index f65cc052eaf5..4af5a2ed3084 100644 --- a/crates/router/src/core/payments.rs +++ b/crates/router/src/core/payments.rs @@ -3279,7 +3279,7 @@ where && should_do_retry { let retryable_connector_data = helpers::get_apple_pay_retryable_connectors( - state, + &state, merchant_account, payment_data, key_store, @@ -3303,6 +3303,8 @@ where .first() .ok_or(errors::ApiErrorResponse::IncorrectPaymentMethodConfiguration)?; + helpers::override_setup_future_usage_to_on_session(&*state.store, payment_data).await?; + return Ok(ConnectorCallType::PreDetermined( first_pre_routing_connector_data_list.clone(), )); @@ -3598,24 +3600,7 @@ pub async fn decide_multiplex_connector_for_normal_or_recurring_payment { - let skip_saving_wallet_at_connector_optional = - helpers::config_skip_saving_wallet_at_connector( - &*state.store, - &payment_data.payment_intent.merchant_id, - ) - .await?; - - if let Some(skip_saving_wallet_at_connector) = skip_saving_wallet_at_connector_optional - { - if let Some(payment_method_type) = payment_data.payment_attempt.payment_method_type - { - if skip_saving_wallet_at_connector.contains(&payment_method_type) { - logger::debug!("Override setup_future_usage from off_session to on_session based on the merchant's skip_saving_wallet_at_connector configuration to avoid creating a connector mandate."); - payment_data.payment_intent.setup_future_usage = - Some(enums::FutureUsage::OnSession); - } - } - }; + helpers::override_setup_future_usage_to_on_session(&*state.store, payment_data).await?; let first_choice = connectors .first() diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs index 534da72a1d9e..e59e5e6dd8eb 100644 --- a/crates/router/src/core/payments/helpers.rs +++ b/crates/router/src/core/payments/helpers.rs @@ -451,6 +451,7 @@ pub async fn get_token_pm_type_mandate_details( merchant_account: &domain::MerchantAccount, merchant_key_store: &domain::MerchantKeyStore, payment_method_id: Option, + customer_id: &Option, ) -> RouterResult { let mandate_data = request.mandate_data.clone().map(MandateData::foreign_from); let ( @@ -554,7 +555,9 @@ pub async fn get_token_pm_type_mandate_details( || request.payment_method_type == Some(api_models::enums::PaymentMethodType::GooglePay) { - if let Some(customer_id) = &request.customer_id { + if let Some(customer_id) = + &request.customer_id.clone().or(customer_id.clone()) + { let customer_saved_pm_option = match state .store .find_payment_method_by_customer_id_merchant_id_list( @@ -4158,7 +4161,7 @@ pub fn get_applepay_metadata( #[cfg(all(feature = "retry", feature = "connector_choice_mca_id"))] pub async fn get_apple_pay_retryable_connectors( - state: SessionState, + state: &SessionState, merchant_account: &domain::MerchantAccount, payment_data: &mut PaymentData, key_store: &domain::MerchantKeyStore, @@ -4182,7 +4185,7 @@ where .ok_or(errors::ApiErrorResponse::IncorrectPaymentMethodConfiguration)?; let merchant_connector_account_type = get_merchant_connector_account( - &state, + state, merchant_account.merchant_id.as_str(), payment_data.creds_identifier.to_owned(), key_store, @@ -4999,3 +5002,25 @@ pub async fn config_skip_saving_wallet_at_connector( } }) } + +pub async fn override_setup_future_usage_to_on_session( + db: &dyn StorageInterface, + payment_data: &mut PaymentData, +) -> CustomResult<(), errors::ApiErrorResponse> { + if payment_data.payment_intent.setup_future_usage == Some(enums::FutureUsage::OffSession) { + let skip_saving_wallet_at_connector_optional = + config_skip_saving_wallet_at_connector(db, &payment_data.payment_intent.merchant_id) + .await?; + + if let Some(skip_saving_wallet_at_connector) = skip_saving_wallet_at_connector_optional { + if let Some(payment_method_type) = payment_data.payment_attempt.payment_method_type { + if skip_saving_wallet_at_connector.contains(&payment_method_type) { + logger::debug!("Override setup_future_usage from off_session to on_session based on the merchant's skip_saving_wallet_at_connector configuration to avoid creating a connector mandate."); + payment_data.payment_intent.setup_future_usage = + Some(enums::FutureUsage::OnSession); + } + } + }; + }; + Ok(()) +} diff --git a/crates/router/src/core/payments/operations/payment_complete_authorize.rs b/crates/router/src/core/payments/operations/payment_complete_authorize.rs index 4fd427e38fac..7188a8216ded 100644 --- a/crates/router/src/core/payments/operations/payment_complete_authorize.rs +++ b/crates/router/src/core/payments/operations/payment_complete_authorize.rs @@ -124,6 +124,7 @@ impl GetTracker, api::PaymentsRequest> for Co merchant_account, key_store, payment_attempt.payment_method_id.clone(), + &payment_intent.customer_id, ) .await?; let token = token.or_else(|| payment_attempt.payment_token.clone()); diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs index 5e35b918a0f0..99ac4e3e97c1 100644 --- a/crates/router/src/core/payments/operations/payment_confirm.rs +++ b/crates/router/src/core/payments/operations/payment_confirm.rs @@ -495,6 +495,8 @@ impl GetTracker, api::PaymentsRequest> for Pa let m_request = request.clone(); let m_key_store = key_store.clone(); + let payment_intent_customer_id = payment_intent.customer_id.clone(); + let mandate_details_fut = tokio::spawn( async move { helpers::get_token_pm_type_mandate_details( @@ -504,6 +506,7 @@ impl GetTracker, api::PaymentsRequest> for Pa &m_merchant_account, &m_key_store, None, + &payment_intent_customer_id, ) .await } diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs index b8045487d9f7..fc1f53d358dc 100644 --- a/crates/router/src/core/payments/operations/payment_create.rs +++ b/crates/router/src/core/payments/operations/payment_create.rs @@ -136,6 +136,7 @@ impl GetTracker, api::PaymentsRequest> for Pa merchant_account, merchant_key_store, None, + &request.customer_id, ) .await?; diff --git a/crates/router/src/core/payments/operations/payment_update.rs b/crates/router/src/core/payments/operations/payment_update.rs index c34a40fd9086..c9b778fae319 100644 --- a/crates/router/src/core/payments/operations/payment_update.rs +++ b/crates/router/src/core/payments/operations/payment_update.rs @@ -148,6 +148,7 @@ impl GetTracker, api::PaymentsRequest> for Pa merchant_account, key_store, None, + &payment_intent.customer_id, ) .await?; helpers::validate_amount_to_capture_and_capture_method(Some(&payment_attempt), request)?;