Last active
October 19, 2017 20:04
-
-
Save entone/ee9a59bb1ec6b14739f1c81f02432c77 to your computer and use it in GitHub Desktop.
parse credit card data from magnetic card reader and input it into NAR Booth Payments form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var credit_card_data = ""; | |
var timer = null; | |
function parse(val){ | |
var tracks = val.split("?"); | |
var parts = tracks[0].split("^"); | |
var card = parts[0].replace("%B", ""); | |
var card_type = get_card_type(card); | |
var names = parts[1].split("/") | |
var name = names[1].trim()+" "+names[0].trim(); | |
var yy = parts[2].substr(0, 2); | |
var mm = parts[2].substr(2, 2); | |
return { | |
card: card, | |
card_type: card_type, | |
name: name, | |
yy: yy, | |
mm: mm | |
} | |
} | |
function get_card_type(number){ | |
// visa | |
var re = new RegExp("^4"); | |
if (number.match(re) != null) return "Visa"; | |
// Mastercard | |
// Updated for Mastercard 2017 BINs expansion | |
if (/^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$/.test(number)) return "Master Card"; | |
// AMEX | |
re = new RegExp("^3[47]"); | |
if (number.match(re) != null) return "American Express"; | |
// Discover | |
re = new RegExp("^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)"); | |
if (number.match(re) != null) return "Discover"; | |
// Diners | |
re = new RegExp("^36"); | |
if (number.match(re) != null) return "Diners"; | |
// Diners - Carte Blanche | |
re = new RegExp("^30[0-5]"); | |
if (number.match(re) != null) return "Diners - Carte Blanche"; | |
// JCB | |
re = new RegExp("^35(2[89]|[3-8][0-9])"); | |
if (number.match(re) != null) return "JCB"; | |
// Visa Electron | |
re = new RegExp("^(4026|417500|4508|4844|491(3|7))"); | |
if (number.match(re) != null) return "Visa Electron"; | |
return ""; | |
} | |
function apply(values){ | |
console.log(values); | |
$("#CardNumber").val(values.card); | |
$("#AccountName").val(values.name); | |
$("#YY option").each(function(){ | |
var v = $(this).text().trim(); | |
if(v == values.yy){ | |
$(this).prop("selected", true); | |
} | |
}) | |
$("#MM option").each(function(){ | |
var v = $(this).text().trim(); | |
if(v == values.mm){ | |
$(this).prop("selected", true); | |
} | |
}) | |
$("input:radio").each(function(){ | |
var v = $(this).val().trim(); | |
if(v == values.card_type){ | |
$(this).prop("checked", true); | |
} | |
}) | |
} | |
$(document).ready(function(){ | |
$(document).on("keydown", function(d){ | |
if(d.key != "Shift" && d.key != "Enter" && d.key != "Alt"){ | |
if(!timer){ | |
timer = setTimeout(function(){ | |
values = parse(credit_card_data); | |
apply(values); | |
credit_card_data = ""; | |
clearTimeout(timer); | |
timer = null; | |
validate(); | |
}, 500); | |
} | |
credit_card_data = credit_card_data + d.key; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment