Skip to content

Commit 92c6319

Browse files
committed
Add support for 8 digit IINs and 2 digit last_digits
Previously issuer_id_number was expected to be 6 digits and last_4_digits to be 4 digits. This changes the validation to additionally allow for 8 digit issuer_id_numbers and 2 digit last_4_digits. Additionally last_4_digits has been deprecated in favor of the more appropriately named last_digits.
1 parent 8d5a4b9 commit 92c6319

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ assessment = Minfraud::Assessments.new(
123123
},
124124
credit_card: {
125125
issuer_id_number: '411111',
126-
last_4_digits: '7643',
126+
last_digits: '7643',
127127
bank_name: 'Bank of No Hope',
128128
bank_phone_country_code: '1',
129129
bank_phone_number: '123-456-1234',

lib/minfraud/components/credit_card.rb

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,27 @@ module Components
88
class CreditCard < Base
99
include Minfraud::Validates
1010

11-
# The issuer ID number for the credit card. This is the first 6 digits of
12-
# the credit card number. It identifies the issuing bank.
11+
# The issuer ID number for the credit card. This is the first 6 or 8
12+
# digits of the credit card number. It identifies the issuing bank.
1313
#
1414
# @return [String, nil]
1515
attr_accessor :issuer_id_number
1616

17-
# The last four digits of the credit card number.
17+
# The last digits of the credit card number. This is the last 4
18+
# digits if the issuer ID number is 6 digits, or the last 2 digits
19+
# if the issuer ID number is 8 digits.
1820
#
1921
# @return [String, nil]
20-
attr_accessor :last_4_digits
22+
attr_accessor :last_digits
23+
24+
# The last digits of the credit card number. This is the last 4
25+
# digits if the issuer ID number is 6 digits, or the last 2 digits
26+
# if the issuer ID number is 8 digits.
27+
#
28+
# @deprecated Use {::last_digits} instead.
29+
#
30+
# @return [String, nil]
31+
alias last_4_digits last_digits
2132

2233
# The name of the issuing bank as provided by the end user.
2334
#
@@ -77,7 +88,7 @@ class CreditCard < Base
7788
def initialize(params = {})
7889
@bank_phone_country_code = params[:bank_phone_country_code]
7990
@issuer_id_number = params[:issuer_id_number]
80-
@last_4_digits = params[:last_4_digits]
91+
@last_digits = params[:last_digits] || params[:last_4_digits]
8192
@bank_name = params[:bank_name]
8293
@bank_phone_number = params[:bank_phone_number]
8394
@avs_result = params[:avs_result]
@@ -94,8 +105,8 @@ def validate
94105
return if !Minfraud.enable_validation
95106

96107
validate_telephone_country_code('bank_phone_country_code', @bank_phone_country_code)
97-
validate_regex('issuer_id_number', /\A[0-9]{6}\z/, @issuer_id_number)
98-
validate_regex('last_4_digits', /\A[0-9]{4}\z/, @last_4_digits)
108+
validate_regex('issuer_id_number', /\A(?:[0-9]{6}|[0-9]{8})\z/, @issuer_id_number)
109+
validate_regex('last_digits', /\A(?:[0-9]{2}|[0-9]{4})\z/, @last_digits)
99110
validate_string('bank_name', 255, @bank_name)
100111
validate_string('bank_phone_number', 255, @bank_phone_number)
101112
validate_string('avs_result', 1, @avs_result)

spec/components/credit_card_spec.rb

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@
1616
end.to raise_exception(Minfraud::InvalidInputError)
1717
end
1818

19+
it 'raises an exception for an invalid last_digits' do
20+
expect do
21+
Minfraud::Components::CreditCard.new(
22+
last_digits: '6',
23+
)
24+
end.to raise_exception(Minfraud::InvalidInputError)
25+
end
26+
1927
it 'raises an exception for an invalid last_4_digits' do
2028
expect do
2129
Minfraud::Components::CreditCard.new(
22-
last_4_digits: '6',
30+
last_digits: '6',
2331
)
2432
end.to raise_exception(Minfraud::InvalidInputError)
2533
end
@@ -40,19 +48,41 @@
4048
end.to raise_exception(Minfraud::InvalidInputError)
4149
end
4250

43-
it 'does not raise an exception for valid values' do
44-
Minfraud::Components::CreditCard.new(
51+
it 'does not raise an exception for valid values (deprecated last_4_digits)' do
52+
@cc = Minfraud::Components::CreditCard.new(
4553
issuer_id_number: '123456',
4654
last_4_digits: '1234',
4755
token: 'abcd',
4856
was_3d_secure_successful: true,
4957
)
58+
expect(@cc.last_digits).to be(@cc.last_4_digits)
59+
expect(@cc.last_digits).to be('1234')
60+
end
61+
62+
it 'does not raise an exception for valid values (eight digit issuer_id_number)' do
63+
Minfraud::Components::CreditCard.new(
64+
issuer_id_number: '12345678',
65+
last_4_digits: '1234',
66+
token: 'abcd',
67+
was_3d_secure_successful: true,
68+
)
69+
end
70+
71+
it 'does not raise an exception for valid values (two digit last_digits)' do
72+
@cc = Minfraud::Components::CreditCard.new(
73+
issuer_id_number: '12345678',
74+
last_digits: '34',
75+
token: 'abcd',
76+
was_3d_secure_successful: true,
77+
)
78+
expect(@cc.last_digits).to be(@cc.last_4_digits)
79+
expect(@cc.last_digits).to be('34')
5080
end
5181

5282
it 'does not raise an exception for valid values (token is all digits)' do
5383
Minfraud::Components::CreditCard.new(
5484
issuer_id_number: '123456',
55-
last_4_digits: '1234',
85+
last_digits: '1234',
5686
token: '1' * 20,
5787
was_3d_secure_successful: true,
5888
)

0 commit comments

Comments
 (0)