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(blocklist): extend card-bin blocking with more attributes and surface deny code (#13474)

Ayush Anandef23a45733f5
diff --git a/api-reference/v1/openapi_spec_v1.json b/api-reference/v1/openapi_spec_v1.json
index ea5a91ded5..f2b40fd104 100644
--- a/api-reference/v1/openapi_spec_v1.json
+++ b/api-reference/v1/openapi_spec_v1.json
@@ -14668,6 +14668,24 @@
}
}
},
+ "BlockReasonCode": {
+ "type": "string",
+ "description": "A stable, machine-readable identifier for the reason a payment was blocked.",
+ "enum": [
+ "blocked_bin",
+ "blocked_card_info_unavailable",
+ "blocked_card_type",
+ "blocked_card_network",
+ "blocked_funding_source",
+ "blocked_card_subtype",
+ "blocked_card_segment_type",
+ "blocked_virtual_card",
+ "blocked_non_reloadable_prepaid_card",
+ "blocked_gambling_card",
+ "blocked_issuer_country",
+ "blocked_issuer"
+ ]
+ },
"BlocklistDataKind": {
"type": "string",
"enum": [
@@ -26971,6 +26989,14 @@
"properties": {
"message": {
"type": "string"
+ },
+ "code": {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/BlockReasonCode"
+ }
+ ],
+ "nullable": true
}
}
}
diff --git a/api-reference/v2/openapi_spec_v2.json b/api-reference/v2/openapi_spec_v2.json
index 9ce3ea2efe..b835e38c1a 100644
--- a/api-reference/v2/openapi_spec_v2.json
+++ b/api-reference/v2/openapi_spec_v2.json
@@ -9578,6 +9578,24 @@
}
}
},
+ "BlockReasonCode": {
+ "type": "string",
+ "description": "A stable, machine-readable identifier for the reason a payment was blocked.",
+ "enum": [
+ "blocked_bin",
+ "blocked_card_info_unavailable",
+ "blocked_card_type",
+ "blocked_card_network",
+ "blocked_funding_source",
+ "blocked_card_subtype",
+ "blocked_card_segment_type",
+ "blocked_virtual_card",
+ "blocked_non_reloadable_prepaid_card",
+ "blocked_gambling_card",
+ "blocked_issuer_country",
+ "blocked_issuer"
+ ]
+ },
"BlocklistDataKind": {
"type": "string",
"enum": [
@@ -19408,6 +19426,14 @@
"properties": {
"message": {
"type": "string"
+ },
+ "code": {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/BlockReasonCode"
+ }
+ ],
+ "nullable": true
}
}
}
diff --git a/crates/api_models/src/payments.rs b/crates/api_models/src/payments.rs
index edd9a72c89..3b5e7350f8 100644
--- a/crates/api_models/src/payments.rs
+++ b/crates/api_models/src/payments.rs
@@ -10843,7 +10843,13 @@ pub enum NextActionCall {
/// The next action is to await for a merchant callback
AwaitMerchantCallback,
/// The next action is to deny the payment with an error message
- Deny { message: String },
+ Deny {
+ message: String,
+ /// A stable, machine-readable identifier for the reason the payment was denied
+ #[schema(value_type = Option<BlockReasonCode>)]
+ #[smithy(value_type = "Option<BlockReasonCode>")]
+ code: Option<api_enums::BlockReasonCode>,
+ },
/// The next action is to perform eligibility check
EligibilityCheck,
}
diff --git a/crates/common_enums/src/enums.rs b/crates/common_enums/src/enums.rs
index 6761571959..36340bd598 100644
--- a/crates/common_enums/src/enums.rs
+++ b/crates/common_enums/src/enums.rs
@@ -680,25 +680,77 @@ pub enum BlocklistDataKind {
ExtendedCardBin,
}
-/// Reasons for blocking a payment method.
-#[derive(Debug, serde::Deserialize, serde::Serialize, ToSchema)]
-#[serde(rename_all = "snake_case")]
+#[derive(Debug)]
pub enum BlockReason {
BlockedBin,
+ BlockedCardInfoUnavailable,
BlockedCardType(CardType),
+ BlockedCardNetwork,
+ BlockedFundingSource,
+ BlockedCardSubtype,
+ BlockedCardSegmentType,
+ BlockedVirtualCard,
+ BlockedNonReloadablePrepaidCard,
+ BlockedGamblingCard,
+ BlockedIssuerCountry,
+ BlockedIssuer,
+}
+
+/// A stable, machine-readable identifier for the reason a payment was blocked.
+#[derive(
+ Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize, SmithyModel, ToSchema,
+)]
+#[serde(rename_all = "snake_case")]
+#[smithy(namespace = "com.hyperswitch.smithy.types")]
+pub enum BlockReasonCode {
+ BlockedBin,
+ BlockedCardInfoUnavailable,
+ BlockedCardType,
+ BlockedCardNetwork,
+ BlockedFundingSource,
BlockedCardSubtype,
+ BlockedCardSegmentType,
+ BlockedVirtualCard,
+ BlockedNonReloadablePrepaidCard,
+ BlockedGamblingCard,
BlockedIssuerCountry,
BlockedIssuer,
}
+impl From<BlockReason> for BlockReasonCode {
+ fn from(block_reason: BlockReason) -> Self {
+ match block_reason {
+ BlockReason::BlockedBin => Self::BlockedBin,
+ BlockReason::BlockedCardInfoUnavailable => Self::BlockedCardInfoUnavailable,
+ BlockReason::BlockedCardType(_) => Self::BlockedCardType,
+ BlockReason::BlockedCardNetwork => Self::BlockedCardNetwork,
+ BlockReason::BlockedFundingSource => Self::BlockedFundingSource,
+ BlockReason::BlockedCardSubtype => Self::BlockedCardSubtype,
+ BlockReason::BlockedCardSegmentType => Self::BlockedCardSegmentType,
+ BlockReason::BlockedVirtualCard => Self::BlockedVirtualCard,
+ BlockReason::BlockedNonReloadablePrepaidCard => Self::BlockedNonReloadablePrepaidCard,
+ BlockReason::BlockedGamblingCard => Self::BlockedGamblingCard,
+ BlockReason::BlockedIssuerCountry => Self::BlockedIssuerCountry,
+ BlockReason::BlockedIssuer => Self::BlockedIssuer,
+ }
+ }
+}
+
impl BlockReason {
pub fn error_message(&self) -> String {
match self {
Self::BlockedBin => "We're unable to accept this card, please try another card or a different payment method".to_string(),
+ Self::BlockedCardInfoUnavailable => "We couldn't verify this card's information, please try a different card".to_string(),
Self::BlockedCardType(card_type) => {
format!("{} cards are not accepted for this transaction, please try a different card", card_type.title_case())
}
+ Self::BlockedCardNetwork => "This card network is not accepted for this transaction, please try a different card".to_string(),
+ Self::BlockedFundingSource => "This card funding source is not accepted for this transaction, please try a different card".to_string(),
Self::BlockedCardSubtype => "This card is not accepted for this transaction, please try a different card".to_string(),
+ Self::BlockedCardSegmentType => "This card segment is not accepted for this transaction, please try a different card".to_string(),
+ Self::BlockedVirtualCard => "Virtual cards are not accepted for this transaction, please try a different card".to_string(),
+ Self::BlockedNonReloadablePrepaidCard => "Non-reloadable prepaid cards are not accepted for this transaction, please try a different card".to_string(),
+ Self::BlockedGamblingCard => "Cards associated with gambling are not accepted for this transaction, please try a different card".to_string(),
Self::BlockedIssuerCountry => "Cards issued in your region aren't supported for this transaction, please try a different card".to_string(),
Self::BlockedIssuer => "We can't process payments from this bank, please try another card or a different payment method".to_string(),
}
diff --git a/crates/openapi/src/openapi.rs b/crates/openapi/src/openapi.rs
index 9a340e45ff..58932b9892 100644
--- a/crates/openapi/src/openapi.rs
+++ b/crates/openapi/src/openapi.rs
@@ -718,6 +718,7 @@ Never share your secret api keys. Keep them guarded and secure.
api_models::payments::ApplepaySessionTokenResponse,
api_models::payments::SdkNextAction,
api_models::payments::NextActionCall,
+ api_models::enums::BlockReasonCode,
api_models::payments::SdkNextActionData,
api_models::payments::SamsungPayWalletData,
api_models::payments::WeChatPay,
diff --git a/crates/openapi/src/openapi_v2.rs b/crates/openapi/src/openapi_v2.rs
index e2b09f6e1c..87d0be95d5 100644
--- a/crates/openapi/src/openapi_v2.rs
+++ b/crates/openapi/src/openapi_v2.rs
@@ -598,6 +598,7 @@ Never share your secret api keys. Keep them guarded and secure.
api_models::payments::ApplepaySessionTokenResponse,
api_models::payments::SdkNextAction,
api_models::payments::NextActionCall,
+ api_models::enums::BlockReasonCode,
api_models::payments::SdkNextActionData,
api_models::payments::SamsungPayWalletData,
api_models::payments::WeChatPay,
diff --git a/crates/router/src/core/blocklist/utils.rs b/crates/router/src/core/blocklist/utils.rs
index 07a94f0c8d..f7c4124eb1 100644
--- a/crates/router/src/core/blocklist/utils.rs
+++ b/crates/router/src/core/blocklist/utils.rs
@@ -1,3 +1,5 @@
+use std::collections::HashSet;
+
use api_models::blocklist as api_blocklist;
use common_enums::{BlockReason, MerchantDecision};
use common_utils::errors::CustomResult;
@@ -536,6 +538,20 @@ pub async fn should_payment_be_blocked_by_profile_config(
});
if let (Some(card_config), Some(card_isin)) = (card_config, card_isin) {
+ let CardBlockingConfig {
+ issuing_country,
+ card_types,
+ card_subtypes,
+ issuers,
+ block_if_bin_info_unavailable,
+ card_networks,
+ funding_sources,
+ card_segment_types,
+ block_virtual_cards,
+ block_non_reloadable_prepaid_cards,
+ gambling_blocked,
+ } = card_config;
+
let card_info = state
.store
.get_card_info(&card_isin)
@@ -546,19 +562,19 @@ pub async fn should_payment_be_blocked_by_profile_config(
match card_info {
None => {
- if card_config.should_block_if_bin_info_unavailable() {
- block_reason = Some(BlockReason::BlockedBin);
+ if *block_if_bin_info_unavailable == Some(true) {
+ block_reason = Some(BlockReason::BlockedCardInfoUnavailable);
}
}
Some(info) => {
block_reason = CardBlockingConfig::should_block_by_attribute(
- &card_config.issuing_country,
+ issuing_country,
info.country_code.as_deref(),
)
.then_some(BlockReason::BlockedIssuerCountry)
.or_else(|| {
CardBlockingConfig::should_block_by_attribute(
- &card_config.card_types,
+ card_types,
info.card_type.as_deref(),
)
.then(|| {
@@ -569,19 +585,54 @@ pub async fn should_payment_be_blocked_by_profile_config(
})
.flatten()
})
+ .or_else(|| {
+ funding_sources
+ .as_ref()
+ .zip(info.funding_source.as_ref())
+ .is_some_and(|(blocked_sources, source)| blocked_sources.contains(source))
+ .then_some(BlockReason::BlockedFundingSource)
+ })
+ .or_else(|| {
+ card_networks
+ .as_ref()
+ .zip(info.card_network.as_ref())
+ .is_some_and(|(blocked_networks, network)| {
+ blocked_networks.contains(network)
+ })
+ .then_some(BlockReason::BlockedCardNetwork)
+ })
.or_else(|| {
CardBlockingConfig::should_block_by_attribute(
- &card_config.card_subtypes,
+ card_subtypes,
info.card_subtype.as_deref(),
)
.then_some(BlockReason::BlockedCardSubtype)
+ })
+ .or_else(|| {
+ CardBlockingConfig::should_block_by_attribute(
+ card_segment_types,
+ info.card_segment_type.as_deref(),
+ )
+ .then_some(BlockReason::BlockedCardSegmentType)
+ })
+ .or_else(|| {
+ (*block_virtual_cards == Some(true) && info.virtual_card == Some(true))
+ .then_some(BlockReason::BlockedVirtualCard)
+ })
+ .or_else(|| {
+ (*block_non_reloadable_prepaid_cards == Some(true)
+ && info.prepaid == Some(true)
+ && info.reloadable_prepaid == Some(false))
+ .then_some(BlockReason::BlockedNonReloadablePrepaidCard)
+ })
+ .or_else(|| {
+ (*gambling_blocked == Some(true) && info.gambling_blocked == Some(true))
+ .then_some(BlockReason::BlockedGamblingCard)
});
// Check card issuer — profile stores IDs, cards_info has name
if block_reason.is_none() {
- if let (Some(blocked_ids), Some(issuer_name)) =
- (&card_config.issuers, &info.card_issuer)
- {
+ if let (Some(blocked_ids), Some(issuer_name)) = (issuers, &info.card_issuer) {
let issuer_ids = blocked_ids
.iter()
.filter_map(|id| {
@@ -597,7 +648,7 @@ pub async fn should_payment_be_blocked_by_profile_config(
.unwrap_or_default()
.into_iter()
.map(|i| i.issuer_name)
- .collect::<std::collections::HashSet<_>>();
+ .collect::<HashSet<_>>();
if resolved_names.contains(issuer_name.as_str()) {
block_reason = Some(BlockReason::BlockedIssuer);
}
@@ -641,3 +692,41 @@ pub async fn generate_payment_fingerprint(
},
)
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn block_reasons_have_distinct_error_messages() {
+ let network_message = BlockReason::BlockedCardNetwork.error_message();
+ let funding_source_message = BlockReason::BlockedFundingSource.error_message();
+
+ assert_eq!(
+ network_message,
+ "This card network is not accepted for this transaction, please try a different card"
+ );
+ assert_eq!(
+ funding_source_message,
+ "This card funding source is not accepted for this transaction, please try a different card"
+ );
+
+ let messages = [
+ BlockReason::BlockedBin.error_message(),
+ BlockReason::BlockedCardInfoUnavailable.error_message(),
+ BlockReason::BlockedCardType(common_enums::CardType::Credit).error_message(),
+ network_message,
+ funding_source_message,
+ BlockReason::BlockedCardSubtype.error_message(),
+ BlockReason::BlockedCardSegmentType.error_message(),
+ BlockReason::BlockedVirtualCard.error_message(),
+ BlockReason::BlockedNonReloadablePrepaidCard.error_message(),
+ BlockReason::BlockedGamblingCard.error_message(),
+ BlockReason::BlockedIssuerCountry.error_message(),
+ BlockReason::BlockedIssuer.error_message(),
+ ];
+ let unique_messages = messages.iter().collect::<HashSet<_>>();
+
+ assert_eq!(messages.len(), unique_messages.len());
+ }
+}
diff --git a/crates/router/src/core/payments.rs b/crates/router/src/core/payments.rs
index e1201188db..f2e38ac6cf 100644
--- a/crates/router/src/core/payments.rs
+++ b/crates/router/src/core/payments.rs
@@ -13451,7 +13451,10 @@ trait EligibilityCheck {
#[derive(Debug, Clone)]
pub enum CheckResult {
Allow,
- Deny { message: String },
+ Deny {
+ message: String,
+ code: Option<common_enums::BlockReasonCode>,
+ },
}
#[cfg(feature = "v1")]
@@ -13459,8 +13462,8 @@ impl From<CheckResult> for Option<api_models::payments::SdkNextAction> {
fn from(result: CheckResult) -> Self {
match result {
CheckResult::Allow => None,
- CheckResult::Deny { message } => Some(api_models::payments::SdkNextAction {
- next_action: api_models::payments::NextActionCall::Deny { message },
+ CheckResult::Deny { message, code } => Some(api_models::payments::SdkNextAction {
+ next_action: api_models::payments::NextActionCall::Deny { message, code },
should_block_confirm: None,
}),
}
@@ -13520,6 +13523,7 @@ impl EligibilityCheck for BlockListCheck {
logger::warn!(block_reason = ?reason, "Payment blocked by blocklist");
Ok(CheckResult::Deny {
message: reason.error_message(),
+ code: Some(reason.into()),
})
}
None => Ok(CheckResult::Allow),
@@ -13578,6 +13582,7 @@ impl EligibilityCheck for CardTestingCheck {
errors::ApiErrorResponse::PreconditionFailed { message } => {
Ok(CheckResult::Deny {
message: message.to_string(),
+ code: None,
})
}
// For any other error, propagate it