Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixing isValid method #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/creditcard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import CARDS from './cards';

const MILLENNIUM = 1000;
const DEFAULT_CODE_LENGTH = 3;
const CARD_NUMBER_LENGTH = 16;

export const getCreditCardNameByNumber = (number) => {
return findCreditCardObjectByNumber(number).name || 'Credit card is invalid!';
Expand Down Expand Up @@ -47,7 +48,7 @@ function validateCards(number, cards) {
}

function hasCorrectLength(number) {
return number && number.length <= 19;
return number && number.length === CARD_NUMBER_LENGTH;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot define that the card number must have 16 digits. For example AMEX has 15 digits, and some others up to 19, and sometimes 13.

https://www.pcidssguide.com/what-do-the-credit-card-numbers-mean/

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, so following this article and the others bellow, the range of credit card number length is 12 to 19, right?!
In this way, I believe it is possible to define this range to avoid errors like the one described in the issue. What do you think?

https://www.discoverglobalnetwork.com/content/dam/discover/en_us/dgn/docs/IPP-VAR-Enabler-Compliance.pdf
https://www.forbes.com/advisor/credit-cards/what-does-your-credit-card-number-mean/

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I evaluated the list of supported cards and added the number of digits for each one:

American Express (15 digits)
Aura (19 digits)
Banescard (16 digits)
Cabal (16 digits)
Diners (14 digits)
Discover (16 digits)
Elo (16 digits)
Goodcard (16 digits)
Hipercard (16 digits)
Mastercard (16 digits)
Maxxvan (16 digits)
Visa (16 digits)

With this we can conclude that the cards must have between 14 and 19 digits.

What do you think of we add a "cardNumberLenght" property to each card in cards.js and create a method to check the minimum and maximum number of digits for a valid card? This way we avoid adding the number manually in the isValid method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree, looks like a good way. I can close this PR and open another one with this implementation.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly why i did in another version :) and why in the public release i don't validate by length

}

function removeNonNumbersCaracteres(number) {
Expand Down
4 changes: 4 additions & 0 deletions src/creditcard.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.