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(router): Validate payment method type in payments request against given payment method data for non-card flows (#1236)

Kritik Modi7607b6b67153
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index 6f421a752d..a38c3677fd 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -37,6 +37,7 @@ use crate::{
types::{self, AsyncLift},
},
storage::{self, enums as storage_enums, ephemeral_key, CustomerUpdate::Update},
+ transformers::ForeignInto,
ErrorResponse, RouterData,
},
utils::{
@@ -1371,6 +1372,18 @@ pub(crate) fn validate_payment_method_fields_present(
},
)?;
+ utils::when(
+ !matches!(
+ req.payment_method,
+ Some(api_enums::PaymentMethod::Card) | None
+ ) && (req.payment_method_type.is_none()),
+ || {
+ Err(errors::ApiErrorResponse::MissingRequiredField {
+ field_name: "payment_method_type",
+ })
+ },
+ )?;
+
utils::when(
req.payment_method.is_some()
&& req.payment_method_data.is_none()
@@ -1382,6 +1395,56 @@ pub(crate) fn validate_payment_method_fields_present(
},
)?;
+ let payment_method: Option<api_enums::PaymentMethod> =
+ (req.payment_method_type).map(ForeignInto::foreign_into);
+
+ utils::when(
+ req.payment_method.is_some()
+ && req.payment_method_type.is_some()
+ && (req.payment_method != payment_method),
+ || {
+ Err(errors::ApiErrorResponse::InvalidRequestData {
+ message: ("payment_method_type doesn't correspond to the specified payment_method"
+ .to_string()),
+ })
+ },
+ )?;
+
+ utils::when(
+ !matches!(
+ req.payment_method
+ .as_ref()
+ .zip(req.payment_method_data.as_ref()),
+ Some(
+ (
+ api_enums::PaymentMethod::Card,
+ api::PaymentMethodData::Card(..)
+ ) | (
+ api_enums::PaymentMethod::Wallet,
+ api::PaymentMethodData::Wallet(..)
+ ) | (
+ api_enums::PaymentMethod::PayLater,
+ api::PaymentMethodData::PayLater(..)
+ ) | (
+ api_enums::PaymentMethod::BankRedirect,
+ api::PaymentMethodData::BankRedirect(..)
+ ) | (
+ api_enums::PaymentMethod::BankDebit,
+ api::PaymentMethodData::BankDebit(..)
+ ) | (
+ api_enums::PaymentMethod::Crypto,
+ api::PaymentMethodData::Crypto(..)
+ )
+ ) | None
+ ),
+ || {
+ Err(errors::ApiErrorResponse::InvalidRequestData {
+ message: "payment_method_data doesn't correspond to the specified payment_method"
+ .to_string(),
+ })
+ },
+ )?;
+
Ok(())
}