Skip to content

Instantly share code, notes, and snippets.

@craigmaslowski
Last active July 10, 2019 18:03
Show Gist options
  • Save craigmaslowski/18b4fd730e6acc4aedc4 to your computer and use it in GitHub Desktop.
Save craigmaslowski/18b4fd730e6acc4aedc4 to your computer and use it in GitHub Desktop.
Credit Card Type Detection
// card type prefixes sourced from: https://en.wikipedia.org/wiki/Bank_card_number#Issuer_identification_number_.28IIN.29
var cardTypes = {
'American Express': [
[34],
[37]
],
'Diners Club Intl': [
[300, 305],
[309],
[36],
[38, 39]
],
'Discover': [
[6011],
[622126, 622925],
[644, 649],
[65]
],
'JCB': [
[3528, 3589]
],
'Mastercard': [
[50, 55]
],
'Visa': [
[4]
],
};
function startsWith (source, subString) {
return source.indexOf(subString) === 0;
}
function between(cardNumber, x, y) {
var cardNumberPrefix = cardNumber.substring(0, x.toString().length);
if (isNaN(cardNumberPrefix)) return false;
return cardNumberPrefix >= x && cardNumberPrefix <= y;
}
function detectCardType (cardNumber, prefixes) {
for (var i = 0, n = prefixes.length; i < n; i++) {
if (prefixes[i].length == 1) {
if (startsWith(cardNumber, prefixes[i][0].toString()))
return true;
} else if (prefixes[i].length == 2) {
if (between(cardNumber, prefixes[i][0], prefixes[i][1])) {
return true;
}
}
}
return false;
}
module.exports = function (cardNumber) {
var cardType = '';
for (var property in cardTypes) {
if (cardTypes.hasOwnProperty(property)) {
if (detectCardType(cardNumber, cardTypes[property])) {
return property;
}
}
}
return cardType;
};
var detectCardType = require('./index'),
chai = require('chai');
chai.should();
describe('detectCardType', function () {
// AMERICAN EXPRESS
it('should return "American Express" for a card number starting with 34', function () {
var cardType = detectCardType('3400000000000000');
cardType.should.equal('American Express');
});
it('should return "American Express" for a card number starting with 37', function () {
var cardType = detectCardType('3700000000000000');
cardType.should.equal('American Express');
});
//DINER'S CLUB INTL
it('should return "Diners Club Intl" for a card number starting with 300', function () {
var cardType = detectCardType('3000000000000000');
cardType.should.equal('Diners Club Intl');
});
it('should return "Diners Club Intl" for a card number starting with 301', function () {
var cardType = detectCardType('3010000000000000');
cardType.should.equal('Diners Club Intl');
});
it('should return "Diners Club Intl" for a card number starting with 302', function () {
var cardType = detectCardType('3020000000000000');
cardType.should.equal('Diners Club Intl');
});
it('should return "Diners Club Intl" for a card number starting with 303', function () {
var cardType = detectCardType('3030000000000000');
cardType.should.equal('Diners Club Intl');
});
it('should return "Diners Club Intl" for a card number starting with 304', function () {
var cardType = detectCardType('3040000000000000');
cardType.should.equal('Diners Club Intl');
});
it('should return "Diners Club Intl" for a card number starting with 305', function () {
var cardType = detectCardType('3050000000000000');
cardType.should.equal('Diners Club Intl');
});
it('should return "Diners Club Intl" for a card number starting with 309', function () {
var cardType = detectCardType('3090000000000000');
cardType.should.equal('Diners Club Intl');
});
it('should return "Diners Club Intl" for a card number starting with 36', function () {
var cardType = detectCardType('3600000000000000');
cardType.should.equal('Diners Club Intl');
});
it('should return "Diners Club Intl" for a card number starting with 38', function () {
var cardType = detectCardType('3800000000000000');
cardType.should.equal('Diners Club Intl');
});
it('should return "Diners Club Intl" for a card number starting with 39', function () {
var cardType = detectCardType('3900000000000000');
cardType.should.equal('Diners Club Intl');
});
// DISCOVER
it('should return "Discover" for a card number starting with 6011', function () {
var cardType = detectCardType('6011000000000000');
cardType.should.equal('Discover');
});
it('should return "Discover" for a card number starting with 644', function () {
var cardType = detectCardType('6440000000000000');
cardType.should.equal('Discover');
});
it('should return "Discover" for a card number starting with 645', function () {
var cardType = detectCardType('6450000000000000');
cardType.should.equal('Discover');
});
it('should return "Discover" for a card number starting with 646', function () {
var cardType = detectCardType('6460000000000000');
cardType.should.equal('Discover');
});
it('should return "Discover" for a card number starting with 647', function () {
var cardType = detectCardType('6470000000000000');
cardType.should.equal('Discover');
});
it('should return "Discover" for a card number starting with 648', function () {
var cardType = detectCardType('6480000000000000');
cardType.should.equal('Discover');
});
it('should return "Discover" for a card number starting with 649', function () {
var cardType = detectCardType('6490000000000000');
cardType.should.equal('Discover');
});
it('should return "Discover" for a handful of card numbers between 622126 and 622925', function () {
var cardType = detectCardType('6221260000000000');
cardType.should.equal('Discover');
cardType = detectCardType('6229250000000000');
cardType.should.equal('Discover');
cardType = detectCardType('6226250000000000');
cardType.should.equal('Discover');
cardType = detectCardType('6227250000000000');
cardType.should.equal('Discover');
cardType = detectCardType('6228250000000000');
cardType.should.equal('Discover');
});
// JCB
it('should return "JCB" for a handful of card numbers between 3528 and 3589', function () {
var cardType = detectCardType('3528000000000000');
cardType.should.equal('JCB');
cardType = detectCardType('3589000000000000');
cardType.should.equal('JCB');
cardType = detectCardType('3548000000000000');
cardType.should.equal('JCB');
cardType = detectCardType('3558000000000000');
cardType.should.equal('JCB');
cardType = detectCardType('3568000000000000');
cardType.should.equal('JCB');
});
// MASTERCARD
it('should return "Mastercard" for a card number starting with 50', function () {
var cardType = detectCardType('5000000000000000');
cardType.should.equal('Mastercard');
});
it('should return "Mastercard" for a card number starting with 51', function () {
var cardType = detectCardType('5111111111111111');
cardType.should.equal('Mastercard');
});
it('should return "Mastercard" for a card number starting with 52', function () {
var cardType = detectCardType('5222222222222222');
cardType.should.equal('Mastercard');
});
it('should return "Mastercard" for a card number starting with 53', function () {
var cardType = detectCardType('5333333333333333');
cardType.should.equal('Mastercard');
});
it('should return Mastercard for a card number starting with 54', function () {
var cardType = detectCardType('5444444444444444');
cardType.should.equal('Mastercard');
});
it('should return "Mastercard" for a card number starting with 55', function () {
var cardType = detectCardType('5555555555555555');
cardType.should.equal('Mastercard');
});
// VISA
it('should return "Visa" for a card number starting with 4', function () {
var cardType = detectCardType('4111111111111111');
cardType.should.equal('Visa');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment