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

Code before:
                            .change_context(errors::ApiErrorResponse::InternalServerError)
                            .attach_printable("Failed while getting card details from locker")?,
                        locker_card_response.get_metadata(),
                    )
                } else {
                    (
                        self.get_card_details_without_locker_fallback(&pm).await?,
                        None,
                    )
                };
                (Some(card_detail), payload_metadata)
            } else {
                (None, None)
            };
        Ok(services::ApplicationResponse::Json(
            api::PaymentMethodResponse {
                merchant_id: pm.merchant_id.clone(),
                customer_id: Some(pm.customer_id.clone()),
                payment_method_id: pm.payment_method_id.clone(),
                payment_method: pm.get_payment_method_type(),
                payment_method_type: pm.get_payment_method_subtype(),
                #[cfg(feature = "payouts")]
                bank_transfer: None,
                card,
                // merge payload metadata with pm metadata
                metadata: merge_json_values(payload_metadata, pm.metadata),
                created: Some(pm.created_at),
                recurring_enabled: Some(false),
                installment_payment_enabled: Some(false),
                payment_experience: Some(vec![api_models::enums::PaymentExperience::RedirectToUrl]),
                last_used_at: Some(pm.last_used_at),
                client_secret: pm.client_secret,
            },
        ))
    }

    #[cfg(feature = "v1")]
    #[instrument(skip_all)]
    async fn delete_payment_method(
        &self,
        pm_id: api::PaymentMethodId,
    ) -> errors::RouterResponse<api::PaymentMethodDeleteResponse> {
        let db = self.state.store.as_ref();
        let key = db
            .find_payment_method(
                self.provider.get_key_store(),
                pm_id.payment_method_id.as_str(),
                self.provider.get_account().storage_scheme,
            )
            .await
            .to_not_found_response(errors::ApiErrorResponse::PaymentMethodNotFound)?;

Review comment (suggestion):
Can we have it as a impl on `PaymentMethod`

pm.get_payment_method_metadata(locker_card_response.metadata)

Code after:
                        locker_card_response.get_metadata(),
                    )
                } else {
                    (
                        self.get_card_details_without_locker_fallback(&pm).await?,
                        None,
                    )
                };
                (Some(card_detail), locker_metadata)
            } else {
                (None, None)
            };
        Ok(services::ApplicationResponse::Json(
            api::PaymentMethodResponse {
                merchant_id: pm.merchant_id.clone(),
                customer_id: Some(pm.customer_id.clone()),
                payment_method_id: pm.payment_method_id.clone(),
                payment_method: pm.get_payment_method_type(),
                payment_method_type: pm.get_payment_method_subtype(),
                #[cfg(feature = "payouts")]
                bank_transfer: None,
                card,
                // merge payload metadata with pm metadata
                metadata: pm.get_payment_method_metadata(locker_metadata),
                created: Some(pm.created_at),
                recurring_enabled: Some(false),
                installment_payment_enabled: Some(false),
                payment_experience: Some(vec![api_models::enums::PaymentExperience::RedirectToUrl]),
                last_used_at: Some(pm.last_used_at),
                client_secret: pm.client_secret,
            },
        ))
    }

    #[cfg(feature = "v1")]
    #[instrument(skip_all)]
    async fn delete_payment_method(
        &self,
        pm_id: api::PaymentMethodId,
    ) -> errors::RouterResponse<api::PaymentMethodDeleteResponse> {
        let db = self.state.store.as_ref();
        let key = db
            .find_payment_method(
                self.provider.get_key_store(),
                pm_id.payment_method_id.as_str(),
                self.provider.get_account().storage_scheme,
            )
            .await
            .to_not_found_response(errors::ApiErrorResponse::PaymentMethodNotFound)?;

        let customer = db