diff --git a/crates/router/src/core/payment_methods.rs b/crates/router/src/core/payment_methods.rs index 8841ad7f383a..5baa66a28956 100644 --- a/crates/router/src/core/payment_methods.rs +++ b/crates/router/src/core/payment_methods.rs @@ -523,6 +523,7 @@ pub async fn retrieve_payment_method_with_token( merchant_key_store: &domain::MerchantKeyStore, token_data: &storage::PaymentTokenData, payment_intent: &PaymentIntent, + payment_attempt: &PaymentAttempt, card_token_data: Option<&domain::CardToken>, customer: &Option, storage_scheme: common_enums::enums::MerchantStorageScheme, @@ -536,6 +537,7 @@ pub async fn retrieve_payment_method_with_token( state, &generic_token.token, payment_intent, + payment_attempt, merchant_key_store, card_token_data, ) @@ -555,6 +557,7 @@ pub async fn retrieve_payment_method_with_token( state, &generic_token.token, payment_intent, + payment_attempt, merchant_key_store, card_token_data, ) diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs index 323a5aed4735..c3a47a740c67 100644 --- a/crates/router/src/core/payments/helpers.rs +++ b/crates/router/src/core/payments/helpers.rs @@ -1709,6 +1709,7 @@ pub async fn retrieve_payment_method_with_temporary_token( state: &SessionState, token: &str, payment_intent: &PaymentIntent, + payment_attempt: &PaymentAttempt, merchant_key_store: &domain::MerchantKeyStore, card_token_data: Option<&domain::CardToken>, ) -> RouterResult> { @@ -1735,12 +1736,15 @@ pub async fn retrieve_payment_method_with_temporary_token( // The card_holder_name from locker retrieved card is considered if it is a non-empty string or else card_holder_name is picked // from payment_method_data.card_token object - let name_on_card = card_token_data.and_then(|token_data| { - is_card_updated = true; - token_data.card_holder_name.clone() - }); + let name_on_card = + card_token_data.and_then(|token_data| token_data.card_holder_name.clone()); - updated_card.nick_name = name_on_card; + if let Some(name) = name_on_card.clone() { + if !name.peek().is_empty() { + is_card_updated = true; + updated_card.nick_name = name_on_card; + } + } if let Some(token_data) = card_token_data { if let Some(cvc) = token_data.card_cvc.clone() { @@ -1749,6 +1753,38 @@ pub async fn retrieve_payment_method_with_temporary_token( } } + // populate additional card details from payment_attempt.payment_method_data (additional_payment_data) if not present in the locker + if updated_card.card_issuer.is_none() + || updated_card.card_network.is_none() + || updated_card.card_type.is_none() + || updated_card.card_issuing_country.is_none() + { + let additional_payment_method_data: Option< + api_models::payments::AdditionalPaymentData, + > = payment_attempt + .payment_method_data + .clone() + .and_then(|data| match data { + serde_json::Value::Null => None, // This is to handle the case when the payment_method_data is null + _ => Some(data.parse_value("AdditionalPaymentData")), + }) + .transpose() + .map_err(|err| logger::error!("Failed to parse AdditionalPaymentData {err:?}")) + .ok() + .flatten(); + if let Some(api_models::payments::AdditionalPaymentData::Card(card)) = + additional_payment_method_data + { + is_card_updated = true; + updated_card.card_issuer = updated_card.card_issuer.or(card.card_issuer); + updated_card.card_network = updated_card.card_network.or(card.card_network); + updated_card.card_type = updated_card.card_type.or(card.card_type); + updated_card.card_issuing_country = updated_card + .card_issuing_country + .or(card.card_issuing_country); + }; + }; + if is_card_updated { let updated_pm = domain::PaymentMethodData::Card(updated_card); vault::Vault::store_payment_method_data_in_locker( @@ -2278,6 +2314,7 @@ pub async fn make_pm_data<'a, F: Clone, R, D>( merchant_key_store, hyperswitch_token, &payment_data.payment_intent, + &payment_data.payment_attempt, card_token_data.as_ref(), customer, storage_scheme, @@ -4296,7 +4333,7 @@ pub async fn get_additional_payment_data( api_models::payments::AdditionalPaymentData::Card(Box::new( api_models::payments::AdditionalCardInfo { card_issuer: card_info.card_issuer, - card_network, + card_network: card_info.card_network, bank_code: card_info.bank_code, card_type: card_info.card_type, card_issuing_country: card_info.card_issuing_country, @@ -5637,6 +5674,7 @@ pub async fn get_payment_method_details_from_payment_token( state, &generic_token.token, payment_intent, + payment_attempt, key_store, None, ) @@ -5648,6 +5686,7 @@ pub async fn get_payment_method_details_from_payment_token( state, &generic_token.token, payment_intent, + payment_attempt, key_store, None, )