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(connector): [checkout] Amex reference id (#13355)

Nithin Nd60a087be16c
diff --git a/crates/hyperswitch_connectors/src/connectors/checkout.rs b/crates/hyperswitch_connectors/src/connectors/checkout.rs
index fed8bc380b..d84b68a16e 100644
--- a/crates/hyperswitch_connectors/src/connectors/checkout.rs
+++ b/crates/hyperswitch_connectors/src/connectors/checkout.rs
@@ -1319,42 +1319,33 @@ impl webhooks::IncomingWebhook for Checkout {
.body
.parse_struct("CheckoutWebhookBody")
.change_context(errors::ConnectorError::WebhookReferenceIdNotFound)?;
- let ref_id: api_models::webhooks::ObjectReferenceId =
- if checkout::is_chargeback_event(&details.transaction_type) {
- let reference = match details.data.reference {
- Some(reference) => {
- api_models::payments::PaymentIdType::PaymentAttemptId(reference)
- }
- None => api_models::payments::PaymentIdType::ConnectorTransactionId(
- details
- .data
- .payment_id
- .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?,
- ),
- };
- api_models::webhooks::ObjectReferenceId::PaymentId(reference)
- } else if checkout::is_refund_event(&details.transaction_type) {
- let refund_reference = match details.data.reference {
- Some(reference) => api_models::webhooks::RefundIdType::RefundId(reference),
- None => api_models::webhooks::RefundIdType::ConnectorRefundId(
- details
- .data
- .action_id
- .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?,
- ),
- };
- api_models::webhooks::ObjectReferenceId::RefundId(refund_reference)
- } else {
- let reference_id = match details.data.reference {
- Some(reference) => {
- api_models::payments::PaymentIdType::PaymentAttemptId(reference)
- }
- None => {
- api_models::payments::PaymentIdType::ConnectorTransactionId(details.data.id)
- }
- };
- api_models::webhooks::ObjectReferenceId::PaymentId(reference_id)
+ let ref_id = if checkout::is_chargeback_event(&details.transaction_type) {
+ api_models::webhooks::ObjectReferenceId::PaymentId(
+ api_models::payments::PaymentIdType::ConnectorTransactionId(
+ details
+ .data
+ .payment_id
+ .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?,
+ ),
+ )
+ } else if checkout::is_refund_event(&details.transaction_type) {
+ let refund_reference = match details.data.reference {
+ Some(reference) => api_models::webhooks::RefundIdType::RefundId(reference),
+ None => api_models::webhooks::RefundIdType::ConnectorRefundId(
+ details
+ .data
+ .action_id
+ .ok_or(errors::ConnectorError::WebhookReferenceIdNotFound)?,
+ ),
};
+ api_models::webhooks::ObjectReferenceId::RefundId(refund_reference)
+ } else {
+ api_models::webhooks::ObjectReferenceId::PaymentId(
+ api_models::payments::PaymentIdType::ConnectorTransactionId(
+ details.data.payment_id.unwrap_or(details.data.id),
+ ),
+ )
+ };
Ok(ref_id)
}
@@ -1685,6 +1676,8 @@ static CHECKOUT_SUPPORTED_WEBHOOK_FLOWS: [enums::EventClass; 3] = [
enums::EventClass::Disputes,
];
+const AMEX_PAYMENT_REFERENCE_LENGTH: usize = 30;
+
impl ConnectorSpecifications for Checkout {
fn get_connector_about(&self) -> Option<&'static ConnectorInfo> {
Some(&CHECKOUT_CONNECTOR_INFO)
@@ -1697,4 +1690,29 @@ impl ConnectorSpecifications for Checkout {
fn get_supported_webhook_flows(&self) -> Option<&'static [enums::EventClass]> {
Some(&CHECKOUT_SUPPORTED_WEBHOOK_FLOWS)
}
+
+ #[cfg(feature = "v1")]
+ fn generate_connector_request_reference_id(
+ &self,
+ _payment_intent: &hyperswitch_domain_models::payments::PaymentIntent,
+ payment_attempt: &hyperswitch_domain_models::payments::payment_attempt::PaymentAttempt,
+ _is_config_enabled_to_send_payment_id_as_connector_request_id: bool,
+ ) -> String {
+ let is_amex = payment_attempt
+ .get_payment_method_data()
+ .and_then(|data| data.get_additional_card_info())
+ .is_some_and(|card_info| {
+ card_info.card_isin.as_deref().is_some_and(|card_isin| {
+ utils::CardIssuer::from_isin(card_isin)
+ == Some(utils::CardIssuer::AmericanExpress)
+ })
+ });
+
+ let attempt_id = payment_attempt.attempt_id.clone();
+ if is_amex & (attempt_id.clone().len() > AMEX_PAYMENT_REFERENCE_LENGTH) {
+ nanoid::nanoid!(AMEX_PAYMENT_REFERENCE_LENGTH)
+ } else {
+ payment_attempt.attempt_id.clone()
+ }
+ }
}
diff --git a/crates/hyperswitch_connectors/src/utils.rs b/crates/hyperswitch_connectors/src/utils.rs
index 45a901f9cb..e7efbf56a9 100644
--- a/crates/hyperswitch_connectors/src/utils.rs
+++ b/crates/hyperswitch_connectors/src/utils.rs
@@ -1214,6 +1214,59 @@ pub enum CardIssuer {
UnionPay,
}
+impl CardIssuer {
+ /// Identifies a card issuer from the first six digits of an ISIN/BIN.
+ ///
+ /// Returns `None` when the value has fewer than six digits, contains
+ /// non-numeric characters, or does not match a known issuer range.
+ pub fn from_isin(isin: &str) -> Option<Self> {
+ CARD_ISIN_REGEX.iter().find_map(|(issuer, regex)| {
+ regex
+ .as_ref()
+ .ok()
+ .filter(|regex| regex.is_match(isin))
+ .map(|_| *issuer)
+ })
+ }
+}
+
+static CARD_ISIN_REGEX: LazyLock<Vec<(CardIssuer, Result<Regex, regex::Error>)>> = LazyLock::new(
+ || {
+ vec![
+ // Specific ranges must precede broader overlapping ranges.
+ (CardIssuer::CarteBlanche, Regex::new(r"^389[0-9]{3}")),
+ (
+ CardIssuer::Discover,
+ Regex::new(
+ r"^(?:6011[0-9]{2}|64[4-9][0-9]{3}|65[0-9]{4}|622(?:12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[01][0-9]|92[0-5]))",
+ ),
+ ),
+ (CardIssuer::AmericanExpress, Regex::new(r"^3[47][0-9]{4}")),
+ (
+ CardIssuer::Master,
+ Regex::new(
+ r"^(?:5[1-5][0-9]{4}|2(?:2(?:2[1-9]|[3-9][0-9])|[3-6][0-9]{2}|7(?:[01][0-9]|20))[0-9]{2})",
+ ),
+ ),
+ (
+ CardIssuer::Maestro,
+ Regex::new(r"^(?:5018|5020|5038|5893|6304|6759|676[1-3])[0-9]{2}"),
+ ),
+ (
+ CardIssuer::DinersClub,
+ Regex::new(r"^3(?:0[0-5]|[68][0-9])[0-9]{3}"),
+ ),
+ (
+ CardIssuer::JCB,
+ Regex::new(r"^3(?:088|096|112|158|337|5(?:2[89]|[3-8][0-9]))[0-9]{2}"),
+ ),
+ (CardIssuer::UnionPay, Regex::new(r"^62[0-9]{4}")),
+ (CardIssuer::Visa, Regex::new(r"^4[0-9]{5}")),
+ // Cartes Bancaires is co-badged and has no unique ISIN range.
+ ]
+ },
+);
+
pub trait CardData {
fn get_card_expiry_year_2_digit(&self) -> Result<Secret<String>, errors::ConnectorError>;
fn get_card_expiry_month_2_digit(&self) -> Result<Secret<String>, errors::ConnectorError>;