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
Rust adyenbeginner-friendlyfeaturedfinancefintechhacktoberfesthigh-performanceopen-sourceorchestrationpaymentpayment-gatewaypayment-integrationpayment-processingpaymentspayments-platformrestful-apirustsdkstripeworks-with-react
fix(payment_methods): block deprecated routes for modular merchants (#11592)
Shankar Singh C04a5c53f4297
diff --git a/crates/router/src/compatibility/stripe/errors.rs b/crates/router/src/compatibility/stripe/errors.rsindex 734c1cd7d4..4fba447656 100644--- a/crates/router/src/compatibility/stripe/errors.rs+++ b/crates/router/src/compatibility/stripe/errors.rs@@ -879,6 +879,7 @@ impl ErrorSwitch<StripeErrorCode> for CustomersErrorResponse {Self::MandateActive => SC::MandateActive,Self::CustomerNotFound => SC::CustomerNotFound,Self::CustomerAlreadyExists => SC::DuplicateCustomer,+ Self::AccessForbidden { .. } => SC::Unauthorized,}}}diff --git a/crates/router/src/core/errors/customers_error_response.rs b/crates/router/src/core/errors/customers_error_response.rsindex cde84198b2..41f43a4eb1 100644--- a/crates/router/src/core/errors/customers_error_response.rs+++ b/crates/router/src/core/errors/customers_error_response.rs@@ -19,6 +19,9 @@ pub enum CustomersErrorResponse {#[error("Customer with the given customer id already exists")]CustomerAlreadyExists,++ #[error("Access forbidden")]+ AccessForbidden { message: String },}impl actix_web::ResponseError for CustomersErrorResponse {diff --git a/crates/router/src/core/errors/transformers.rs b/crates/router/src/core/errors/transformers.rsindex fc4fb26fcb..1e8f1ed306 100644--- a/crates/router/src/core/errors/transformers.rs+++ b/crates/router/src/core/errors/transformers.rs@@ -40,6 +40,15 @@ impl ErrorSwitch<api_models::errors::types::ApiErrorResponse> for CustomersError"Customer with the given `customer_id` already exists",None,)),+ Self::AccessForbidden { message } => AER::ForbiddenCommonResource(ApiError::new(+ "IR",+ 22,+ format!(+ "Access forbidden. Not authorized to access this resource {}",+ message+ ),+ None,+ )),}}}@@ -68,7 +77,24 @@ impl ErrorSwitch<CustomersErrorResponse> for ApiErrorResponse {Self::InternalServerError => CER::InternalServerError,Self::MandateActive => CER::MandateActive,Self::CustomerNotFound => CER::CustomerNotFound,+ Self::AccessForbidden { resource } => CER::AccessForbidden {+ message: resource.clone(),+ },_ => CER::InternalServerError,}}}++impl From<ApiErrorResponse> for CustomersErrorResponse {+ fn from(error: ApiErrorResponse) -> Self {+ match error {+ ApiErrorResponse::InternalServerError => Self::InternalServerError,+ ApiErrorResponse::MandateActive => Self::MandateActive,+ ApiErrorResponse::CustomerNotFound => Self::CustomerNotFound,+ ApiErrorResponse::AccessForbidden { resource } => Self::AccessForbidden {+ message: resource.clone(),+ },+ _ => Self::InternalServerError,+ }+ }+}diff --git a/crates/router/src/core/utils.rs b/crates/router/src/core/utils.rsindex 47f89119d2..97c023793e 100644--- a/crates/router/src/core/utils.rs+++ b/crates/router/src/core/utils.rs@@ -81,6 +81,25 @@ pub async fn get_feature_config(}}+#[cfg(feature = "v1")]+pub async fn validate_legacy_endpoint_access<E>(+ state: &SessionState,+ platform: &domain::Platform,+) -> error_stack::Result<(), E>+where+ E: From<errors::ApiErrorResponse> + error_stack::Context,+{+ let feature_config = get_feature_config(state, platform).await;+ common_utils::fp_utils::when(feature_config.is_payment_method_modular_allowed, || {+ Err(error_stack::report!(E::from(+ errors::ApiErrorResponse::AccessForbidden {+ resource: "Deprecated route".to_string(),+ },+ )))+ })?;+ Ok(())+}+pub const IRRELEVANT_CONNECTOR_REQUEST_REFERENCE_ID_IN_DISPUTE_FLOW: &str ="irrelevant_connector_request_reference_id_in_dispute_flow";const IRRELEVANT_ATTEMPT_ID_IN_DISPUTE_FLOW: &str = "irrelevant_attempt_id_in_dispute_flow";diff --git a/crates/router/src/routes/customers.rs b/crates/router/src/routes/customers.rsindex c58ad7d812..4b815da86f 100644--- a/crates/router/src/routes/customers.rs+++ b/crates/router/src/routes/customers.rs@@ -3,11 +3,14 @@ use common_utils::id_type;use router_env::{instrument, tracing, Flow};use super::app::AppState;+#[cfg(feature = "v1")]+use crate::core::utils as core_utils;use crate::{core::{api_locking, customers::*},services::{api, authentication as auth, authorization::permissions::Permission},types::api::customers,};+#[cfg(feature = "v2")]#[instrument(skip_all, fields(flow = ?Flow::CustomersCreate))]pub async fn customers_create(@@ -46,6 +49,7 @@ pub async fn customers_create()).await}+#[cfg(feature = "v1")]#[instrument(skip_all, fields(flow = ?Flow::CustomersCreate))]pub async fn customers_create(@@ -60,13 +64,17 @@ pub async fn customers_create(&req,json_payload.into_inner(),|state, auth: auth::AuthenticationData, req, _| {- create_customer(- state,- auth.platform.get_provider().clone(),- auth.platform.get_initiator().cloned(),- req,- None,- )+ Box::pin(async move {+ core_utils::validate_legacy_endpoint_access(&state, &auth.platform).await?;+ create_customer(+ state,+ auth.platform.get_provider().clone(),+ auth.platform.get_initiator().cloned(),+ req,+ None,+ )+ .await+ })},auth::auth_type(&auth::HeaderAuth(auth::ApiKeyAuth {@@ -118,14 +126,18 @@ pub async fn customers_retrieve(state,&req,customer_id,- |state, auth, customer_id, _| {- let profile_id = auth.profile.map(|profile| profile.get_id().clone());- retrieve_customer(- state,- auth.platform.get_provider().clone(),- profile_id,- customer_id,- )+ move |state, auth: auth::AuthenticationData, customer_id, _| {+ Box::pin(async move {+ core_utils::validate_legacy_endpoint_access(&state, &auth.platform).await?;+ let profile_id = auth.profile.map(|profile| profile.get_id().clone());+ retrieve_customer(+ state,+ auth.platform.get_provider().clone(),+ profile_id,+ customer_id,+ )+ .await+ })},&*auth,api_locking::LockAction::NotApplicable,@@ -185,6 +197,7 @@ pub async fn customers_retrieve()).await}+#[cfg(feature = "v2")]#[instrument(skip_all, fields(flow = ?Flow::CustomersList))]pub async fn customers_list(@@ -219,6 +232,7 @@ pub async fn customers_list()).await}+#[cfg(feature = "v1")]#[instrument(skip_all, fields(flow = ?Flow::CustomersList))]pub async fn customers_list(@@ -345,12 +359,16 @@ pub async fn customers_update(&req,request_internal,|state, auth: auth::AuthenticationData, request_internal, _| {- update_customer(- state,- auth.platform.get_provider().clone(),- auth.platform.get_initiator().cloned(),- request_internal,- )+ Box::pin(async move {+ core_utils::validate_legacy_endpoint_access(&state, &auth.platform).await?;+ update_customer(+ state,+ auth.platform.get_provider().clone(),+ auth.platform.get_initiator().cloned(),+ request_internal,+ )+ .await+ })},auth::auth_type(&auth::ApiKeyAuth {@@ -463,12 +481,16 @@ pub async fn customers_delete(&req,customer_id,|state, auth: auth::AuthenticationData, customer_id, _| {- delete_customer(- state,- auth.platform.get_provider().clone(),- auth.platform.get_initiator().cloned(),- customer_id,- )+ Box::pin(async move {+ core_utils::validate_legacy_endpoint_access(&state, &auth.platform).await?;+ delete_customer(+ state,+ auth.platform.get_provider().clone(),+ auth.platform.get_initiator().cloned(),+ customer_id,+ )+ .await+ })},auth::auth_type(&auth::HeaderAuth(auth::ApiKeyAuth {diff --git a/crates/router/src/routes/payment_methods.rs b/crates/router/src/routes/payment_methods.rsindex f4a5db63da..7fd9a3fcc5 100644--- a/crates/router/src/routes/payment_methods.rs+++ b/crates/router/src/routes/payment_methods.rs@@ -18,6 +18,8 @@ use hyperswitch_domain_models::{use router_env::{instrument, logger, tracing, Flow};use super::app::{AppState, SessionState};+#[cfg(feature = "v1")]+use crate::core::utils::validate_legacy_endpoint_access;#[cfg(all(feature = "v1", any(feature = "olap", feature = "oltp")))]use crate::core::{customers,@@ -51,14 +53,17 @@ pub async fn create_payment_method_api(state,&req,json_payload.into_inner(),- |state, auth: auth::AuthenticationData, req, _| async move {- Box::pin(cards::get_client_secret_or_add_payment_method(- &state,- req,- auth.platform.get_provider(),- auth.platform.get_initiator(),- ))- .await+ |state, auth: auth::AuthenticationData, req, _| {+ Box::pin(async move {+ validate_legacy_endpoint_access(&state, &auth.platform).await?;+ cards::get_client_secret_or_add_payment_method(+ &state,+ req,+ auth.platform.get_provider(),+ auth.platform.get_initiator(),+ )+ .await+ })},&auth::HeaderAuth(auth::ApiKeyAuth {allow_connected_scope_operation: true,@@ -630,18 +635,23 @@ pub async fn save_payment_method_api(state,&req,payload,- |state, auth: auth::AuthenticationData, mut req, _| {- if let Some(client_secret) = auth.client_secret {- req.client_secret = Some(client_secret);- }+ move |state, auth: auth::AuthenticationData, mut req, _| {+ let pm_id = pm_id.clone();+ Box::pin(async move {+ validate_legacy_endpoint_access(&state, &auth.platform).await?;+ if let Some(client_secret) = auth.client_secret {+ req.client_secret = Some(client_secret);+ }- Box::pin(cards::add_payment_method_data(- state,- req,- auth.platform.get_provider().clone(),- auth.platform.get_initiator().cloned(),- pm_id.clone(),- ))+ cards::add_payment_method_data(+ state,+ req,+ auth.platform.get_provider().clone(),+ auth.platform.get_initiator().cloned(),+ pm_id,+ )+ .await+ })},&*auth,api_locking::LockAction::NotApplicable,@@ -722,18 +732,23 @@ pub async fn list_customer_payment_method_api(state,&req,payload,- |state, auth: auth::AuthenticationData, mut req, _| {- if let Some(client_secret) = auth.client_secret {- req.client_secret = Some(client_secret);- }+ move |state, auth: auth::AuthenticationData, mut req, _| {+ let customer_id = customer_id.clone();+ Box::pin(async move {+ validate_legacy_endpoint_access(&state, &auth.platform).await?;+ if let Some(client_secret) = auth.client_secret {+ req.client_secret = Some(client_secret);+ }- cards::do_list_customer_pm_fetch_customer_if_not_passed(- state,- auth.platform,- Some(req),- Some(&customer_id),- None,- )+ cards::do_list_customer_pm_fetch_customer_if_not_passed(+ state,+ auth.platform,+ Some(req),+ Some(&customer_id),+ None,+ )+ .await+ })},&*auth_type,api_locking::LockAction::NotApplicable,@@ -790,17 +805,21 @@ pub async fn list_customer_payment_method_api_client(&req,payload,|state, auth: auth::AuthenticationData, mut req, _| {- if let Some(client_secret) = auth.client_secret {- req.client_secret = Some(client_secret);- }+ Box::pin(async move {+ validate_legacy_endpoint_access(&state, &auth.platform).await?;+ if let Some(client_secret) = auth.client_secret {+ req.client_secret = Some(client_secret);+ }- cards::do_list_customer_pm_fetch_customer_if_not_passed(- state,- auth.platform,- Some(req),- None,- is_ephemeral_auth.then_some(api_key).flatten(),- )+ cards::do_list_customer_pm_fetch_customer_if_not_passed(+ state,+ auth.platform,+ Some(req),+ None,+ is_ephemeral_auth.then_some(api_key).flatten(),+ )+ .await+ })},&*auth,api_locking::LockAction::NotApplicable,@@ -1014,6 +1033,7 @@ pub async fn payment_method_retrieve_api(&req,payload,|state, auth: auth::AuthenticationData, pm, _| async move {+ validate_legacy_endpoint_access(&state, &auth.platform).await?;cards::PmCards {state: &state,provider: auth.platform.get_provider(),@@ -1056,19 +1076,24 @@ pub async fn payment_method_update_api(state,&req,payload,- |state, auth: auth::AuthenticationData, mut req, _| {- if let Some(client_secret) = auth.client_secret {- req.client_secret = Some(client_secret);- }+ move |state, auth: auth::AuthenticationData, mut req, _| {+ let payment_method_id = payment_method_id.clone();+ Box::pin(async move {+ validate_legacy_endpoint_access(&state, &auth.platform).await?;+ if let Some(client_secret) = auth.client_secret {+ req.client_secret = Some(client_secret);+ }- cards::update_customer_payment_method(- state,- auth.platform.get_provider().clone(),- auth.platform.get_initiator().cloned(),- req,- &payment_method_id,- None,- )+ Box::pin(cards::update_customer_payment_method(+ state,+ auth.platform.get_provider().clone(),+ auth.platform.get_initiator().cloned(),+ req,+ &payment_method_id,+ None,+ ))+ .await+ })},&*auth,api_locking::LockAction::NotApplicable,@@ -1103,6 +1128,7 @@ pub async fn payment_method_delete_api(&req,pm,|state, auth: auth::AuthenticationData, req, _| async move {+ validate_legacy_endpoint_access(&state, &auth.platform).await?;cards::PmCards {state: &state,provider: auth.platform.get_provider(),@@ -1239,6 +1265,7 @@ pub async fn default_payment_method_set_api(&req,payload,|state, auth: auth::AuthenticationData, default_payment_method, _| async move {+ validate_legacy_endpoint_access(&state, &auth.platform).await?;cards::PmCards {state: &state,provider: auth.platform.get_provider(),