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_methods): enable JWT auth for list payment methods API (#11595)

Gitanjli2ba0417735f4
diff --git a/crates/router/src/core/payment_methods/cards.rs b/crates/router/src/core/payment_methods/cards.rs
index 160b36dd84..e5b0593854 100644
--- a/crates/router/src/core/payment_methods/cards.rs
+++ b/crates/router/src/core/payment_methods/cards.rs
@@ -2952,6 +2952,9 @@ fn get_val(str: String, val: &serde_json::Value) -> Option<String> {
pub async fn list_payment_methods(
state: routes::SessionState,
platform: domain::Platform,
+ // Profile resolved during JWT authentication. Used as a fallback to determine
+ // the business profile when no client_secret is provided and payment_intent is unavailable.
+ auth_profile: Option<Profile>,
mut req: api::PaymentMethodListRequest,
) -> errors::RouterResponse<api::PaymentMethodListResponse> {
let db = &*state.store;
@@ -3071,24 +3074,34 @@ pub async fn list_payment_methods(
.await
.to_not_found_response(errors::ApiErrorResponse::MerchantAccountNotFound)?;
- let profile_id = payment_intent
- .as_ref()
- .and_then(|payment_intent| payment_intent.profile_id.as_ref())
- .get_required_value("profile_id")
- .change_context(errors::ApiErrorResponse::GenericNotFoundError {
- message: "Profile id not found".to_string(),
- })?;
- let business_profile = db
- .find_business_profile_by_profile_id(platform.get_processor().get_key_store(), profile_id)
- .await
- .to_not_found_response(errors::ApiErrorResponse::ProfileNotFound {
- id: profile_id.get_string_repr().to_owned(),
- })?;
+ let (profile_id, business_profile) =
+ match payment_intent.as_ref().and_then(|pi| pi.profile_id.clone()) {
+ Some(profile_id) => {
+ let business_profile = db
+ .find_business_profile_by_profile_id(
+ platform.get_processor().get_key_store(),
+ &profile_id,
+ )
+ .await
+ .to_not_found_response(errors::ApiErrorResponse::ProfileNotFound {
+ id: profile_id.get_string_repr().to_owned(),
+ })?;
+ (profile_id, business_profile)
+ }
+ None => {
+ let profile =
+ auth_profile.ok_or(errors::ApiErrorResponse::GenericNotFoundError {
+ message: "Profile id not found".to_string(),
+ })?;
+ let profile_id = profile.get_id().clone();
+ (profile_id, profile)
+ }
+ };
// filter out payment connectors based on profile_id
let filtered_mcas = all_mcas
.clone()
- .filter_based_on_profile_and_connector_type(profile_id, ConnectorType::PaymentProcessor);
+ .filter_based_on_profile_and_connector_type(&profile_id, ConnectorType::PaymentProcessor);
logger::debug!(mca_before_filtering=?filtered_mcas);
diff --git a/crates/router/src/routes/payment_methods.rs b/crates/router/src/routes/payment_methods.rs
index 7fd9a3fcc5..7ad9dbe4f6 100644
--- a/crates/router/src/routes/payment_methods.rs
+++ b/crates/router/src/routes/payment_methods.rs
@@ -673,9 +673,20 @@ pub async fn list_payment_method_api(
allow_platform_self_operation: true,
};
- let (auth, _) = match auth::check_sdk_auth_and_get_auth(req.headers(), &payload, api_auth) {
- Ok((auth, _auth_flow)) => (auth, _auth_flow),
- Err(e) => return api::log_and_return_error_response(e),
+ let (auth_type, _auth_flow) = if auth::is_jwt_auth(req.headers()) {
+ let jwt_auth: Box<dyn auth::AuthenticateAndFetch<auth::AuthenticationData, _>> =
+ Box::new(auth::JWTAuth {
+ permission: Permission::MerchantPaymentRead,
+ allow_connected: true,
+ allow_platform: true,
+ });
+
+ (jwt_auth, api::AuthFlow::Merchant)
+ } else {
+ match auth::check_sdk_auth_and_get_auth(req.headers(), &payload, api_auth) {
+ Ok(auth) => auth,
+ Err(e) => return api::log_and_return_error_response(e),
+ }
};
Box::pin(api::server_wrap(
@@ -689,9 +700,9 @@ pub async fn list_payment_method_api(
}
// TODO (#7195): Fill platform_merchant_account in the client secret auth and pass it here.
- cards::list_payment_methods(state, auth.platform, req)
+ cards::list_payment_methods(state, auth.platform, auth.profile, req)
},
- &*auth,
+ &*auth_type,
api_locking::LockAction::NotApplicable,
))
.await