From 567e9b537eb8a572113796b5824554aaad8031a5 Mon Sep 17 00:00:00 2001 From: Kashif Date: Fri, 8 Nov 2024 15:20:29 +0530 Subject: [PATCH 1/6] fix(payments): populate payment_method_type in payment_attempt for cards --- crates/diesel_models/src/payment_attempt.rs | 60 +++++++++++++++++++ .../src/payments/payment_attempt.rs | 11 ++++ .../payments/operations/payment_response.rs | 56 ++++++++++++++++- 3 files changed, 124 insertions(+), 3 deletions(-) diff --git a/crates/diesel_models/src/payment_attempt.rs b/crates/diesel_models/src/payment_attempt.rs index f8654d3cd963..ceb2c76550f3 100644 --- a/crates/diesel_models/src/payment_attempt.rs +++ b/crates/diesel_models/src/payment_attempt.rs @@ -549,6 +549,10 @@ pub enum PaymentAttemptUpdate { updated_by: String, connector_metadata: Option, }, + PaymentMethodTypeUpdate { + payment_method_type: Option, + updated_by: String, + }, } #[cfg(feature = "v2")] @@ -3279,6 +3283,62 @@ impl From for PaymentAttemptUpdateInternal { connector_transaction_data: None, connector_mandate_detail: None, }, + PaymentAttemptUpdate::PaymentMethodTypeUpdate { + payment_method_type, + updated_by, + } => Self { + modified_at: common_utils::date_time::now(), + payment_method_type, + updated_by, + status: None, + error_code: None, + error_message: None, + error_reason: None, + unified_code: None, + unified_message: None, + amount: None, + net_amount: None, + currency: None, + connector_transaction_id: None, + amount_to_capture: None, + connector: None, + authentication_type: None, + payment_method: None, + payment_method_id: None, + cancellation_reason: None, + mandate_id: None, + browser_info: None, + payment_token: None, + connector_metadata: None, + payment_method_data: None, + payment_experience: None, + business_sub_label: None, + straight_through_algorithm: None, + preprocessing_step_id: None, + capture_method: None, + connector_response_reference_id: None, + multiple_capture_count: None, + surcharge_amount: None, + tax_amount: None, + amount_capturable: None, + merchant_connector_id: None, + authentication_data: None, + encoded_data: None, + external_three_ds_authentication_attempted: None, + authentication_connector: None, + authentication_id: None, + fingerprint_id: None, + payment_method_billing_address_id: None, + charge_id: None, + client_source: None, + client_version: None, + customer_acceptance: None, + card_network: None, + shipping_cost: None, + order_tax_amount: None, + connector_transaction_data: None, + connector_mandate_detail: None, + }, } } } diff --git a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs index 831e97b54a17..3a126c356f5c 100644 --- a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs +++ b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs @@ -914,6 +914,10 @@ pub enum PaymentAttemptUpdate { updated_by: String, connector_metadata: Option, }, + PaymentMethodTypeUpdate { + payment_method_type: Option, + updated_by: String, + }, } #[cfg(feature = "v1")] @@ -1276,6 +1280,13 @@ impl PaymentAttemptUpdate { updated_by, connector_metadata, }, + Self::PaymentMethodTypeUpdate { + payment_method_type, + updated_by, + } => DieselPaymentAttemptUpdate::PaymentMethodTypeUpdate { + payment_method_type, + updated_by, + }, } } } diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rs index 0234b97032c7..1dac07e33986 100644 --- a/crates/router/src/core/payments/operations/payment_response.rs +++ b/crates/router/src/core/payments/operations/payment_response.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, str::FromStr}; use api_models::payments::{ConnectorMandateReferenceId, MandateReferenceId}; #[cfg(feature = "dynamic_routing")] @@ -36,7 +36,7 @@ use crate::{ }, tokenization, types::MultipleCaptureData, - PaymentData, PaymentMethodChecker, + OperationSessionGetters, PaymentData, PaymentMethodChecker, }, utils as core_utils, }, @@ -1243,6 +1243,54 @@ impl PostUpdateTracker, types::CompleteAuthorizeData } } +async fn update_payment_method_type_for_cards( + state: &SessionState, + storage_scheme: enums::MerchantStorageScheme, + payment_data: &mut PaymentData, +) -> RouterResult<()> { + // Parse Value to AdditionalPaymentData + let additional_payment_method_data: Option = + payment_data + .get_payment_attempt() + .payment_method_data + .clone() + .map(|data| data.parse_value("payment_method_data")) + .transpose() + .change_context(errors::ApiErrorResponse::InvalidDataValue { + field_name: "payment_method_data", + })?; + + // Fetch card_type from this struct for cards + let payment_method_type = match additional_payment_method_data { + Some(api_models::payments::AdditionalPaymentData::Card(card)) => card + .card_type + .map(|card_type_str| enums::PaymentMethodType::from_str(&card_type_str.to_lowercase())) + .transpose() + .change_context(errors::ApiErrorResponse::InternalServerError)?, + _ => None, + }; + + // If payment_method_type in PaymentAttempt does not match the one in payment_method_data + // update payment_method_type in PaymentAttempt table + if payment_data.payment_attempt.payment_method_type.as_ref() != payment_method_type.as_ref() { + payment_data.payment_attempt.payment_method_type = payment_method_type; + let payment_attempt_update = storage::PaymentAttemptUpdate::PaymentMethodTypeUpdate { + payment_method_type, + updated_by: storage_scheme.to_string(), + }; + state + .store + .update_payment_attempt_with_attempt_id( + payment_data.payment_attempt.clone(), + payment_attempt_update, + storage_scheme, + ) + .await + .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?; + } + Ok(()) +} + #[cfg(feature = "v1")] #[instrument(skip_all)] #[allow(clippy::too_many_arguments)] @@ -1258,8 +1306,10 @@ async fn payment_response_update_tracker( >, #[cfg(all(feature = "v1", feature = "dynamic_routing"))] business_profile: &domain::Profile, ) -> RouterResult> { - // Update additional payment data with the payment method response that we received from connector + // Populate payment_method_type for cards in case it's empty or mismatched + update_payment_method_type_for_cards(state, storage_scheme, &mut payment_data).await?; + // Update additional payment data with the payment method response that we received from connector let additional_payment_method_data = match payment_data.payment_method_data.clone() { Some(payment_method_data) => match payment_method_data { hyperswitch_domain_models::payment_method_data::PaymentMethodData::Card(_) From e2177c1b9cb8314bfce7a8d9f6d63571ec4eda40 Mon Sep 17 00:00:00 2001 From: Kashif Date: Fri, 8 Nov 2024 19:04:43 +0530 Subject: [PATCH 2/6] refactor: resolve comments --- .../payments/operations/payment_response.rs | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rs index 1dac07e33986..105bd122db79 100644 --- a/crates/router/src/core/payments/operations/payment_response.rs +++ b/crates/router/src/core/payments/operations/payment_response.rs @@ -1254,25 +1254,38 @@ async fn update_payment_method_type_for_cards( .get_payment_attempt() .payment_method_data .clone() - .map(|data| data.parse_value("payment_method_data")) + .map(|data| data.parse_value("AdditionalPaymentData")) .transpose() .change_context(errors::ApiErrorResponse::InvalidDataValue { field_name: "payment_method_data", })?; // Fetch card_type from this struct for cards - let payment_method_type = match additional_payment_method_data { - Some(api_models::payments::AdditionalPaymentData::Card(card)) => card - .card_type - .map(|card_type_str| enums::PaymentMethodType::from_str(&card_type_str.to_lowercase())) - .transpose() - .change_context(errors::ApiErrorResponse::InternalServerError)?, + let payment_method_type = additional_payment_method_data.and_then(|data| match data { + api_models::payments::AdditionalPaymentData::Card(card) => { + card.card_type.as_ref().and_then(|card_type_str| { + match enums::PaymentMethodType::from_str(&card_type_str.to_lowercase()) { + Ok(payment_method_type) => Some(payment_method_type), + // Do not throw errors in case of deserialization errors + Err(e) => { + logger::error!( + "Invalid card_type value in AdditionalPaymentData - {:?}\nErr - {:?}", + card.card_type, + e + ); + None + } + } + }) + } _ => None, - }; + }); // If payment_method_type in PaymentAttempt does not match the one in payment_method_data // update payment_method_type in PaymentAttempt table - if payment_data.payment_attempt.payment_method_type.as_ref() != payment_method_type.as_ref() { + if payment_method_type.is_some() + && payment_data.payment_attempt.payment_method_type.as_ref() != payment_method_type.as_ref() + { payment_data.payment_attempt.payment_method_type = payment_method_type; let payment_attempt_update = storage::PaymentAttemptUpdate::PaymentMethodTypeUpdate { payment_method_type, From 4363515ef3c373a22a7b5e992db409d617eef0e2 Mon Sep 17 00:00:00 2001 From: Kashif Date: Sun, 10 Nov 2024 12:19:56 +0530 Subject: [PATCH 3/6] refactor: populate payment_method_type for card during PaymentAttempt insert --- crates/diesel_models/src/payment_attempt.rs | 60 ---------------- .../src/payments/payment_attempt.rs | 11 --- .../payments/operations/payment_create.rs | 32 ++++++++- .../payments/operations/payment_response.rs | 68 +------------------ 4 files changed, 33 insertions(+), 138 deletions(-) diff --git a/crates/diesel_models/src/payment_attempt.rs b/crates/diesel_models/src/payment_attempt.rs index 7fc861e003ee..e233cfe390bb 100644 --- a/crates/diesel_models/src/payment_attempt.rs +++ b/crates/diesel_models/src/payment_attempt.rs @@ -549,10 +549,6 @@ pub enum PaymentAttemptUpdate { updated_by: String, connector_metadata: Option, }, - PaymentMethodTypeUpdate { - payment_method_type: Option, - updated_by: String, - }, } #[cfg(feature = "v2")] @@ -3283,62 +3279,6 @@ impl From for PaymentAttemptUpdateInternal { connector_transaction_data: None, connector_mandate_detail: None, }, - PaymentAttemptUpdate::PaymentMethodTypeUpdate { - payment_method_type, - updated_by, - } => Self { - modified_at: common_utils::date_time::now(), - payment_method_type, - updated_by, - status: None, - error_code: None, - error_message: None, - error_reason: None, - unified_code: None, - unified_message: None, - amount: None, - net_amount: None, - currency: None, - connector_transaction_id: None, - amount_to_capture: None, - connector: None, - authentication_type: None, - payment_method: None, - payment_method_id: None, - cancellation_reason: None, - mandate_id: None, - browser_info: None, - payment_token: None, - connector_metadata: None, - payment_method_data: None, - payment_experience: None, - business_sub_label: None, - straight_through_algorithm: None, - preprocessing_step_id: None, - capture_method: None, - connector_response_reference_id: None, - multiple_capture_count: None, - surcharge_amount: None, - tax_amount: None, - amount_capturable: None, - merchant_connector_id: None, - authentication_data: None, - encoded_data: None, - external_three_ds_authentication_attempted: None, - authentication_connector: None, - authentication_id: None, - fingerprint_id: None, - payment_method_billing_address_id: None, - charge_id: None, - client_source: None, - client_version: None, - customer_acceptance: None, - card_network: None, - shipping_cost: None, - order_tax_amount: None, - connector_transaction_data: None, - connector_mandate_detail: None, - }, } } } diff --git a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs index 1211aa0275d9..a65abe1495e1 100644 --- a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs +++ b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs @@ -914,10 +914,6 @@ pub enum PaymentAttemptUpdate { updated_by: String, connector_metadata: Option, }, - PaymentMethodTypeUpdate { - payment_method_type: Option, - updated_by: String, - }, } #[cfg(feature = "v1")] @@ -1280,13 +1276,6 @@ impl PaymentAttemptUpdate { updated_by, connector_metadata, }, - Self::PaymentMethodTypeUpdate { - payment_method_type, - updated_by, - } => DieselPaymentAttemptUpdate::PaymentMethodTypeUpdate { - payment_method_type, - updated_by, - }, } } } diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs index c65e58ea2d48..abe4a55e8daf 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; +use std::{marker::PhantomData, str::FromStr}; use api_models::{ enums::FrmSuggestion, mandates::RecurringDetails, payment_methods::PaymentMethodsData, @@ -1195,6 +1195,36 @@ impl PaymentCreate { None }; + 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) + }); + Ok(( storage::PaymentAttemptNew { payment_id: payment_id.to_owned(), diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rs index ec3f845a4a69..45a4eacfc276 100644 --- a/crates/router/src/core/payments/operations/payment_response.rs +++ b/crates/router/src/core/payments/operations/payment_response.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, str::FromStr}; +use std::collections::HashMap; use api_models::payments::{ConnectorMandateReferenceId, MandateReferenceId}; #[cfg(feature = "dynamic_routing")] @@ -36,7 +36,7 @@ use crate::{ }, tokenization, types::MultipleCaptureData, - OperationSessionGetters, PaymentData, PaymentMethodChecker, + PaymentData, PaymentMethodChecker, }, utils as core_utils, }, @@ -1243,67 +1243,6 @@ impl PostUpdateTracker, types::CompleteAuthorizeData } } -async fn update_payment_method_type_for_cards( - state: &SessionState, - storage_scheme: enums::MerchantStorageScheme, - payment_data: &mut PaymentData, -) -> RouterResult<()> { - // Parse Value to AdditionalPaymentData - let additional_payment_method_data: Option = - payment_data - .get_payment_attempt() - .payment_method_data - .clone() - .map(|data| data.parse_value("AdditionalPaymentData")) - .transpose() - .change_context(errors::ApiErrorResponse::InvalidDataValue { - field_name: "payment_method_data", - })?; - - // Fetch card_type from this struct for cards - let payment_method_type = additional_payment_method_data.and_then(|data| match data { - api_models::payments::AdditionalPaymentData::Card(card) => { - card.card_type.as_ref().and_then(|card_type_str| { - match enums::PaymentMethodType::from_str(&card_type_str.to_lowercase()) { - Ok(payment_method_type) => Some(payment_method_type), - // Do not throw errors in case of deserialization errors - Err(e) => { - logger::error!( - "Invalid card_type value in AdditionalPaymentData - {:?}\nErr - {:?}", - card.card_type, - e - ); - None - } - } - }) - } - _ => None, - }); - - // If payment_method_type in PaymentAttempt does not match the one in payment_method_data - // update payment_method_type in PaymentAttempt table - if payment_method_type.is_some() - && payment_data.payment_attempt.payment_method_type.as_ref() != payment_method_type.as_ref() - { - payment_data.payment_attempt.payment_method_type = payment_method_type; - let payment_attempt_update = storage::PaymentAttemptUpdate::PaymentMethodTypeUpdate { - payment_method_type, - updated_by: storage_scheme.to_string(), - }; - state - .store - .update_payment_attempt_with_attempt_id( - payment_data.payment_attempt.clone(), - payment_attempt_update, - storage_scheme, - ) - .await - .to_not_found_response(errors::ApiErrorResponse::PaymentNotFound)?; - } - Ok(()) -} - #[cfg(feature = "v1")] #[instrument(skip_all)] #[allow(clippy::too_many_arguments)] @@ -1319,9 +1258,6 @@ async fn payment_response_update_tracker( >, #[cfg(all(feature = "v1", feature = "dynamic_routing"))] business_profile: &domain::Profile, ) -> RouterResult> { - // Populate payment_method_type for cards in case it's empty or mismatched - update_payment_method_type_for_cards(state, storage_scheme, &mut payment_data).await?; - // Update additional payment data with the payment method response that we received from connector let additional_payment_method_data = match payment_data.payment_method_data.clone() { Some(payment_method_data) => match payment_method_data { From fd38e6224d26841def853850c3925d9b65c1ba46 Mon Sep 17 00:00:00 2001 From: Kashif Date: Sun, 10 Nov 2024 12:22:08 +0530 Subject: [PATCH 4/6] refactor: remove redundant code --- crates/router/src/core/payments/operations/payment_response.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rs index 45a4eacfc276..427a406dbf59 100644 --- a/crates/router/src/core/payments/operations/payment_response.rs +++ b/crates/router/src/core/payments/operations/payment_response.rs @@ -1259,6 +1259,7 @@ async fn payment_response_update_tracker( #[cfg(all(feature = "v1", feature = "dynamic_routing"))] business_profile: &domain::Profile, ) -> RouterResult> { // Update additional payment data with the payment method response that we received from connector + let additional_payment_method_data = match payment_data.payment_method_data.clone() { Some(payment_method_data) => match payment_method_data { hyperswitch_domain_models::payment_method_data::PaymentMethodData::Card(_) From 7c9ff20286e08127652ed25e495c2c368562fadd Mon Sep 17 00:00:00 2001 From: Kashif Date: Sun, 10 Nov 2024 19:21:54 +0530 Subject: [PATCH 5/6] 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 { From 1396e043fb3ffd275a5ddfcf8bdd7144b225d19c Mon Sep 17 00:00:00 2001 From: Kashif Date: Mon, 11 Nov 2024 17:56:28 +0530 Subject: [PATCH 6/6] refactor: use impls instead of dangling fns --- crates/router/src/core/payments/helpers.rs | 33 ----------------- .../payments/operations/payment_confirm.rs | 9 +++-- .../payments/operations/payment_create.rs | 9 +++-- .../router/src/core/payments/transformers.rs | 35 +++++++++++++++++++ 4 files changed, 43 insertions(+), 43 deletions(-) diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs index df34c9ff6181..acaccccbb17b 100644 --- a/crates/router/src/core/payments/helpers.rs +++ b/crates/router/src/core/payments/helpers.rs @@ -6102,36 +6102,3 @@ 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 c1eb9da23737..9bbe7cca3912 100644 --- a/crates/router/src/core/payments/operations/payment_confirm.rs +++ b/crates/router/src/core/payments/operations/payment_confirm.rs @@ -644,11 +644,10 @@ 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(), - ); + let payment_method_type = Option::::foreign_from(( + payment_method_type, + additional_pm_data.as_ref(), + )); payment_attempt.payment_method_type = payment_method_type .or(payment_attempt.payment_method_type) diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs index 4a8b830ae70d..1f0884fe3849 100644 --- a/crates/router/src/core/payments/operations/payment_create.rs +++ b/crates/router/src/core/payments/operations/payment_create.rs @@ -1195,11 +1195,10 @@ impl PaymentCreate { None }; - let payment_method_type = - helpers::get_optional_card_type_from_additional_payment_method_data( - payment_method_type, - additional_pm_data.as_ref(), - ); + let payment_method_type = Option::::foreign_from(( + payment_method_type, + additional_pm_data.as_ref(), + )); Ok(( storage::PaymentAttemptNew { diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs index d37b249cd8a9..ba77ee5ae8b7 100644 --- a/crates/router/src/core/payments/transformers.rs +++ b/crates/router/src/core/payments/transformers.rs @@ -3357,3 +3357,38 @@ impl ForeignFrom for DieselConnectorMandateReferenc } } } + +impl ForeignFrom<(Self, Option<&api_models::payments::AdditionalPaymentData>)> + for Option +{ + fn foreign_from(req: (Self, Option<&api_models::payments::AdditionalPaymentData>)) -> Self { + let (payment_method_type, additional_pm_data) = req; + 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| { + crate::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 { + crate::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) + }) + } +}