Created
July 12, 2017 13:47
-
-
Save gusanthiago/c3c656fdbddaeff0079babe4f147382b to your computer and use it in GitHub Desktop.
Testing credit cards
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
/** | |
* Regex credit card numbers test | |
* | |
* author <gusanthiagodv@gmail> | |
*/ | |
const cardCompanies = [ | |
{name: 'visa', pattern: /^4[0-9]{15}$/}, | |
{name: 'master', pattern: /^5[0-5][0-9]{14}$|2[2-6][0-9]{14}$|271[0-9]{13}$|2720[0-9]{12}/}, | |
{name: 'american_express', pattern: /^3[47][0-9]{13}$/}, | |
{name: 'diners_club', pattern: /^3(0[0-5]|[68]\d)\d{11}$/}, | |
{name: 'elo', pattern: /^((50670[7-8])|(506715)|(50671[7-9])|(50672[0-1])|(50672[4-9])|(50673[0-3])|(506739)|(50674[0-8])|(50675[0-3])|(50677[4-8])|(50900[0-9])|(50901[3-9])|(50902[0-9])|(50903[1-9])|(50904[0-9])|(50905[0-9])|(50906[0-4])|(50906[6-9])|(50907[0-2])|(50907[4-5])|(636368)|(636297)|(504175)|(438935)|(40117[8-9])|(45763[1-2])|(457393)|(431274)|(50907[6-9])|(50908[0-9])|(627780))/}, | |
{name: 'hipercard', pattern: /^((606282|637095|637568)[0-9]{10}|38[0-9]{14,17})$/}, | |
{name: 'aura', pattern: /^5[0-9]{18}$/}, | |
{name: 'jcb', pattern: /^35(28|29|[3-8]\d)\d{12}$/}, | |
{name: 'switch', pattern: /^6759\d{12}(\d{2,3})?$/}, | |
{name: 'discover', pattern: /^(6011|65\d{2}|64[4-9]\d)\d{12}|(62\d{14})$/}, | |
{name: 'solo', pattern: /^6767\d{12}(\d{2,3})?$/}, | |
{name: 'dankort', pattern: /^5019\d{12}$/}, | |
{name: 'maestro', pattern: /^(5[06-8]|6\d)\d{10,17}$/}, | |
{name: 'forbrugsforeningen', pattern: /^600722\d{10}$/}, | |
{name: 'laser', pattern: /^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/} | |
]; | |
test = [ | |
{testing: '378282246310005', expected: 'american_express'}, | |
{testing: '371449635398431', expected: 'american_express'}, | |
{testing: '30569309025904', expected: 'diners_club'}, | |
{testing: '38520000023237', expected: 'diners_club'}, | |
{testing: '6011111111111117', expected: 'discover'}, | |
{testing: '6011000990139424', expected: 'discover'}, | |
{testing: '3530111333300000', expected: 'jcb'}, | |
{testing: '3566002020360505', expected: 'jcb'}, | |
{testing: '5555555555554444', expected: 'master'}, | |
{testing: '5105105105105100', expected: 'master'}, | |
{testing: '4111111111111111', expected: 'visa'}, | |
{testing: '4012888888881881', expected: 'visa'}, | |
{testing: '4222222222222', expected: 'visa'} | |
]; | |
/** | |
* Test cards | |
*/ | |
cardTest = (strCard) => { | |
return cardCompanies.filter((elm) => { | |
if (strCard.match(elm.pattern)) { | |
return elm; | |
} | |
}); | |
}; | |
console.log(cardTest(test[0].testing)); | |
test.map((elm) => { | |
// try | |
// { | |
console.log("Test " + elm.expected + ":", elm.expected == cardTest(elm.testing)[0].name ? "success" : "fail"); | |
// } catch (err) { | |
// // console.log(err); | |
// } | |
}); | |
/** | |
* ref () => { | |
* EBANX : https://www.ebanx.com/business/en/developers/api-reference/payment-reference/validation-rules | |
* Active Merchant : https://github.com/activemerchant/active_merchant/blob/master/lib/active_merchant/billing/credit_card_methods.rb | |
* PayPal : https://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm | |
* } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment