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(router): populate card_network for wallet payment attempts (#13955)

Ayush Anand281f11fbbbe1
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs
index 32f0c7c7ef..19caff36d7 100644
--- a/crates/api_models/src/payments.rs
+++ b/crates/api_models/src/payments.rs
@@ -4374,6 +4374,31 @@ impl AdditionalPaymentData {
_ => None,
}
}
+
+ /// Wallet providers report the network as a free-form string in their own spelling.
+ pub fn get_wallet_card_network(&self) -> Option<&str> {
+ match self {
+ Self::Wallet {
+ apple_pay,
+ google_pay,
+ samsung_pay,
+ paypal: _,
+ } => apple_pay
+ .as_ref()
+ .map(|apple_pay| apple_pay.network.as_str())
+ .or_else(|| {
+ google_pay
+ .as_ref()
+ .and_then(|google_pay| google_pay.card_network.as_deref())
+ })
+ .or_else(|| {
+ samsung_pay
+ .as_ref()
+ .and_then(|samsung_pay| samsung_pay.card_network.as_deref())
+ }),
+ _ => None,
+ }
+ }
}
#[derive(Debug, Clone, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
diff --git a/crates/common_enums/src/enums.rs b/crates/common_enums/src/enums.rs
index 97b98924e0..c665947368 100644
--- a/crates/common_enums/src/enums.rs
+++ b/crates/common_enums/src/enums.rs
@@ -3435,26 +3435,43 @@ pub enum MandateStatus {
#[smithy(namespace = "com.hyperswitch.smithy.types")]
pub enum CardNetwork {
#[serde(alias = "VISA")]
+ #[strum(to_string = "Visa", serialize = "VISA")]
Visa,
#[serde(alias = "MASTERCARD")]
+ #[strum(
+ to_string = "Mastercard",
+ serialize = "MasterCard",
+ serialize = "MASTERCARD"
+ )]
Mastercard,
#[serde(alias = "AMERICANEXPRESS")]
#[serde(alias = "AMEX")]
+ #[strum(
+ to_string = "AmericanExpress",
+ serialize = "AMEX",
+ serialize = "AmEx",
+ serialize = "Amex",
+ serialize = "AMERICAN EXPRESS"
+ )]
AmericanExpress,
JCB,
#[serde(alias = "DINERSCLUB")]
DinersClub,
#[serde(alias = "DISCOVER")]
+ #[strum(to_string = "Discover", serialize = "DISCOVER")]
Discover,
#[serde(alias = "CARTESBANCAIRES")]
CartesBancaires,
#[serde(alias = "UNIONPAY")]
+ // Apple Pay sends UnionPay under its full name. Not seen in production traffic.
+ #[strum(to_string = "UnionPay", serialize = "ChinaUnionPay")]
UnionPay,
#[serde(alias = "INTERAC")]
Interac,
#[serde(alias = "RUPAY")]
RuPay,
#[serde(alias = "MAESTRO")]
+ #[strum(to_string = "Maestro", serialize = "MAESTRO")]
Maestro,
#[serde(alias = "STAR")]
Star,
@@ -3658,6 +3675,37 @@ impl CardNetwork {
| Self::Dinacard => false,
}
}
+
+ pub fn from_payment_method_data(payment_method_data: &serde_json::Value) -> Option<Self> {
+ let wallet = payment_method_data.get("wallet");
+
+ // Absent wallet providers serialise as `null` rather than being omitted, so each provider is
+ // matched on the network it yields, not on whether its key is present.
+ let network = match (
+ payment_method_data
+ .get("card")
+ .and_then(|card| card.get("card_network")),
+ wallet
+ .and_then(|wallet| wallet.get("apple_pay"))
+ .and_then(|apple_pay| apple_pay.get("network")),
+ wallet
+ .and_then(|wallet| wallet.get("google_pay"))
+ .and_then(|google_pay| google_pay.get("card_network")),
+ wallet
+ .and_then(|wallet| wallet.get("samsung_pay"))
+ .and_then(|samsung_pay| samsung_pay.get("card_network")),
+ ) {
+ (Some(network), ..)
+ | (_, Some(network), ..)
+ | (_, _, Some(network), _)
+ | (_, _, _, Some(network)) => Some(network),
+ (None, None, None, None) => None,
+ };
+
+ network
+ .and_then(|network| network.as_str())
+ .and_then(|network| Self::from_str(network).ok())
+ }
}
/// If a card is associated with a secondary (co-badged) network — e.g. Star, Pulse, Nyce —
diff --git a/crates/diesel_models/src/payment_attempt.rs b/crates/diesel_models/src/payment_attempt.rs
index a51449c275..10cc892247 100644
--- a/crates/diesel_models/src/payment_attempt.rs
+++ b/crates/diesel_models/src/payment_attempt.rs
@@ -1197,11 +1197,7 @@ impl PaymentAttemptUpdateInternal {
update_internal.card_network = update_internal
.payment_method_data
.as_ref()
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card"))
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card_network"))
- .and_then(|network| network.as_str())
+ .and_then(common_enums::CardNetwork::from_payment_method_data)
.map(|network| network.to_string());
update_internal
}
diff --git a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
index 39a722aba6..cb4dce8aaf 100644
--- a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
+++ b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
@@ -1829,8 +1829,18 @@ impl PaymentAttempt {
)
.ok()
})
- .and_then(|data| data.get_additional_card_info())
- .and_then(|card_info| card_info.card_network)
+ .and_then(|data| match data {
+ api_models::payments::AdditionalPaymentData::Card(additional_card_info) => {
+ additional_card_info.card_network
+ }
+ wallet_data => wallet_data.get_wallet_card_network().and_then(|network| {
+ common_enums::CardNetwork::from_str(network)
+ .map_err(|err| {
+ logger::error!("Failed to parse card network {network}: {err:?}")
+ })
+ .ok()
+ }),
+ })
}
pub fn get_payment_method_data(&self) -> Option<api_models::payments::AdditionalPaymentData> {
@@ -3005,13 +3015,7 @@ impl behaviour::Conversion for PaymentAttempt {
async fn convert(self) -> CustomResult<Self::DstType, ValidationError> {
let card_network = self
- .payment_method_data
- .as_ref()
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card"))
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card_network"))
- .and_then(|network| network.as_str())
+ .extract_card_network()
.map(|network| network.to_string());
let (connector_transaction_id, processor_transaction_data) = self
.connector_transaction_id
@@ -3271,13 +3275,7 @@ impl behaviour::Conversion for PaymentAttempt {
async fn construct_new(self) -> CustomResult<Self::NewDstType, ValidationError> {
let card_network = self
- .payment_method_data
- .as_ref()
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card"))
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card_network"))
- .and_then(|network| network.as_str())
+ .extract_card_network()
.map(|network| network.to_string());
Ok(DieselPaymentAttemptNew {
payment_id: self.payment_id,
diff --git a/crates/router/src/services/kafka/payment_attempt.rs b/crates/router/src/services/kafka/payment_attempt.rs
index 4442635096..0b1d5da64b 100644
--- a/crates/router/src/services/kafka/payment_attempt.rs
+++ b/crates/router/src/services/kafka/payment_attempt.rs
@@ -128,13 +128,7 @@ impl<'a> KafkaPaymentAttempt<'a> {
profile_id: &attempt.profile_id,
organization_id: &attempt.organization_id,
card_network: attempt
- .payment_method_data
- .as_ref()
- .and_then(|data| data.as_object())
- .and_then(|pm| pm.get("card"))
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card_network"))
- .and_then(|network| network.as_str())
+ .extract_card_network()
.map(|network| network.to_string()),
card_discovery: attempt
.card_discovery
@@ -342,14 +336,8 @@ impl<'a> KafkaPaymentAttempt<'a> {
client_version: client_version.as_ref(),
profile_id,
organization_id,
- card_network: payment_method_data
- .as_ref()
- .map(|data| data.peek())
- .and_then(|data| data.as_object())
- .and_then(|pm| pm.get("card"))
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card_network"))
- .and_then(|network| network.as_str())
+ card_network: attempt
+ .extract_card_network()
.map(|network| network.to_string()),
card_discovery: card_discovery.map(|discovery| discovery.to_string()),
payment_token: payment_token.clone(),
diff --git a/crates/router/src/services/kafka/payment_attempt_event.rs b/crates/router/src/services/kafka/payment_attempt_event.rs
index 78ea37885d..93874295ff 100644
--- a/crates/router/src/services/kafka/payment_attempt_event.rs
+++ b/crates/router/src/services/kafka/payment_attempt_event.rs
@@ -129,13 +129,7 @@ impl<'a> KafkaPaymentAttemptEvent<'a> {
profile_id: &attempt.profile_id,
organization_id: &attempt.organization_id,
card_network: attempt
- .payment_method_data
- .as_ref()
- .and_then(|data| data.as_object())
- .and_then(|pm| pm.get("card"))
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card_network"))
- .and_then(|network| network.as_str())
+ .extract_card_network()
.map(|network| network.to_string()),
card_discovery: attempt
.card_discovery
@@ -344,14 +338,8 @@ impl<'a> KafkaPaymentAttemptEvent<'a> {
client_version: client_version.as_ref(),
profile_id,
organization_id,
- card_network: payment_method_data
- .as_ref()
- .map(|data| data.peek())
- .and_then(|data| data.as_object())
- .and_then(|pm| pm.get("card"))
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card_network"))
- .and_then(|network| network.as_str())
+ card_network: attempt
+ .extract_card_network()
.map(|network| network.to_string()),
card_discovery: card_discovery.map(|discovery| discovery.to_string()),
payment_token: payment_token.clone(),
diff --git a/crates/storage_impl/src/payments/payment_attempt.rs b/crates/storage_impl/src/payments/payment_attempt.rs
index 413b9bd492..f6a77959af 100644
--- a/crates/storage_impl/src/payments/payment_attempt.rs
+++ b/crates/storage_impl/src/payments/payment_attempt.rs
@@ -2109,13 +2109,7 @@ impl Conversion for PaymentAttempt {
use common_utils::encryption::Encryption;
let card_network = self
- .payment_method_data
- .as_ref()
- .and_then(|data| data.peek().as_object())
- .and_then(|card| card.get("card"))
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card_network"))
- .and_then(|network| network.as_str())
+ .extract_card_network()
.map(|network| network.to_string());
let Self {
@@ -2432,6 +2426,11 @@ impl Conversion for PaymentAttempt {
async fn construct_new(self) -> CustomResult<Self::NewDstType, ValidationError> {
use common_utils::encryption::Encryption;
+
+ let card_network = self
+ .extract_card_network()
+ .map(|network| network.to_string());
+
let Self {
payment_id,
merchant_id,
@@ -2491,15 +2490,6 @@ impl Conversion for PaymentAttempt {
payment_account_reference,
} = self;
- let card_network = payment_method_data
- .as_ref()
- .and_then(|data| data.peek().as_object())
- .and_then(|card| card.get("card"))
- .and_then(|data| data.as_object())
- .and_then(|card| card.get("card_network"))
- .and_then(|network| network.as_str())
- .map(|network| network.to_string());
-
let error_details = error;
Ok(DieselPaymentAttemptNew {