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

Update CC brand detection regex #1477

Merged
merged 4 commits into from
Sep 29, 2016

Conversation

jhawthorn
Copy link
Contributor

@jhawthorn jhawthorn commented Sep 28, 2016

The existing hash was incomplete, missing some brands. It was also missing MasterCard's new (October 2016) BIN range (222100-272099).

This hash was originally added in spree/spree#3918 and has not been changed since. I don't know the original source, but I would rather trust the Hash taken from ActiveMerchant.

One notable difference is that this will treat maestro and MasterCard's as separate brands.

NB: For most stores, this code will not be run, and instead jqyery.payment on the frontend will be responsible for providing the credit card brand (already updated in #608 and understands MasterCard's new BIN. Thanks @mtomov).

The existing hash was incomplete, missing some brands. It
was also missing MasterCard's new (October 2016) BIN range
(222100-272099).

This hash was originally added in
spree/spree#3918
and has not been changed since. I don't know the original source, but I
would rather trust the Hash taken from ActiveMerchant.

One notable difference is that this will treat maestro and mastercard as
separate brands.

NB: For most stores, this code will not be run, and instead
jquery.payment on the frontend will be responsible for providing the
credit card brand.
The removal of whitespace is already done (more completely) in number=.
The existing code returned from within a find, and if the find ever
succeeded, would return '' because nil (the return of the find) .to_s is
''.

This works, but is difficult to read. I prefer being explicit about the
return value being an empty string if there isn't an early return.
@@ -97,8 +105,10 @@ def set_last_digits
# @return [String] the credit card type if it can be determined from the
# number, otherwise the empty string
def try_type_from_number
numbers = number.delete(' ') if number
CARD_TYPES.find{ |type, pattern| return type.to_s if numbers =~ pattern }.to_s
CARD_TYPES.each do |type, pattern|
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't detect have the same effect?

Also, I think you don't have to cast strings on type Your hash keys are all strings already.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, nasty. You have the early return because you return empty string below if a number matches.

I would prefer .select {...} || '', but it's on you to decide

Copy link
Member

Choose a reason for hiding this comment

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

.detect obviously 🙄

Copy link
Contributor Author

@jhawthorn jhawthorn Sep 28, 2016

Choose a reason for hiding this comment

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

.detect would return us (for example) ['visa', /^4\d{12}(\d{3})?(\d{3})?$/] so the code would need to be something like

card_type_pair = CARD_TYPES.detect { |type, pattern| numbers =~ pattern }
if card_type_pair
  card_type_pair[0]
else
  ''
end

I think the return is more clear.

I will remove the .to_s

discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/,
jcb: /^(?:2131|1800|35\d{3})\d{11}$/
}
'visa' => /^4\d{12}(\d{3})?(\d{3})?$/,
Copy link
Member

Choose a reason for hiding this comment

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

Why not take the Regex from ActiveMerchant instead of copy it over. This way we have to always have to have an eye on this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pro reducing and removing our reliance on active_merchant in core, so I'm 👍 on copying it in (it also isn't something that changes frequently).

Copy link
Contributor

Choose a reason for hiding this comment

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

reducing and removing our reliance on active_merchant

Ditto

discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/,
jcb: /^(?:2131|1800|35\d{3})\d{11}$/
}
'visa' => /^4\d{12}(\d{3})?(\d{3})?$/,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pro reducing and removing our reliance on active_merchant in core, so I'm 👍 on copying it in (it also isn't something that changes frequently).

Our CARD_TYPES hash now uses strings as keys so we no longer need to
stringify them.
@jhawthorn jhawthorn merged commit 2f2f1ef into solidusio:master Sep 29, 2016
@jhawthorn jhawthorn deleted the credit_card_regex branch September 29, 2016 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants