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): override payment_method_type only for cards (#9241)

Kashif50a8013de13e
diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs
index 0e343a437b..bcd71636f0 100644
--- a/crates/router/src/core/payments/operations/payment_confirm.rs
+++ b/crates/router/src/core/payments/operations/payment_confirm.rs
@@ -662,6 +662,7 @@ impl<F: Send + Clone + Sync> GetTracker<F, PaymentData<F>, api::PaymentsRequest>
let payment_method_type = Option::<api_models::enums::PaymentMethodType>::foreign_from((
payment_method_type,
additional_pm_data.as_ref(),
+ payment_method,
));
payment_attempt.payment_method_type = payment_method_type
diff --git a/crates/router/src/core/payments/operations/payment_create.rs b/crates/router/src/core/payments/operations/payment_create.rs
index 79bfbebf49..1f54b1b72a 100644
--- a/crates/router/src/core/payments/operations/payment_create.rs
+++ b/crates/router/src/core/payments/operations/payment_create.rs
@@ -1266,6 +1266,7 @@ impl PaymentCreate {
let payment_method_type = Option::<enums::PaymentMethodType>::foreign_from((
payment_method_type,
additional_pm_data.as_ref(),
+ payment_method,
));
// TODO: remove once https://github.com/juspay/hyperswitch/issues/7421 is fixed
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index 66de9b4ee6..484a3ec757 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -5961,38 +5961,67 @@ impl ForeignFrom<diesel_models::ConnectorTokenDetails>
}
}
-impl ForeignFrom<(Self, Option<&api_models::payments::AdditionalPaymentData>)>
- for Option<enums::PaymentMethodType>
+impl
+ ForeignFrom<(
+ Self,
+ Option<&api_models::payments::AdditionalPaymentData>,
+ Option<enums::PaymentMethod>,
+ )> for Option<enums::PaymentMethodType>
{
- fn foreign_from(req: (Self, Option<&api_models::payments::AdditionalPaymentData>)) -> Self {
- let (payment_method_type, additional_pm_data) = req;
- additional_pm_data
- .and_then(|pm_data| {
- if let api_models::payments::AdditionalPaymentData::Card(card_info) = pm_data {
- card_info.card_type.as_ref().and_then(|card_type_str| {
- api_models::enums::PaymentMethodType::from_str(&card_type_str.to_lowercase()).map_err(|err| {
- crate::logger::error!(
- "Err - {:?}\nInvalid card_type value found in BIN DB - {:?}",
- err,
- card_type_str,
- );
- }).ok()
- })
- } else {
- None
- }
- })
- .map_or(payment_method_type, |card_type_in_bin_store| {
- if let Some(card_type_in_req) = payment_method_type {
- if card_type_in_req != card_type_in_bin_store {
+ fn foreign_from(
+ req: (
+ Self,
+ Option<&api_models::payments::AdditionalPaymentData>,
+ Option<enums::PaymentMethod>,
+ ),
+ ) -> Self {
+ let (payment_method_type, additional_pm_data, payment_method) = req;
+
+ match (additional_pm_data, payment_method, payment_method_type) {
+ (
+ Some(api_models::payments::AdditionalPaymentData::Card(card_info)),
+ Some(enums::PaymentMethod::Card),
+ original_type,
+ ) => {
+ let bin_card_type = card_info.card_type.as_ref().and_then(|card_type_str| {
+ let normalized_type = card_type_str.trim().to_lowercase();
+ if normalized_type.is_empty() {
+ return None;
+ }
+ api_models::enums::PaymentMethodType::from_str(&normalized_type)
+ .map_err(|_| {
+ crate::logger::warn!("Invalid BIN card_type: '{}'", card_type_str);
+ })
+ .ok()
+ });
+
+ match (original_type, bin_card_type) {
+ // Override when there's a mismatch
+ (
+ Some(
+ original @ (enums::PaymentMethodType::Debit
+ | enums::PaymentMethodType::Credit),
+ ),
+ Some(bin_type),
+ ) if original != bin_type => {
+ crate::logger::info!("BIN lookup override: {} -> {}", original, bin_type);
+ bin_card_type
+ }
+ // Use BIN lookup if no original type exists
+ (None, Some(bin_type)) => {
crate::logger::info!(
- "Mismatch in card_type\nAPI request - {}; BIN lookup - {}\nOverriding with {}",
- card_type_in_req, card_type_in_bin_store, card_type_in_bin_store,
+ "BIN lookup override: No original payment method type, using BIN result={}",
+ bin_type
);
+ Some(bin_type)
}
+ // Default
+ _ => original_type,
}
- Some(card_type_in_bin_store)
- })
+ }
+ // Skip BIN lookup for non-card payments
+ _ => payment_method_type,
+ }
}
}