Bug 1485105 - Allow 12-19 digit length card numbers. r=MattN

Differential Revision: https://phabricator.services.mozilla.com/D8271

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Sam Foster 2018-10-10 23:23:11 +00:00
Родитель 8632e09b6b
Коммит 51eef1bdaf
2 изменённых файлов: 42 добавлений и 14 удалений

Просмотреть файл

@ -108,9 +108,9 @@ class CreditCard {
if (value) {
let normalizedNumber = value.replace(/[-\s]/g, "");
// Based on the information on wiki[1], the shortest valid length should be
// 9 digits (Canadian SIN).
// [1] https://en.wikipedia.org/wiki/Social_Insurance_Number
normalizedNumber = normalizedNumber.match(/^\d{9,}$/) ?
// 12 digits (Maestro).
// [1] https://en.wikipedia.org/wiki/Payment_card_number
normalizedNumber = normalizedNumber.match(/^\d{12,}$/) ?
normalizedNumber : null;
this._number = normalizedNumber;
}
@ -126,6 +126,8 @@ class CreditCard {
// Implements the Luhn checksum algorithm as described at
// http://wikipedia.org/wiki/Luhn_algorithm
// Number digit lengths vary with network, but should fall within 12-19 range. [2]
// More details at https://en.wikipedia.org/wiki/Payment_card_number
isValidNumber() {
if (!this._number) {
return false;
@ -135,7 +137,7 @@ class CreditCard {
let number = this._number.replace(/[\-\s]/g, "");
let len = number.length;
if (len != 9 && len != 15 && len != 16) {
if (len < 12 || len > 19) {
return false;
}

Просмотреть файл

@ -15,20 +15,42 @@ add_task(function isValidNumber() {
}
testValid("0000000000000000", true);
testValid("41111111112", false); // passes Luhn but too short
testValid("4111-1111-112", false); // passes Luhn but too short
testValid("55555555555544440018", false); // passes Luhn but too long
testValid("5555 5555 5555 4444 0018", false); // passes Luhn but too long
testValid("4929001587121045", true);
testValid("5103059495477870", true);
testValid("6011029476355493", true);
testValid("3589993783099582", true);
testValid("5415425865751454", true);
if (CreditCard.isValidNumber("30190729470495")) {
ok(false, "todo: 14-digit numbers (Diners Club) aren't supported by isValidNumber yet");
}
if (CreditCard.isValidNumber("36333851788250")) {
ok(false, "todo: 14-digit numbers (Diners Club) aren't supported by isValidNumber yet");
}
if (CreditCard.isValidNumber("3532596776688495393")) {
ok(false, "todo: 19-digit numbers (JCB, Discover, Maestro) could have 16-19 digits");
}
testValid("378282246310005", true); // American Express test number
testValid("371449635398431", true); // American Express test number
testValid("378734493671000", true); // American Express Corporate test number
testValid("5610591081018250", true); // Australian BankCard test number
testValid("6759649826438453", true); // Maestro test number
testValid("6799990100000000019", true); // 19 digit Maestro test number
testValid("6799-9901-0000-0000019", true); // 19 digit Maestro test number
testValid("30569309025904", true); // 14 digit Diners Club test number
testValid("38520000023237", true); // 14 digit Diners Club test number
testValid("6011111111111117", true); // Discover test number
testValid("6011000990139424", true); // Discover test number
testValid("3530111333300000", true); // JCB test number
testValid("3566002020360505", true); // JCB test number
testValid("3532596776688495393", true); // 19-digit JCB number. JCB, Discover, Maestro could have 16-19 digits
testValid("3532 5967 7668 8495393", true); // 19-digit JCB number. JCB, Discover, Maestro could have 16-19 digits
testValid("5555555555554444", true); // MasterCard test number
testValid("5105105105105100", true); // MasterCard test number
testValid("2221000000000009", true); // 2-series MasterCard test number
testValid("4111111111111111", true); // Visa test number
testValid("4012888888881881", true); // Visa test number
testValid("4222222222222", true); // 13 digit Visa test number
testValid("4222 2222 22222", true); // 13 digit Visa test number
testValid("4035 5010 0000 0008", true); // Visadebit/Cartebancaire test number
testValid("5038146897157463", true);
testValid("4026313395502338", true);
testValid("6387060366272981", true);
@ -55,7 +77,8 @@ add_task(function isValidNumber() {
testValid("0000-0000-0080-4609", true);
testValid("0000 0000 0222 331", true);
testValid("344060747836806", true);
testValid("001064088", true);
testValid("001064088", false); // too short
testValid("00-10-64-088", false); // still too short
testValid("4929001587121046", false);
testValid("5103059495477876", false);
testValid("6011029476355494", false);
@ -117,6 +140,7 @@ add_task(function test_maskNumber() {
testMask("3589993783099582", "**** 9582");
testMask("5415425865751454", "**** 1454");
testMask("344060747836806", "**** 6806");
testMask("6799990100000000019", "**** 0019");
Assert.throws(() => (new CreditCard({number: "1234"})).maskedNumber,
/Invalid credit card number/,
"Four or less numbers should throw when retrieving the maskedNumber");
@ -135,6 +159,8 @@ add_task(function test_longMaskedNumber() {
testMask("3589993783099582", "************9582");
testMask("5415425865751454", "************1454");
testMask("344060747836806", "***********6806");
testMask("6799990100000000019", "***************0019");
Assert.throws(() => (new CreditCard({number: "1234"})).longMaskedNumber,
/Invalid credit card number/,
"Four or less numbers should throw when retrieving the maskedNumber");