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

feat(payment_method): set the initial payment method as default until its explicitly set (#3970)

Amisha Prabhat34c1b905b178
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index c0595f14f0..dca0d2ccb3 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -86,7 +86,8 @@ pub async fn create_payment_method(
key_store: &domain::MerchantKeyStore,
connector_mandate_details: Option<serde_json::Value>,
) -> errors::CustomResult<storage::PaymentMethod, errors::ApiErrorResponse> {
- db.find_customer_by_customer_id_merchant_id(customer_id, merchant_id, key_store)
+ let customer = db
+ .find_customer_by_customer_id_merchant_id(customer_id, merchant_id, key_store)
.await
.to_not_found_response(errors::ApiErrorResponse::CustomerNotFound)?;
@@ -110,6 +111,17 @@ pub async fn create_payment_method(
.change_context(errors::ApiErrorResponse::InternalServerError)
.attach_printable("Failed to add payment method in db")?;
+ if customer.default_payment_method_id.is_none() {
+ let _ = set_default_payment_method(
+ db,
+ merchant_id.to_string(),
+ key_store.clone(),
+ customer_id,
+ payment_method_id.to_owned(),
+ )
+ .await
+ .map_err(|err| logger::error!(error=?err,"Failed to set the payment method as default"));
+ }
Ok(response)
}
@@ -3137,20 +3149,15 @@ async fn get_bank_account_connector_details(
}
}
pub async fn set_default_payment_method(
- state: routes::AppState,
- merchant_account: domain::MerchantAccount,
+ db: &dyn db::StorageInterface,
+ merchant_id: String,
key_store: domain::MerchantKeyStore,
customer_id: &str,
payment_method_id: String,
) -> errors::RouterResponse<CustomerDefaultPaymentMethodResponse> {
- let db = &*state.store;
//check for the customer
let customer = db
- .find_customer_by_customer_id_merchant_id(
- customer_id,
- &merchant_account.merchant_id,
- &key_store,
- )
+ .find_customer_by_customer_id_merchant_id(customer_id, &merchant_id, &key_store)
.await
.to_not_found_response(errors::ApiErrorResponse::CustomerNotFound)?;
// check for the presence of payment_method
@@ -3160,8 +3167,7 @@ pub async fn set_default_payment_method(
.to_not_found_response(errors::ApiErrorResponse::PaymentMethodNotFound)?;
utils::when(
- payment_method.customer_id != customer_id
- && payment_method.merchant_id != merchant_account.merchant_id,
+ payment_method.customer_id != customer_id || payment_method.merchant_id != merchant_id,
|| {
Err(errors::ApiErrorResponse::PreconditionFailed {
message: "The payment_method_id is not valid".to_string(),
@@ -3181,14 +3187,14 @@ pub async fn set_default_payment_method(
)?;
let customer_update = CustomerUpdate::UpdateDefaultPaymentMethod {
- default_payment_method_id: Some(payment_method_id.clone()),
+ default_payment_method_id: Some(payment_method_id.to_owned()),
};
// update the db with the default payment method id
let updated_customer_details = db
.update_customer_by_customer_id_merchant_id(
customer_id.to_owned(),
- merchant_account.merchant_id,
+ merchant_id.to_owned(),
customer_update,
&key_store,
)
diff --git a/crates/router/src/core/payments/tokenization.rs b/crates/router/src/core/payments/tokenization.rs
index c449b76c60..3a7ab7d9ab 100644
--- a/crates/router/src/core/payments/tokenization.rs
+++ b/crates/router/src/core/payments/tokenization.rs
@@ -380,6 +380,7 @@ where
} else {
None
};
+
resp.payment_method_id = generate_id(consts::ID_LENGTH, "pm");
payment_methods::cards::create_payment_method(
db,
diff --git a/crates/router/src/routes/payment_methods.rs b/crates/router/src/routes/payment_methods.rs
index 5469f98165..939cfaf93d 100644
--- a/crates/router/src/routes/payment_methods.rs
+++ b/crates/router/src/routes/payment_methods.rs
@@ -275,15 +275,16 @@ pub async fn default_payment_method_set_api(
Ok(auth) => auth,
Err(err) => return api::log_and_return_error_response(err),
};
+ let db = &*state.store.clone();
Box::pin(api::server_wrap(
flow,
state,
&req,
payload,
- |state, auth: auth::AuthenticationData, default_payment_method| {
+ |_state, auth: auth::AuthenticationData, default_payment_method| {
cards::set_default_payment_method(
- state,
- auth.merchant_account,
+ db,
+ auth.merchant_account.merchant_id,
auth.key_store,
&customer_id,
default_payment_method.payment_method_id,