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
Merged
Show file tree
Hide file tree
Changes from 3 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
28 changes: 19 additions & 9 deletions core/app/models/spree/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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})?$/,
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

'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)
Expand Down Expand Up @@ -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

return type.to_s if number =~ pattern
end
''
end

# @return [Boolean] true when a verification value is present
Expand Down
4 changes: 4 additions & 0 deletions core/spec/models/spree/credit_card_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ def self.payment_states
credit_card.cc_type = ''
expect(credit_card.cc_type).to eq('master')

credit_card.number = '2221000000000000'
credit_card.cc_type = ''
expect(credit_card.cc_type).to eq('master')

credit_card.number = '378282246310005'
credit_card.cc_type = ''
expect(credit_card.cc_type).to eq('american_express')
Expand Down