Skip to content

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

refactor(payment_methods): Fix db insert for payment method create (#1651)

Sarthak Soni73f91a5eee30
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index 0b4d58b8d3..499b8d2419 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -86,20 +86,26 @@ pub async fn add_payment_method(
let merchant_id = &merchant_account.merchant_id;
let customer_id = req.customer_id.clone().get_required_value("customer_id")?;
let response = match req.card.clone() {
- Some(card) => add_card_to_locker(state, req, card, customer_id, merchant_account)
- .await
- .change_context(errors::ApiErrorResponse::InternalServerError)
- .attach_printable("Add Card Failed"),
+ Some(card) => add_card_to_locker(
+ state,
+ req.clone(),
+ card,
+ customer_id.clone(),
+ merchant_account,
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Add Card Failed"),
None => {
let pm_id = generate_id(consts::ID_LENGTH, "pm");
let payment_method_response = api::PaymentMethodResponse {
merchant_id: merchant_id.to_string(),
- customer_id: Some(customer_id),
+ customer_id: Some(customer_id.clone()),
payment_method_id: pm_id,
payment_method: req.payment_method,
payment_method_type: req.payment_method_type,
card: None,
- metadata: req.metadata,
+ metadata: req.metadata.clone(),
created: Some(common_utils::date_time::now()),
recurring_enabled: false, //[#219]
installment_payment_enabled: false, //[#219]
@@ -108,7 +114,24 @@ pub async fn add_payment_method(
Ok((payment_method_response, false))
}
};
- Ok(response?.0).map(services::ApplicationResponse::Json)
+
+ let (resp, is_duplicate) = response?;
+ if !is_duplicate {
+ let pm_metadata = resp.metadata.as_ref().map(|data| data.peek());
+ create_payment_method(
+ &*state.store,
+ &req,
+ &customer_id,
+ &resp.payment_method_id,
+ &resp.merchant_id,
+ pm_metadata.cloned(),
+ )
+ .await
+ .change_context(errors::ApiErrorResponse::InternalServerError)
+ .attach_printable("Failed to save Payment Method")?;
+ }
+
+ Ok(resp).map(services::ApplicationResponse::Json)
}
#[instrument(skip_all)]