credit_card_validations 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8cc121a408a642f75dfcfc595f63c58299895dfc
4
- data.tar.gz: c30600f8e3925bce9cd79c1e30d0cb954696882f
3
+ metadata.gz: 9dc4aaa6683f8c4b1b0bcfea5ca31076fa3109f2
4
+ data.tar.gz: 33aba134c4e4b5e445f51796740ffe8477d6fdac
5
5
  SHA512:
6
- metadata.gz: 9b2c71d6eeef539a4adc6eb3aa3d971a5f0db812f2bf396de7e1d82be082243c657f43b5dcf4221b2d1e5e856dca2414160c567bedb5b651e8f63e15ff1d1f0e
7
- data.tar.gz: d9979cffa4dec73c7f01b26c80ce654ede26fc4c4aa1458e932282867913d78d5685dbc74594096e8d7e67beca97849f4c068e99773bb2cb076388792d7f5c62
6
+ metadata.gz: 2bf428e04e75942465f12bec35b449943bd3825c20ecc709364a937f861041284c16909430afb74fd0e9cf76492c7b6f167580d6a8179f6273d60e8def127a65
7
+ data.tar.gz: f164f228a43fd51410c03040b11f0b0cdb3c09dd1d9a3a2aa7e193f166eec3ad6d43dd9616b9ffef557d237d07089132a5a75c3ed6762eb1c40546fcdb98b030
data/README.md CHANGED
@@ -34,6 +34,7 @@ The following issuing institutes are accepted:
34
34
  Diners Club | :diners | http://en.wikipedia.org/wiki/Diners_Club_International
35
35
  Dinner Club US | :diners_us | http://en.wikipedia.org/wiki/Diners_Club_International#MasterCard_alliance
36
36
  Discover | :discover | http://en.wikipedia.org/wiki/Discover_Card
37
+ Hipercard | :hipercard | http://pt.wikipedia.org/wiki/Hipercard
37
38
  JCB | :jcb | http://en.wikipedia.org/wiki/Japan_Credit_Bureau
38
39
  Laser | :laser | http://en.wikipedia.org/wiki/Laser_%28debit_card%29
39
40
  Maestro | :maestro | http://en.wikipedia.org/wiki/Maestro_%28debit_card%29
@@ -32,7 +32,8 @@ module CreditCardValidations
32
32
  ],
33
33
 
34
34
  jcb: [
35
- {length: [16], prefixes: ['3528', '3529', '353', '354', '355', '356', '357', '358', '1800', '2131']}
35
+ {length: [15,16], prefixes: ['3528', '3529', '353', '354', '355', '356', '357', '358']},
36
+ {length: [15], prefixes: ['1800', '2131']}
36
37
  ],
37
38
 
38
39
 
@@ -52,11 +53,14 @@ module CreditCardValidations
52
53
  maestro: [
53
54
  {length: [12, 13, 14, 15, 16, 17, 18, 19], prefixes: ['5010', '5011', '5012', '5013', '5014', '5015', '5016', '5017', '5018',
54
55
  '502', '503', '504', '505', '506', '507', '508',
55
- '6012', '6013', '6014', '6015', '6016', '6017', '6018', '6019',
56
+ '56','57', '58', '59',
57
+ '6010', '6012', '6013', '6014', '6015', '6016', '6017', '6018', '6019',
56
58
  '602', '603', '604', '605', '6060',
57
59
  '621', '627', '629',
58
- '677', '675', '674', '673', '672', '671', '670',
59
- '6760', '6761', '6762', '6763', '6764', '6765', '6766', '6768', '6769']}
60
+ '670','671', '672','673', '674', '675', '677',
61
+ '6760', '6761', '6762', '6763', '6764', '6765', '6766', '6768', '6769',
62
+ '679'
63
+ ]}
60
64
  ],
61
65
 
62
66
  # Luhn validation are skipped for union pay cards because they have unknown generation algoritm
@@ -70,8 +74,11 @@ module CreditCardValidations
70
74
 
71
75
  rupay: [
72
76
  {length: [16], prefixes: ['6061', '6062', '6063', '6064', '6065', '6066', '6067', '6068', '6069', '607', '608'], skip_luhn: true}
77
+ ],
78
+
79
+ hipercard: [
80
+ length: [19], prefixes: ['384']
73
81
  ]
74
-
75
82
  }
76
83
  end
77
84
  end
@@ -53,12 +53,11 @@ module CreditCardValidations
53
53
  length = Array.wrap(length)
54
54
  rules[brand] = [] if rules[brand].blank?
55
55
  rules[brand] << {length: length, regexp: compile_regexp(prefixes), prefixes: prefixes, skip_luhn: skip_luhn}
56
- #create methods like visa? mastercard? etc
57
- class_eval <<-BOOLEAN_RULE, __FILE__, __LINE__
58
- def #{brand}?
59
- valid?(:#{brand})
60
- end
61
- BOOLEAN_RULE
56
+
57
+ define_method "#{brand}?".to_sym do
58
+ valid?(brand)
59
+ end unless method_defined? "#{brand}?".to_sym
60
+
62
61
  rules[brand]
63
62
  end
64
63
  end
@@ -1,3 +1,3 @@
1
1
  module CreditCardValidations
2
- VERSION = "1.5.0"
2
+ VERSION = "1.5.1"
3
3
  end
@@ -1,80 +1,73 @@
1
1
  require_relative 'test_helper'
2
+ require 'yaml'
2
3
 
3
4
  class CreditCardValidationsTest < MiniTest::Test
4
5
 
5
- class CreditCardModel
6
- attr_accessor :number
7
- include ActiveModel::Validations
8
- validates :number, credit_card_number: {brands: [:amex, :maestro]}
6
+
7
+ def initialize name
8
+ super name
9
+ @test_valid_numbers = YAML.load_file File.join(File.dirname(__FILE__), 'fixtures/valid_cards.yml')
10
+ @test_invalid_numbers = YAML.load_file File.join(File.dirname(__FILE__), 'fixtures/invalid_cards.yml')
9
11
  end
10
12
 
11
- class CreditCardModelAny
12
- attr_accessor :number
13
- include ActiveModel::Validations
14
- validates :number, presence: true, credit_card_number: true
13
+
14
+ def test_card_luhn
15
+ @test_valid_numbers.each do |brand, card_numbers|
16
+ if has_luhn_check_rule?(brand)
17
+ card_numbers.each do |number|
18
+ assert luhn_valid?(detector(number).number), "#{number} failed luhn"
19
+ end
20
+ end
21
+ end
22
+
15
23
  end
16
24
 
17
- def initialize name
18
- super name
19
- @test_numbers = {
20
- visa: ['4012 8888 8888 1881', '4111111111111111'],
21
- mastercard: ['5274 5763 9425 9961', '5555-5555-5555-4444'],
22
- diners: ['3020 4169 3226 43', '30569309025904'],
23
- amex: ['3782 8224 6310 005', '371449635398431'],
24
- discover: ['6011 1111 1111 1117', '6011000990139424'],
25
- maestro: ['6759 6498 2643 8453'],
26
- jcb: ['3575 7591 5225 4876', '3566002020360505'],
27
- solo: ['6767 6222 2222 2222 222'],
28
- unionpay: ['6264-1852-1292-2132-067', '6288997715452584', '6269 9920 5813 4322'],
29
- dankrot: ['5019717010103742'],
30
- switch: ['6331101999990016'],
31
- rupay: ['6076-6000-0619-9992']
32
- }
33
- end
34
-
35
- def test_card_brand_detection
36
- @test_numbers.each do |key, value|
37
- value.each do |card_number|
38
- assert detector(card_number).send("#{key}?")
39
- assert_equal key, detector(card_number).brand
25
+ def test_card_if_credit_card_valid
26
+ @test_valid_numbers.each do |brand, card_numbers|
27
+ card_numbers.each do |card_number|
28
+ assert detector(card_number).send("#{brand}?"), "#{card_number} is #{brand}"
29
+ assert_equal brand, detector(card_number).brand, "#{card_number} detects as #{brand}"
40
30
  end
41
31
  end
42
32
  end
43
33
 
44
- def test_card_brand_is_nil_if_credit_card_invalid
45
- assert_nil detector('1111111111111111').brand
34
+ def test_card_if_credit_card_invalid
35
+ @test_invalid_numbers.each do |card_number|
36
+ assert !detector(card_number).valid?
37
+ assert_nil detector(card_number).brand
38
+ @test_valid_numbers.keys.each do |brand|
39
+ assert !detector(card_number).send("#{brand}?")
40
+ end
41
+ end
46
42
  end
47
43
 
48
44
  def test_card_brand_detection_with_restriction
49
- @test_numbers.slice(:visa, :mastercard).each do |key, value|
45
+ @test_valid_numbers.slice(:visa, :mastercard).each do |key, value|
50
46
  assert_equal key, detector(value.first).brand(:visa, :mastercard)
51
47
  end
52
48
 
53
- @test_numbers.except(:visa, :mastercard).each do |key, value|
49
+ @test_valid_numbers.except(:visa, :mastercard).each do |key, value|
54
50
  assert_nil detector(value.first).brand(:visa, :mastercard)
55
51
  end
56
52
  end
57
53
 
58
54
  def test_card_valid_method
59
- @test_numbers.each do |key, value|
55
+ @test_valid_numbers.each do |key, value|
60
56
  value.each do |card_number|
61
57
  assert detector(card_number).valid?(key)
62
58
  assert detector(card_number).valid?
63
59
  end
64
60
  end
65
- assert !detector('1111111111111111').valid?
66
61
  end
67
62
 
68
-
69
63
  def test_card_particular_brand_valid
70
- assert !detector(@test_numbers[:visa].first).valid?(:mastercard)
71
- assert !detector(@test_numbers[:mastercard].first).valid?(:visa)
64
+ assert !detector(@test_valid_numbers[:visa].first).valid?(:mastercard)
65
+ assert !detector(@test_valid_numbers[:mastercard].first).valid?(:visa)
72
66
  end
73
67
 
74
-
75
68
  def test_card_particular_brands_valid
76
- assert detector(@test_numbers[:visa].first).valid?(:mastercard, :visa)
77
- assert !detector(@test_numbers[:visa].first).valid?(:mastercard, :amex)
69
+ assert detector(@test_valid_numbers[:visa].first).valid?(:mastercard, :visa)
70
+ assert !detector(@test_valid_numbers[:visa].first).valid?(:mastercard, :amex)
78
71
  end
79
72
 
80
73
  #add rules which were not present before
@@ -89,59 +82,70 @@ class CreditCardValidationsTest < MiniTest::Test
89
82
  assert !detector(voyager_test_card_number).mastercard?
90
83
  end
91
84
 
85
+ def test_active_model_any_validator
86
+ cc = AnyCreditCard.new
87
+ cc.number='1'
88
+ assert !cc.valid?
89
+ cc.number = @test_valid_numbers[:mastercard].first
90
+ assert cc.valid?
91
+ end
92
+
92
93
  def test_active_model_validator
93
- cc = CreditCardModel.new
94
- cc.number = @test_numbers[:maestro].first
94
+ cc = CreditCard.new
95
+ cc.number = @test_valid_numbers[:maestro].first
95
96
  assert cc.valid?
96
97
 
97
- cc = CreditCardModel.new
98
- cc.number = @test_numbers[:mastercard].first
98
+ cc = CreditCard.new
99
+ cc.number = @test_valid_numbers[:mastercard].first
99
100
  assert !cc.valid?
100
101
  assert cc.errors[:number].include?(cc.errors.generate_message(:number, :invalid))
101
102
 
102
-
103
- cc = CreditCardModelAny.new
104
- cc.number='1'
105
- assert !cc.valid?
106
- cc.number = @test_numbers[:mastercard].first
107
- assert cc.valid?
108
-
109
103
  end
110
104
 
111
105
  def test_string_extension
106
+ assert !@test_valid_numbers[:mastercard].first.respond_to?(:credit_card_brand)
107
+ assert !@test_valid_numbers[:mastercard].first.respond_to?(:valid_credit_card_brand?)
112
108
  require 'credit_card_validations/string'
113
- assert_equal @test_numbers[:mastercard].first.credit_card_brand, :mastercard
114
- assert @test_numbers[:mastercard].first.valid_credit_card_brand?(:mastercard)
115
- assert !@test_numbers[:mastercard].first.valid_credit_card_brand?(:visa, :amex)
109
+ assert_equal @test_valid_numbers[:mastercard].first.credit_card_brand, :mastercard
110
+ assert @test_valid_numbers[:mastercard].first.valid_credit_card_brand?(:mastercard)
111
+ assert !@test_valid_numbers[:mastercard].first.valid_credit_card_brand?(:visa, :amex)
116
112
  end
117
113
 
114
+ def test_call_luhn
115
+ d = detector(@test_valid_numbers[:unionpay].first)
116
+ d.expects(:valid_luhn?).once
117
+ assert !d.valid?(:visa)
118
118
 
119
- def test_skip_luhn
120
- d = detector(@test_numbers[:unionpay].first)
119
+ d.expects(:valid_luhn?).once
120
+ assert d.valid?(:visa, :unionpay)
121
+ end
121
122
 
123
+ def test_skip_luhn
124
+ d = detector(@test_valid_numbers[:unionpay].first)
122
125
  d.expects(:valid_luhn?).never
123
126
  assert d.valid?(:unionpay)
124
-
125
- d.expects(:valid_luhn?).once
126
- assert !d.valid?(:visa)
127
-
128
127
  d.expects(:valid_luhn?).never
129
128
  assert d.valid?(:unionpay, :visa)
130
-
131
- d.expects(:valid_luhn?).once
132
- assert d.valid?(:visa, :unionpay)
133
129
  end
134
130
 
135
131
  def test_mmi
136
- d = detector(@test_numbers[:visa])
137
- assert_equal d.issuer_category, CreditCardValidations::Mmi::ISSUER_CATEGORIES[@test_numbers[:visa][0]]
132
+ d = detector(@test_valid_numbers[:visa].first)
133
+ assert_equal d.issuer_category, CreditCardValidations::Mmi::ISSUER_CATEGORIES[d.number[0]]
138
134
  end
139
135
 
140
136
  protected
141
137
 
138
+ def luhn_valid?(number)
139
+ CreditCardValidations::Luhn.valid?(number)
140
+ end
141
+
142
142
  def detector(number)
143
143
  CreditCardValidations::Detector.new(number)
144
144
  end
145
145
 
146
146
 
147
+ def has_luhn_check_rule?(brand)
148
+ CreditCardValidations::Detector.rules[brand].any? { |rule| !rule[:skip_luhn] }
149
+ end
150
+
147
151
  end
@@ -0,0 +1,6 @@
1
+ ---
2
+ - '1111111111111111'
3
+ - '00000000000000'
4
+ - fakenumber
5
+ - c4111111111111111
6
+ - 4111111111111111i
@@ -0,0 +1,79 @@
1
+ ---
2
+ :visa:
3
+ - 4012 8888 8888 1881
4
+ - 4111 1111 1111 1111
5
+ - 4222 2222 2222 2
6
+ - 4917 6100 0000 0000
7
+ :mastercard:
8
+ - 5274 5763 9425 9961
9
+ - 5555 5555 5555 4444
10
+ - 5105 1051 0510 5100
11
+ :diners:
12
+ - 3020 4169 3226 43
13
+ - 3021 8047 1965 57
14
+ - 3022 1511 5632 52
15
+ - 3600 0000 0000 08
16
+ - 3614 8900 6479 13
17
+ - 3670 0102 0000 00
18
+ - 3852 0000 0232 37
19
+ - 3056 9309 0259 04
20
+ - 3020 4169 3226 43
21
+ :amex:
22
+ - 3714 4963 5398 431
23
+ - 3787 3449 3671 000
24
+ - 3400 0000 0000 009
25
+ - 3411 1111 1111 111
26
+ - 3434 3434 3434 343
27
+ - 3468 2763 0435 344
28
+ - 3700 0000 0000 002
29
+ - 3700 0020 0000 000
30
+ - 3704 0726 9909 809
31
+ - 3705 5601 9309 221
32
+ - 3714 4963 5398 431
33
+ - 3742 0000 0000 004
34
+ - 3764 6228 0921 451
35
+ - 3777 5274 9896 404
36
+ - 3782 8224 6310 005
37
+ :discover:
38
+ - 6011 1111 1111 1117
39
+ - 6011 0009 9013 9424
40
+ - 6011 0000 0000 0004
41
+ - 6011 0004 0000 0000
42
+ - 6011 1532 1637 1980
43
+ - 6011 6011 6011 6611
44
+ - 6011 6874 8256 4166
45
+ - 6011 8148 3690 5651
46
+ :maestro:
47
+ - 6759 6498 2643 8453
48
+ - 5641 8200 0000 0005
49
+ - 5033 9619 8909 17
50
+ - 5868 2416 0825 5333 38
51
+ - 6799 9901 0000 0000 019
52
+ :jcb:
53
+ - 3575 7591 5225 4876
54
+ - 3566 0020 2036 0505
55
+ - 1800 0163 8277 392
56
+ - 3569 9900 0000 0009
57
+ - 3530 1113 3330 0000
58
+ :solo:
59
+ - 6767 6222 2222 2222 222
60
+ - 6334 5805 0000 0000
61
+ - 6334 9000 0000 0005
62
+ - 6334 7306 0000 0000 00
63
+ - 6767 6767 6767 6767 671
64
+ :unionpay:
65
+ - 6264 1852 1292 2132 067
66
+ - 6288 9977 1545 2584
67
+ - 6269 9920 5813 4322
68
+ :dankrot:
69
+ - 5019 7170 1010 3742
70
+ :switch:
71
+ - 6331 1019 9999 0016
72
+ :laser:
73
+ - 6304 9506 0000 0000 00
74
+ - 6304 9000 1774 0292 441
75
+ :rupay:
76
+ - 6076 6000 0619 9992
77
+ - 6070 5500 5000 0047
78
+ :hipercard:
79
+ - 3841 0058 9908 8180 330
@@ -0,0 +1,6 @@
1
+ class AnyCreditCard
2
+ attr_accessor :number
3
+ include ActiveModel::Validations
4
+ validates :number, presence: true, credit_card_number: true
5
+ end
6
+
@@ -0,0 +1,5 @@
1
+ class CreditCard
2
+ attr_accessor :number
3
+ include ActiveModel::Validations
4
+ validates :number, credit_card_number: {brands: [:amex, :maestro]}
5
+ end
data/test/test_helper.rb CHANGED
@@ -11,5 +11,9 @@ $:.unshift(unit_tests)
11
11
 
12
12
  I18n.config.enforce_available_locales = true
13
13
 
14
-
15
14
  require 'credit_card_validations'
15
+ require 'models/credit_card'
16
+ require 'models/any_credit_card'
17
+
18
+
19
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: credit_card_validations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-06 00:00:00.000000000 Z
11
+ date: 2014-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -100,6 +100,10 @@ files:
100
100
  - lib/credit_card_validations/string.rb
101
101
  - lib/credit_card_validations/version.rb
102
102
  - test/credit_card_validations_test.rb
103
+ - test/fixtures/invalid_cards.yml
104
+ - test/fixtures/valid_cards.yml
105
+ - test/models/any_credit_card.rb
106
+ - test/models/credit_card.rb
103
107
  - test/test_helper.rb
104
108
  homepage: https://github.com/Fivell/credit_card_validations
105
109
  licenses:
@@ -127,4 +131,8 @@ specification_version: 4
127
131
  summary: gem for credit card numbers validation, card brands detections
128
132
  test_files:
129
133
  - test/credit_card_validations_test.rb
134
+ - test/fixtures/invalid_cards.yml
135
+ - test/fixtures/valid_cards.yml
136
+ - test/models/any_credit_card.rb
137
+ - test/models/credit_card.rb
130
138
  - test/test_helper.rb