Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
fix(core): Update pm_status accordingly for the respective attempt status (#5557)
Amisha Prabhat9d44eaeb92bc
diff --git a/crates/common_enums/src/enums.rs b/crates/common_enums/src/enums.rsindex 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<AttemptStatus> 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<AttemptStatus> 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.rsindex 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<F: Clone>(storage_scheme: enums::MerchantStorageScheme,is_connector_agnostic_mit_enabled: Option<bool>,) -> 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 deletedif 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<F: Clone>(}).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<F: Clone>(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<F: Clone>(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.rsindex 144c7e87e2..ecbfbf03a6 100644--- a/crates/router/src/core/payments/tokenization.rs+++ b/crates/router/src/core/payments/tokenization.rs@@ -321,7 +321,7 @@ wherepm_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 @@ wherepm_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),