Skip to content

Commit

Permalink
Fix Maestro potential validity bugs
Browse files Browse the repository at this point in the history
Closes braintree#20.
  • Loading branch information
braintreeps committed Feb 2, 2016
1 parent b20ac92 commit 9565f30
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/card-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function verification(card, isPotentiallyValid, isValid) {
}

function cardNumber(value) {
var potentialTypes, cardType, valid, i, maxLength;
var potentialTypes, cardType, isPotentiallyValid, isValid, i, maxLength;

if (isNumber(value)) {
value = String(value);
Expand All @@ -32,24 +32,21 @@ function cardNumber(value) {
cardType = potentialTypes[0];

if (cardType.type === 'unionpay') { // UnionPay is not Luhn 10 compliant
valid = true;
isValid = true;
} else {
valid = luhn10(value);
isValid = luhn10(value);
}

maxLength = Math.max.apply(null, cardType.lengths);

for (i = 0; i < cardType.lengths.length; i++) {
if (cardType.lengths[i] === value.length) {
return verification(cardType, valid, valid);
isPotentiallyValid = (value.length !== maxLength) || isValid;
return verification(cardType, isPotentiallyValid, isValid);
}
}

maxLength = Math.max.apply(null, cardType.lengths);

if (value.length < maxLength) {
return verification(cardType, true, false);
}

return verification(cardType, false, false);
return verification(cardType, value.length < maxLength, false);
}

module.exports = cardNumber;
13 changes: 13 additions & 0 deletions test/unit/card-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ describe('number validates', function () {
]);
});

describe('Maestro', function () {
table([
['500000000000', {card: 'maestro', isPotentiallyValid: true, isValid: false}],
['500000000000061', {card: 'maestro', isPotentiallyValid: true, isValid: false}],
['5000000000000611', {card: 'maestro', isPotentiallyValid: true, isValid: true}],
['5000000000000612', {card: 'maestro', isPotentiallyValid: true, isValid: false}],
['500000000000000005', {card: 'maestro', isPotentiallyValid: true, isValid: false}],
['5000000000000000005', {card: 'maestro', isPotentiallyValid: true, isValid: true}],
['5000000000000000001', {card: 'maestro', isPotentiallyValid: false, isValid: false}],
['50000000000000000009', {card: 'maestro', isPotentiallyValid: false, isValid: false}],
]);
});

describe('Amex', function () {
table([
['3782',
Expand Down

0 comments on commit 9565f30

Please sign in to comment.