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

fix(payments): write a foreign_from implementation for payment_method_data and add missing payment methods in helpers.rs (#1801)

Kritik Modi50298c19674c
diff --git a/crates/common_enums/src/enums.rs b/crates/common_enums/src/enums.rs
index 690d45efb7..97f48da103 100644
--- a/crates/common_enums/src/enums.rs
+++ b/crates/common_enums/src/enums.rs
@@ -956,6 +956,7 @@ pub enum PaymentMethod {
Reward,
Upi,
Voucher,
+ GiftCard,
}
#[derive(
diff --git a/crates/router/src/core/payments/helpers.rs b/crates/router/src/core/payments/helpers.rs
index 2953a5a390..326ac77196 100644
--- a/crates/router/src/core/payments/helpers.rs
+++ b/crates/router/src/core/payments/helpers.rs
@@ -37,7 +37,7 @@ use crate::{
types::{self, AsyncLift},
},
storage::{self, enums as storage_enums, ephemeral_key, CustomerUpdate::Update},
- transformers::ForeignInto,
+ transformers::ForeignTryFrom,
ErrorResponse, RouterData,
},
utils::{
@@ -1400,65 +1400,156 @@ 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),
+ req.payment_method.is_some() && req.payment_method_type.is_some(),
|| {
- Err(errors::ApiErrorResponse::InvalidRequestData {
- message: ("payment_method_type doesn't correspond to the specified payment_method"
- .to_string()),
- })
+ req.payment_method
+ .map_or(Ok(()), |req_payment_method| {
+ req.payment_method_type.map_or(Ok(()), |req_payment_method_type| {
+ if !validate_payment_method_type_against_payment_method(req_payment_method, req_payment_method_type) {
+ Err(errors::ApiErrorResponse::InvalidRequestData {
+ message: ("payment_method_type doesn't correspond to the specified payment_method"
+ .to_string()),
+ })
+ } else {
+ Ok(())
+ }
+ })
+ })
},
)?;
+ let validate_payment_method_and_payment_method_data =
+ |req_payment_method_data, req_payment_method: api_enums::PaymentMethod| {
+ api_enums::PaymentMethod::foreign_try_from(req_payment_method_data).and_then(|payment_method|
+ if req_payment_method != payment_method {
+ Err(errors::ApiErrorResponse::InvalidRequestData {
+ message: ("payment_method_data doesn't correspond to the specified payment_method"
+ .to_string()),
+ })
+ } else {
+ Ok(())
+ })
+ };
+
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(..)
- ) | (
- api_enums::PaymentMethod::Upi,
- api::PaymentMethodData::Upi(..)
- ) | (
- api_enums::PaymentMethod::Voucher,
- api::PaymentMethodData::Voucher(..)
- )
- ) | None
- ),
+ req.payment_method.is_some() && req.payment_method_data.is_some(),
|| {
- Err(errors::ApiErrorResponse::InvalidRequestData {
- message: "payment_method_data doesn't correspond to the specified payment_method"
- .to_string(),
- })
+ req.payment_method_data
+ .clone()
+ .map_or(Ok(()), |req_payment_method_data| {
+ req.payment_method.map_or(Ok(()), |req_payment_method| {
+ validate_payment_method_and_payment_method_data(
+ req_payment_method_data,
+ req_payment_method,
+ )
+ })
+ })
},
)?;
Ok(())
}
+pub fn validate_payment_method_type_against_payment_method(
+ payment_method: api_enums::PaymentMethod,
+ payment_method_type: api_enums::PaymentMethodType,
+) -> bool {
+ match payment_method {
+ api_enums::PaymentMethod::Card => matches!(
+ payment_method_type,
+ api_enums::PaymentMethodType::Credit | api_enums::PaymentMethodType::Debit
+ ),
+ api_enums::PaymentMethod::PayLater => matches!(
+ payment_method_type,
+ api_enums::PaymentMethodType::Affirm
+ | api_enums::PaymentMethodType::Alma
+ | api_enums::PaymentMethodType::AfterpayClearpay
+ | api_enums::PaymentMethodType::Klarna
+ | api_enums::PaymentMethodType::PayBright
+ | api_enums::PaymentMethodType::Atome
+ | api_enums::PaymentMethodType::Walley
+ ),
+ api_enums::PaymentMethod::Wallet => matches!(
+ payment_method_type,
+ api_enums::PaymentMethodType::ApplePay
+ | api_enums::PaymentMethodType::GooglePay
+ | api_enums::PaymentMethodType::Paypal
+ | api_enums::PaymentMethodType::AliPay
+ | api_enums::PaymentMethodType::AliPayHk
+ | api_enums::PaymentMethodType::Dana
+ | api_enums::PaymentMethodType::MbWay
+ | api_enums::PaymentMethodType::MobilePay
+ | api_enums::PaymentMethodType::SamsungPay
+ | api_enums::PaymentMethodType::Twint
+ | api_enums::PaymentMethodType::Vipps
+ | api_enums::PaymentMethodType::TouchNGo
+ | api_enums::PaymentMethodType::Swish
+ | api_enums::PaymentMethodType::WeChatPay
+ | api_enums::PaymentMethodType::GoPay
+ | api_enums::PaymentMethodType::Gcash
+ | api_enums::PaymentMethodType::Momo
+ | api_enums::PaymentMethodType::KakaoPay
+ ),
+ api_enums::PaymentMethod::BankRedirect => matches!(
+ payment_method_type,
+ api_enums::PaymentMethodType::Giropay
+ | api_enums::PaymentMethodType::Ideal
+ | api_enums::PaymentMethodType::Sofort
+ | api_enums::PaymentMethodType::Eps
+ | api_enums::PaymentMethodType::BancontactCard
+ | api_enums::PaymentMethodType::Blik
+ | api_enums::PaymentMethodType::OnlineBankingThailand
+ | api_enums::PaymentMethodType::OnlineBankingCzechRepublic
+ | api_enums::PaymentMethodType::OnlineBankingFinland
+ | api_enums::PaymentMethodType::OnlineBankingFpx
+ | api_enums::PaymentMethodType::OnlineBankingPoland
+ | api_enums::PaymentMethodType::OnlineBankingSlovakia
+ | api_enums::PaymentMethodType::Przelewy24
+ | api_enums::PaymentMethodType::Trustly
+ | api_enums::PaymentMethodType::Bizum
+ | api_enums::PaymentMethodType::Interac
+ ),
+ api_enums::PaymentMethod::BankTransfer => matches!(
+ payment_method_type,
+ api_enums::PaymentMethodType::Ach
+ | api_enums::PaymentMethodType::Sepa
+ | api_enums::PaymentMethodType::Bacs
+ | api_enums::PaymentMethodType::Multibanco
+ | api_enums::PaymentMethodType::Pix
+ | api_enums::PaymentMethodType::Pse
+ ),
+ api_enums::PaymentMethod::BankDebit => matches!(
+ payment_method_type,
+ api_enums::PaymentMethodType::Ach
+ | api_enums::PaymentMethodType::Sepa
+ | api_enums::PaymentMethodType::Bacs
+ | api_enums::PaymentMethodType::Becs
+ ),
+ api_enums::PaymentMethod::Crypto => matches!(
+ payment_method_type,
+ api_enums::PaymentMethodType::CryptoCurrency
+ ),
+ api_enums::PaymentMethod::Reward => matches!(
+ payment_method_type,
+ api_enums::PaymentMethodType::Evoucher | api_enums::PaymentMethodType::ClassicReward
+ ),
+ api_enums::PaymentMethod::Upi => matches!(
+ payment_method_type,
+ api_enums::PaymentMethodType::UpiCollect
+ ),
+ api_enums::PaymentMethod::Voucher => matches!(
+ payment_method_type,
+ api_enums::PaymentMethodType::Boleto
+ | api_enums::PaymentMethodType::Efecty
+ | api_enums::PaymentMethodType::PagoEfectivo
+ | api_enums::PaymentMethodType::RedCompra
+ | api_enums::PaymentMethodType::RedPagos
+ ),
+ api_enums::PaymentMethod::GiftCard => false,
+ }
+}
+
pub fn check_force_psync_precondition(
status: &storage_enums::AttemptStatus,
connector_transaction_id: &Option<String>,
diff --git a/crates/router/src/types/transformers.rs b/crates/router/src/types/transformers.rs
index b4d0b1298b..5d9d9a73d9 100644
--- a/crates/router/src/types/transformers.rs
+++ b/crates/router/src/types/transformers.rs
@@ -230,6 +230,32 @@ impl ForeignFrom<api_enums::PaymentMethodType> for api_enums::PaymentMethod {
}
}
+impl ForeignTryFrom<api_models::payments::PaymentMethodData> for api_enums::PaymentMethod {
+ type Error = errors::ApiErrorResponse;
+ fn foreign_try_from(
+ payment_method_data: api_models::payments::PaymentMethodData,
+ ) -> Result<Self, Self::Error> {
+ match payment_method_data {
+ api_models::payments::PaymentMethodData::Card(..) => Ok(Self::Card),
+ api_models::payments::PaymentMethodData::Wallet(..) => Ok(Self::Wallet),
+ api_models::payments::PaymentMethodData::PayLater(..) => Ok(Self::PayLater),
+ api_models::payments::PaymentMethodData::BankRedirect(..) => Ok(Self::BankRedirect),
+ api_models::payments::PaymentMethodData::BankDebit(..) => Ok(Self::BankDebit),
+ api_models::payments::PaymentMethodData::BankTransfer(..) => Ok(Self::BankTransfer),
+ api_models::payments::PaymentMethodData::Crypto(..) => Ok(Self::Crypto),
+ api_models::payments::PaymentMethodData::Reward(..) => Ok(Self::Reward),
+ api_models::payments::PaymentMethodData::Upi(..) => Ok(Self::Upi),
+ api_models::payments::PaymentMethodData::Voucher(..) => Ok(Self::Voucher),
+ api_models::payments::PaymentMethodData::GiftCard(..) => Ok(Self::GiftCard),
+ api_models::payments::PaymentMethodData::MandatePayment => {
+ Err(errors::ApiErrorResponse::InvalidRequestData {
+ message: ("Mandate payments cannot have payment_method_data field".to_string()),
+ })
+ }
+ }
+ }
+}
+
impl ForeignTryFrom<storage_enums::RefundStatus> for storage_enums::EventType {
type Error = errors::ValidationError;
diff --git a/openapi/openapi_spec.json b/openapi/openapi_spec.json
index 79c70fda30..ac8c594e9c 100644
--- a/openapi/openapi_spec.json
+++ b/openapi/openapi_spec.json
@@ -7003,7 +7003,8 @@
"bank_debit",
"reward",
"upi",
- "voucher"
+ "voucher",
+ "gift_card"
]
},
"PaymentMethodCreate": {