Repository: juspay/hyperswitch
File: crates/router/src/core/payment_methods/cards.rs (Rust)

Code before:
                        merchant_context.get_merchant_account().storage_scheme,
                    )
                    .await
                    .change_context(errors::ApiErrorResponse::InternalServerError)
                    .attach_printable("Failed to update payment method in db")?;

                    return Err(e.attach_printable("Failed to add card to locker"));
                }
            }
        }
    }
}

#[cfg(feature = "v1")]
#[instrument(skip_all)]
pub async fn update_customer_payment_method(
    state: routes::SessionState,
    merchant_context: domain::MerchantContext,
    req: api::PaymentMethodUpdate,
    payment_method_id: &str,
    pm_data: Option<domain::PaymentMethod>,
) -> errors::RouterResponse<api::PaymentMethodResponse> {
    // Currently update is supported only for cards
    if let Some(card_update) = req.card.clone() {
        let db = state.store.as_ref();
        let pm = if let Some(pm) = pm_data.clone() {
            pm
        } else {
            db.find_payment_method(
                &((&state).into()),
                merchant_context.get_merchant_key_store(),
                payment_method_id,
                merchant_context.get_merchant_account().storage_scheme,
            )
            .await
            .to_not_found_response(errors::ApiErrorResponse::PaymentMethodNotFound)?
        };

        if let Some(cs) = &req.client_secret {
            let is_client_secret_expired = authenticate_pm_client_secret_and_check_expiry(cs, &pm)?;

            if is_client_secret_expired {
                return Err((errors::ApiErrorResponse::ClientSecretExpired).into());
            };
        };

        if pm.status == enums::PaymentMethodStatus::AwaitingData {
            return Err(report!(errors::ApiErrorResponse::NotSupported {
                message: "Payment method is awaiting data so it cannot be updated".into()
            }));
        }

Review comment (suggestion):
```suggestion
        let pm = if let Some(pm) = pm_data {
```

Code after:
                        &((&state).into()),
                        merchant_context.get_merchant_key_store(),
                        payment_method,
                        pm_update,
                        merchant_context.get_merchant_account().storage_scheme,
                    )
                    .await
                    .change_context(errors::ApiErrorResponse::InternalServerError)
                    .attach_printable("Failed to update payment method in db")?;

                    return Err(e.attach_printable("Failed to add card to locker"));
                }
            }
        }
    }
}

#[cfg(feature = "v1")]
#[instrument(skip_all)]
pub async fn update_customer_payment_method(
    state: routes::SessionState,
    merchant_context: domain::MerchantContext,
    req: api::PaymentMethodUpdate,
    payment_method_id: &str,

    pm_data: Option<domain::PaymentMethod>,
) -> errors::RouterResponse<api::CustomerPaymentMethodUpdateResponse> {
    let db = state.store.as_ref();

    let pm = if let Some(pm) = pm_data {
        pm
    } else {
        db.find_payment_method(
            &((&state).into()),
            merchant_context.get_merchant_key_store(),
            payment_method_id,
            merchant_context.get_merchant_account().storage_scheme,
        )
        .await
        .to_not_found_response(errors::ApiErrorResponse::PaymentMethodNotFound)?
    };

    if let Some(cs) = &req.client_secret {
        let is_client_secret_expired = authenticate_pm_client_secret_and_check_expiry(cs, &pm)?;

        if is_client_secret_expired {
            return Err((errors::ApiErrorResponse::ClientSecretExpired).into());
        };
    };

    // Currently update is supported only for cards and wallets