我想知道如何仅根据信用卡的号码来判断信用卡的类型。有谁知道确定可靠的方法吗?
当前回答
斯威夫特 5+
extension String {
func isMatch(_ Regex: String) -> Bool {
do {
let regex = try NSRegularExpression(pattern: Regex)
let results = regex.matches(in: self, range: NSRange(self.startIndex..., in: self))
return results.map {
String(self[Range($0.range, in: self)!])
}.count > 0
} catch {
return false
}
}
func getCreditCardType() -> String? {
let VISA_Regex = "^4[0-9]{6,}$"
let MasterCard_Regex = "^5[1-5][0-9]{5,}|222[1-9][0-9]{3,}|22[3-9][0-9]{4,}|2[3-6][0-9]{5,}|27[01][0-9]{4,}|2720[0-9]{3,}$"
let AmericanExpress_Regex = "^3[47][0-9]{5,}$"
let DinersClub_Regex = "^3(?:0[0-5]|[68][0-9])[0-9]{4,}$"
let Discover_Regex = "^6(?:011|5[0-9]{2})[0-9]{3,}$"
let JCB_Regex = "^(?:2131|1800|35[0-9]{3})[0-9]{3,}$"
if self.isMatch(VISA_Regex) {
return "VISA"
} else if self.isMatch(MasterCard_Regex) {
return "MasterCard"
} else if self.isMatch(AmericanExpress_Regex) {
return "AmericanExpress"
} else if self.isMatch(DinersClub_Regex) {
return "DinersClub"
} else if self.isMatch(Discover_Regex) {
return "Discover"
} else if self.isMatch(JCB_Regex) {
return "JCB"
} else {
return nil
}
}
}
使用。
"1234123412341234".getCreditCardType()
其他回答
不要试图检测信用卡类型作为处理支付的一部分。您正在冒着拒绝有效事务的风险。
如果您需要向您的支付处理器提供信息(例如,PayPal信用卡对象需要命名卡类型),那么从可用的最少信息中猜测它,例如。
$credit_card['pan'] = preg_replace('/[^0-9]/', '', $credit_card['pan']);
$inn = (int) mb_substr($credit_card['pan'], 0, 2);
// @see http://en.wikipedia.org/wiki/List_of_Bank_Identification_Numbers#Overview
if ($inn >= 40 && $inn <= 49) {
$type = 'visa';
} else if ($inn >= 51 && $inn <= 55) {
$type = 'mastercard';
} else if ($inn >= 60 && $inn <= 65) {
$type = 'discover';
} else if ($inn >= 34 && $inn <= 37) {
$type = 'amex';
} else {
throw new \UnexpectedValueException('Unsupported card type.');
}
这个实现(只使用前两位数字)足以识别所有主要的(在PayPal的情况下是所有受支持的)卡片方案。实际上,您可能希望完全跳过异常,并默认使用最流行的卡片类型。让支付网关/处理器告诉您在响应您的请求时是否存在验证错误。
现实情况是,你的支付网关并不关心你提供的价值。
看看这个:
http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CC70060A01B
function isValidCreditCard(type, ccnum) {
/* Visa: length 16, prefix 4, dashes optional.
Mastercard: length 16, prefix 51-55, dashes optional.
Discover: length 16, prefix 6011, dashes optional.
American Express: length 15, prefix 34 or 37.
Diners: length 14, prefix 30, 36, or 38. */
var re = new Regex({
"visa": "/^4\d{3}-?\d{4}-?\d{4}-?\d",
"mc": "/^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/",
"disc": "/^6011-?\d{4}-?\d{4}-?\d{4}$/",
"amex": "/^3[47]\d{13}$/",
"diners": "/^3[068]\d{12}$/"
}[type.toLowerCase()])
if (!re.test(ccnum)) return false;
// Remove all dashes for the checksum checks to eliminate negative numbers
ccnum = ccnum.split("-").join("");
// Checksum ("Mod 10")
// Add even digits in even length strings or odd digits in odd length strings.
var checksum = 0;
for (var i = (2 - (ccnum.length % 2)); i <= ccnum.length; i += 2) {
checksum += parseInt(ccnum.charAt(i - 1));
}
// Analyze odd digits in even length strings or even digits in odd length strings.
for (var i = (ccnum.length % 2) + 1; i < ccnum.length; i += 2) {
var digit = parseInt(ccnum.charAt(i - 1)) * 2;
if (digit < 10) { checksum += digit; } else { checksum += (digit - 9); }
}
if ((checksum % 10) == 0) return true;
else return false;
}
下面是一些用Python编写的布尔函数的示例,如果根据函数名检测到卡片,则返回True。
def is_american_express(cc_number):
"""Checks if the card is an american express. If us billing address country code, & is_amex, use vpos
https://en.wikipedia.org/wiki/Bank_card_number#cite_note-GenCardFeatures-3
:param cc_number: unicode card number
"""
return bool(re.match(r'^3[47][0-9]{13}$', cc_number))
def is_visa(cc_number):
"""Checks if the card is a visa, begins with 4 and 12 or 15 additional digits.
:param cc_number: unicode card number
"""
# Standard Visa is 13 or 16, debit can be 19
if bool(re.match(r'^4', cc_number)) and len(cc_number) in [13, 16, 19]:
return True
return False
def is_mastercard(cc_number):
"""Checks if the card is a mastercard. Begins with 51-55 or 2221-2720 and 16 in length.
:param cc_number: unicode card number
"""
if len(cc_number) == 16 and cc_number.isdigit(): # Check digit, before cast to int
return bool(re.match(r'^5[1-5]', cc_number)) or int(cc_number[:4]) in range(2221, 2721)
return False
def is_discover(cc_number):
"""Checks if the card is discover, re would be too hard to maintain. Not a supported card.
:param cc_number: unicode card number
"""
if len(cc_number) == 16:
try:
# return bool(cc_number[:4] == '6011' or cc_number[:2] == '65' or cc_number[:6] in range(622126, 622926))
return bool(cc_number[:4] == '6011' or cc_number[:2] == '65' or 622126 <= int(cc_number[:6]) <= 622925)
except ValueError:
return False
return False
def is_jcb(cc_number):
"""Checks if the card is a jcb. Not a supported card.
:param cc_number: unicode card number
"""
# return bool(re.match(r'^(?:2131|1800|35\d{3})\d{11}$', cc_number)) # wikipedia
return bool(re.match(r'^35(2[89]|[3-8][0-9])[0-9]{12}$', cc_number)) # PawelDecowski
def is_diners_club(cc_number):
"""Checks if the card is a diners club. Not a supported card.
:param cc_number: unicode card number
"""
return bool(re.match(r'^3(?:0[0-6]|[68][0-9])[0-9]{11}$', cc_number)) # 0-5 = carte blance, 6 = international
def is_laser(cc_number):
"""Checks if the card is laser. Not a supported card.
:param cc_number: unicode card number
"""
return bool(re.match(r'^(6304|670[69]|6771)', cc_number))
def is_maestro(cc_number):
"""Checks if the card is maestro. Not a supported card.
:param cc_number: unicode card number
"""
possible_lengths = [12, 13, 14, 15, 16, 17, 18, 19]
return bool(re.match(r'^(50|5[6-9]|6[0-9])', cc_number)) and len(cc_number) in possible_lengths
# Child cards
def is_visa_electron(cc_number):
"""Child of visa. Checks if the card is a visa electron. Not a supported card.
:param cc_number: unicode card number
"""
return bool(re.match(r'^(4026|417500|4508|4844|491(3|7))', cc_number)) and len(cc_number) == 16
def is_total_rewards_visa(cc_number):
"""Child of visa. Checks if the card is a Total Rewards Visa. Not a supported card.
:param cc_number: unicode card number
"""
return bool(re.match(r'^41277777[0-9]{8}$', cc_number))
def is_diners_club_carte_blanche(cc_number):
"""Child card of diners. Checks if the card is a diners club carte blance. Not a supported card.
:param cc_number: unicode card number
"""
return bool(re.match(r'^30[0-5][0-9]{11}$', cc_number)) # github PawelDecowski, jquery-creditcardvalidator
def is_diners_club_carte_international(cc_number):
"""Child card of diners. Checks if the card is a diners club international. Not a supported card.
:param cc_number: unicode card number
"""
return bool(re.match(r'^36[0-9]{12}$', cc_number)) # jquery-creditcardvalidator
信用卡/借记卡号码被称为PAN,或主要帐户号码。PAN的前六位数字取自属于开证行的IIN(或发证人识别号码)(IIN以前称为BIN -银行识别号码-因此您可能会在一些文件中看到该术语的引用)。这六位数字符合国际标准ISO/IEC 7812,可用于从数字中确定卡片的类型。
不幸的是,实际的ISO/IEC 7812数据库并不是公开的,但是,有一些非官方的列表,包括商业的和免费的,包括在维基百科上。
无论如何,为了从数字中检测类型,您可以使用如下所示的正则表达式
Visa: ^4[0-9]{6,}$ Visa卡号以4开头。
万事达卡:^5[1-5][0-9]{5,}|222[1-9][0-9]{3,}|22[3-9][0-9]{4,}|2[3-6][0-9]{5,}|27[01][0-9]{4,}|2720[0-9]{3,}$ 2016年之前,万事达卡号码从数字51到55开始,但这将只检测万事达卡信用卡;还有其他使用万事达卡系统发行的卡不属于这个IIN范围。2016年,他们将在222100-272099范围内增加数字。
^3[47][0-9]{5,}$美国运通卡号以34或37开头。
大莱俱乐部:^3(?:0[0-5]|[68][0-9])[0-9]{4}$大莱俱乐部卡号以300到305、36或38开头。大莱卡的开头是5,有16位数字。这是大莱俱乐部和万事达卡的合资企业,应该像万事达卡一样处理。
发现:^6(?:011|5[0-9]{2})[0-9]{3,}$发现卡号以6011或65开头。
JCB: ^(?:2131|1800|35[0-9]{3})[0-9]{3,}$ JCB卡以2131、1800或35开头。
不幸的是,万事达卡系统处理的一些卡类型不在万事达卡的IIN范围内(编号从51…55开始);最重要的例子是Maestro卡,其中许多都是从其他银行的IIN系列发行的,因此遍布整个数字空间。因此,最好假设任何不是你接受的其他类型的卡都必须是万事达卡。
重要提示:卡号长度不同;例如,Visa过去曾发行过13位pan和16位pan卡。Visa目前的文件显示,它可能发行或可能已经发行了12到19位数的数字。因此,您不应该检查卡号的长度,而应该验证它至少有7位数字(对于一个完整的IIN加上一个检查数字,它应该与Luhn算法预测的值匹配)。
进一步提示:在处理持卡人PAN之前,从输入中去掉任何空白和标点符号。为什么?因为分组输入数字通常要容易得多,类似于它们在实际信用卡正面的显示方式。
4444 4444 4444 4444
比正确输入容易得多吗
4444444444444444
因为用户输入了您不希望看到的字符而惩罚用户实际上没有任何好处。
这也意味着要确保输入字段至少有24个字符的空间,否则输入空格的用户将会耗尽空间。我建议你设置足够宽的字段以显示32个字符,并允许最多64个字符;这为扩张提供了充足的空间。
下面这张图可以让我们更深入地了解:
更新(2016):万事达卡将从Ach支付开始实施新的BIN范围。
在卡牌范围识别(CRR)中,使用一系列正则表达式或其他硬编码范围的算法的一个缺点是,根据我的经验,bin / iin会随着时间的推移而变化。信用卡的联合品牌是一个持续的复杂问题。不同的购卡商/商家可能会要求你不同地对待同一张卡,这取决于例如地理位置。
Additionally, in the last few years with e.g. UnionPay cards in wider circulation, existing models do not cope with new ranges that sometimes interleave with broader ranges that they supersede. Knowing the geography your system needs to cover may help, as some ranges are restricted to use in particular countries. For example, ranges 62 include some AAA sub-ranges in the US, but if your merchant base is outside the US, you may be able to treat all 62 as UnionPay. You may be also asked to treat a card differently based on merchant location. E.g. to treat certain UK cards as debit domestically, but as credit internationally.
There are very useful set of rules maintained by one major Acquiring Bank. E.g. https://www.barclaycard.co.uk/business/files/BIN-Rules-EIRE.pdf and https://www.barclaycard.co.uk/business/files/BIN-Rules-UK.pdf. (Valid links as of June 2017, thanks to the user who provided a link to updated reference.) But be aware of the caveat that, while these CRR rules may represent the Card Issuing universe as it applies to the merchants acquired by that entity, it does not include e.g. ranges identified as CUP/UPI.
这些注释适用于磁条(MagStripe)或PKE (Pan Key Entry)场景。在ICC/EMV领域,情况又有所不同。
更新:本页上的其他答案(以及链接的维基百科页面)的JCB始终是16长。然而,在我的公司,我们有一个专门的工程师团队,他们在多个银行和地区认证我们的POS设备和软件。这个团队从JCB获得的最新认证卡包,有一个19长PAN的合格案例。