On the verification of the legitimacy of the bank card number, the online mainstream verification algorithm is the LUHN algorithm, the code is as follows:
/** * Get check digit with LUHM check algorithm from bank card number without check digit * @author Mengrang * @since 2016/09/18 * * public static Char Getbankcardcheckcode (String noncheckcodecardid) {if (Noncheckcodecardid = = NULL | | noncheckcodecardid.tri M (). Length () = = 0 | | !noncheckcodecardid.matches ("\\d+") | | Noncheckcodecardid.trim (). Length () <15 | |
Noncheckcodecardid.trim (). Length () >18) {//If the transmitted data is not valid return n return ' n ';
} char[] CHS = Noncheckcodecardid.trim (). ToCharArray ();
int luhmsum = 0;
Execute Luh Algorithm for (int i = chs.length-1, j = 0; I >= 0; I--, j + +) {int k = chs[i]-' 0 ';
if (j% 2 = = 0) {//even digit processing K *= 2;
K = k/10 + k% 10;
} luhmsum + = k; } return (luhmsum% 10 = = 0)?
' 0 ': (char) ((10-luhmsum% 10) + ' 0 '); }
The last digit of the bank card number is the check digit.
But for some local commercial banks, this algorithm does not apply, the actual application found Ping an bank (formerly Shenzhen Development Bank 16 debit card) Old card can not pass the Luhn algorithm.
Helpless can only verify the legitimacy of the bank card bin number, the legitimacy of the actual card number transferred to the financial payment interface to judge, this piece of no algorithm support ...
The most legitimate online verification bin number is tabular, the code is really troublesome, so all kinds of online check, and finally found a conscience interface, Ali provides the verification bin number of the free interface:
Https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo= bank card number to be verified & Cardbincheck=true
The JSON data is returned as follows:
{"Bank": "Spabank", "validated": True, "Cardtype": "DC", "Key": "6225380004804588", "Messages": [], "stat": "OK"}
{"Bank": "Bjbank", "validated": True, "Cardtype": "CC", "Key": "1475288866977-8125-10.208.0.26-684929885", "Messages": [], "stat": "OK"}
{"Validated": false, "key": "62129611060012231", "stat": "OK", "messages": [{"Errorcodes": "Card_bin_not_match", "name" : "Cardno"}]}
This interface returns the legal or not of the bank bin number (validated), the bank, bank card type (CARDTYPE,DC: Debit, CC: credit card).
Originally want to use in the foreground page, because involves cross-domain operation, so use JSONP, but the returned data browser always reported to receive data syntax error, has been unable to find specific reasons, using the code as follows:
$.ajax ({
URL: ' https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo= ' + cardnum+ ' &cardbincheck=true ',
type: "Get",
dataType: "Jsonp",
Async:false,
jsonp: ' Callback ',
jsonpcallback: ' Success_jsonpcallback ',
success:function (data) {
var ob = data;
}
});
Firebug always prompt to return data missing ";", JS does not perform properly. This piece is not very familiar, the future will have the opportunity to study again.
Through the background can be called normal, through the HTTP GET method call, the code is as follows:
public static string HttpGet (string url) {//closes httpclient system log;
System.setproperty ("Org.apache.commons.logging.Log", "Org.apache.commons.logging.impl.SimpleLog");
System.setproperty ("Org.apache.commons.logging.simplelog.showdatetime", "true");
System.setproperty ("Org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "stdout");
HttpClient HttpClient = new Defaulthttpclient ();
HttpGet get = new HttpGet (URL);
HttpResponse response = null;
Httpentity httpentity = null;
String content = null;
try {response = Httpclient.execute (get); if (Response.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {//Determine gzip, unzip if (! Objectutil.isempty (Response.getlastheader ("content-encoding")) && (Response.getlastheader (" Content-encoding "). ToString ()). IndexOf (" gzip ") >=0) {response.setentity (New gzipdecompressingentity (
Response.getentity ()));
} httpentity = Response.getentity ();
Content = entityutils.tostring (httpentity);
} } catch (Exception e) {e.printstacktrace ();
} finally {if (null! = httpentity) {try {httpentity.consumecontent ();
} catch (IOException e) {e.printstacktrace ();
}}} return content; }
public static void Main (string[] args) throws IOException {
String url = "https://ccdcapi.alipay.com/ Validateandcachecardinfo.json?_input_charset=utf-8&cardno=621226***600******2&cardbincheck=true ";
String res = httpclientutil.httpget (URL);
System.out.println (res);
Jsonobject jsonob = Json.parseobject (res);
String Bank = jsonob.getstring ("bank");
SYSTEM.OUT.PRINTLN (bank);
}
Print the data as follows:
{"Bank": "ICBC", "validated": True, "Cardtype": "DC", "Key": "6212261106001211212", "Messages": [], "stat": "OK"}
ICBC
Alipay Bank Partners Page: https://ab.alipay.com/i/yinhang.htm
Alipay Bank Simple code page: https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.M7qlQG&treeId=63&articleId= 103763&doctype=1
Reference Document: Https://www.digglife.net/articles/cnbankcard.html