Skip to content

Commit

Permalink
feat(router): migrate payment_method_data only for cards
Browse files Browse the repository at this point in the history
  • Loading branch information
ShankarSinghC committed Nov 21, 2023
1 parent 94897d8 commit bc62ea6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 49 deletions.
101 changes: 56 additions & 45 deletions crates/router/src/core/locker_migration.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -79,52 +79,63 @@ pub async fn call_to_locker(
) -> CustomResult<usize, errors::ApiErrorResponse> {
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)
Expand Down
15 changes: 11 additions & 4 deletions crates/router/src/core/payment_methods/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit bc62ea6

Please sign in to comment.