ruby_psigate 0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/CHANGELOG +2 -0
  2. data/Gemfile +12 -0
  3. data/LICENSE +0 -0
  4. data/Manifest +45 -0
  5. data/README.markdown +99 -0
  6. data/Rakefile +28 -0
  7. data/lib/certs/cacert.pem +2633 -0
  8. data/lib/ruby_psigate/account.rb +152 -0
  9. data/lib/ruby_psigate/account_manager_api.rb +137 -0
  10. data/lib/ruby_psigate/account_methods.rb +5 -0
  11. data/lib/ruby_psigate/address.rb +54 -0
  12. data/lib/ruby_psigate/connection.rb +96 -0
  13. data/lib/ruby_psigate/credit_card.rb +104 -0
  14. data/lib/ruby_psigate/credit_card_methods.rb +12 -0
  15. data/lib/ruby_psigate/error.rb +33 -0
  16. data/lib/ruby_psigate/gateway.rb +126 -0
  17. data/lib/ruby_psigate/hash_variables.rb +58 -0
  18. data/lib/ruby_psigate/item.rb +65 -0
  19. data/lib/ruby_psigate/item_option.rb +10 -0
  20. data/lib/ruby_psigate/number_validation_methods.rb +12 -0
  21. data/lib/ruby_psigate/order.rb +120 -0
  22. data/lib/ruby_psigate/recurring_charge.rb +53 -0
  23. data/lib/ruby_psigate/recurring_item.rb +26 -0
  24. data/lib/ruby_psigate/response.rb +109 -0
  25. data/lib/ruby_psigate/serializer.rb +59 -0
  26. data/lib/ruby_psigate/transaction_methods.rb +21 -0
  27. data/lib/ruby_psigate/utils.rb +18 -0
  28. data/lib/ruby_psigate.rb +57 -0
  29. data/ruby_psigate.gemspec +31 -0
  30. data/test/remote/remote_account_test.rb +33 -0
  31. data/test/remote/remote_gateway_test.rb +32 -0
  32. data/test/test_helper.rb +144 -0
  33. data/test/unit/account_manager_api_test.rb +96 -0
  34. data/test/unit/account_test.rb +388 -0
  35. data/test/unit/address_test.rb +99 -0
  36. data/test/unit/connection_test.rb +153 -0
  37. data/test/unit/credit_card_test.rb +152 -0
  38. data/test/unit/gateway_test.rb +112 -0
  39. data/test/unit/item_option_test.rb +19 -0
  40. data/test/unit/item_test.rb +106 -0
  41. data/test/unit/order_test.rb +491 -0
  42. data/test/unit/recurring_charge_test.rb +89 -0
  43. data/test/unit/recurring_item_test.rb +62 -0
  44. data/test/unit/response_test.rb +110 -0
  45. data/test/unit/serializer_test.rb +89 -0
  46. data/test/unit/xml_api_test.rb +25 -0
  47. metadata +154 -0
