fix(core): Update pm_status accordingly for the respective attempt status (#5557)

Co-authored-by: Shankar Singh C <shankar.singh@juspay.in>
Co-authored-by: Shankar Singh C <83439957+ShankarSinghC@users.noreply.github.com>
This commit is contained in:
Amisha Prabhat
2024-08-07 21:05:41 +05:30
committed by GitHub
parent 8881e9774e
commit 9d44eaeb92
3 changed files with 40 additions and 23 deletions

View File

@ -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,
}
}
}

View File

@ -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 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<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")?;

View File

@ -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),