From bc62ea640f45bd31bc543a57d5ef52ad9478a321 Mon Sep 17 00:00:00 2001 From: Shankar Singh C Date: Tue, 21 Nov 2023 15:05:30 +0530 Subject: [PATCH 1/3] feat(router): migrate payment_method_data only for cards --- crates/router/src/core/locker_migration.rs | 101 ++++++++++-------- .../router/src/core/payment_methods/cards.rs | 15 ++- 2 files changed, 67 insertions(+), 49 deletions(-) diff --git a/crates/router/src/core/locker_migration.rs b/crates/router/src/core/locker_migration.rs index f036a03a2f0e..5f61acea8f3a 100644 --- a/crates/router/src/core/locker_migration.rs +++ b/crates/router/src/core/locker_migration.rs @@ -1,6 +1,6 @@ use api_models::{enums as api_enums, locker_migration::MigrateCardResponse}; use common_utils::errors::CustomResult; -use diesel_models::PaymentMethod; +use diesel_models::{enums as storage_enums, PaymentMethod}; use error_stack::{FutureExt, ResultExt}; use futures::TryFutureExt; @@ -79,52 +79,63 @@ pub async fn call_to_locker( ) -> CustomResult { let mut cards_moved = 0; - for pm in payment_methods { - let card = - cards::get_card_from_locker(state, customer_id, merchant_id, &pm.payment_method_id) - .await?; - - let card_details = api::CardDetail { - card_number: card.card_number, - card_exp_month: card.card_exp_month, - card_exp_year: card.card_exp_year, - card_holder_name: card.name_on_card, - nick_name: card.nick_name.map(masking::Secret::new), - }; - - let pm_create = api::PaymentMethodCreate { - payment_method: pm.payment_method, - payment_method_type: pm.payment_method_type, - payment_method_issuer: pm.payment_method_issuer, - payment_method_issuer_code: pm.payment_method_issuer_code, - card: Some(card_details.clone()), - metadata: pm.metadata, - customer_id: Some(pm.customer_id), - card_network: card.card_brand, + for pm in payment_methods + .into_iter() + .filter(|pm| matches!(pm.payment_method, storage_enums::PaymentMethod::Card)) + { + let falliable: Result<(), errors::ApiErrorResponse> = { + let card = + cards::get_card_from_locker(state, customer_id, merchant_id, &pm.payment_method_id) + .await?; + + let card_details = api::CardDetail { + card_number: card.card_number, + card_exp_month: card.card_exp_month, + card_exp_year: card.card_exp_year, + card_holder_name: card.name_on_card, + nick_name: card.nick_name.map(masking::Secret::new), + }; + + let pm_create = api::PaymentMethodCreate { + payment_method: pm.payment_method, + payment_method_type: pm.payment_method_type, + payment_method_issuer: pm.payment_method_issuer, + payment_method_issuer_code: pm.payment_method_issuer_code, + card: Some(card_details.clone()), + metadata: pm.metadata, + customer_id: Some(pm.customer_id), + card_network: card.card_brand, + }; + + let (_add_card_rs_resp, _is_duplicate) = cards::add_card_hs( + state, + pm_create, + &card_details, + customer_id.to_string(), + merchant_account, + api_enums::LockerChoice::Tartarus, + Some(&pm.payment_method_id), + ) + .await + .change_context(errors::ApiErrorResponse::InternalServerError) + .attach_printable(format!( + "Card migration failed for merchant_id: {merchant_id}, customer_id: {customer_id}, payment_method_id: {} ", + pm.payment_method_id + ))?; + + cards_moved += 1; + + logger::info!( + "Card migrated for merchant_id: {merchant_id}, customer_id: {customer_id}, payment_method_id: {} ", + pm.payment_method_id + ); + Ok(()) }; - let (_add_card_rs_resp, _is_duplicate) = cards::add_card_hs( - state, - pm_create, - &card_details, - customer_id.to_string(), - merchant_account, - api_enums::LockerChoice::Tartarus, - Some(&pm.payment_method_id), - ) - .await - .change_context(errors::ApiErrorResponse::InternalServerError) - .attach_printable(format!( - "Card migration failed for merchant_id: {merchant_id}, customer_id: {customer_id}, payment_method_id: {} ", - pm.payment_method_id - ))?; - - cards_moved += 1; - - logger::info!( - "Card migrated for merchant_id: {merchant_id}, customer_id: {customer_id}, payment_method_id: {} ", - pm.payment_method_id - ); + match falliable { + Ok(_) => {} + Err(err) => logger::error!(error =? err, "Failed while migrating"), + } } Ok(cards_moved) diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs index 80daf66a6926..229235ded382 100644 --- a/crates/router/src/core/payment_methods/cards.rs +++ b/crates/router/src/core/payment_methods/cards.rs @@ -251,11 +251,18 @@ pub async fn add_card_to_locker( &metrics::CARD_ADD_TIME, &[], ) - .await?; - - logger::debug!("card added to rust locker"); + .await; - Ok(add_card_to_rs_resp) + match add_card_to_rs_resp { + value @ Ok(_) => { + logger::debug!("Card added successfully"); + value + } + Err(err) => { + logger::debug!(error =? err,"failed to add card"); + Ok(add_card_to_hs_resp) + } + } } pub async fn get_card_from_locker( From f27ebf9c4e719c937b8ce1352071404b907c0f69 Mon Sep 17 00:00:00 2001 From: Shankar Singh C Date: Tue, 21 Nov 2023 19:14:44 +0530 Subject: [PATCH 2/3] address pr comments --- crates/router/src/core/locker_migration.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/router/src/core/locker_migration.rs b/crates/router/src/core/locker_migration.rs index 5f61acea8f3a..7817398a0db9 100644 --- a/crates/router/src/core/locker_migration.rs +++ b/crates/router/src/core/locker_migration.rs @@ -83,7 +83,7 @@ pub async fn call_to_locker( .into_iter() .filter(|pm| matches!(pm.payment_method, storage_enums::PaymentMethod::Card)) { - let falliable: Result<(), errors::ApiErrorResponse> = { + let fallible: Result<(), errors::ApiErrorResponse> = { let card = cards::get_card_from_locker(state, customer_id, merchant_id, &pm.payment_method_id) .await?; @@ -132,7 +132,7 @@ pub async fn call_to_locker( Ok(()) }; - match falliable { + match fallible { Ok(_) => {} Err(err) => logger::error!(error =? err, "Failed while migrating"), } From fd4caae314a12b22c72db742fb7354fa86668e05 Mon Sep 17 00:00:00 2001 From: Shankar Singh C Date: Tue, 21 Nov 2023 20:08:19 +0530 Subject: [PATCH 3/3] refactor for error suppressing --- crates/router/src/core/locker_migration.rs | 78 ++++++++++++---------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/crates/router/src/core/locker_migration.rs b/crates/router/src/core/locker_migration.rs index 7817398a0db9..3f56cddee126 100644 --- a/crates/router/src/core/locker_migration.rs +++ b/crates/router/src/core/locker_migration.rs @@ -83,31 +83,38 @@ pub async fn call_to_locker( .into_iter() .filter(|pm| matches!(pm.payment_method, storage_enums::PaymentMethod::Card)) { - let fallible: Result<(), errors::ApiErrorResponse> = { - let card = - cards::get_card_from_locker(state, customer_id, merchant_id, &pm.payment_method_id) - .await?; - - let card_details = api::CardDetail { - card_number: card.card_number, - card_exp_month: card.card_exp_month, - card_exp_year: card.card_exp_year, - card_holder_name: card.name_on_card, - nick_name: card.nick_name.map(masking::Secret::new), - }; - - let pm_create = api::PaymentMethodCreate { - payment_method: pm.payment_method, - payment_method_type: pm.payment_method_type, - payment_method_issuer: pm.payment_method_issuer, - payment_method_issuer_code: pm.payment_method_issuer_code, - card: Some(card_details.clone()), - metadata: pm.metadata, - customer_id: Some(pm.customer_id), - card_network: card.card_brand, - }; - - let (_add_card_rs_resp, _is_duplicate) = cards::add_card_hs( + let card = + cards::get_card_from_locker(state, customer_id, merchant_id, &pm.payment_method_id) + .await; + + let card = match card { + Ok(card) => card, + Err(err) => { + logger::error!("Failed to fetch card from Basilisk HS locker : {:?}", err); + continue; + } + }; + + let card_details = api::CardDetail { + card_number: card.card_number, + card_exp_month: card.card_exp_month, + card_exp_year: card.card_exp_year, + card_holder_name: card.name_on_card, + nick_name: card.nick_name.map(masking::Secret::new), + }; + + let pm_create = api::PaymentMethodCreate { + payment_method: pm.payment_method, + payment_method_type: pm.payment_method_type, + payment_method_issuer: pm.payment_method_issuer, + payment_method_issuer_code: pm.payment_method_issuer_code, + card: Some(card_details.clone()), + metadata: pm.metadata, + customer_id: Some(pm.customer_id), + card_network: card.card_brand, + }; + + let add_card_result = cards::add_card_hs( state, pm_create, &card_details, @@ -121,21 +128,22 @@ pub async fn call_to_locker( .attach_printable(format!( "Card migration failed for merchant_id: {merchant_id}, customer_id: {customer_id}, payment_method_id: {} ", pm.payment_method_id - ))?; + )); + + let (_add_card_rs_resp, _is_duplicate) = match add_card_result { + Ok(output) => output, + Err(err) => { + logger::error!("Failed to add card to Rust locker : {:?}", err); + continue; + } + }; - cards_moved += 1; + cards_moved += 1; - logger::info!( + logger::info!( "Card migrated for merchant_id: {merchant_id}, customer_id: {customer_id}, payment_method_id: {} ", pm.payment_method_id ); - Ok(()) - }; - - match fallible { - Ok(_) => {} - Err(err) => logger::error!(error =? err, "Failed while migrating"), - } } Ok(cards_moved)