Skip to content

Commit 915a245

Browse files
add option to perform a credit card extra validation #39
1 parent dcede2e commit 915a245

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,24 @@ credit_card = MyMoip::CreditCard.new(
132132
owner_name: 'Juquinha da Rocha',
133133
owner_birthday: '03/11/1984',
134134
owner_phone: '5130405060',
135-
owner_cpf: '52211670695'
135+
owner_cpf: '52211670695',
136+
perform_extra_validation: true # optional: see the next sub section
136137
)
137138

138139
credit_card_payment = MyMoip::CreditCardPayment.new(credit_card, installments: 1)
139140
payment_request = MyMoip::PaymentRequest.new('your_logging_id')
140141
payment_request.api_call(credit_card_payment, token: transparent_request.token)
141142
```
142143

144+
##### Credit card extra validation
145+
146+
There is a already [reported](http://goo.gl/celJIZ) bug that the API don't
147+
requires some attributes returning a successful
148+
[response](https://gist.github.com/Irio/4032350). To "fix it" you can enable an
149+
extra validation with `perform_extra_validation` option. It will require the
150+
presence of all credit card attributes.
151+
152+
143153
#### Payment slip (aka boleto)
144154

145155
```ruby

lib/mymoip/credit_card.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ class CreditCard
33
include ActiveModel::Validations
44

55
attr_accessor :logo, :card_number, :expiration_date, :security_code,
6-
:owner_name, :owner_birthday, :owner_phone, :owner_cpf
6+
:owner_name, :owner_birthday, :owner_phone, :owner_cpf,
7+
:perform_extra_validation
78

89
AVAILABLE_LOGOS = [
910
:american_express, :diners, :hipercard, :mastercard, :visa
@@ -15,6 +16,10 @@ class CreditCard
1516
validates_format_of :expiration_date, with: /\A(?:(?:0[1-9])|(?:1[0-2]))\/\d{2}\Z/ # %m/%y
1617
validates_inclusion_of :logo, in: AVAILABLE_LOGOS
1718
validate :owner_birthday_format
19+
validates_presence_of :card_number, :expiration_date, :owner_name,
20+
:owner_phone, :owner_cpf,
21+
if: ->(resource) { resource.perform_extra_validation }
22+
1823

1924
def initialize(attrs)
2025
attrs.each do |attr, value|

test/lib/test_creditcard.rb

+30-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ def test_initialization_and_setters
1010
owner_name: "Juquinha da Rocha",
1111
owner_birthday: Date.new(1984, 11, 3),
1212
owner_phone: "5130405060",
13-
owner_cpf: "522.116.706-95"
13+
owner_cpf: "522.116.706-95",
14+
perform_extra_validation: true
1415
)
1516

1617
assert_equal :visa, subject.logo
@@ -21,18 +22,20 @@ def test_initialization_and_setters
2122
assert_equal Date.new(1984, 11, 3), subject.owner_birthday
2223
assert_equal "5130405060", subject.owner_phone
2324
assert_equal "52211670695", subject.owner_cpf
25+
assert_equal true, subject.perform_extra_validation
2426
end
2527

2628
def test_initialization_and_setters_with_string_keys
2729
subject = MyMoip::CreditCard.new(
28-
'logo' => :visa,
29-
'card_number' => '4916654211627608',
30-
'expiration_date' => '06/15',
31-
'security_code' => '000',
32-
'owner_name' => 'Juquinha da Rocha',
33-
'owner_birthday' => Date.new(1984, 11, 3),
34-
'owner_phone' => '5130405060',
35-
'owner_cpf' => '522.116.706-95'
30+
'logo' => :visa,
31+
'card_number' => '4916654211627608',
32+
'expiration_date' => '06/15',
33+
'security_code' => '000',
34+
'owner_name' => 'Juquinha da Rocha',
35+
'owner_birthday' => Date.new(1984, 11, 3),
36+
'owner_phone' => '5130405060',
37+
'owner_cpf' => '522.116.706-95',
38+
'perform_extra_validation' => false
3639
)
3740

3841
assert_equal :visa, subject.logo
@@ -43,6 +46,7 @@ def test_initialization_and_setters_with_string_keys
4346
assert_equal Date.new(1984, 11, 3), subject.owner_birthday
4447
assert_equal "5130405060", subject.owner_phone
4548
assert_equal "52211670695", subject.owner_cpf
49+
assert_equal false, subject.perform_extra_validation
4650
end
4751

4852
def test_validate_presence_of_logo_attribute
@@ -94,6 +98,23 @@ def test_validate_length_of_owner_phone_attribute_in_10_or_11_chars
9498
'should not accept strings with other than 10 or 11 chars'
9599
end
96100

101+
def test_perform_extra_validation
102+
subject = Fixture.credit_card({
103+
card_number: nil,
104+
expiration_date: nil,
105+
owner_name: nil,
106+
owner_phone: nil,
107+
owner_cpf: nil,
108+
perform_extra_validation: true
109+
})
110+
assert subject.invalid?
111+
assert subject.errors[:card_number], "can't be blank"
112+
assert subject.errors[:expiration_date], "can't be blank"
113+
assert subject.errors[:owner_name], "can't be blank"
114+
assert subject.errors[:owner_phone], "can't be blank"
115+
assert subject.errors[:owner_cpf], "can't be blank"
116+
end
117+
97118
def test_remove_left_zeros_from_owner_phone
98119
subject = Fixture.credit_card
99120
subject.owner_phone = '05130405060'

0 commit comments

Comments
 (0)