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

Replace the payment dependency with credit-card-type and luhn #77

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

cassiocardoso
Copy link
Contributor

@cassiocardoso cassiocardoso commented May 2, 2020

Proposal

Remove Payment dependency in favor of credit-card-type + luhn.

Closes #75

Details

After removing Payment, we needed to store the list of supported card types inside the library, I chose to do that using the internal component state.

Some small tweaks were needed since the libs identified the card types differently:

  • amex -> american-express
  • dinersclub -> diners-club
  • visaelectron -> visa-electron (this one was just for consistency 😅 )

No changes were made to the component API.

Caveats

Currently, Dankort is not supported by credit-card-type which led to some inline changes here to make it work.

If/when this PR is approved: braintree/credit-card-type#106 we'll be able to remove these changes on our side and rely on it

src/cardTypes.js Outdated
@@ -0,0 +1,35 @@
export const dankort = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I created these 3 definitions here while they aren't supported by credit-card-type

Copy link
Contributor

Choose a reason for hiding this comment

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

As we discussed, use this file (with a different name) to abstract the usage of credit-card-type and luhn so it will be easy to replace it in the future if we need to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved this file to /utils.

Initially, I had the helpers and these objects in the same file, but it was a bit confusing for me, so I decided to split into 2 files just to be easier to understand what's being done in each of them.

Let me know what do you think.

src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated
Comment on lines 145 to 109
if (number && !preview) {
const validatedIssuer = creditCardType(sanitizeNumber(number)).length ? creditCardType(sanitizeNumber(number))[0].type : 'unknown';

if (validCardTypes.includes(validatedIssuer)) {
updatedIssuer = validatedIssuer;
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to tweak the logic here since creditCardType will always return an array of card type objects.

Also added the option to bypass this logic if the preview prop is passed, in this case, we use the issuer prop

Copy link
Contributor

Choose a reason for hiding this comment

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

Another reason for the abstraction.
Get the issuer only if there's a single item in the array.

and we already have an issuer getter that handles that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got your point on the issuer getter, makes total sense, reverted the logic here.

But I noticed one thing with this part:

Get the issuer only if there's a single item in the array.

In this case, we get a lot more unknowns, for example:

number type
4 unknown
40 unknown
401 unknown
4011 unknown
40117 unknown
401176 visa
401178 elo

Before we got visa from 4 until 40117, and only switched to elo if the bin matched. Do you think this could be an issue?

src/utils.js Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
@gilbarbara gilbarbara changed the title remove Payment dependency Replace the payment dependency with credit-card-type + luhn May 2, 2020
@gilbarbara gilbarbara changed the title Replace the payment dependency with credit-card-type + luhn Replace the payment dependency with credit-card-type and luhn May 2, 2020
Copy link
Contributor

@gilbarbara gilbarbara left a comment

Choose a reason for hiding this comment

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

A few changes

README.md Outdated Show resolved Hide resolved
package.json Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
src/cardTypes.js Outdated
@@ -0,0 +1,35 @@
export const dankort = {
Copy link
Contributor

Choose a reason for hiding this comment

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

As we discussed, use this file (with a different name) to abstract the usage of credit-card-type and luhn so it will be easy to replace it in the future if we need to.

src/index.js Outdated
Comment on lines 145 to 109
if (number && !preview) {
const validatedIssuer = creditCardType(sanitizeNumber(number)).length ? creditCardType(sanitizeNumber(number))[0].type : 'unknown';

if (validCardTypes.includes(validatedIssuer)) {
updatedIssuer = validatedIssuer;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Another reason for the abstraction.
Get the issuer only if there's a single item in the array.

and we already have an issuer getter that handles that.

src/index.js Outdated Show resolved Hide resolved
@@ -26,9 +26,9 @@ $rccs-background-transition: all 0.5s ease-out !default;
$rccs-animate-background: true;

/** ISSUERS **/
$rccs-amex-background: linear-gradient(25deg, #308c67, #a3f2cf) !default;
Copy link
Contributor

Choose a reason for hiding this comment

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

we might need to revert these

src/utils.js Outdated Show resolved Hide resolved
@@ -78,11 +78,11 @@ describe('ReactCreditCards', () => {
focused: 'number',
});

expect(wrapper.find('.rccs__card').hasClass('rccs__card--amex')).toBe(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

The same thing here, we might need to revert these changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if I got your point here, but given the newly created cardTypesMap:

export const cardTypesMap = {
  amex: ['amex', 'americanexpress', 'american-express'],
  dinersclub: ['diners', 'dinersclub', 'diners-club'],
  visaelectron: ['visaelectron', 'visa-electron'],
};

Should we use the object keys as our identifiers for the CSS class?

- use credit-card-type + luhn to achieve the same functionality previously provided by the payment lib
- some types had their names updated to match the new lib pattern (american-express and diners-club)
- add new card type information
- update local development section
- use class getter and setter to store the validCardTypes value instead of using the component state
- refactor config function name to be more explicit about its implementation
- fix issue reported by DeepScan
@jonyw4
Copy link

jonyw4 commented Aug 19, 2020

any progress in this pr?

@AnthonyCrowcroft
Copy link

@cassiocardoso and @gilbarbara was there anything left outstanding on this it'd be super cool to see this unit finished :)

@cassioseffrin
Copy link

This PR seems to be very useful. Any estimative when it will be merged it with master?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

migrate to a different validation library
5 participants