Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(router): migrate payment_method_data to rust locker only if payment_method is card #2931

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 40 additions & 21 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,10 +79,21 @@ pub async fn call_to_locker(
) -> CustomResult<usize, errors::ApiErrorResponse> {
let mut cards_moved = 0;

for pm in payment_methods {
for pm in payment_methods
.into_iter()
.filter(|pm| matches!(pm.payment_method, storage_enums::PaymentMethod::Card))
{
let card =
cards::get_card_from_locker(state, customer_id, merchant_id, &pm.payment_method_id)
.await?;
.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,
Expand All @@ -103,28 +114,36 @@ pub async fn call_to_locker(
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
))?;
let add_card_result = 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
));

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;

logger::info!(
"Card migrated for merchant_id: {merchant_id}, customer_id: {customer_id}, payment_method_id: {} ",
pm.payment_method_id
);
"Card migrated for merchant_id: {merchant_id}, customer_id: {customer_id}, payment_method_id: {} ",
pm.payment_method_id
);
}

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
Loading