Skip to content

Commit 45f8a0d

Browse files
authored
Merge pull request #111 from edlebert/master
Added support for Ruby 2.4 BigDecimal with empty string.
2 parents 83a4bbe + cf44360 commit 45f8a0d

2 files changed

Lines changed: 60 additions & 45 deletions

File tree

lib/authorize_net/authorize_net.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ module AuthorizeNet
66
# Some type conversion routines that will be injected into our
77
# Transaction/Response classes.
88
module TypeConversions
9-
9+
1010
API_FIELD_PREFIX = 'x_'
11-
11+
1212
# Converts a value received from Authorize.Net into a boolean if
1313
# possible. This is designed to handle the wide range of boolean
1414
# formats that Authorize.Net uses.
@@ -38,6 +38,7 @@ def boolean_to_value(bool)
3838

3939
# Converts a value received from Authorize.Net into a BigDecimal.
4040
def value_to_decimal(value)
41+
value = 0 if value == '' # Ruby 2.4+ does not accept ""
4142
BigDecimal.new(value)
4243
end
4344

@@ -105,15 +106,14 @@ def integer_to_value(int)
105106
# prefixed with key_prefix when being converted to a parameter name.
106107
def to_param(key, value, key_prefix = API_FIELD_PREFIX)
107108
key_str = "#{key_prefix}#{key}="
108-
if value.kind_of?(Array)
109+
if value.kind_of?(Array)
109110
(value.collect do |v|
110111
key_str + CGI::escape(v.to_s)
111112
end).join('&')
112113
else
113114
key_str + CGI::escape(value.to_s)
114115
end
115-
end
116-
116+
end
117117

118118
# Converts an internal field name (Symbol) into an external field
119119
# name (Symbol) that can be consumed by the Authorize.Net API.
@@ -130,10 +130,10 @@ def to_internal_field(key)
130130
k_str[API_FIELD_PREFIX.length..k_str.length].to_sym
131131
end
132132
end
133-
133+
134134
# Provides some basic methods used by the various model classes.
135135
module Model
136-
136+
137137
# The constructor for models. Takes any of the supported attributes
138138
# as key/value pairs.
139139
def initialize(fields = {})
@@ -144,18 +144,18 @@ def initialize(fields = {})
144144
end
145145
end
146146
end
147-
147+
148148
def to_a
149149
[self]
150150
end
151-
151+
152152
#:enddoc:
153153
protected
154-
154+
155155
def handle_multivalue_hashing(obj)
156156
obj.to_a.collect(&:to_hash)
157157
end
158-
158+
159159
end
160160

161-
end
161+
end

spec/authorize_net_spec.rb

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
defined?(AuthorizeNet).should be_truthy
77
AuthorizeNet.class.should equal(Module)
88
end
9-
9+
1010
it "should have a module called AIM" do
1111
defined?(AuthorizeNet::AIM).should be_truthy
1212
AuthorizeNet::AIM.class.should equal(Module)
@@ -15,41 +15,41 @@
1515
end
1616

1717
describe AuthorizeNet::CreditCard do
18-
18+
1919
before do
2020
@card_number = '4111111111111111'
2121
@expiry = '01' + (Time.now + (3600 * 24 * 365)).strftime('%y')
2222
end
23-
23+
2424
it "should support instantiation" do
2525
AuthorizeNet::CreditCard.new(@card_number, @expiry).should be_instance_of(AuthorizeNet::CreditCard)
2626
end
27-
27+
2828
it "should support converting itself into a hash" do
2929
card = AuthorizeNet::CreditCard.new(@card_number, @expiry)
3030
card.should respond_to(:to_hash)
3131
card.to_hash.should be_kind_of(Hash)
3232
end
33-
33+
3434
it "should have the right payment method type" do
3535
card = AuthorizeNet::CreditCard.new(@card_number, @expiry)
3636
fields = card.to_hash
3737
fields[:method].should == AuthorizeNet::PaymentMethodType::CREDIT_CARD
3838
end
39-
39+
4040
it "should respond to attributes" do
4141
card = AuthorizeNet::CreditCard.new(@card_number, @expiry)
4242
card.card_number.should == @card_number
4343
card.expiration.should == @expiry
4444
card.card_code.should be_nil
4545
end
4646
=begin
47-
it 'should clean the track' do
47+
it 'should clean the track' do
4848
card = AuthorizeNet::CreditCard.new(@card_number, @expiry, track_1: '%1111111111111111=11111111111111111111?3', track_2: ';2222222222222222=22222222222222222222?3')
4949
card_hash = card.to_hash
5050
expect(card_hash[:track_1]).to eq('1111111111111111=11111111111111111111')
5151
expect(card_hash[:track_2]).to eq('2222222222222222=22222222222222222222')
52-
52+
5353
card = AuthorizeNet::CreditCard.new(@card_number, @expiry, track_1: '1111111111111111=11111111111111111111', track_2: '2222222222222222=22222222222222222222')
5454
card_hash = card.to_hash
5555
expect(card_hash[:track_1]).to eq('1111111111111111=11111111111111111111')
@@ -59,24 +59,24 @@
5959
end
6060

6161
describe AuthorizeNet::ECheck do
62-
62+
6363
before do
6464
@routing_number = '322271627'
6565
@account_number = '123456789'
6666
@bank_name = 'JPMorgan Chase Bank'
6767
@account_holder_name = 'John Doe'
6868
end
69-
69+
7070
it "should support instantiation" do
7171
AuthorizeNet::ECheck.new(@routing_number, @account_number, @bank_name, @account_holder_name).should be_instance_of(AuthorizeNet::ECheck)
7272
end
73-
73+
7474
it "should support converting itself into a hash" do
7575
echeck = AuthorizeNet::ECheck.new(@routing_number, @account_number, @bank_name, @account_holder_name)
7676
echeck.should respond_to(:to_hash)
7777
echeck.to_hash.should be_kind_of(Hash)
7878
end
79-
79+
8080
it "should support payment method code retrival" do
8181
echeck = AuthorizeNet::ECheck.new(@routing_number, @account_number, @bank_name, @account_holder_name)
8282
fields = echeck.to_hash
@@ -85,27 +85,27 @@
8585
end
8686

8787
describe AuthorizeNet::Address do
88-
88+
8989
before do
9090
end
91-
91+
9292
it "should support instantiation" do
9393
AuthorizeNet::Address.new.should be_instance_of(AuthorizeNet::Address)
9494
end
95-
95+
9696
it "should support converting itself into a hash" do
9797
address = AuthorizeNet::Address.new
9898
address.should respond_to(:to_hash)
9999
address.to_hash.should be_kind_of(Hash)
100100
end
101-
101+
102102
it "should ignore unknown fields" do
103103
address = AuthorizeNet::Address.new(:tax => '123')
104104
hash = address.to_hash
105105
hash.should be_kind_of(Hash)
106106
hash.should == {}
107107
end
108-
108+
109109
it "should accept known fields" do
110110
address = AuthorizeNet::Address.new(:first_name => '123')
111111
hash = address.to_hash
@@ -115,27 +115,27 @@
115115
end
116116

117117
describe AuthorizeNet::ShippingAddress do
118-
118+
119119
before do
120120
end
121-
121+
122122
it "should support instantiation" do
123123
AuthorizeNet::ShippingAddress.new.should be_instance_of(AuthorizeNet::ShippingAddress)
124124
end
125-
125+
126126
it "should support converting itself into a hash" do
127127
address = AuthorizeNet::ShippingAddress.new
128128
address.should respond_to(:to_hash)
129129
address.to_hash.should be_kind_of(Hash)
130130
end
131-
131+
132132
it "should ignore unknown fields" do
133133
address = AuthorizeNet::ShippingAddress.new(:pie => '123')
134134
hash = address.to_hash
135135
hash.should be_kind_of(Hash)
136136
hash.should == {}
137137
end
138-
138+
139139
it "should accept known fields" do
140140
address = AuthorizeNet::ShippingAddress.new(:first_name => '123')
141141
hash = address.to_hash
@@ -145,34 +145,34 @@
145145
end
146146

147147
describe AuthorizeNet::Customer do
148-
148+
149149
before do
150150
end
151-
151+
152152
it "should support instantiation" do
153153
AuthorizeNet::Customer.new.should be_instance_of(AuthorizeNet::Customer)
154154
end
155-
155+
156156
it "should support converting itself into a hash" do
157157
customer = AuthorizeNet::Customer.new
158158
customer.should respond_to(:to_hash)
159159
customer.to_hash.should be_kind_of(Hash)
160160
end
161-
161+
162162
it "should ignore unknown fields" do
163163
customer = AuthorizeNet::Customer.new(:name => '123')
164164
hash = customer.to_hash
165165
hash.should be_kind_of(Hash)
166166
hash.should == {}
167167
end
168-
168+
169169
it "should accept known fields" do
170170
customer = AuthorizeNet::Customer.new(:id => '123')
171171
hash = customer.to_hash
172172
hash.should be_kind_of(Hash)
173173
hash.should == {:cust_id => '123'}
174174
end
175-
175+
176176
it "should accept an address record" do
177177
address = AuthorizeNet::Address.new(:first_name => 'Tester', :last_name => 'Testerson')
178178
customer = AuthorizeNet::Customer.new(:address => address)
@@ -183,31 +183,46 @@
183183
end
184184

185185
describe AuthorizeNet::EmailReceipt do
186-
186+
187187
before do
188188
end
189-
189+
190190
it "should support instantiation" do
191191
AuthorizeNet::EmailReceipt.new.should be_instance_of(AuthorizeNet::EmailReceipt)
192192
end
193-
193+
194194
it "should support converting itself into a hash" do
195195
email = AuthorizeNet::EmailReceipt.new
196196
email.should respond_to(:to_hash)
197197
email.to_hash.should be_kind_of(Hash)
198198
end
199-
199+
200200
it "should ignore unknown fields" do
201201
email = AuthorizeNet::EmailReceipt.new(:name => '123')
202202
hash = email.to_hash
203203
hash.should be_kind_of(Hash)
204204
hash.should == {}
205205
end
206-
206+
207207
it "should accept known fields" do
208208
email = AuthorizeNet::EmailReceipt.new(:header => '123')
209209
hash = email.to_hash
210210
hash.should be_kind_of(Hash)
211211
hash.should == {:header => '123'}
212212
end
213213
end
214+
215+
describe AuthorizeNet::TypeConversions do
216+
217+
subject do
218+
Object.new.extend(described_class)
219+
end
220+
221+
it 'should convert decimal strings' do
222+
subject.value_to_decimal('1.2345').should == BigDecimal.new('1.2345')
223+
end
224+
225+
it 'should convert empty decimal strings' do
226+
subject.value_to_decimal('').should == BigDecimal.new('0.0')
227+
end
228+
end

0 commit comments

Comments
 (0)