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(offers): send up to 9-digit card BIN to Offer Engine (#14259)

Apoorv Dixiteee4abac0d73
diff --git a/crates/cards/src/validate.rs b/crates/cards/src/validate.rs
index 7050d7dbe4..08524c15ee 100644
--- a/crates/cards/src/validate.rs
+++ b/crates/cards/src/validate.rs
@@ -22,6 +22,11 @@ pub const MIN_CARD_BIN_LENGTH: usize = 6;
/// because blocklist prefixes are stored in plaintext, unlike vault-hashed card numbers.
pub const MAX_CARD_BIN_LENGTH: usize = 10;
+/// Card BIN length sent to Offer Engine — the longest BIN it accepts. Its `CARD_BIN` filter
+/// matches any prefix from 6 digits up to the length it receives, so sending the longest BIN
+/// lets 7-9 digit BIN filters match too.
+pub const MAX_OFFER_CARD_BIN_LENGTH: usize = 9;
+
#[derive(Debug, Deserialize, Serialize, Error)]
#[error("{0}")]
pub struct CardNumberValidationErr(&'static str);
@@ -45,6 +50,12 @@ impl CardBin {
self.0.chars().take(6).collect()
}
+ /// The leading [`MAX_OFFER_CARD_BIN_LENGTH`] digits of the BIN, or all of them when fewer
+ /// were provided
+ pub fn get_offer_card_bin(&self) -> String {
+ self.0.chars().take(MAX_OFFER_CARD_BIN_LENGTH).collect()
+ }
+
/// Every blocklist-relevant prefix derivable from this BIN: lengths
/// [`MIN_CARD_BIN_LENGTH`] up to the number of digits actually provided
/// (capped at [`MAX_CARD_BIN_LENGTH`]).
@@ -84,6 +95,12 @@ impl CardNumber {
self.0.peek().chars().take(len).collect::<String>()
}
+ /// Card BIN of [`MAX_OFFER_CARD_BIN_LENGTH`] digits. Takes no length, so no caller can widen
+ /// it towards the full card number.
+ pub fn get_offer_card_bin(&self) -> String {
+ self.get_bin_prefix(MAX_OFFER_CARD_BIN_LENGTH)
+ }
+
pub fn get_card_isin(&self) -> String {
self.get_bin_prefix(6)
}
@@ -413,6 +430,25 @@ mod tests {
);
}
+ #[test]
+ fn offer_card_bin_from_card_number() {
+ let card_number = CardNumber::from_str("4111111111111111").unwrap();
+ assert_eq!(card_number.get_offer_card_bin(), "411111111");
+ assert_eq!(
+ card_number.get_offer_card_bin().len(),
+ MAX_OFFER_CARD_BIN_LENGTH
+ );
+ }
+
+ #[test]
+ fn offer_card_bin_from_card_bin() {
+ let eight_digit_bin = CardBin::from_str("41111111").unwrap();
+ // capped by the digits actually provided
+ assert_eq!(eight_digit_bin.get_offer_card_bin(), "41111111");
+ let ten_digit_bin = CardBin::from_str("4111111111").unwrap();
+ assert_eq!(ten_digit_bin.get_offer_card_bin(), "411111111");
+ }
+
#[test]
fn card_number_with_non_digit_character() {
let s = "371446431 A";
diff --git a/crates/hyperswitch_domain_models/src/payment_method_data.rs b/crates/hyperswitch_domain_models/src/payment_method_data.rs
index d49c072eb2..9da88cabb9 100644
--- a/crates/hyperswitch_domain_models/src/payment_method_data.rs
+++ b/crates/hyperswitch_domain_models/src/payment_method_data.rs
@@ -402,6 +402,11 @@ impl EligibilityCardBin {
self.card_bin.get_card_isin()
}
+ /// The BIN digits sent to Offer Engine, or all of them when fewer were provided
+ pub fn get_offer_card_bin(&self) -> String {
+ self.card_bin.get_offer_card_bin()
+ }
+
/// Every blocklist-relevant prefix derivable from this BIN (lengths 6 up to the
/// number of digits provided)
pub fn get_blocklist_bin_prefixes(&self) -> Vec<String> {
@@ -446,6 +451,14 @@ impl EligibilityPaymentMethodData {
}
}
+ pub fn get_offer_card_bin(&self) -> Option<String> {
+ match self {
+ Self::Card(card) => Some(card.card_number.get_offer_card_bin()),
+ Self::CardBin(card_bin) => Some(card_bin.get_offer_card_bin()),
+ _ => None,
+ }
+ }
+
pub fn get_card_data(&self) -> Option<&EligibilityCard> {
if let Self::Card(card) = self {
Some(card)
diff --git a/crates/router/src/core/offer_engine/apply.rs b/crates/router/src/core/offer_engine/apply.rs
index f4257f8d2b..a1b7b591f0 100644
--- a/crates/router/src/core/offer_engine/apply.rs
+++ b/crates/router/src/core/offer_engine/apply.rs
@@ -1,4 +1,5 @@
use common_utils::{errors::CustomResult, id_type, types::MinorUnit};
+use hyperswitch_masking::Secret;
use super::{
client::OfferEngineClient,
@@ -33,7 +34,7 @@ pub struct OfferApplyContext {
/// Card network, if available.
pub payment_method: Option<String>,
/// Card BIN/IIN, if available.
- pub card_bin: Option<String>,
+ pub card_bin: Option<Secret<String>>,
/// Card type, if available.
pub card_type: Option<String>,
/// Issuing bank code, if available.
diff --git a/crates/router/src/core/offer_engine/eligibility.rs b/crates/router/src/core/offer_engine/eligibility.rs
index 612e77aab4..6778e71a1c 100644
--- a/crates/router/src/core/offer_engine/eligibility.rs
+++ b/crates/router/src/core/offer_engine/eligibility.rs
@@ -1,4 +1,5 @@
use common_utils::{errors::CustomResult, id_type, types::MinorUnit};
+use hyperswitch_masking::Secret;
use super::{
amount,
@@ -26,7 +27,7 @@ pub struct OfferEligibilityContext {
/// Card network, if available.
pub payment_method: Option<String>,
/// Card BIN/IIN, if available.
- pub card_bin: Option<String>,
+ pub card_bin: Option<Secret<String>>,
/// Card type (e.g. `CREDIT`), if available.
pub card_type: Option<String>,
/// Issuing bank code, if available.
diff --git a/crates/router/src/core/offer_engine/types.rs b/crates/router/src/core/offer_engine/types.rs
index abbd22343e..b93ab5c76f 100644
--- a/crates/router/src/core/offer_engine/types.rs
+++ b/crates/router/src/core/offer_engine/types.rs
@@ -70,7 +70,7 @@ pub struct OfferPaymentMethodInfo {
#[serde(skip_serializing_if = "Option::is_none")]
pub payment_method: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
- pub card_bin: Option<String>,
+ pub card_bin: Option<Secret<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub card_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index fe4e3c3362..fe0c35bde3 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -15114,7 +15114,8 @@ pub async fn payments_submit_eligibility(
let offer_card_bin = payment_eligibility_data
.payment_method_data
.as_ref()
- .and_then(|pmd| pmd.get_card_iin());
+ .and_then(|pmd| pmd.get_offer_card_bin())
+ .map(Secret::new);
// Forward whatever card attributes the request carried; Offer Engine uses
// them when present and ignores the rest.
let offer_card = payment_eligibility_data
@@ -15206,7 +15207,7 @@ async fn resolve_offer_eligibility_details(
currency: Option<common_enums::Currency>,
customer_id: Option<&id_type::CustomerId>,
payment_method_type: String,
- card_bin: Option<String>,
+ card_bin: Option<Secret<String>>,
card_network: Option<String>,
card_type: Option<String>,
bank_code: Option<String>,
diff --git a/crates/router/src/core/payments/operations/payment_confirm.rs b/crates/router/src/core/payments/operations/payment_confirm.rs
index 425e0483a0..baf70dd053 100644
--- a/crates/router/src/core/payments/operations/payment_confirm.rs
+++ b/crates/router/src/core/payments/operations/payment_confirm.rs
@@ -3386,7 +3386,8 @@ async fn apply_selected_offer<F: Clone + Send + Sync>(
}),
card_bin: offer_pmd
.as_ref()
- .and_then(|offer_pmd| offer_pmd.get_card_iin()),
+ .and_then(|offer_pmd| offer_pmd.get_offer_card_bin())
+ .map(hyperswitch_masking::Secret::new),
card_type: offer_card.and_then(|card| card.card_type.clone()),
bank_code: offer_card.and_then(|card| card.bank_code.clone()),
card_country: offer_card.and_then(|card| card.card_issuing_country.clone()),