Skip to content

Instantly share code, notes, and snippets.

@stracqan
Last active October 28, 2023 14:14
Show Gist options
  • Save stracqan/af4af3bf8dd5dcaa9528 to your computer and use it in GitHub Desktop.
Save stracqan/af4af3bf8dd5dcaa9528 to your computer and use it in GitHub Desktop.
Very simple jQuery implementation that reads card data from a USB Magnetic Strip Credit Card Reader.
/**
*
* Simple jQuery Script to parse credit card data
* that was collected via a USB Magnetic Stripe
* Credit Card Reader.
*
* To get this to work, focus your cursor (either
* programmatically or via a click, it's your choice) on an input field #credit-card-number
* and then with the card reader plugged in, swipe
* the card and it will take over from there
*
*/
$(document).ready(function(){
$('#creditCardNumber').click(function(){
//document.getElementById('SwipeNowAlert').innerHTML = 'You may now swipe.';
}).blur(function(){
document.getElementById('SwipeNowAlert').innerHTML = 'Don\'t Swipe';
}).focus(function(){
document.getElementById('SwipeNowAlert').innerHTML = 'SWIPE NOW';
}).keyup(function(event) {
if (event.keyCode == 13) {
var ccNum = $('#credit-card-number').val();
var isCaretPresent = false;
var isEqualPresent = false;
if (ccNum.indexOf("^") != -1)
isCaretPresent = true
else
isCaretPresent = false;
if (ccNum.indexOf("=") != -1)
isEqualPresent = true
else
isEqualPresent = false;
//handle parsing differently depending on card format
if (isCaretPresent) {
var cardData = ccNum.split('^');
$("#first-name").val(formatFirstName(cardData[1]));
$("#last-name").val(formatLastName(cardData[1]));
var decryptedCardNumber = formatCardNumber(cardData[0]);
$("#card-number").val(decryptedCardNumber);
$("#card-type").val(getCardType(decryptedCardNumber));
$("#expiration-month").val(cardData[2].substring(2, 4));
$("#expiration-year").val(cardData[2].substring(0, 2));
} else if (isEqualPresent) {
var cardData = ccNum.split('=');
var decryptedCardNumber = formatCardNumber(cardData[0]);
$("#CardNumber").val(decryptedCardNumber);
$("#CardType").val(getCardType(decryptedCardNumber));
$("#ExpirationMonth").val(cardData[2].substring(2, 4));
$("#ExpirationYear").val(cardData[2].substring(0, 2));
}
} else {
return true;
}
});
function formatCardNumber(cardNum) {
var result = "";
result = cardNum.replace(/[^0-9]*/, "");
return result;
}
function formatFirstName(name) {
if (name.indexOf("/") != -1) {
var nameSplit = name.split('/');
return nameSplit[1];
} else {
return "";
}
}
function FormatLastName(name) {
if (name.indexOf("/") != -1) {
var nameSplit = name.split('/');
return nameSplit[0];
} else {
return "";
}
}
function getCardType(number) {
var re = new RegExp("^4");
if (number.match(re) != null)
return "Visa";
re = new RegExp("^(34|37)");
if (number.match(re) != null)
return "American Express";
re = new RegExp("^5[1-5]");
if (number.match(re) != null)
return "MasterCard";
re = new RegExp("^6011");
if (number.match(re) != null)
return "Discover";
return "";
}
});
@ingjveras
Copy link

Hi friend,
I want to see if you can help me program a device that uses a magnetic strip reader, sunmi brand, p2 pro model, I can pay you money for your help, I hope you can help me. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment