validate_credit_card_fields 0.9.5 → 0.9.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +8 -1
- data/lib/validate_credit_card_fields/version.rb +1 -1
- data/lib/validate_credit_card_fields.rb +22 -7
- data/spec/dummy/named_user.rb +21 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/validate_credit_card_fields_spec.rb +70 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: efa05300aa911f202e15eaa78bfde95eed37e77b
|
4
|
+
data.tar.gz: 205943230670a96425e1469b13f2ad6a2ac1238b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a7964ac49e2d875da01604cb4e31e7e3bebae517f0937b3446315d3521394fe1a2746bc627aa3082e043ab1fa6a0366bda9ecd523d186c4891ecab0c7ca79346
|
7
|
+
data.tar.gz: 0505b5113a2737041939b29748408f428b370150cee33e19872d2489c41749e2bb2aebea989f2ceb2743496ca4a74cfa1fb3825edf043d6e278f2844647430f3
|
data/README.md
CHANGED
@@ -32,11 +32,18 @@ In place of `:your_something_field` place keys representing desired value in you
|
|
32
32
|
|
33
33
|
`providers` are used to specify provider limitations. Supply it with a list of **supported** providers (those you want to be valid). Leaving it blank will allow any of the accepted providers below:
|
34
34
|
|
35
|
-
:visa, :master_card, :maestro, :diners_club, :amex, :discover, :jcb
|
35
|
+
:visa, :master_card, :maestro, :diners_club, :amex, :discover, :jcb, :solo, :china_union, :dankort
|
36
36
|
|
37
37
|
When a field name isn't presented, validator will use default values:
|
38
38
|
|
39
39
|
:cc_number, :cc_cvv, :cc_month, :cc_year, :cc_owner
|
40
|
+
|
41
|
+
Now possible to validate credit card owner using first and last names, for example:
|
42
|
+
|
43
|
+
validate_credit_card_fields first_name: :your_first_name_field,
|
44
|
+
last_name: :your_last_name_field
|
45
|
+
|
46
|
+
If both `:first_name` and `:last_name` keys are provided, those two fields will be validated, otherwise it'll fall back to `:owner`.
|
40
47
|
|
41
48
|
|
42
49
|
## Contributing
|
@@ -13,15 +13,19 @@ module ActiveModel
|
|
13
13
|
PROVIDERS = {
|
14
14
|
visa: /^4[0-9]{12}(?:[0-9]{3})?$/,
|
15
15
|
master_card: /^5[1-5][0-9]{14}$/,
|
16
|
-
maestro:
|
16
|
+
maestro: /^(?:5[0678]\d\d|6304|6390|67\d\d)\d{8,15}$/,
|
17
17
|
diners_club: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/,
|
18
18
|
amex: /^3[47][0-9]{13}$/,
|
19
19
|
discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/,
|
20
|
-
jcb: /^(?:2131|1800|35\d{3})\d{11}
|
20
|
+
jcb: /^(?:2131|1800|35\d{3})\d{11}$/,
|
21
|
+
solo: /(^(6334)[5-9](\d{11}$|\d{13,14}$)) |(^(6767)(\d{12}$|\d{14,15}$))/,
|
22
|
+
china_union: /^62[0-5]\d{13,16}$/,
|
23
|
+
dankort: /^5019\d{12}$/
|
21
24
|
}
|
22
25
|
|
23
26
|
attr_accessor :options, :cc_number, :cc_cvv, :cc_month,
|
24
|
-
:cc_year, :cc_owner, :cc_providers, :cc_type, :custom_messages
|
27
|
+
:cc_year, :cc_owner, :cc_providers, :cc_type, :custom_messages,
|
28
|
+
:cc_first_name, :cc_last_name, :using_owner
|
25
29
|
|
26
30
|
def initialize(options={})
|
27
31
|
@options = options
|
@@ -30,8 +34,14 @@ module ActiveModel
|
|
30
34
|
@cc_cvv = init_option(:cvv, :cc_cvv)
|
31
35
|
@cc_month = init_option(:month, :cc_month)
|
32
36
|
@cc_year = init_option(:year, :cc_year)
|
33
|
-
@cc_owner = init_option(:owner, :cc_owner)
|
34
37
|
@cc_providers = options[:providers]
|
38
|
+
@using_owner = !options[:owner].nil? || options[:first_name].nil? || options[:last_name].nil?
|
39
|
+
if @using_owner
|
40
|
+
@cc_owner = init_option(:owner, :cc_owner)
|
41
|
+
else
|
42
|
+
@cc_first_name = init_option(:first_name, :cc_first_name)
|
43
|
+
@cc_last_name = init_option(:last_name, :cc_last_name)
|
44
|
+
end
|
35
45
|
end
|
36
46
|
|
37
47
|
def init_option(key, default)
|
@@ -62,8 +72,12 @@ module ActiveModel
|
|
62
72
|
|
63
73
|
private
|
64
74
|
|
75
|
+
def validated_fields
|
76
|
+
[cc_number, cc_cvv, cc_month, cc_year] + (using_owner ? [cc_owner] : [cc_first_name, cc_last_name])
|
77
|
+
end
|
78
|
+
|
65
79
|
def validate_fields_presence
|
66
|
-
|
80
|
+
validated_fields.each do |field|
|
67
81
|
add_error(field, :blank) if @record.public_send(field).blank?
|
68
82
|
end
|
69
83
|
end
|
@@ -72,7 +86,7 @@ module ActiveModel
|
|
72
86
|
err = if @cc_type.nil? || !luhn_algorithm_valid?
|
73
87
|
:invalid
|
74
88
|
elsif !cc_providers.blank? && !cc_providers.include?(@cc_type)
|
75
|
-
:
|
89
|
+
:inclusion
|
76
90
|
end
|
77
91
|
add_error(cc_number, err) if err
|
78
92
|
end
|
@@ -113,6 +127,7 @@ module ActiveModel
|
|
113
127
|
end
|
114
128
|
|
115
129
|
def luhn_algorithm_valid?
|
130
|
+
return true if @cc_type == :china_union
|
116
131
|
s1 = s2 = 0
|
117
132
|
@record.public_send(cc_number).to_s.reverse.chars.each_slice(2) do |odd, even|
|
118
133
|
s1 += odd.to_i
|
@@ -140,7 +155,7 @@ module ActiveModel
|
|
140
155
|
end
|
141
156
|
|
142
157
|
def check_fields_format
|
143
|
-
invalid_attr =
|
158
|
+
invalid_attr = validated_fields.find do |attr|
|
144
159
|
!@record.public_send(attr).is_a?(NilClass) && !@record.public_send(attr).is_a?(String)
|
145
160
|
end
|
146
161
|
if invalid_attr
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
class NamedUser
|
4
|
+
include ActiveModel::Validations
|
5
|
+
|
6
|
+
attr_accessor :credit_card_number, :credit_card_cvv, :credit_card_month,
|
7
|
+
:credit_card_year, :first_name, :last_name
|
8
|
+
|
9
|
+
validate_credit_card_fields number: :credit_card_number,
|
10
|
+
cvv: :credit_card_cvv,
|
11
|
+
month: { field: :credit_card_month },
|
12
|
+
year: :credit_card_year,
|
13
|
+
first_name: :first_name,
|
14
|
+
last_name: :last_name,
|
15
|
+
providers: [:amex, :visa]
|
16
|
+
|
17
|
+
def [](key)
|
18
|
+
send(key)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -36,6 +36,46 @@ describe ValidateCreditCardFields do
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
+
context 'first and last name instead of owner' do
|
40
|
+
let(:dummy) { NamedUser.new }
|
41
|
+
|
42
|
+
context 'first_name' do
|
43
|
+
|
44
|
+
it 'must be present' do
|
45
|
+
dummy.first_name = nil
|
46
|
+
should_have_error :first_name, 'can\'t be blank'
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'raises an error if it\'s not a string' do
|
50
|
+
dummy.first_name = 1
|
51
|
+
expect{dummy.valid?}.to raise_error{CCTypeError}
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'not raising error if nil' do
|
55
|
+
dummy.first_name = nil
|
56
|
+
expect{dummy.valid?}.not_to raise_error{CCTypeError}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'last_name' do
|
61
|
+
|
62
|
+
it 'must be present' do
|
63
|
+
dummy.last_name = nil
|
64
|
+
should_have_error :last_name, 'can\'t be blank'
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'raises an error if it\'s not a string' do
|
68
|
+
dummy.last_name = 1
|
69
|
+
expect{dummy.valid?}.to raise_error{CCTypeError}
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'not raising error if nil' do
|
73
|
+
dummy.last_name = nil
|
74
|
+
expect{dummy.valid?}.not_to raise_error{CCTypeError}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
39
79
|
context 'number' do
|
40
80
|
|
41
81
|
it 'must be present' do
|
@@ -295,7 +335,37 @@ describe ValidateCreditCardFields do
|
|
295
335
|
end
|
296
336
|
end
|
297
337
|
|
338
|
+
context 'with both first and last name provided' do
|
339
|
+
let(:fields) { [:cc_number, :cc_cvv, :cc_month, :cc_year, :cc_first_name, :cc_last_name] }
|
340
|
+
let(:options) { {first_name: :cc_first_name, last_name: :cc_last_name} }
|
341
|
+
before do
|
342
|
+
dummy.cc_first_name = 'Tomasz'
|
343
|
+
dummy.cc_last_name = 'Czosnek'
|
344
|
+
end
|
345
|
+
|
346
|
+
it 'raises no owner errors' do
|
347
|
+
has_valid :cc_owner
|
348
|
+
end
|
349
|
+
|
350
|
+
it 'raises first name errors' do
|
351
|
+
dummy.cc_first_name = ''
|
352
|
+
should_have_error :cc_first_name, 'can\'t be blank'
|
353
|
+
end
|
298
354
|
|
355
|
+
it 'raises last name errors' do
|
356
|
+
dummy.cc_last_name = ''
|
357
|
+
should_have_error :cc_last_name, 'can\'t be blank'
|
358
|
+
end
|
359
|
+
end
|
360
|
+
|
361
|
+
context 'without first or last name provided' do
|
362
|
+
let(:options) { {last_name: 'whatever'} }
|
363
|
+
|
364
|
+
it 'falls back to owner requirement' do
|
365
|
+
dummy.valid?
|
366
|
+
expect(dummy.errors).to include(:cc_owner)
|
367
|
+
end
|
368
|
+
end
|
299
369
|
end
|
300
370
|
|
301
371
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validate_credit_card_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Kruczek
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2015-
|
14
|
+
date: 2015-07-10 00:00:00.000000000 Z
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: activemodel
|
@@ -98,6 +98,7 @@ files:
|
|
98
98
|
- Rakefile
|
99
99
|
- lib/validate_credit_card_fields.rb
|
100
100
|
- lib/validate_credit_card_fields/version.rb
|
101
|
+
- spec/dummy/named_user.rb
|
101
102
|
- spec/dummy/user.rb
|
102
103
|
- spec/spec_helper.rb
|
103
104
|
- spec/validate_credit_card_fields_spec.rb
|
@@ -127,6 +128,7 @@ signing_key:
|
|
127
128
|
specification_version: 4
|
128
129
|
summary: Credit card validation with all dependant fields
|
129
130
|
test_files:
|
131
|
+
- spec/dummy/named_user.rb
|
130
132
|
- spec/dummy/user.rb
|
131
133
|
- spec/spec_helper.rb
|
132
134
|
- spec/validate_credit_card_fields_spec.rb
|