所以我试图通过javascript从读卡器获取信用卡信息。
这就是我所拥有的:
function readCard () {
$("#swiped").focus();
//setup before functions
var typingTimer; //timer identifier
var doneTypingInterval = 2000; //time in ms, 5 second for example
//on keyup, start the countdown
$('#swiped').keyup(function(){
clearTimeout(typingTimer);
typingTimer = setTimeout(doneTyping, doneTypingInterval);
});
//on keydown, clear the countdown
$('#swiped').keydown(function(){
clearTimeout(typingTimer);
});
function doneTyping () {
var card_data = document.getElementById('swiped').value;
var details1 = card_data.split("^");
var card_number = details1[0];
card_number = card_number.substring(2);
var names = details1[1].split("/");
var first_name = names[1];
var last_name = names[0];
var details2 = details1[2].split(";");
details2 = details2[1].split("=");
var exp_date = details2[1];
exp_date = exp_date.substring(0, exp_date.length - 1);
exp_date_month = exp_date.substring(2,4);
exp_date_year = exp_date.substring(3, 1);
//document.getElementById("first_name").value = first_name;
//document.getElementById("last_name").value = last_name;
document.getElementById("cardNumber").value = card_number;
document.getElementById("cardExpMonth").value = exp_date_month;
document.getElementById("cardExpYear").value = exp_date_year;
$("#swiped").val('');
$("#swiped").blur();
}
}
它启动一个计时器,然后当该计时器用完时,它会执行代码。一切正常。它撕毁了信用卡读卡器的字符串,并显示除了到期年份之外的一切正确。所以我遇到的问题是:
var exp_date = details2[1];
exp_date = exp_date.substring(0, exp_date.length - 1);
exp_date_month = exp_date.substring(2,4);
exp_date_year = exp_date.substring(3, 1);
我从这个问题http://stackoverflow.com/questions/19018799/parsing-credit-card-data-from-magnetic-stripe-reader-using-javascript得到了这个代码,除了过期之外它起作用了。我想不明白。
它应该导致exp月:10和exp年:15但是它导致exp月:10和exp年:51。
读卡器的字符串如下所示:
%B4242424242424242^EVANS/MITCH^15101011906300074000000?;4242424242424242=151010119063074?
其中:
4242424242424242将是卡号
和:
15101011906300074000000将是到期号码
并非这不是真正的信用卡数据;)