信用卡的种类是否可以仅通过信用卡号码确定?
这样做是否推荐,还是我们应该始终询问客户他们使用的信用卡类型?
我在谷歌上搜索了相关算法,并找到了这个链接:http://cuinl.tripod.com/Tips/o-1.htm ,这可靠吗?
信用卡的种类是否可以仅通过信用卡号码确定?
这样做是否推荐,还是我们应该始终询问客户他们使用的信用卡类型?
我在谷歌上搜索了相关算法,并找到了这个链接:http://cuinl.tripod.com/Tips/o-1.htm ,这可靠吗?
是的,你提到的网站是正确的。许多网站,包括 Google Checkout,都依赖于卡类型的自动检测。这很方便,使用户界面更简洁(少一个输入框),并节省时间。请继续!
我听说过一个让客户选择付款方式的好理由(即使你可以自己解决)。这样客户就知道你接受哪些信用卡。
以下是我使用的脚本,适用于当前卡号范围,并对卡号进行有效性检查。
/**
* checks a given string for a valid credit card
* @returns:
* -1 invalid
* 1 mastercard
* 2 visa
* 3 amex
* 4 diners club
* 5 discover
* 6 enRoute
* 7 jcb
*/
function checkCC(val) {
String.prototype.startsWith = function (str) {
return (this.match("^" + str) == str)
}
Array.prototype.has=function(v,i){
for (var j=0;j<this.length;j++){
if (this[j]==v) return (!i ? true : j);
}
return false;
}
// get rid of all non-numbers (space etc)
val = val.replace(/[^0-9]/g, "");
// now get digits
var d = new Array();
var a = 0;
var len = 0;
var cval = val;
while (cval != 0) {
d[a] = cval%10;
cval -= d[a];
cval /= 10;
a++;
len++;
}
if (len < 13)
return -1;
var cType = -1;
// mastercard
if (val.startsWith("5")) {
if (len != 16)
return -1;
cType = 1;
} else
// visa
if (val.startsWith("4")) {
if (len != 16 && len != 13)
return -1;
cType = 2;
} else
// amex
if (val.startsWith("34") || val.startsWith("37")) {
if (len != 15)
return -1;
cType = 3;
} else
// diners
if (val.startsWith("36") || val.startsWith("38") || val.startsWith("300") || val.startsWith("301") || val.startsWith("302") || val.startsWith("303") || val.startsWith("304") || val.startsWith("305")) {
if (len != 14)
return -1;
cType = 4;
} else
// discover
if (val.startsWith("6011")) {
if (len != 15 && len != 16)
return -1;
cType = 5;
} else
// enRoute
if (val.startsWith("2014") || val.startsWith("2149")) {
if (len != 15 && len != 16)
return -1;
// any digit check
return 6;
} else
// jcb
if (val.startsWith("3")) {
if (len != 16)
return -1;
cType = 7;
} else
// jcb
if (val.startsWith("2131") || val.startsWith("1800")) {
if (len != 15)
return -1;
cType = 7;
} else
return - 1;
// invalid cc company
// lets do some calculation
var sum = 0;
var i;
for (i = 1; i < len; i += 2) {
var s = d[i] * 2;
sum += s % 10;
sum += (s - s%10) /10;
}
for (i = 0; i < len; i += 2)
sum += d[i];
// musst be %10
if (sum%10 != 0)
return - 1;
return cType;
}
这里是一个关于各种信用卡相关事项的完整C#或VB代码,可以在codeproject上找到。点击此处
这篇文章已经发布了几年,没有任何负面评论。
这是同第一篇帖子提到的算法的PHP版本
<?php
function CreditCardType($CardNo)
{
/*
'*CARD TYPES *PREFIX *WIDTH
'American Express 34, 37 15
'Diners Club 300 to 305, 36 14
'Carte Blanche 38 14
'Discover 6011 16
'EnRoute 2014, 2149 15
'JCB 3 16
'JCB 2131, 1800 15
'Master Card 51 to 55 16
'Visa 4 13, 16
*/
//Just in case nothing is found
$CreditCardType = "Unknown";
//Remove all spaces and dashes from the passed string
$CardNo = str_replace("-", "",str_replace(" ", "",$CardNo));
//Check that the minimum length of the string isn't less
//than fourteen characters and -is- numeric
If(strlen($CardNo) < 14 || !is_numeric($CardNo))
return false;
//Check the first two digits first
switch(substr($CardNo,0, 2))
{
Case 34: Case 37:
$CreditCardType = "American Express";
break;
Case 36:
$CreditCardType = "Diners Club";
break;
Case 38:
$CreditCardType = "Carte Blanche";
break;
Case 51: Case 52: Case 53: Case 54: Case 55:
$CreditCardType = "Master Card";
break;
}
//None of the above - so check the
if($CreditCardType == "Unknown")
{
//first four digits collectively
switch(substr($CardNo,0, 4))
{
Case 2014:Case 2149:
$CreditCardType = "EnRoute";
break;
Case 2131:Case 1800:
$CreditCardType = "JCB";
break;
Case 6011:
$CreditCardType = "Discover";
break;
}
}
//None of the above - so check the
if($CreditCardType == "Unknown")
{
//first three digits collectively
if(substr($CardNo,0, 3) >= 300 && substr($CardNo,0, 3) <= 305)
{
$CreditCardType = "American Diners Club";
}
}
//None of the above -
if($CreditCardType == "Unknown")
{
//So simply check the first digit
switch(substr($CardNo,0, 1))
{
Case 3:
$CreditCardType = "JCB";
break;
Case 4:
$CreditCardType = "Visa";
break;
}
}
return $CreditCardType;
}
?>
维基百科上包含了大部分信用卡前缀的列表。你提供的链接中有一些卡片并未列出。不过,你提供的链接是有效的。
询问信用卡类型的一个原因是为了进行额外的验证,将用户提供的信息与卡号进行比对。
Stripe提供了这个极好的用于卡方案检测的javascript库。让我添加一些代码片段并向您展示如何使用它。
首先将其包含到您的网页中,如下:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.payment/1.2.3/jquery.payment.js " ></script>
$(document).ready(function() {
var type = $.payment.cardType("4242 4242 4242 4242"); //test card number
console.log(type);
});