From 9d44eaeb92bc110e2ce7e5d6fd69e175a6e25492 Mon Sep 17 00:00:00 2001 From: Amisha Prabhat <55580080+Aprabhat19@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:05:41 +0530 Subject: [PATCH] fix(core): Update pm_status accordingly for the respective attempt status (#5557) Co-authored-by: Shankar Singh C Co-authored-by: Shankar Singh C <83439957+ShankarSinghC@users.noreply.github.com> --- crates/common_enums/src/enums.rs | 9 +++--- .../payments/operations/payment_response.rs | 31 +++++++++++++------ .../router/src/core/payments/tokenization.rs | 23 +++++++++----- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/crates/common_enums/src/enums.rs b/crates/common_enums/src/enums.rs index d8a8d9f60b..7f8f878dc9 100644 --- a/crates/common_enums/src/enums.rs +++ b/crates/common_enums/src/enums.rs @@ -1413,8 +1413,8 @@ pub enum PaymentMethodStatus { impl From for PaymentMethodStatus { fn from(attempt_status: AttemptStatus) -> Self { match attempt_status { - AttemptStatus::Failure => Self::Inactive, - AttemptStatus::Voided + AttemptStatus::Failure + | AttemptStatus::Voided | AttemptStatus::Started | AttemptStatus::Pending | AttemptStatus::Unresolved @@ -1434,9 +1434,8 @@ impl From for PaymentMethodStatus { | AttemptStatus::PartialCharged | AttemptStatus::PartialChargedAndChargeable | AttemptStatus::ConfirmationAwaited - | AttemptStatus::DeviceDataCollectionPending - | AttemptStatus::Charged - | AttemptStatus::Authorized => Self::Active, + | AttemptStatus::DeviceDataCollectionPending => Self::Inactive, + AttemptStatus::Charged | AttemptStatus::Authorized => Self::Active, } } } diff --git a/crates/router/src/core/payments/operations/payment_response.rs b/crates/router/src/core/payments/operations/payment_response.rs index a9449e38d2..d7cebc7d9e 100644 --- a/crates/router/src/core/payments/operations/payment_response.rs +++ b/crates/router/src/core/payments/operations/payment_response.rs @@ -1404,12 +1404,25 @@ async fn update_payment_method_status_and_ntid( storage_scheme: enums::MerchantStorageScheme, is_connector_agnostic_mit_enabled: Option, ) -> RouterResult<()> { + // If the payment_method is deleted then ignore the error related to retrieving payment method + // This should be handled when the payment method is soft deleted if let Some(id) = &payment_data.payment_attempt.payment_method_id { - let pm = state - .store - .find_payment_method(id, storage_scheme) - .await - .to_not_found_response(errors::ApiErrorResponse::PaymentMethodNotFound)?; + let payment_method = match state.store.find_payment_method(id, storage_scheme).await { + Ok(payment_method) => payment_method, + Err(error) => { + if error.current_context().is_db_not_found() { + logger::info!( + "Payment Method not found in db and skipping payment method update {:?}", + error + ); + return Ok(()); + } else { + Err(error) + .change_context(errors::ApiErrorResponse::InternalServerError) + .attach_printable("Error retrieving payment method from db in update_payment_method_status_and_ntid")? + } + } + }; let pm_resp_network_transaction_id = payment_response .map(|resp| if let types::PaymentsResponseData::TransactionResponse { network_txn_id: network_transaction_id, .. } = resp { @@ -1420,7 +1433,6 @@ async fn update_payment_method_status_and_ntid( }) .ok() .flatten(); - let network_transaction_id = if let Some(network_transaction_id) = pm_resp_network_transaction_id { if is_connector_agnostic_mit_enabled == Some(true) @@ -1436,11 +1448,10 @@ async fn update_payment_method_status_and_ntid( None }; - let pm_update = if pm.status != common_enums::PaymentMethodStatus::Active - && pm.status != attempt_status.into() + let pm_update = if payment_method.status != common_enums::PaymentMethodStatus::Active + && payment_method.status != attempt_status.into() { let updated_pm_status = common_enums::PaymentMethodStatus::from(attempt_status); - payment_data .payment_method_info .as_mut() @@ -1458,7 +1469,7 @@ async fn update_payment_method_status_and_ntid( state .store - .update_payment_method(pm, pm_update, storage_scheme) + .update_payment_method(payment_method, pm_update, storage_scheme) .await .change_context(errors::ApiErrorResponse::InternalServerError) .attach_printable("Failed to update payment method in db")?; diff --git a/crates/router/src/core/payments/tokenization.rs b/crates/router/src/core/payments/tokenization.rs index 144c7e87e2..ecbfbf03a6 100644 --- a/crates/router/src/core/payments/tokenization.rs +++ b/crates/router/src/core/payments/tokenization.rs @@ -321,7 +321,7 @@ where pm_data_encrypted.map(Into::into), key_store, connector_mandate_details, - None, + pm_status, network_transaction_id, merchant_account.storage_scheme, encrypted_payment_method_billing_address @@ -406,21 +406,28 @@ where } Err(err) => { if err.current_context().is_db_not_found() { - payment_methods::cards::insert_payment_method( + payment_methods::cards::create_payment_method( state, - &resp, - &payment_method_create_request.clone(), - key_store, - merchant_account.get_id(), + &payment_method_create_request, &customer_id, + &resp.payment_method_id, + locker_id, + merchant_id, resp.metadata.clone().map(|val| val.expose()), customer_acceptance, - locker_id, + pm_data_encrypted.map(Into::into), + key_store, connector_mandate_details, + pm_status, network_transaction_id, merchant_account.storage_scheme, encrypted_payment_method_billing_address .map(Into::into), + resp.card.and_then(|card| { + card.card_network.map(|card_network| { + card_network.to_string() + }) + }), ) .await } else { @@ -610,7 +617,7 @@ where pm_data_encrypted.map(Into::into), key_store, connector_mandate_details, - None, + pm_status, network_transaction_id, merchant_account.storage_scheme, encrypted_payment_method_billing_address.map(Into::into),