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(rev-rec): add card data in payment intent (#13778)

Rohan Mali661db36e0488
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index e68f76cb94..94bc04de13 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -9518,6 +9518,24 @@
"description": "Card Issuer",
"example": "JP MORGAN CHASE",
"nullable": true
+ },
+ "card_type": {
+ "type": "string",
+ "description": "Funding type of the card, `credit` or `debit`, enriched from the card bin",
+ "example": "credit",
+ "nullable": true
+ },
+ "card_issuing_country": {
+ "type": "string",
+ "description": "Country in which the card was issued, enriched from the card bin",
+ "example": "INDIA",
+ "nullable": true
+ },
+ "card_isin": {
+ "type": "string",
+ "description": "Issuer identification number of the card, retained so that any further card details can\nbe looked up from it later",
+ "example": "424242",
+ "nullable": true
}
}
},
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs
index c9f2e9833c..60652627d7 100644
--- a/crates/api_models/src/payments.rs
+++ b/crates/api_models/src/payments.rs
@@ -13685,6 +13685,16 @@ pub struct BillingConnectorAdditionalCardInfo {
#[schema(value_type = Option<String>, example = "JP MORGAN CHASE")]
/// Card Issuer
pub card_issuer: Option<String>,
+ /// Funding type of the card, `credit` or `debit`, enriched from the card bin
+ #[schema(value_type = Option<String>, example = "credit")]
+ pub card_type: Option<String>,
+ /// Country in which the card was issued, enriched from the card bin
+ #[schema(value_type = Option<String>, example = "INDIA")]
+ pub card_issuing_country: Option<String>,
+ /// Issuer identification number of the card, retained so that any further card details can
+ /// be looked up from it later
+ #[schema(value_type = Option<String>, example = "424242")]
+ pub card_isin: Option<String>,
}
#[cfg(feature = "v2")]
diff --git a/crates/diesel_models/src/types.rs b/crates/diesel_models/src/types.rs
index 341de0dca6..5173a59664 100644
--- a/crates/diesel_models/src/types.rs
+++ b/crates/diesel_models/src/types.rs
@@ -522,4 +522,11 @@ pub struct BillingConnectorAdditionalCardInfo {
pub card_network: Option<common_enums::enums::CardNetwork>,
/// Card Issuer
pub card_issuer: Option<String>,
+ /// Funding type of the card, `credit` or `debit`, enriched from the card bin
+ pub card_type: Option<String>,
+ /// Country in which the card was issued, enriched from the card bin
+ pub card_issuing_country: Option<String>,
+ /// Issuer identification number of the card, retained so that any further card details can
+ /// be looked up from it later
+ pub card_isin: Option<String>,
}
diff --git a/crates/hyperswitch_domain_models/src/lib.rs b/crates/hyperswitch_domain_models/src/lib.rs
index 01485410ec..e52f957cc4 100644
--- a/crates/hyperswitch_domain_models/src/lib.rs
+++ b/crates/hyperswitch_domain_models/src/lib.rs
@@ -893,6 +893,9 @@ impl ApiModelToDieselModelConvertor<ApiBillingConnectorAdditionalCardInfo>
Self {
card_issuer: from.card_issuer,
card_network: from.card_network,
+ card_type: from.card_type,
+ card_issuing_country: from.card_issuing_country,
+ card_isin: from.card_isin,
}
}
@@ -900,6 +903,9 @@ impl ApiModelToDieselModelConvertor<ApiBillingConnectorAdditionalCardInfo>
ApiBillingConnectorAdditionalCardInfo {
card_issuer: self.card_issuer,
card_network: self.card_network,
+ card_type: self.card_type,
+ card_issuing_country: self.card_issuing_country,
+ card_isin: self.card_isin,
}
}
}
diff --git a/crates/hyperswitch_domain_models/src/payments.rs b/crates/hyperswitch_domain_models/src/payments.rs
index c295f2296f..61cc758525 100644
--- a/crates/hyperswitch_domain_models/src/payments.rs
+++ b/crates/hyperswitch_domain_models/src/payments.rs
@@ -1609,11 +1609,16 @@ where
},
);
+ // The bin derived details are enriched onto the attempt's payment method data rather than
+ // being sent by the billing connector, so they are read back from there.
let billing_connector_payment_method_details = Some(
diesel_models::types::BillingConnectorPaymentMethodDetails::Card(
diesel_models::types::BillingConnectorAdditionalCardInfo {
card_network: self.revenue_recovery_data.card_network.clone(),
card_issuer: self.revenue_recovery_data.card_issuer.clone(),
+ card_type: self.payment_attempt.extract_card_type(),
+ card_issuing_country: self.payment_attempt.extract_card_issuing_country(),
+ card_isin: self.payment_attempt.extract_card_isin(),
},
),
);
diff --git a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
index cfbddcf433..e03ae72570 100644
--- a/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
+++ b/crates/hyperswitch_domain_models/src/payments/payment_attempt.rs
@@ -1650,6 +1650,32 @@ impl PaymentAttempt {
todo!()
}
+ /// Funding type of the card, as stored on the attempt's payment method data.
+ pub fn extract_card_type(&self) -> Option<String> {
+ self.extract_additional_card_info()
+ .and_then(|card_info| card_info.card_type)
+ }
+
+ /// Country in which the card was issued, as stored on the attempt's payment method data.
+ pub fn extract_card_issuing_country(&self) -> Option<String> {
+ self.extract_additional_card_info()
+ .and_then(|card_info| card_info.card_issuing_country)
+ }
+
+ /// Issuer identification number of the card, as stored on the attempt's payment method data.
+ pub fn extract_card_isin(&self) -> Option<String> {
+ self.extract_additional_card_info()
+ .and_then(|card_info| card_info.card_isin)
+ }
+
+ /// The card details recorded on the attempt, if the payment method data holds a card.
+ fn extract_additional_card_info(&self) -> Option<api_models::payments::AdditionalCardInfo> {
+ self.get_payment_method_data()
+ .ok()
+ .flatten()
+ .and_then(|data| data.get_additional_card_info())
+ }
+
fn get_connector_metadata_value(&self) -> Option<&Value> {
self.connector_metadata
.as_ref()
@@ -3664,3 +3690,58 @@ impl From<DieselPaymentAttemptFeatureMetadata> for PaymentAttemptFeatureMetadata
Self { revenue_recovery }
}
}
+
+#[cfg(all(test, feature = "v2"))]
+mod card_info_extraction_tests {
+ use api_models::payments::{AdditionalCardInfo, AdditionalPaymentData};
+ use common_utils::ext_traits::{Encode, ValueExt};
+
+ /// The enriched card details are stored on the attempt's `payment_method_data` and read back
+ /// from there when the payment intent's feature metadata is built, so the fields have to
+ /// survive that json round trip.
+ #[test]
+ fn enriched_card_details_survive_the_payment_method_data_round_trip() {
+ let card_info = AdditionalCardInfo {
+ card_issuer: Some("JP MORGAN CHASE".to_string()),
+ card_type: Some("credit".to_string()),
+ card_issuing_country: Some("UNITED STATES".to_string()),
+ card_isin: Some("424242".to_string()),
+ ..Default::default()
+ };
+
+ let stored_value = AdditionalPaymentData::Card(Box::new(card_info))
+ .encode_to_value()
+ .expect("additional payment data should serialize");
+
+ let parsed = stored_value
+ .parse_value::<AdditionalPaymentData>("AdditionalPaymentData")
+ .expect("additional payment data should deserialize")
+ .get_additional_card_info()
+ .expect("card details should be present");
+
+ assert_eq!(parsed.card_type.as_deref(), Some("credit"));
+ assert_eq!(
+ parsed.card_issuing_country.as_deref(),
+ Some("UNITED STATES")
+ );
+ assert_eq!(parsed.card_issuer.as_deref(), Some("JP MORGAN CHASE"));
+ assert_eq!(parsed.card_isin.as_deref(), Some("424242"));
+ }
+
+ /// A webhook without a card bin leaves the enriched fields empty rather than failing.
+ #[test]
+ fn missing_card_details_round_trip_as_none() {
+ let stored_value = AdditionalPaymentData::Card(Box::default())
+ .encode_to_value()
+ .expect("additional payment data should serialize");
+
+ let parsed = stored_value
+ .parse_value::<AdditionalPaymentData>("AdditionalPaymentData")
+ .expect("additional payment data should deserialize")
+ .get_additional_card_info()
+ .expect("card details should be present");
+
+ assert_eq!(parsed.card_type, None);
+ assert_eq!(parsed.card_issuing_country, None);
+ }
+}
diff --git a/crates/router/src/core/payments/transformers.rs b/crates/router/src/core/payments/transformers.rs
index e58c90fd3c..4c08e22469 100644
--- a/crates/router/src/core/payments/transformers.rs
+++ b/crates/router/src/core/payments/transformers.rs
@@ -7604,6 +7604,9 @@ impl ForeignFrom<&diesel_models::types::BillingConnectorPaymentMethodDetails>
Self::Card(api_models::payments::BillingConnectorAdditionalCardInfo {
card_issuer: card_details.card_issuer.clone(),
card_network: card_details.card_network.clone(),
+ card_type: card_details.card_type.clone(),
+ card_issuing_country: card_details.card_issuing_country.clone(),
+ card_isin: card_details.card_isin.clone(),
})
}
}
diff --git a/crates/router/src/core/webhooks/recovery_incoming.rs b/crates/router/src/core/webhooks/recovery_incoming.rs
index 285375021c..641aa65ae1 100644
--- a/crates/router/src/core/webhooks/recovery_incoming.rs
+++ b/crates/router/src/core/webhooks/recovery_incoming.rs
@@ -517,6 +517,46 @@ impl RevenueRecoveryAttempt {
},
)
}
+ /// Billing connectors send the card bin but not the issuer level details of the card, those
+ /// have to be looked up locally from the `card_info` table using the bin. This enriches the
+ /// attempt's card info in place, so that every consumer built from it, the payment attempt,
+ /// the payment intent's feature metadata and the payment processor token in redis, sees the
+ /// same enriched data. Details already sent by the billing connector are left untouched, and
+ /// a webhook without a bin is left as is instead of being failed.
+ async fn enrich_card_info_using_card_bin(&mut self, state: &SessionState) {
+ let card_bin_info = self
+ .0
+ .card_info
+ .card_isin
+ .clone()
+ .async_and_then(|card_isin| async move {
+ state
+ .store
+ .get_card_info(card_isin.as_str())
+ .await
+ .map_err(|error| services::logger::warn!(card_info_error=?error))
+ .ok()
+ })
+ .await
+ .flatten();
+
+ if let Some(card_bin_info) = card_bin_info {
+ let card_info = &mut self.0.card_info;
+ card_info.card_issuer = card_info.card_issuer.take().or(card_bin_info.card_issuer);
+ card_info.card_network = card_info.card_network.take().or(card_bin_info.card_network);
+ card_info.card_type = card_info.card_type.take().or(card_bin_info.card_type);
+ card_info.card_issuing_country = card_info
+ .card_issuing_country
+ .take()
+ .or(card_bin_info.card_issuing_country);
+ card_info.card_issuing_country_code = card_info
+ .card_issuing_country_code
+ .take()
+ .or(card_bin_info.country_code);
+ card_info.bank_code = card_info.bank_code.take().or(card_bin_info.bank_code);
+ }
+ }
+
pub fn get_revenue_recovery_attempt(
payment_intent: &domain_payments::PaymentIntent,
revenue_recovery_metadata: &api_payments::PaymentRevenueRecoveryMetadata,
@@ -851,12 +891,17 @@ impl RevenueRecoveryAttempt {
> {
let payment_attempt_with_recovery_intent = match is_recovery_transaction_event {
true => {
- let invoice_transaction_details = Self::get_recovery_invoice_transaction_details(
- connector_enum,
- request_details,
- billing_connector_payment_details,
- invoice_details,
- )?;
+ let mut invoice_transaction_details =
+ Self::get_recovery_invoice_transaction_details(
+ connector_enum,
+ request_details,
+ billing_connector_payment_details,
+ invoice_details,
+ )?;
+
+ // Boxed to keep this future off the enclosing state machine, the recovery webhook
+ // chain is deep enough that inlining it can overflow the worker thread's stack.
+ Box::pin(invoice_transaction_details.enrich_card_info_using_card_bin(state)).await;
// Find the payment merchant connector ID at the top level to avoid multiple DB calls.
let payment_merchant_connector_account = invoice_transaction_details