Skip to content

Commit

Permalink
fix(router): update nick_name only if card_token.card_holder_name is …
Browse files Browse the repository at this point in the history
…non empty and populate additional card_details from payment_attempt if not present in the locker (#6345)
  • Loading branch information
sai-harsha-vardhan authored Oct 17, 2024
1 parent 75a67f2 commit 3f29209
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
3 changes: 3 additions & 0 deletions crates/router/src/core/payment_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<domain::Customer>,
storage_scheme: common_enums::enums::MerchantStorageScheme,
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand Down
51 changes: 45 additions & 6 deletions crates/router/src/core/payments/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<(domain::PaymentMethodData, enums::PaymentMethod)>> {
Expand All @@ -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() {
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand Down

0 comments on commit 3f29209

Please sign in to comment.