braintree / card-validator

Validate credit cards as users type.
MIT License
897 stars 112 forks source link

Card number validation fails if there are multiple potencial card types #64

Closed iagomelanias closed 6 years ago

iagomelanias commented 6 years ago

General information

Issue description

Currently, the card number validation logic does not allow multiple potencial card types. https://github.com/braintree/card-validator/blob/2f14aed55390a8582084feb48510e5bea6fba309/src/card-number.js#L29-L31

It means that even if the card number is completely valid based on luhn algorithm, the validation result will always return isValid as false.

Here is a example of a valid card number, which can be Elo or Maestro:

const cardValidator = require('card-validator');
const cardValidationResult = cardValidator.number('5067278453558075'); // This is a Elo card number that was generated in https://developer.paypal.com/developer/creditCardGenerator/

console.log(cardValidationResult);

// The validation above returns:
// {
//   card: null,
//   isPotentiallyValid: true,
//   isValid: false
// }

I believe this behavior was created to avoid the card type to be specified very early, when the user is typing the first numbers still. Ex: only typing 5 could trigger the card type to be Visa, but it can be Visa, Maestro or Elo.

Possible solutions

  1. Since most patterns are based on the first 4-6 digits, we can add a logic that only triggers this behavior if the user didn't type more than 5 digits. The first potencial card type is returned, since we can control the order of the card types.
  2. We can update the Elo card patterns, but it's hard to maintain and it can lead to similar problems in the future, since new card bins can be added.
crookedneighbor commented 6 years ago

You're right, we'll look at this and come up with a solution.

crookedneighbor commented 6 years ago

Looks like the underlying issue is that the card type detection was showing the card type could be Elo or Maestro, when it could really only be Elo. We elected to go with a different way of detecting the card type, in such a way that should be easier to maintain.

https://github.com/braintree/credit-card-type/pull/81

crookedneighbor commented 6 years ago

This should be fixed in v6.0.0

iagomelanias commented 6 years ago

That was a great refactoring! Now it's much simpler to manage card types. ... Elo is always making our lives more difficult 😂