@@ -0,0 +1,491 @@
1
+ require 'test_helper'
2
+
3
+ module RubyPsigate
4
+ class OrderTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @order = Order.new
8
+ end
9
+
10
+ should "have a getter/setter for :action" do
11
+ assert @order.respond_to?(:action)
12
+ assert @order.respond_to?(:action=)
13
+ end
14
+
15
+ should "raise error if action set is not included in list" do
16
+ assert_raises(InvalidAction) do
17
+ @order.action = :invalid
18
+ end
19
+ end
20
+
21
+ [:sale, :preauth, :postauth, :credit, :force_postauth, :void].each do |action|
22
+ should "not raise error when action is set to #{action}" do
23
+ assert_nothing_raised do
24
+ @order.action = action
25
+ end
26
+ end
27
+ end
28
+
29
+ should "return cardaction of 0 for sale" do
30
+ assert_equal Order::CARD_ACTION[:sale], 0
31
+ end
32
+
33
+ should "return cardaction of 1 for preauth" do
34
+ assert_equal Order::CARD_ACTION[:preauth], 1
35
+ end
36
+
37
+ should "return cardaction of 2 for postauth" do
38
+ assert_equal Order::CARD_ACTION[:postauth], 2
39
+ end
40
+
41
+ should "return cardaction of 3 for credit" do
42
+ assert_equal Order::CARD_ACTION[:credit], 3
43
+ end
44
+
45
+ should "return cardaction of 4 for force postauth" do
46
+ assert_equal Order::CARD_ACTION[:force_postauth], 4
47
+ end
48
+
49
+ should "return cardaction of 9 for void" do
50
+ assert_equal Order::CARD_ACTION[:void], 9
51
+ end
52
+
53
+ should "accept items" do
54
+ assert @order.items.is_a?(Array)
55
+ end
56
+
57
+ should "accept an Item object" do
58
+ @item = mock()
59
+ @item.expects(:is_a?).with(RubyPsigate::Item).returns(true)
60
+ assert_nothing_raised do
61
+ @order << @item
62
+ end
63
+ end
64
+
65
+ should "raise error if item passed into order is not an Item object" do
66
+ assert_raises(InvalidItem) do
67
+ @order << "hello"
68
+ end
69
+ end
70
+
71
+ should "accept billing address" do
72
+ assert @order.respond_to?(:billing)
73
+ assert @order.respond_to?(:billing=)
74
+ end
75
+
76
+ should "raise error if billing address is not an Address object" do
77
+ assert_raises(InvalidAddress) do
78
+ @order.billing = "my address"
79
+ end
80
+ end
81
+
82
+ should "accept shipping address" do
83
+ assert @order.respond_to?(:shipping)
84
+ assert @order.respond_to?(:shipping=)
85
+ end
86
+
87
+ should "raise error if shipping address is not an Address object" do
88
+ assert_raises(InvalidAddress) do
89
+ @order.shipping = "my address"
90
+ end
91
+ end
92
+
93
+ should "responds to comment" do
94
+ assert @order.respond_to?(:comment)
95
+ assert @order.respond_to?(:comment=)
96
+ end
97
+
98
+ should "set comment" do
99
+ comment = "This is a comment"
100
+ @order.comment = comment
101
+ assert_equal @order.comment, comment
102
+ end
103
+
104
+ should "respond to amount getter/setter" do
105
+ assert @order.respond_to?(:amount)
106
+ assert @order.respond_to?(:amount=)
107
+ end
108
+
109
+ should "set the amount to 100.00" do
110
+ @order.amount = "100.00"
111
+ assert_equal Money.new(100.00*100), @order.amount
112
+ end
113
+
114
+ should "raise error if amount is negative" do
115
+ assert_raises(InvalidAmount) do
116
+ @order.amount = "-10.00"
117
+ end
118
+ end
119
+
120
+ should "respond to card_auth_number getter/setter" do
121
+ assert @order.respond_to?(:card_auth_number)
122
+ assert @order.respond_to?(:card_auth_number=)
123
+ end
124
+
125
+ should "respond to trans_ref_number getter/setter" do
126
+ assert @order.respond_to?(:trans_ref_number)
127
+ assert @order.respond_to?(:trans_ref_number=)
128
+ end
129
+
130
+ %w( tax1 tax2 tax3 tax4 tax5 ).each do |tax|
131
+ should "respond to getter/setter for #{tax}" do
132
+ getter = tax.to_sym
133
+ setter = (tax+"=").to_sym
134
+ assert @order.respond_to?(getter)
135
+ assert @order.respond_to?(setter)
136
+ end
137
+
138
+ should "set #{tax}" do
139
+ getter = tax.to_sym
140
+ setter = (tax+"=").to_sym
141
+ @order.send(setter, 100.00)
142
+ assert_equal Money.new(100.00*100), @order.send(getter)
143
+ end
144
+
145
+ should "raise error if #{tax} is negative" do
146
+ setter = (tax+"=").to_sym
147
+ assert_raises(InvalidAmount) do
148
+ @order.send(setter, -10.00)
149
+ end
150
+ end
151
+ end
152
+
153
+ should "respond to email getter/setter" do
154
+ assert @order.respond_to?(:email)
155
+ assert @order.respond_to?(:email=)
156
+ end
157
+
158
+ should "respond to unique order id" do
159
+ assert @order.respond_to?(:order_id)
160
+ assert @order.respond_to?(:order_id=)
161
+ end
162
+
163
+ context "validity testing" do
164
+ setup do
165
+ @order.amount = 10.00
166
+ @order.action = :sale
167
+ @order.email = "bob@gmail.com"
168
+ end
169
+
170
+ should "be true" do
171
+ assert @order.valid?
172
+ end
173
+
174
+ should "be false if amount is missing" do
175
+ @order.amount = nil
176
+ refute @order.valid?
177
+ end
178
+
179
+ should "be false if action is missing" do
180
+ @order = Order.new
181
+ @order.amount = 10.00
182
+ @order.email = "bob@gmail.com"
183
+ assert_nil @order.action
184
+ refute @order.valid?
185
+ end
186
+
187
+ should "be false if email is missing" do
188
+ @order.email = nil
189
+ refute @order.valid?
190
+ end
191
+
192
+ context ":postauth action" do
193
+ setup do
194
+ @order.action = :postauth
195
+ @order.order_id = "200409191133332929"
196
+ end
197
+
198
+ should "be false if order_id is nil" do
199
+ @order.order_id = nil
200
+ refute @order.valid?
201
+ end
202
+
203
+ should "be true" do
204
+ assert @order.valid?
205
+ end
206
+
207
+ should "be true if amount is nil" do
208
+ @order.amount = nil
209
+ assert @order.valid?
210
+ end
211
+ end
212
+
213
+ context ":credit action" do
214
+ setup do
215
+ @order.action = :credit
216
+ @order.order_id = "200409191133332929"
217
+ end
218
+
219
+ should "be true" do
220
+ assert @order.valid?
221
+ end
222
+
223
+ should "be false if order_id is nil" do
224
+ @order.order_id = nil
225
+ refute @order.valid?
226
+ end
227
+ end
228
+
229
+ should "be false if card_auth_number is missing when action is :force_postauth" do
230
+ @order.action = :force_postauth
231
+ @order.card_auth_number = nil
232
+ refute @order.valid?
233
+ end
234
+
235
+ context ":void action" do
236
+ setup do
237
+ @order.action = :void
238
+ @order.order_id = "200409191133332929"
239
+ @order.trans_ref_number = "07abef21992"
240
+ end
241
+
242
+ should "be false if trans_ref_number is nil" do
243
+ @order.trans_ref_number = nil
244
+ refute @order.valid?
245
+ end
246
+
247
+ should "be false if order_id is nil" do
248
+ @order.order_id = nil
249
+ refute @order.valid?
250
+ end
251
+
252
+ should "be true even if amount is nil" do
253
+ @order.amount = nil
254
+ assert @order.valid?
255
+ end
256
+ end
257
+ end
258
+
259
+ context "credit card interface" do
260
+ setup do
261
+ @order = Order.new
262
+ end
263
+
264
+ should "accept a CreditCard object" do
265
+ assert @order.respond_to?(:cc=)
266
+ end
267
+
268
+ should "not raise error when setting credit card object" do
269
+ @cc = mock()
270
+ @cc.expects(:is_a?).with(RubyPsigate::CreditCard).returns(true)
271
+ assert_nothing_raised do
272
+ @order.cc = @cc
273
+ end
274
+ end
275
+
276
+ should "raise error if credit card object is not RubyPsigate::CreditCard" do
277
+ assert_raises(InvalidCreditCard) do
278
+ @cc = mock()
279
+ @cc.expects(:is_a?).with(RubyPsigate::CreditCard).returns(false)
280
+ @order.cc = @cc
281
+ end
282
+ end
283
+ end
284
+
285
+ should "return a simple hash for :sale" do
286
+ @expectation = {
287
+ :Email => "bob@gmail.com",
288
+ :CardAction => 0,
289
+ :Subtotal => 10.00
290
+ }
291
+ @expectation.merge!(valid_credit_card_hash)
292
+
293
+ @order = Order.new
294
+ @order.amount = 10.00
295
+ @order.email = "bob@gmail.com"
296
+ @order.action = :sale
297
+ @order.cc = valid_credit_card
298
+
299
+ assert_equal @expectation, @order.to_hash(@order.action)
300
+ end
301
+
302
+ should "return a hash for :sale with billing address" do
303
+ @expectation = {
304
+ :Email => "bob@gmail.com",
305
+ :CardAction => 0,
306
+ :Subtotal => 10.00,
307
+ :Bname => "Bob Parsons",
308
+ :Bcompany => "Bob Parson's Co.",
309
+ :Baddress1 => "1234 West Street",
310
+ :Baddress2 => "Apt 100",
311
+ :Bcity => "Toronto",
312
+ :Bprovince => "Ontario",
313
+ :Bpostalcode => "L3N9J2",
314
+ :Bcountry => "CA",
315
+ :Phone => "416-333-3333",
316
+ :Fax => "416-322-2222"
317
+ }
318
+ @expectation.merge!(valid_credit_card_hash)
319
+
320
+ @order = Order.new
321
+ @order.amount = 10.00
322
+ @order.email = "bob@gmail.com"
323
+ @order.action = :sale
324
+ @order.billing = valid_address
325
+ @order.cc = valid_credit_card
326
+
327
+ assert_equal @expectation, @order.to_hash(@order.action)
328
+ end
329
+
330
+ should "return a hash for :sale with billing and shipping address" do
331
+ @expectation = {
332
+ :Email => "bob@gmail.com",
333
+ :CardAction => 0,
334
+ :Subtotal => 10.00,
335
+ :Bname => "Bob Parsons",
336
+ :Bcompany => "Bob Parson's Co.",
337
+ :Baddress1 => "1234 West Street",
338
+ :Baddress2 => "Apt 100",
339
+ :Bcity => "Toronto",
340
+ :Bprovince => "Ontario",
341
+ :Bpostalcode => "L3N9J2",
342
+ :Bcountry => "CA",
343
+ :Phone => "416-333-3333",
344
+ :Fax => "416-322-2222",
345
+ :Sname => "Bob Parsons",
346
+ :Scompany => "Bob Parson's Co.",
347
+ :Saddress1 => "1234 West Street",
348
+ :Saddress2 => "Apt 100",
349
+ :Scity => "Toronto",
350
+ :Sprovince => "Ontario",
351
+ :Spostalcode => "L3N9J2",
352
+ :Scountry => "CA"
353
+ }
354
+ @expectation.merge!(valid_credit_card_hash)
355
+
356
+ @order = Order.new
357
+ @order.amount = 10.00
358
+ @order.email = "bob@gmail.com"
359
+ @order.action = :sale
360
+ @order.billing = valid_address
361
+ @order.shipping = valid_address
362
+ @order.cc = valid_credit_card
363
+
364
+ assert_equal @expectation, @order.to_hash(@order.action)
365
+ end
366
+
367
+ should "return a hash for :sale with item and option details" do
368
+ @expectation = {
369
+ :Email => "bob@gmail.com",
370
+ :CardAction => 0,
371
+ :Subtotal => 10.00,
372
+ :Item => [ {
373
+ :ItemID => "PSI-BOOK",
374
+ :ItemDescription => "XML Interface Doc",
375
+ :ItemQty => 2,
376
+ :ItemPrice => 10.00,
377
+ :Option => { :size => "large", :color => "pink" }
378
+ }, {
379
+ :ItemID => "COUPON",
380
+ :ItemDescription => "10% discount",
381
+ :ItemQty => 1,
382
+ :ItemPrice => -2.00
383
+ }
384
+ ]
385
+ }
386
+ @expectation.merge!(valid_credit_card_hash)
387
+
388
+ @order = Order.new
389
+ @order.amount = 10.00
390
+ @order.email = "bob@gmail.com"
391
+ @order.action = :sale
392
+ @order.cc = valid_credit_card
393
+
394
+ @item1 = valid_item
395
+ @item1 << valid_option(:size => "large")
396
+ @item1 << valid_option(:color => "pink")
397
+ @item2 = valid_coupon
398
+
399
+ @order << @item1
400
+ @order << @item2
401
+
402
+ assert_equal @expectation, @order.to_hash(@order.action)
403
+ end
404
+
405
+ should "return a hash for :preauth" do
406
+ @expectation = {
407
+ :Email => "bob@gmail.com",
408
+ :CardAction => 1,
409
+ :Subtotal => 10.00,
410
+ }
411
+ @expectation.merge!(valid_credit_card_hash)
412
+
413
+ @order = Order.new
414
+ @order.amount = 10.00
415
+ @order.email = "bob@gmail.com"
416
+ @order.action = :preauth
417
+ @order.cc = valid_credit_card
418
+
419
+ assert_equal @expectation, @order.to_hash(@order.action)
420
+ end
421
+
422
+ should "return a hash for :postauth" do
423
+ @expectation = {
424
+ :Email => "bob@gmail.com",
425
+ :CardAction => 2,
426
+ :Subtotal => 10.00,
427
+ :OrderID => "200409191133332929"
428
+ }
429
+
430
+ @order = Order.new
431
+ @order.amount = 10.00
432
+ @order.email = "bob@gmail.com"
433
+ @order.order_id = "200409191133332929"
434
+ @order.action = :postauth
435
+
436
+ assert_equal @expectation, @order.to_hash(@order.action)
437
+ end
438
+
439
+ should "return a hash for :credit" do
440
+ @expectation = {
441
+ :Email => "bob@gmail.com",
442
+ :CardAction => 3,
443
+ :Subtotal => 10.00,
444
+ :OrderID => "200409191133332929"
445
+ }
446
+
447
+ @order = Order.new
448
+ @order.amount = 10.00
449
+ @order.email = "bob@gmail.com"
450
+ @order.order_id = "200409191133332929"
451
+ @order.action = :credit
452
+
453
+ assert_equal @expectation, @order.to_hash(@order.action)
454
+ end
455
+
456
+ should "return a hash for :force_postauth" do
457
+ @expectation = {
458
+ :Email => "bob@gmail.com",
459
+ :CardAction => 4,
460
+ :Subtotal => 10.00,
461
+ :CardAuthNumber => "102145"
462
+ }
463
+ @expectation.merge!(valid_credit_card_hash)
464
+
465
+ @order = Order.new
466
+ @order.amount = 10.00
467
+ @order.email = "bob@gmail.com"
468
+ @order.action = :force_postauth
469
+ @order.cc = valid_credit_card
470
+ @order.card_auth_number = "102145"
471
+
472
+ assert_equal @expectation, @order.to_hash(@order.action)
473
+ end
474
+
475
+ should "return a hash for :void" do
476
+ @expectation = {
477
+ :CardAction => 9,
478
+ :OrderID => "200409191133332929",
479
+ :TransRefNumber => "07abef21992"
480
+ }
481
+
482
+ @order = Order.new
483
+ @order.action = :void
484
+ @order.order_id = "200409191133332929"
485
+ @order.trans_ref_number = "07abef21992"
486
+
487
+ assert_equal @expectation, @order.to_hash(@order.action)
488
+ end
489
+
490
+ end
491
+ end
@@ -0,0 +1,89 @@
1
+ require 'test_helper'
2
+
3
+ module RubyPsigate
4
+ class RecurringChargeTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @recurring_charge = RecurringCharge.new
8
+ end
9
+
10
+ %w( rbcid rbname interval rbtrigger status starttime endtime processtype ).each do |attribute|
11
+ should "have getter/setter for #{attribute}" do
12
+ getter = "#{attribute}".to_sym
13
+ setter = "#{attribute}=".to_sym
14
+ assert @recurring_charge.respond_to?(getter)
15
+ assert @recurring_charge.respond_to?(setter)
16
+ end
17
+ end
18
+
19
+ should "respond to items" do
20
+ assert @recurring_charge.respond_to?(:items)
21
+ end
22
+
23
+ should "add options via << method" do
24
+ assert_equal 0, @recurring_charge.items.count
25
+ @recurring_charge << valid_recurring_item
26
+ assert_equal [valid_recurring_item_hash], @recurring_charge.items
27
+ end
28
+
29
+ should "raise an error unless added option is a ItemOption class" do
30
+ assert_raises(InvalidRecurringItem) do
31
+ @recurring_charge << "Something Here"
32
+ end
33
+ end
34
+
35
+ should "return a hash" do
36
+ @expectation = {
37
+ :RBCID => "9999999999",
38
+ :RBName => "Charge123",
39
+ :Interval => "M",
40
+ :RBTrigger => "25",
41
+ :ProcessType => "A",
42
+ :Status => "Active",
43
+ :StartTime => "2010.08.01",
44
+ :EndTime => "2015.12.31"
45
+ }
46
+ @recurring_charge = valid_recurring_charge
47
+ assert_equal @expectation, @recurring_charge.to_hash
48
+ end
49
+
50
+ should "trim the hash of nil value attributes" do
51
+ @expectation = {
52
+ :RBCID => "9999999999",
53
+ :RBName => "Charge123",
54
+ :Interval => "M",
55
+ :RBTrigger => "25",
56
+ :StartTime => "2010.08.01",
57
+ :EndTime => "2015.12.31"
58
+ }
59
+ @recurring_charge = valid_recurring_charge
60
+ @recurring_charge.processtype = nil
61
+ @recurring_charge.status = nil
62
+
63
+ assert_equal @expectation, @recurring_charge.to_hash
64
+ end
65
+
66
+ should "return a hash with items" do
67
+ @expectation = {
68
+ :RBCID => "9999999999",
69
+ :RBName => "Charge123",
70
+ :Interval => "M",
71
+ :RBTrigger => "25",
72
+ :ProcessType => "A",
73
+ :StartTime => "2010.08.01",
74
+ :EndTime => "2015.12.31"
75
+ }
76
+ @expectation[:ItemInfo] = Array.new
77
+ @expectation[:ItemInfo] << valid_recurring_item_hash
78
+ @expectation[:ItemInfo] << valid_recurring_item_hash
79
+
80
+ @recurring_charge = valid_recurring_charge
81
+ @recurring_charge.status = nil
82
+ @recurring_charge << valid_recurring_item
83
+ @recurring_charge << valid_recurring_item
84
+
85
+ assert_equal @expectation, @recurring_charge.to_hash
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ module RubyPsigate
4
+ class RecurringItemTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @recurring_item = RecurringItem.new
8
+ end
9
+
10
+ %w( product_id description quantity price tax1 tax2 cost ).each do |attribute|
11
+ should "have getter/setter for #{attribute}" do
12
+ getter = attribute.to_sym
13
+ setter = "#{attribute}=".to_sym
14
+ assert @recurring_item.respond_to?(getter)
15
+ assert @recurring_item.respond_to?(setter)
16
+ end
17
+ end
18
+
19
+ should "return a hash" do
20
+ @expectation = {
21
+ :ProductID => "Newspaper",
22
+ :Description => "Toronto Star",
23
+ :Quantity => 1,
24
+ :Price => "25.00",
25
+ :Tax1 => "2.00",
26
+ :Tax2 => "1.25"
27
+ }
28
+
29
+ @recurring_item = RecurringItem.new(
30
+ :product_id => "Newspaper",
31
+ :description => "Toronto Star",
32
+ :quantity => 1,
33
+ :price => "25.00",
34
+ :tax1 => "2.00",
35
+ :tax2 => "1.25"
36
+ )
37
+
38
+ assert_equal @expectation, @recurring_item.to_hash
39
+ end
40
+
41
+ should "return a hash without nil values" do
42
+ @expectation = {
43
+ :ProductID => "Newspaper",
44
+ :Description => "Toronto Star",
45
+ :Quantity => "1",
46
+ :Price => "25.00"
47
+ }
48
+
49
+ @recurring_item = RecurringItem.new(
50
+ :product_id => "Newspaper",
51
+ :description => "Toronto Star",
52
+ :quantity => "1",
53
+ :price => "25.00",
54
+ :tax1 => nil,
55
+ :tax2 => nil
56
+ )
57
+
58
+ assert_equal @expectation, @recurring_item.to_hash
59
+ end
60
+
61
+ end
62
+ end