Skip to content

Commit

Permalink
fix(router): Persist card_network if present for non co-badged cards (#…
Browse files Browse the repository at this point in the history
…6212)

Co-authored-by: AkshayaFoiger <akshaya.shankar@juspay.in>
Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com>
Co-authored-by: AkshayaFoiger <131388445+AkshayaFoiger@users.noreply.github.com>
  • Loading branch information
4 people authored Oct 4, 2024
1 parent 2e54186 commit 7564826
Show file tree
Hide file tree
Showing 13 changed files with 383 additions and 147 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 14 additions & 10 deletions crates/api_models/src/payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use cards::CardNumber;
use common_utils::{
consts::default_payments_list_limit,
crypto,
errors::ValidationError,
ext_traits::{ConfigExt, Encode, ValueExt},
hashing::HashedString,
id_type,
Expand Down Expand Up @@ -1396,8 +1397,11 @@ impl GetAddressFromPaymentMethodData for Card {
}

impl Card {
fn apply_additional_card_info(&self, additional_card_info: AdditionalCardInfo) -> Self {
Self {
fn apply_additional_card_info(
&self,
additional_card_info: AdditionalCardInfo,
) -> Result<Self, error_stack::Report<ValidationError>> {
Ok(Self {
card_number: self.card_number.clone(),
card_exp_month: self.card_exp_month.clone(),
card_exp_year: self.card_exp_year.clone(),
Expand All @@ -1410,15 +1414,15 @@ impl Card {
card_network: self
.card_network
.clone()
.or(additional_card_info.card_network),
.or(additional_card_info.card_network.clone()),
card_type: self.card_type.clone().or(additional_card_info.card_type),
card_issuing_country: self
.card_issuing_country
.clone()
.or(additional_card_info.card_issuing_country),
bank_code: self.bank_code.clone().or(additional_card_info.bank_code),
nick_name: self.nick_name.clone(),
}
})
}
}

Expand Down Expand Up @@ -1858,16 +1862,16 @@ impl PaymentMethodData {
pub fn apply_additional_payment_data(
&self,
additional_payment_data: AdditionalPaymentData,
) -> Self {
) -> Result<Self, error_stack::Report<ValidationError>> {
if let AdditionalPaymentData::Card(additional_card_info) = additional_payment_data {
match self {
Self::Card(card) => {
Self::Card(card.apply_additional_card_info(*additional_card_info))
}
_ => self.to_owned(),
Self::Card(card) => Ok(Self::Card(
card.apply_additional_card_info(*additional_card_info)?,
)),
_ => Ok(self.to_owned()),
}
} else {
self.to_owned()
Ok(self.to_owned())
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/cards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ error-stack = "0.4.1"
serde = { version = "1.0.197", features = ["derive"] }
thiserror = "1.0.58"
time = "0.3.35"
regex = "1.10.4"

# First party crates
common_utils = { version = "0.1.0", path = "../common_utils" }
Expand Down
54 changes: 53 additions & 1 deletion crates/cards/src/validate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::{fmt, ops::Deref, str::FromStr};
use std::{collections::HashMap, fmt, ops::Deref, str::FromStr};

use common_utils::errors::ValidationError;
use error_stack::report;
use masking::{PeekInterface, Strategy, StrongSecret, WithType};
use regex::Regex;
use router_env::once_cell::sync::Lazy;
#[cfg(not(target_arch = "wasm32"))]
use router_env::{logger, which as router_env_which, Env};
use serde::{Deserialize, Deserializer, Serialize};
Expand Down Expand Up @@ -46,6 +50,54 @@ impl CardNumber {
.rev()
.collect::<String>()
}
pub fn is_cobadged_card(&self) -> Result<bool, error_stack::Report<ValidationError>> {
/// Regex to identify card networks
static CARD_NETWORK_REGEX: Lazy<HashMap<&str, Result<Regex, regex::Error>>> = Lazy::new(
|| {
let mut map = HashMap::new();
map.insert("Mastercard", Regex::new(r"^(5[1-5][0-9]{14}|2(2(2[1-9]|[3-9][0-9])|[3-6][0-9][0-9]|7([0-1][0-9]|20))[0-9]{12})$"));
map.insert("American Express", Regex::new(r"^3[47][0-9]{13}$"));
map.insert("Visa", Regex::new(r"^4[0-9]{12}(?:[0-9]{3})?$"));
map.insert("Discover", Regex::new(r"^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$"));
map.insert(
"Maestro",
Regex::new(r"^(5018|5081|5044|504681|504993|5020|502260|5038|603845|603123|6304|6759|676[1-3]|6220|504834|504817|504645|504775|600206|627741)"),
);
map.insert(
"RuPay",
Regex::new(r"^(508227|508[5-9]|603741|60698[5-9]|60699|607[0-8]|6079[0-7]|60798[0-4]|60800[1-9]|6080[1-9]|608[1-4]|608500|6521[5-9]|652[2-9]|6530|6531[0-4]|817290|817368|817378|353800)"),
);
map.insert("Diners Club", Regex::new(r"^(36|38|30[0-5])"));
map.insert(
"JCB",
Regex::new(r"^(3(?:088|096|112|158|337|5(?:2[89]|[3-8][0-9]))\d{12})$"),
);
map.insert("CarteBlanche", Regex::new(r"^389[0-9]{11}$"));
map.insert("Sodex", Regex::new(r"^(637513)"));
map.insert("BAJAJ", Regex::new(r"^(203040)"));
map
},
);
let mut no_of_supported_card_networks = 0;

let card_number_str = self.get_card_no();
for (_, regex) in CARD_NETWORK_REGEX.iter() {
let card_regex = match regex.as_ref() {
Ok(regex) => Ok(regex),
Err(_) => Err(report!(ValidationError::InvalidValue {
message: "Invalid regex expression".into(),
})),
}?;

if card_regex.is_match(&card_number_str) {
no_of_supported_card_networks += 1;
if no_of_supported_card_networks > 1 {
break;
}
}
}
Ok(no_of_supported_card_networks > 1)
}
}

impl FromStr for CardNumber {
Expand Down
64 changes: 60 additions & 4 deletions crates/router/src/connector/adyen/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,38 @@ pub enum CardBrand {
Visa,
MC,
Amex,
Argencard,
Bcmc,
Bijcard,
Cabal,
Cartebancaire,
Codensa,
Cup,
Dankort,
Diners,
Discover,
Electron,
Elo,
Forbrugsforeningen,
Hiper,
Hipercard,
Jcb,
Karenmillen,
Laser,
Maestro,
Maestrouk,
Mcalphabankbonus,
Mir,
Naranja,
Oasis,
Rupay,
Shopping,
Solo,
Troy,
Uatp,
Visaalphabankbonus,
Visadankort,
Warehouse,
}

#[derive(Default, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -1931,6 +1963,22 @@ impl<'a> TryFrom<&domain::GiftCardData> for AdyenPaymentMethod<'a> {
}
}

fn get_adyen_card_network(card_network: common_enums::CardNetwork) -> Option<CardBrand> {
match card_network {
common_enums::CardNetwork::Visa => Some(CardBrand::Visa),
common_enums::CardNetwork::Mastercard => Some(CardBrand::MC),
common_enums::CardNetwork::CartesBancaires => Some(CardBrand::Cartebancaire),
common_enums::CardNetwork::AmericanExpress => Some(CardBrand::Amex),
common_enums::CardNetwork::JCB => Some(CardBrand::Jcb),
common_enums::CardNetwork::DinersClub => Some(CardBrand::Diners),
common_enums::CardNetwork::Discover => Some(CardBrand::Discover),
common_enums::CardNetwork::UnionPay => Some(CardBrand::Cup),
common_enums::CardNetwork::RuPay => Some(CardBrand::Rupay),
common_enums::CardNetwork::Maestro => Some(CardBrand::Maestro),
common_enums::CardNetwork::Interac => None,
}
}

impl<'a> TryFrom<(&domain::Card, Option<Secret<String>>)> for AdyenPaymentMethod<'a> {
type Error = Error;
fn try_from(
Expand All @@ -1943,7 +1991,7 @@ impl<'a> TryFrom<(&domain::Card, Option<Secret<String>>)> for AdyenPaymentMethod
expiry_year: card.get_expiry_year_4_digit(),
cvc: Some(card.card_cvc.clone()),
holder_name: card_holder_name,
brand: None,
brand: card.card_network.clone().and_then(get_adyen_card_network),
network_payment_reference: None,
};
Ok(AdyenPaymentMethod::AdyenCard(Box::new(adyen_card)))
Expand Down Expand Up @@ -1998,7 +2046,11 @@ impl TryFrom<&utils::CardIssuer> for CardBrand {
utils::CardIssuer::AmericanExpress => Ok(Self::Amex),
utils::CardIssuer::Master => Ok(Self::MC),
utils::CardIssuer::Visa => Ok(Self::Visa),
_ => Err(errors::ConnectorError::NotImplemented("CardBrand".to_string()).into()),
utils::CardIssuer::Maestro => Ok(Self::Maestro),
utils::CardIssuer::Discover => Ok(Self::Discover),
utils::CardIssuer::DinersClub => Ok(Self::Diners),
utils::CardIssuer::JCB => Ok(Self::Jcb),
utils::CardIssuer::CarteBlanche => Ok(Self::Cartebancaire),
}
}
}
Expand Down Expand Up @@ -2529,8 +2581,12 @@ impl<'a>
payments::MandateReferenceId::NetworkMandateId(network_mandate_id) => {
match item.router_data.request.payment_method_data {
domain::PaymentMethodData::Card(ref card) => {
let card_issuer = card.get_card_issuer()?;
let brand = CardBrand::try_from(&card_issuer)?;
let brand = match card.card_network.clone().and_then(get_adyen_card_network)
{
Some(card_network) => card_network,
None => CardBrand::try_from(&card.get_card_issuer()?)?,
};

let card_holder_name = item.router_data.get_optional_billing_full_name();
let adyen_card = AdyenCard {
payment_type: PaymentType::Scheme,
Expand Down
23 changes: 19 additions & 4 deletions crates/router/src/connector/bankofamerica/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,22 @@ impl From<CardIssuer> for String {
}
}

fn get_boa_card_type(card_network: common_enums::CardNetwork) -> Option<&'static str> {
match card_network {
common_enums::CardNetwork::Visa => Some("001"),
common_enums::CardNetwork::Mastercard => Some("002"),
common_enums::CardNetwork::AmericanExpress => Some("003"),
common_enums::CardNetwork::JCB => Some("007"),
common_enums::CardNetwork::DinersClub => Some("005"),
common_enums::CardNetwork::Discover => Some("004"),
common_enums::CardNetwork::CartesBancaires => Some("006"),
common_enums::CardNetwork::UnionPay => Some("062"),
//"042" is the type code for Masetro Cards(International). For Maestro Cards(UK-Domestic) the mapping should be "024"
common_enums::CardNetwork::Maestro => Some("042"),
common_enums::CardNetwork::Interac | common_enums::CardNetwork::RuPay => None,
}
}

#[derive(Debug, Serialize)]
pub enum PaymentSolution {
ApplePay,
Expand Down Expand Up @@ -2418,10 +2434,9 @@ impl TryFrom<&domain::Card> for PaymentInformation {
type Error = error_stack::Report<errors::ConnectorError>;

fn try_from(ccard: &domain::Card) -> Result<Self, Self::Error> {
let card_issuer = ccard.get_card_issuer();
let card_type = match card_issuer {
Ok(issuer) => Some(String::from(issuer)),
Err(_) => None,
let card_type = match ccard.card_network.clone().and_then(get_boa_card_type) {
Some(card_network) => Some(card_network.to_string()),
None => ccard.get_card_issuer().ok().map(String::from),
};
Ok(Self::Cards(Box::new(CardPaymentInformation {
card: Card {
Expand Down
71 changes: 51 additions & 20 deletions crates/router/src/connector/cybersource/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,13 @@ impl TryFrom<&types::SetupMandateRouterData> for CybersourceZeroMandateRequest {

let (payment_information, solution) = match item.request.payment_method_data.clone() {
domain::PaymentMethodData::Card(ccard) => {
let card_issuer = ccard.get_card_issuer();
let card_type = match card_issuer {
Ok(issuer) => Some(String::from(issuer)),
Err(_) => None,
let card_type = match ccard
.card_network
.clone()
.and_then(get_cybersource_card_type)
{
Some(card_network) => Some(card_network.to_string()),
None => ccard.get_card_issuer().ok().map(String::from),
};
(
PaymentInformation::Cards(Box::new(CardPaymentInformation {
Expand Down Expand Up @@ -1161,10 +1164,13 @@ impl
let bill_to = build_bill_to(item.router_data.get_optional_billing(), email)?;
let order_information = OrderInformationWithBill::from((item, Some(bill_to)));

let card_issuer = ccard.get_card_issuer();
let card_type = match card_issuer {
Ok(issuer) => Some(String::from(issuer)),
Err(_) => None,
let card_type = match ccard
.card_network
.clone()
.and_then(get_cybersource_card_type)
{
Some(card_network) => Some(card_network.to_string()),
None => ccard.get_card_issuer().ok().map(String::from),
};

let security_code = if item
Expand Down Expand Up @@ -1336,10 +1342,13 @@ impl
let bill_to = build_bill_to(item.router_data.get_optional_billing(), email)?;
let order_information = OrderInformationWithBill::from((item, bill_to));

let card_issuer = ccard.get_card_issuer();
let card_type = match card_issuer {
Ok(issuer) => Some(String::from(issuer)),
Err(_) => None,
let card_type = match ccard
.card_network
.clone()
.and_then(get_cybersource_card_type)
{
Some(card_network) => Some(card_network.to_string()),
None => ccard.get_card_issuer().ok().map(String::from),
};

let payment_information = PaymentInformation::Cards(Box::new(CardPaymentInformation {
Expand Down Expand Up @@ -1841,10 +1850,13 @@ impl TryFrom<&CybersourceRouterData<&types::PaymentsAuthorizeRouterData>>
) -> Result<Self, Self::Error> {
match item.router_data.request.payment_method_data.clone() {
domain::PaymentMethodData::Card(ccard) => {
let card_issuer = ccard.get_card_issuer();
let card_type = match card_issuer {
Ok(issuer) => Some(String::from(issuer)),
Err(_) => None,
let card_type = match ccard
.card_network
.clone()
.and_then(get_cybersource_card_type)
{
Some(card_network) => Some(card_network.to_string()),
None => ccard.get_card_issuer().ok().map(String::from),
};
let payment_information =
PaymentInformation::Cards(Box::new(CardPaymentInformation {
Expand Down Expand Up @@ -2565,10 +2577,13 @@ impl TryFrom<&CybersourceRouterData<&types::PaymentsPreProcessingRouterData>>
)?;
let payment_information = match payment_method_data {
domain::PaymentMethodData::Card(ccard) => {
let card_issuer = ccard.get_card_issuer();
let card_type = match card_issuer {
Ok(issuer) => Some(String::from(issuer)),
Err(_) => None,
let card_type = match ccard
.card_network
.clone()
.and_then(get_cybersource_card_type)
{
Some(card_network) => Some(card_network.to_string()),
None => ccard.get_card_issuer().ok().map(String::from),
};
Ok(PaymentInformation::Cards(Box::new(
CardPaymentInformation {
Expand Down Expand Up @@ -3823,3 +3838,19 @@ pub fn get_error_reason(
(None, None, None) => None,
}
}

fn get_cybersource_card_type(card_network: common_enums::CardNetwork) -> Option<&'static str> {
match card_network {
common_enums::CardNetwork::Visa => Some("001"),
common_enums::CardNetwork::Mastercard => Some("002"),
common_enums::CardNetwork::AmericanExpress => Some("003"),
common_enums::CardNetwork::JCB => Some("007"),
common_enums::CardNetwork::DinersClub => Some("005"),
common_enums::CardNetwork::Discover => Some("004"),
common_enums::CardNetwork::CartesBancaires => Some("006"),
common_enums::CardNetwork::UnionPay => Some("062"),
//"042" is the type code for Masetro Cards(International). For Maestro Cards(UK-Domestic) the mapping should be "024"
common_enums::CardNetwork::Maestro => Some("042"),
common_enums::CardNetwork::Interac | common_enums::CardNetwork::RuPay => None,
}
}
Loading

0 comments on commit 7564826

Please # to comment.