forked from AuthorizeNet/sdk-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaim_spec.rb
313 lines (273 loc) · 17.3 KB
/
aim_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
require "spec_helper"
describe AuthorizeNet::AIM::Transaction do
before :all do
begin
creds = YAML.load_file(File.dirname(__FILE__) + "/credentials.yml")
@api_key = creds['api_transaction_key']
@api_login = creds['api_login_id']
@md5_value = creds['md5_value']
@solution_id = "AAA100302"
rescue Errno::ENOENT => e
@api_key = "TEST"
@api_login = "TEST"
@md5_value = "TEST"
warn "WARNING: Running w/o valid AuthorizeNet sandbox credentials. Create spec/credentials.yml."
end
end
before do
@type = AuthorizeNet::AIM::Transaction::Type::AUTHORIZE_AND_CAPTURE
@test_mode = false
@gateway = :sandbox
@amount = (rand(10000) + 100) / 100.0
@credit_card = AuthorizeNet::CreditCard.new('4111111111111111', '01' + (Time.now + (3600 * 24 * 365)).strftime('%y'))
@response = '1,1,1,(TESTMODE) This transaction has been approved.,000000,P,0,,,10.00,CC,auth_capture,,,,,,,,,,,,,,,,,,,,,,,,,,7A3C09A367FED29C9902038440CD8A52,,,,,,,,,,,,,XXXX0027,Visa,,,,,,,,,,,,,,,,'
@echeck = AuthorizeNet::ECheck.new('322271627', '123456789', 'JPMorgan Chase Bank', 'John Doe')
end
it "should support instantiation" do
AuthorizeNet::AIM::Transaction.new(@api_login, @api_key).should be_instance_of(AuthorizeNet::AIM::Transaction)
end
it "should know if its in test mode" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => AuthorizeNet::AIM::Transaction::Gateway::TEST, :test => true)
transaction.test?.should be_truthy
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => AuthorizeNet::AIM::Transaction::Gateway::TEST, :test => false)
transaction.test?.should be_truthy
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => AuthorizeNet::AIM::Transaction::Gateway::LIVE, :test => true)
transaction.test?.should be_truthy
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => AuthorizeNet::AIM::Transaction::Gateway::LIVE, :test => false)
transaction.test?.should be_falsey
end
it "should not have a response if the transaciton hasn't been run" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.has_response?.should be_falsey
end
it "should support the addition of fields to the transaction body" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.should respond_to(:set_fields)
transaction.set_fields({:test => 123}).should == {:test => 123}
end
it "should support the addition of mulitple line_item fields" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.set_fields({:line_item => [123, 123]}).should == {:line_item => [123, 123]}
transaction.set_fields({:line_item => "abc"}).should == {:line_item => "abc"}
end
it "should not support the addition of mulitple fields for non-line-item fields" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.set_fields({:test => 123}).should == {:test => 123}
transaction.set_fields({:test => 123}).should == {:test => 123}
end
it "should support custom fields" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.should respond_to(:set_custom_fields)
transaction.set_custom_fields(:test => 123).should == {:test => 123}
transaction.fields.should == {}
transaction.custom_fields.should == {:test => 123}
end
it "should support the returning its response object" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.should respond_to(:response)
end
it "should parse the response properly" do
Net::HTTP.stub(:start).and_return(@response)
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
response = transaction.purchase(@amount, @credit_card)
response.should be_kind_of(AuthorizeNet::AIM::Response)
response.transaction_id.length.should >= 3
response.success?.should be_truthy
response.avs_response.should == AuthorizeNet::AIM::Response::AVSResponseCode::ADDRESS_AND_ZIP5_MATCH
response.card_type.should == AuthorizeNet::AIM::Response::CardType::VISA
response.card_type.should_not == AuthorizeNet::AIM::Response::CardType::MASTER_CARD
end
it "should support different transaction types" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.should respond_to(:type)
transaction.type.should == AuthorizeNet::AIM::Transaction::Type::AUTHORIZE_AND_CAPTURE
transaction.type = AuthorizeNet::AIM::Transaction::Type::VOID
transaction.type.should == AuthorizeNet::AIM::Transaction::Type::VOID
transaction.type = AuthorizeNet::AIM::Transaction::Type::AUTHORIZE_ONLY
transaction.type.should == AuthorizeNet::AIM::Transaction::Type::AUTHORIZE_ONLY
transaction.type = AuthorizeNet::AIM::Transaction::Type::CAPTURE_ONLY
transaction.type.should == AuthorizeNet::AIM::Transaction::Type::CAPTURE_ONLY
transaction.type = AuthorizeNet::AIM::Transaction::Type::CREDIT
transaction.type.should == AuthorizeNet::AIM::Transaction::Type::CREDIT
transaction.type = AuthorizeNet::AIM::Transaction::Type::PRIOR_AUTHORIZATION_AND_CAPTURE
transaction.type.should == AuthorizeNet::AIM::Transaction::Type::PRIOR_AUTHORIZATION_AND_CAPTURE
end
it "should be able to run a transaction" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.run.should be_kind_of(AuthorizeNet::AIM::Response)
end
it "should not be able to run a transaction a second time" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.run.should be_kind_of(AuthorizeNet::AIM::Response)
transaction.run.should be_nil
end
it "should be able to make a purchase" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.purchase(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
transaction.response.fields[:amount].should == @amount
transaction.response.success?.should be_truthy
end
it "should support custom fields in the response" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.set_custom_fields(:funkey => '123')
transaction.set_custom_fields(:winker => 'ABC')
transaction.set_custom_fields(:bean => '!!!')
transaction.purchase(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
fields = transaction.response.custom_fields
fields.should have_key(:funkey)
fields[:funkey].should == '123'
fields.should have_key(:winker)
fields[:winker].should == 'ABC'
fields.should have_key(:bean)
fields[:bean].should == '!!!'
end
it "should support an encapsulation character in the response" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode, :encapsulation_character => '"')
transaction.set_custom_fields(:funkey => '12,3')
transaction.purchase(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
fields = transaction.response.custom_fields
fields.should have_key(:funkey)
fields[:funkey].should == '12,3'
end
it "should accept an ECheck for a purchase" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.purchase(@amount, @echeck).should be_kind_of(AuthorizeNet::AIM::Response)
transaction.response.success?.should be_truthy
end
it "should support adding line items" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.add_line_item("item1", "Foo", "Bar", "5", "5.00", "TRUE")
transaction.purchase(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
transaction.response.success?.should be_truthy
transaction.fields[:line_item].should == ["item1<|>Foo<|>Bar<|>5<|>5.00<|>TRUE"]
end
it "should support setting a customer" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
customer = AuthorizeNet::Customer.new(:ip => '127.0.0.1')
transaction.set_customer(customer)
transaction.purchase(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
transaction.response.success?.should be_truthy
end
it "should support setting an address" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
address = AuthorizeNet::Address.new(:city => 'San Francisco', :state => 'NY')
transaction.set_address(address)
transaction.purchase(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
transaction.response.success?.should be_truthy
end
it "should support setting a shipping address" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
address = AuthorizeNet::ShippingAddress.new(:city => 'San Francisco', :state => 'NY', :tax => 10.00)
transaction.set_shipping_address(address)
transaction.purchase(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
transaction.response.success?.should be_truthy
end
it "should support setting solution id" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode, :solution_id => @solution_id)
transaction.purchase(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
transaction.response.success?.should be_truthy
fields = transaction.response.transaction
(defined?(fields.solution_id) and fields.solution_id == 'AAA100302').should be_truthy
end
it "should support adding an email receipt" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
email = AuthorizeNet::EmailReceipt.new(:header => 'TEST!')
transaction.set_email_receipt(email)
transaction.purchase(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
transaction.response.success?.should be_truthy
end
it "should be able to void a purchase" do
purchase = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => false)
purchase.purchase(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
purchase.response.fields[:amount].should == @amount
purchase.response.success?.should be_truthy
void = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :gateway => @gateway, :test => false)
void.void(purchase.response.transaction_id)
void.response.success?.should be_truthy
end
it "should be able to capture a prior authorization" do
auth = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => false)
auth.authorize(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
auth.response.fields[:amount].should == @amount
auth.response.success?.should be_truthy
capture = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :gateway => @gateway, :test => false)
capture.prior_auth_capture(auth.response.transaction_id)
capture.response.success?.should be_truthy
end
it "should be able to run a card present purchase with track 1 data" do
@credit_card = AuthorizeNet::CreditCard.new(nil, nil, :track_1 => '%B4111111111111111^DOE/JOHN^1803101000000000020000831000000?')
purchase = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :gateway => :card_present_test, :test => false)
response = purchase.purchase(@amount, @credit_card)
response.success?.should be_truthy
end
it "should be able to run a card present purchase with LRC codes in the track 1 data" do
@credit_card = AuthorizeNet::CreditCard.new(nil, nil, :track_1 => '%B4111111111111111^DOE/JOHN^1803101000000000020000831000000?\xAA')
purchase = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :gateway => :card_present_test, :test => false)
response = purchase.purchase(@amount, @credit_card)
response.success?.should be_truthy
end
it "should be able to run a card present purchase with no LRC or Start/End Sentinels in track 1 data" do
@credit_card = AuthorizeNet::CreditCard.new(nil, nil, :track_1 => "B4111111111111111^DOE/JOHN^1803101000000000020000831000000")
purchase = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :gateway => :card_present_test, :test => false)
response = purchase.purchase(@amount, @credit_card)
response.success?.should be_truthy
end
it "should be able to run a card present purchase with track 2 data" do
@credit_card = AuthorizeNet::CreditCard.new(nil, nil, :track_2 => ';4111111111111111=1803101000020000831?')
purchase = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :gateway => :card_present_test, :test => false)
response = purchase.purchase(@amount, @credit_card)
response.success?.should be_truthy
end
it "should be able to run a card present purchase with LRC codes in the track 2 data" do
@credit_card = AuthorizeNet::CreditCard.new(nil, nil, :track_2 => ";4111111111111111=1803101000020000831?\x33")
purchase = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :gateway => :card_present_test, :test => false)
response = purchase.purchase(@amount, @credit_card)
response.success?.should be_truthy
end
it "should be able to run a card present purchase with no LRC or Start/End Sentinels in track 2 data" do
@credit_card = AuthorizeNet::CreditCard.new(nil, nil, :track_2 => "4111111111111111=1803101000020000831")
purchase = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :gateway => :card_present_test, :test => false)
response = purchase.purchase(@amount, @credit_card)
response.success?.should be_truthy
end
it "should be able to validate the passed MD5 hash" do
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :transaction_type => @type, :gateway => @gateway, :test => @test_mode)
transaction.purchase(@amount, @credit_card).should be_kind_of(AuthorizeNet::AIM::Response)
transaction.response.success?.should be_truthy
transaction.response.valid_md5?(@api_login, @md5_value).should be_truthy
end
it "should be able to validate the passed MD5 hash for card present transactions" do
@credit_card = AuthorizeNet::CreditCard.new(nil, nil, :track_1 => '%B4111111111111111^DOE/JOHN^1803101000000000020000831000000?')
purchase = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :gateway => :card_present_test, :test => false)
response = purchase.purchase(@amount, @credit_card)
response.success?.should be_truthy
response.valid_md5?(@api_login, @md5_value).should be_truthy
end
end
describe AuthorizeNet::AIM::Response do
before :all do
begin
creds = YAML.load_file(File.dirname(__FILE__) + "/credentials.yml")
@api_key = creds['api_transaction_key']
@api_login = creds['api_login_id']
rescue Errno::ENOENT => e
@api_key = "TEST"
@api_login = "TEST"
warn "WARNING: Running w/o valid AuthorizeNet sandbox credentials. Create spec/credentials.yml."
end
end
before do
@cim_response = '1,1,1,This transaction has been approved.,000000,Y,2000000000,none,Test transaction for ValidateCustomerPaymentProfile.,0.01,CC,auth_only,custId123,John,Doe,,123 Main St.,Bellevue,WA,98004,USA,000-000-0000,,[email protected],,,,,,,,,0.00,0.00,0.00,,none,D18EB6B211FE0BBF556B271FDA6F92EE,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,'
end
it "should support a Nokogiri based raw_response" do
builder = Nokogiri::XML::Builder.new(:encoding => 'utf-8') do |x|
x.directResponse(@cim_response)
end
@element = builder.doc.children[0]
@element.should be_kind_of(Nokogiri::XML::Element)
transaction = AuthorizeNet::AIM::Transaction.new(@api_login, @api_key, :test => false)
response = AuthorizeNet::AIM::Response.new(@element, transaction)
response.success?.should be_truthy
response.response_reason_text.should == 'This transaction has been approved.'
end
end