-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,14 +29,22 @@ class CreditCard < Spree::Base | |
# needed for some of the ActiveMerchant gateways (eg. SagePay) | ||
alias_attribute :brand, :cc_type | ||
|
||
# Taken from ActiveMerchant | ||
# https://github.com/activemerchant/active_merchant/blob/2f2acd4696e8de76057b5ed670b9aa022abc1187/lib/active_merchant/billing/credit_card_methods.rb#L5 | ||
CARD_TYPES = { | ||
visa: /^4[0-9]{12}(?:[0-9]{3})?$/, | ||
master: /(^5[1-5][0-9]{14}$)|(^6759[0-9]{2}([0-9]{10})$)|(^6759[0-9]{2}([0-9]{12})$)|(^6759[0-9]{2}([0-9]{13})$)/, | ||
diners_club: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/, | ||
american_express: /^3[47][0-9]{13}$/, | ||
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})?$/, | ||
'master' => /^(5[1-5]\d{4}|677189|222[1-9]\d{2}|22[3-9]\d{3}|2[3-6]\d{4}|27[01]\d{3}|2720\d{2})\d{10}$/, | ||
'discover' => /^(6011|65\d{2}|64[4-9]\d)\d{12}|(62\d{14})$/, | ||
'american_express' => /^3[47]\d{13}$/, | ||
'diners_club' => /^3(0[0-5]|[68]\d)\d{11}$/, | ||
'jcb' => /^35(28|29|[3-8]\d)\d{12}$/, | ||
'switch' => /^6759\d{12}(\d{2,3})?$/, | ||
'solo' => /^6767\d{12}(\d{2,3})?$/, | ||
'dankort' => /^5019\d{12}$/, | ||
'maestro' => /^(5[06-8]|6\d)\d{10,17}$/, | ||
'forbrugsforeningen' => /^600722\d{10}$/, | ||
'laser' => /^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/ | ||
}.freeze | ||
|
||
def address_attributes=(attributes) | ||
self.address = Address.immutable_merge(address, attributes) | ||
|
@@ -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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't Also, I think you don't have to cast strings on There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think the return is more clear. I will remove the |
||
return type.to_s if number =~ pattern | ||
end | ||
'' | ||
end | ||
|
||
# @return [Boolean] true when a verification value is present | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto