Skip to content

Instantly share code, notes, and snippets.

@dpb587
Last active December 15, 2015 08:09
Show Gist options
  • Save dpb587/5229239 to your computer and use it in GitHub Desktop.
Save dpb587/5229239 to your computer and use it in GitHub Desktop.
Bank Card Readers for Web Applications - scanning credit cards into website forms.

Details

Related blog post: dpb587.me/blog/2013/03/23/bank-card-readers-for-web-applications.html

License

MIT License (http://opensource.org/licenses/mit-license.php)

Copyright (c) 2013 Danny Berger <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
define(
[ 'vendor/mootools' ],
function () {
var _ = {};
_.getCardType = function (num) {
if (num.substring(0, 1) == '4') {
return 'visa';
} else if ((num.length == 16) && ((num.substring(0, 2).toInt() >= 51) && (num.substring(0, 2).toInt() <= 55))) {
return 'mastercard';
} else if ((num.length == 15) && (num.substring(0, 2) == '34') || (num.substring(0, 2) == '37')) {
return 'amex';
} else if ((num.length == 16) && (num.substring(0, 4) == '6011')) {
return 'discover';
}
return null;
}
_.listen = function (dom, cb) {
var dom = $(dom);
var timer;
function deferred() {
var result = _.parse(dom.value);
if (result) {
dom.value = '';
cb(result, dom);
}
}
dom.addEvent(
'keypress',
function (e) {
if ((e.key == 'enter') && (dom.value.substring(0, 2) == '%B')) {
e.stop();
timer = setTimeout(deferred, 50);
} else if (timer) {
e.preventDefault();
clearTimeout(timer);
timer = setTimeout(deferred, 50);
}
}
);
return _;
};
_.parse = function (data) {
var result = {};
if (data.substring(0, 2) != '%B') {
return false;
}
var lines = data.split('\n');
var line1 = lines[0].split('^');
if (line1.length < 3) {
return false;
}
result.number = line1[0].substring(2);
var nameslash = line1[1].indexOf('/');
if (-1 != nameslash) {
result.name = line1[1].substring(nameslash + 1).trim() + ' ' + line1[1].substring(0, nameslash).trim();
} else {
result.name = line1[1].trim();
}
result.expm = line1[2].substring(2, 4);
result.expy = '20' + line1[2].substring(0, 2);
return result;
};
return _;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment