From 7c9ff20286e08127652ed25e495c2c368562fadd Mon Sep 17 00:00:00 2001 From: Kashif Date: Sun, 10 Nov 2024 19:21:54 +0530 Subject: [PATCH] refactor: update payment_method_type during payments_confirm --- crates/router/src/core/payments/helpers.rs | 33 ++++++++++++++++++ .../payments/operations/payment_confirm.rs | 18 ++++++---- .../payments/operations/payment_create.rs | 34 +++---------------- 3 files changed, 50 insertions(+), 35 deletions(-) diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs index acaccccbb17b..df34c9ff6181 100644 --- a/crates/router/src/core/payments/helpers.rs +++ b/crates/router/src/core/payments/helpers.rs @@ -6102,3 +6102,36 @@ pub fn validate_platform_fees_for_marketplace( } } } + +pub fn get_optional_card_type_from_additional_payment_method_data( + payment_method_type: Option, + additional_pm_data: Option<&api_models::payments::AdditionalPaymentData>, +) -> Option { + additional_pm_data + .and_then(|pm_data| { + if let api_models::payments::AdditionalPaymentData::Card(card_info) = pm_data { + card_info.card_type.as_ref().and_then(|card_type_str| { + api_models::enums::PaymentMethodType::from_str(&card_type_str.to_lowercase()).map_err(|err| { + logger::error!( + "Err - {:?}\nInvalid card_type value found in BIN DB - {:?}", + err, + card_type_str, + ); + }).ok() + }) + } else { + None + } + }) + .map_or(payment_method_type, |card_type_in_bin_store| { + if let Some(card_type_in_req) = payment_method_type { + if card_type_in_req != card_type_in_bin_store { + logger::info!( + "Mismatch in card_type\nAPI request - {}; BIN lookup - {}\nOverriding with {}", + card_type_in_req, card_type_in_bin_store, card_type_in_bin_store, + ); + } + } + Some(card_type_in_bin_store) + }) +} diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs index 635b3c24cf21..c1eb9da23737 100644 --- a/crates/router/src/core/payments/operations/payment_confirm.rs +++ b/crates/router/src/core/payments/operations/payment_confirm.rs @@ -576,12 +576,6 @@ impl GetTracker, api::PaymentsRequest> for Pa payment_method_info, } = mandate_details; - payment_attempt.payment_method_type = payment_method_type - .or(payment_attempt.payment_method_type) - .or(payment_method_info - .as_ref() - .and_then(|pm_info| pm_info.payment_method_type)); - let token = token.or_else(|| payment_attempt.payment_token.clone()); helpers::validate_pm_or_token_given( @@ -650,6 +644,18 @@ impl GetTracker, api::PaymentsRequest> for Pa payment_attempt.payment_method = payment_method.or(payment_attempt.payment_method); + let payment_method_type = + helpers::get_optional_card_type_from_additional_payment_method_data( + payment_method_type, + additional_pm_data.as_ref(), + ); + + payment_attempt.payment_method_type = payment_method_type + .or(payment_attempt.payment_method_type) + .or(payment_method_info + .as_ref() + .and_then(|pm_info| pm_info.payment_method_type)); + // The operation merges mandate data from both request and payment_attempt let setup_mandate = mandate_data.map(|mut sm| { sm.mandate_type = payment_attempt.mandate_details.clone().or(sm.mandate_type); diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs index abe4a55e8daf..4a8b830ae70d 100644 --- a/crates/router/src/core/payments/operations/payment_create.rs +++ b/crates/router/src/core/payments/operations/payment_create.rs @@ -1,4 +1,4 @@ -use std::{marker::PhantomData, str::FromStr}; +use std::marker::PhantomData; use api_models::{ enums::FrmSuggestion, mandates::RecurringDetails, payment_methods::PaymentMethodsData, @@ -1196,34 +1196,10 @@ impl PaymentCreate { }; let payment_method_type = - additional_pm_data - .as_ref() - .and_then(|pm_data| { - if let api_models::payments::AdditionalPaymentData::Card(card_info) = pm_data { - card_info.card_type.as_ref().and_then(|card_type_str| { - enums::PaymentMethodType::from_str(&card_type_str.to_lowercase()).map_err(|err| { - logger::error!( - "Err - {:?}\nInvalid card_type value found in BIN DB - {:?}", - err, - card_type_str, - ); - }).ok() - }) - } else { - None - } - }) - .map_or(payment_method_type, |card_type_in_bin_store| { - if let Some(card_type_in_req) = payment_method_type { - if card_type_in_req != card_type_in_bin_store { - logger::info!( - "Mismatch in card_type\nAPI request - {}; BIN lookup - {}\nOverriding with {}", - card_type_in_req, card_type_in_bin_store, card_type_in_bin_store, - ); - } - } - Some(card_type_in_bin_store) - }); + helpers::get_optional_card_type_from_additional_payment_method_data( + payment_method_type, + additional_pm_data.as_ref(), + ); Ok(( storage::PaymentAttemptNew {