-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathcyber_source_test.rb
2469 lines (2177 loc) · 148 KB
/
cyber_source_test.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'test_helper'
require 'nokogiri'
class CyberSourceTest < Test::Unit::TestCase
include CommStub
def setup
Base.mode = :test
@gateway = CyberSourceGateway.new(
login: 'l',
password: 'p'
)
@amount = 100
@customer_ip = '127.0.0.1'
@credit_card = credit_card('4111111111111111', brand: 'visa')
@master_credit_card = credit_card('4111111111111111', brand: 'master')
@elo_credit_card = credit_card('5067310000000010', brand: 'elo')
@declined_card = credit_card('801111111111111', brand: 'visa')
@carnet_card = credit_card('5062280000000000', brand: 'carnet')
@network_token = network_tokenization_credit_card('4111111111111111',
brand: 'visa',
transaction_id: '123',
eci: '05',
payment_cryptogram: '111111111100cryptogram',
source: :network_token)
@network_token_mastercard = network_tokenization_credit_card('5555555555554444',
brand: 'master',
transaction_id: '123',
eci: '05',
source: :network_token,
payment_cryptogram: '111111111100cryptogram')
@amex_network_token = network_tokenization_credit_card('378282246310005',
brand: 'american_express',
eci: '05',
payment_cryptogram: '111111111100cryptogram',
source: :network_token)
@apple_pay = network_tokenization_credit_card('4111111111111111',
brand: 'visa',
transaction_id: '123',
eci: '05',
payment_cryptogram: '111111111100cryptogram',
source: :apple_pay)
@apple_pay_discover = network_tokenization_credit_card('6011111111111117',
brand: 'discover',
transaction_id: '123',
eci: '05',
payment_cryptogram: '111111111100cryptogram',
source: :apple_pay)
@apple_pay_master = network_tokenization_credit_card('6011111111111117',
brand: 'master',
transaction_id: '123',
eci: '05',
payment_cryptogram: '111111111100cryptogram',
source: :apple_pay)
@google_pay = network_tokenization_credit_card('4242424242424242', source: :google_pay)
@check = check()
@options = {
ip: @customer_ip,
order_id: '1000',
line_items: [
{
declared_value: @amount,
quantity: 2,
code: 'default',
description: 'Giant Walrus',
sku: 'WA323232323232323',
tax_amount: '10',
national_tax: '5'
}
],
currency: 'USD',
reconciliation_id: '181537'
}
@subscription_options = {
order_id: generate_unique_id,
credit_card: @credit_card,
setup_fee: 100,
subscription: {
frequency: 'weekly',
start_date: Date.today.next_week,
occurrences: 4,
automatic_renew: true,
amount: 100
}
}
@issuer_additional_data = 'PR25000000000011111111111112222222sk111111111111111111111111111'
+ '1111111115555555222233101abcdefghijkl7777777777777777777777777promotionCde'
end
def test_supported_card_types
assert_equal CyberSourceGateway.supported_cardtypes, %i[visa master american_express discover diners_club jcb dankort maestro elo patagonia_365]
end
def test_successful_credit_card_purchase
@gateway.expects(:ssl_post).returns(successful_purchase_response)
assert response = @gateway.purchase(@amount, @credit_card, @options)
assert_equal 'Successful transaction', response.message
assert_success response
assert_equal "#{@options[:order_id]};#{response.params['requestID']};#{response.params['requestToken']};purchase;100;USD;;credit_card", response.authorization
assert response.test?
end
def test_successful_purchase_with_other_tax_fields
stub_comms do
@gateway.purchase(100, @credit_card, @options.merge!(national_tax_indicator: 1, vat_tax_rate: 1.01, merchant_id: 'MerchantId'))
end.check_request do |_endpoint, data, _headers|
assert_match(/<merchantID>MerchantId<\/merchantID>/, data)
assert_match(/<otherTax>\s+<vatTaxRate>1.01<\/vatTaxRate>\s+<nationalTaxIndicator>1<\/nationalTaxIndicator>\s+<\/otherTax>/m, data)
end.respond_with(successful_purchase_response)
end
def test_successful_purchase_with_merchant_category_code
stub_comms do
@gateway.purchase(100, @credit_card, @options.merge!(merchant_category_code: '1111'))
end.check_request do |_endpoint, data, _headers|
assert_match(/<merchantCategoryCode>1111<\/merchantCategoryCode>/, data)
end.respond_with(successful_purchase_response)
end
def test_successful_purchase_with_purchase_totals_data
stub_comms do
@gateway.purchase(100, @credit_card, @options.merge(discount_management_indicator: 'T', purchase_tax_amount: 7.89, original_amount: 1.23, invoice_amount: 1.23))
end.check_request do |_endpoint, data, _headers|
assert_match(/<purchaseTotals>\s+<currency>USD<\/currency>\s+<discountManagementIndicator>T<\/discountManagementIndicator>\s+<taxAmount>7.89<\/taxAmount>\s+<grandTotalAmount>1.00<\/grandTotalAmount>\s+<originalAmount>1.23<\/originalAmount>\s+<invoiceAmount>1.23<\/invoiceAmount>\s+<\/purchaseTotals>/m, data)
end.respond_with(successful_purchase_response)
end
def test_successful_authorize_with_national_tax_indicator
national_tax_indicator = 1
stub_comms do
@gateway.authorize(100, @credit_card, @options.merge(national_tax_indicator:))
end.check_request do |_endpoint, data, _headers|
assert_match(/<otherTax>\s+<nationalTaxIndicator>#{national_tax_indicator}<\/nationalTaxIndicator>\s+<\/otherTax>/m, data)
end.respond_with(successful_authorization_response)
end
def test_successful_authorize_with_merchant_category_code
stub_comms do
@gateway.authorize(100, @credit_card, @options.merge(merchant_category_code: '1111'))
end.check_request do |_endpoint, data, _headers|
assert_match(/<merchantCategoryCode>1111<\/merchantCategoryCode>/, data)
end.respond_with(successful_authorization_response)
end
def test_successful_authorize_with_cc_auth_service_fields
stub_comms do
@gateway.authorize(100, @credit_card, @options.merge(mobile_remote_payment_type: 'T'))
end.check_request do |_endpoint, data, _headers|
assert_match(/<mobileRemotePaymentType>T<\/mobileRemotePaymentType>/, data)
end.respond_with(successful_authorization_response)
end
def test_successful_authorize_with_cc_auth_service_first_recurring_payment
stub_comms do
@gateway.authorize(100, @credit_card, @options.merge(first_recurring_payment: true))
end.check_request do |_endpoint, data, _headers|
assert_match(/<firstRecurringPayment>true<\/firstRecurringPayment>/, data)
end.respond_with(successful_authorization_response)
end
def test_successful_authorize_with_cc_auth_service_aggregator_id
stub_comms do
@gateway.authorize(100, @credit_card, @options.merge(aggregator_id: 'ABCDE'))
end.check_request do |_endpoint, data, _headers|
assert_match(/<aggregatorID>ABCDE<\/aggregatorID>/, data)
end.respond_with(successful_authorization_response)
end
def test_successful_credit_card_purchase_with_elo
@gateway.expects(:ssl_post).returns(successful_purchase_response)
assert response = @gateway.purchase(@amount, @elo_credit_card, @options)
assert_equal 'Successful transaction', response.message
assert_success response
assert_equal "#{@options[:order_id]};#{response.params['requestID']};#{response.params['requestToken']};purchase;100;USD;;credit_card", response.authorization
assert response.test?
end
def test_purchase_includes_customer_ip
customer_ip_regexp = /<ipAddress>#{@customer_ip}<\//
@gateway.expects(:ssl_post).
with(anything, regexp_matches(customer_ip_regexp), anything).
returns('')
@gateway.expects(:parse).returns({})
@gateway.purchase(@amount, @credit_card, @options)
end
def test_purchase_includes_issuer_additional_data
stub_comms do
@gateway.purchase(100, @credit_card, order_id: '1', issuer_additional_data: @issuer_additional_data)
end.check_request do |_endpoint, data, _headers|
assert_match(/<issuer>\s+<additionalData>#{@issuer_additional_data}<\/additionalData>\s+<\/issuer>/m, data)
end.respond_with(successful_purchase_response)
end
def test_purchase_includes_mdd_fields
stub_comms do
@gateway.purchase(100, @credit_card, order_id: '1', mdd_field_2: 'CustomValue2', mdd_field_3: 'CustomValue3')
end.check_request do |_endpoint, data, _headers|
assert_match(/<mddField id=\"2\">CustomValue2</m, data)
end.respond_with(successful_purchase_response)
end
def test_purchase_includes_reconciliation_id
stub_comms do
@gateway.purchase(100, @credit_card, @options.merge(order_id: '1'))
end.check_request do |_endpoint, data, _headers|
assert_match(/<reconciliationID>181537<\/reconciliationID>/, data)
end.respond_with(successful_purchase_response)
end
def test_merchant_description
stub_comms do
@gateway.authorize(100, @credit_card, merchant_descriptor_name: 'Test Name', merchant_descriptor_address1: '123 Main Dr', merchant_descriptor_locality: 'Durham')
end.check_request do |_endpoint, data, _headers|
assert_match(%r(<merchantDescriptor>.*<name>Test Name</name>.*</merchantDescriptor>)m, data)
assert_match(%r(<merchantDescriptor>.*<address1>123 Main Dr</address1>.*</merchantDescriptor>)m, data)
assert_match(%r(<merchantDescriptor>.*<locality>Durham</locality>.*</merchantDescriptor>)m, data)
end.respond_with(successful_purchase_response)
end
def test_allows_nil_values_in_billing_address
billing_address = {
address1: '123 Fourth St',
city: 'Fiveton',
state: '',
country: 'CA'
}
stub_comms do
@gateway.authorize(100, @credit_card, billing_address:)
end.check_request do |_endpoint, data, _headers|
assert_nil billing_address[:zip]
assert_nil billing_address[:phone]
assert_match(%r(<billTo>.*<street1>123 Fourth St</street1>.*</billTo>)m, data)
assert_match(%r(<billTo>.*<city>Fiveton</city>.*</billTo>)m, data)
assert_match(%r(<billTo>.*<state>NC</state>.*</billTo>)m, data)
assert_match(%r(<billTo>.*<postalCode>00000</postalCode>.*</billTo>)m, data)
assert_match(%r(<billTo>.*<country>CA</country>.*</billTo>)m, data)
end.respond_with(successful_purchase_response)
end
def test_uses_names_from_billing_address_if_present
name = 'Wesley Crusher'
stub_comms do
@gateway.authorize(100, @credit_card, billing_address: { name: })
end.check_request do |_endpoint, data, _headers|
assert_match(%r(<billTo>.*<firstName>Wesley</firstName>.*</billTo>)m, data)
assert_match(%r(<billTo>.*<lastName>Crusher</lastName>.*</billTo>)m, data)
end.respond_with(successful_purchase_response)
end
def test_uses_names_from_shipping_address_if_present
name = 'Wesley Crusher'
stub_comms do
@gateway.authorize(100, @credit_card, shipping_address: { name: })
end.check_request do |_endpoint, data, _headers|
assert_match(%r(<shipTo>.*<firstName>Wesley</firstName>.*</shipTo>)m, data)
assert_match(%r(<shipTo>.*<lastName>Crusher</lastName>.*</shipTo>)m, data)
end.respond_with(successful_purchase_response)
end
def test_uses_names_from_the_payment_method
stub_comms do
@gateway.authorize(100, @credit_card)
end.check_request do |_endpoint, data, _headers|
assert_match(%r(<shipTo>.*<firstName>#{@credit_card.first_name}</firstName>.*</shipTo>)m, data)
assert_match(%r(<shipTo>.*<lastName>#{@credit_card.last_name}</lastName>.*</shipTo>)m, data)
assert_match(%r(<billTo>.*<firstName>#{@credit_card.first_name}</firstName>.*</billTo>)m, data)
assert_match(%r(<billTo>.*<lastName>#{@credit_card.last_name}</lastName>.*</billTo>)m, data)
end.respond_with(successful_purchase_response)
end
def test_purchase_includes_invoice_header
stub_comms do
@gateway.purchase(100, @credit_card, merchant_descriptor: 'Spreedly', reference_data_code: '3A', invoice_number: '1234567', merchant_descriptor_city: 'test123', submerchant_id: 'AVSBSGDHJMNGFR', merchant_descriptor_country: 'US', merchant_descriptor_state: 'NY')
end.check_request do |_endpoint, data, _headers|
assert_match(/<merchantDescriptor>Spreedly<\/merchantDescriptor>/, data)
assert_match(/<referenceDataCode>3A<\/referenceDataCode>/, data)
assert_match(/<invoiceNumber>1234567<\/invoiceNumber>/, data)
assert_match(/<merchantDescriptorCity>test123<\/merchantDescriptorCity>/, data)
assert_match(/<submerchantID>AVSBSGDHJMNGFR<\/submerchantID>/, data)
assert_match(/<merchantDescriptorCountry>US<\/merchantDescriptorCountry>/, data)
assert_match(/<merchantDescriptorState>NY<\/merchantDescriptorState>/, data)
end.respond_with(successful_purchase_response)
end
def test_purchase_with_apple_pay_includes_payment_solution_001
stub_comms do
@gateway.purchase(100, @apple_pay)
end.check_request do |_endpoint, data, _headers|
assert_match(/<paymentSolution>001<\/paymentSolution>/, data)
end.respond_with(successful_purchase_response)
end
def test_purchase_with_google_pay_includes_payment_solution_012
stub_comms do
@gateway.purchase(100, @google_pay)
end.check_request do |_endpoint, data, _headers|
assert_match(/<paymentSolution>012<\/paymentSolution>/, data)
end.respond_with(successful_purchase_response)
end
def test_purchase_includes_tax_management_indicator
stub_comms do
@gateway.purchase(100, @credit_card, tax_management_indicator: 3)
end.check_request do |_endpoint, data, _headers|
assert_match(/<taxManagementIndicator>3<\/taxManagementIndicator>/, data)
end.respond_with(successful_purchase_response)
end
def test_auth_includes_gratuity_amount
stub_comms do
@gateway.authorize(100, @credit_card, gratuity_amount: '7.50')
end.check_request do |_endpoint, data, _headers|
assert_match(/<gratuityAmount>7.50<\/gratuityAmount>/, data)
end.respond_with(successful_purchase_response)
end
def test_purchase_includes_gratuity_amount
stub_comms do
@gateway.purchase(100, @credit_card, gratuity_amount: '7.50')
end.check_request do |_endpoint, data, _headers|
assert_match(/<gratuityAmount>7.50<\/gratuityAmount>/, data)
end.respond_with(successful_purchase_response)
end
def test_authorize_includes_issuer_additional_data
stub_comms do
@gateway.authorize(100, @credit_card, order_id: '1', issuer_additional_data: @issuer_additional_data)
end.check_request do |_endpoint, data, _headers|
assert_match(/<issuer>\s+<additionalData>#{@issuer_additional_data}<\/additionalData>\s+<\/issuer>/m, data)
end.respond_with(successful_authorization_response)
end
def test_authorize_includes_mdd_fields
stub_comms do
@gateway.authorize(100, @credit_card, order_id: '1', mdd_field_2: 'CustomValue2', mdd_field_3: 'CustomValue3')
end.check_request do |_endpoint, data, _headers|
assert_match(/<mddField id=\"2\">CustomValue2</m, data)
end.respond_with(successful_authorization_response)
end
def test_authorize_includes_reconciliation_id
stub_comms do
@gateway.authorize(100, @credit_card, @options.merge(order_id: '1'))
end.check_request do |_endpoint, data, _headers|
assert_match(/<reconciliationID>181537<\/reconciliationID>/, data)
end.respond_with(successful_authorization_response)
end
def test_authorize_includes_commerce_indicator
stub_comms do
@gateway.authorize(100, @credit_card, commerce_indicator: 'internet')
end.check_request do |_endpoint, data, _headers|
assert_match(/<commerceIndicator>internet<\/commerceIndicator>/m, data)
end.respond_with(successful_authorization_response)
end
def test_authorize_includes_installment_data
stub_comms do
@gateway.authorize(100, @credit_card, order_id: '1', installment_total_count: 5, installment_plan_type: 1, first_installment_date: '300101', installment_total_amount: 5.05, installment_annual_interest_rate: 1.09, installment_grace_period_duration: 3)
end.check_request do |_endpoint, data, _headers|
assert_xml_valid_to_xsd(data)
assert_match(/<installment>\s+<totalCount>5<\/totalCount>\s+<totalAmount>5.05<\/totalAmount>\s+<planType>1<\/planType>\s+<firstInstallmentDate>300101<\/firstInstallmentDate>\s+<annualInterestRate>1.09<\/annualInterestRate>\s+<gracePeriodDuration>3<\/gracePeriodDuration>\s+<\/installment>/, data)
end.respond_with(successful_authorization_response)
end
def test_authorize_includes_less_installment_data
stub_comms do
@gateway.authorize(100, @credit_card, order_id: '1', installment_grace_period_duration: 3)
end.check_request do |_endpoint, data, _headers|
assert_xml_valid_to_xsd(data)
assert_match(/<installment>\s+<gracePeriodDuration>3<\/gracePeriodDuration>\s+<\/installment>/, data)
end.respond_with(successful_authorization_response)
end
def test_authorize_includes_customer_id
stub_comms do
@gateway.authorize(100, @credit_card, customer_id: '5afefb801188d70023b7debb')
end.check_request do |_endpoint, data, _headers|
assert_match(/<customerID>5afefb801188d70023b7debb<\/customerID>/, data)
end.respond_with(successful_authorization_response)
end
def test_authorize_with_apple_pay_includes_payment_solution_001
stub_comms do
@gateway.authorize(100, @apple_pay)
end.check_request do |_endpoint, data, _headers|
assert_match(/<paymentSolution>001<\/paymentSolution>/, data)
end.respond_with(successful_authorization_response)
end
def test_authorize_with_google_pay_includes_payment_solution_012
stub_comms do
@gateway.authorize(100, @google_pay)
end.check_request do |_endpoint, data, _headers|
assert_match(/<paymentSolution>012<\/paymentSolution>/, data)
end.respond_with(successful_authorization_response)
end
def test_authorize_includes_merchant_tax_id_in_billing_address_but_not_shipping_address
stub_comms do
@gateway.authorize(100, @credit_card, order_id: '1', merchant_tax_id: '123')
end.check_request do |_endpoint, data, _headers|
assert_match(%r(<billTo>.*<merchantTaxID>123</merchantTaxID>.*</billTo>)m, data)
assert_not_match(%r(<shipTo>.*<merchantTaxID>123</merchantTaxID>.*</shipTo>)m, data)
end.respond_with(successful_authorization_response)
end
def test_authorize_includes_sales_slip_number
stub_comms do
@gateway.authorize(100, @credit_card, order_id: '1', sales_slip_number: '123')
end.check_request do |_endpoint, data, _headers|
assert_match(/<salesSlipNumber>123<\/salesSlipNumber>/, data)
end.respond_with(successful_authorization_response)
end
def test_authorize_includes_airline_agent_code
stub_comms do
@gateway.authorize(100, @credit_card, order_id: '1', airline_agent_code: '7Q')
end.check_request do |_endpoint, data, _headers|
assert_match(/<airlineData>\s+<agentCode>7Q<\/agentCode>\s+<\/airlineData>/, data)
end.respond_with(successful_authorization_response)
end
def test_bank_account_purchase_includes_sec_code
stub_comms do
@gateway.purchase(@amount, @check, order_id: '1', sec_code: 'WEB')
end.check_request do |_endpoint, data, _headers|
assert_match(%r(<check>.*<secCode>WEB</secCode>.*</check>)m, data)
end.respond_with(successful_authorization_response)
end
def test_successful_check_purchase
@gateway.expects(:ssl_post).returns(successful_purchase_response)
assert response = @gateway.purchase(@amount, @check, @options)
assert_equal 'Successful transaction', response.message
assert_success response
assert_equal "#{@options[:order_id]};#{response.params['requestID']};#{response.params['requestToken']};purchase;100;USD;;check", response.authorization
assert response.test?
end
def test_successful_pinless_debit_card_purchase
@gateway.expects(:ssl_post).returns(successful_purchase_response)
assert response = @gateway.purchase(@amount, @credit_card, @options.merge(pinless_debit_card: true))
assert_equal 'Successful transaction', response.message
assert_success response
assert_equal "#{@options[:order_id]};#{response.params['requestID']};#{response.params['requestToken']};purchase;100;USD;;credit_card", response.authorization
assert response.test?
end
def test_successful_credit_cart_purchase_single_request_ignore_avs
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_match %r'<ignoreAVSResult>true</ignoreAVSResult>', request_body
assert_not_match %r'<ignoreCVResult>', request_body
true
end.returns(successful_purchase_response)
options = @options.merge(ignore_avs: true)
assert response = @gateway.purchase(@amount, @credit_card, options)
assert_success response
end
def test_successful_network_token_purchase_single_request_ignore_avs
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_match %r'<ignoreAVSResult>true</ignoreAVSResult>', request_body
assert_not_match %r'<ignoreCVResult>', request_body
true
end.returns(successful_purchase_response)
options = @options.merge(ignore_avs: true)
assert response = @gateway.purchase(@amount, @network_token, options)
assert_success response
end
def test_successful_credit_cart_purchase_single_request_without_ignore_avs
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_not_match %r'<ignoreAVSResult>', request_body
assert_not_match %r'<ignoreCVResult>', request_body
true
end.returns(successful_purchase_response)
# globally ignored AVS for gateway instance:
@gateway.options[:ignore_avs] = true
options = @options.merge(ignore_avs: false)
assert response = @gateway.purchase(@amount, @credit_card, options)
assert_success response
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_not_match %r'<ignoreAVSResult>', request_body
assert_not_match %r'<ignoreCVResult>', request_body
true
end.returns(successful_purchase_response)
options = @options.merge(ignore_avs: 'false')
assert response = @gateway.purchase(@amount, @credit_card, options)
assert_success response
end
def test_successful_credit_cart_purchase_single_request_ignore_ccv
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_not_match %r'<ignoreAVSResult>', request_body
assert_match %r'<ignoreCVResult>true</ignoreCVResult>', request_body
true
end.returns(successful_purchase_response)
assert response = @gateway.purchase(@amount, @credit_card, @options.merge(ignore_cvv: true))
assert_success response
end
def test_successful_network_token_purchase_single_request_ignore_cvv
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_not_match %r'<ignoreAVSResult>', request_body
assert_match %r'<ignoreCVResult>true</ignoreCVResult>', request_body
true
end.returns(successful_purchase_response)
options = @options.merge(ignore_cvv: true)
assert response = @gateway.purchase(@amount, @network_token, options)
assert_success response
end
def test_successful_credit_cart_purchase_single_request_without_ignore_ccv
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_not_match %r'<ignoreAVSResult>', request_body
assert_not_match %r'<ignoreCVResult>', request_body
true
end.returns(successful_purchase_response)
assert response = @gateway.purchase(@amount, @credit_card, @options.merge(ignore_cvv: false))
assert_success response
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_not_match %r'<ignoreAVSResult>', request_body
assert_not_match %r'<ignoreCVResult>', request_body
true
end.returns(successful_purchase_response)
assert response = @gateway.purchase(@amount, @credit_card, @options.merge(ignore_cvv: 'false'))
assert_success response
end
def test_successful_apple_pay_purchase_subsequent_auth_visa
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_not_match %r'<cavv>', request_body
assert_not_match %r'<xid>', request_body
assert_match %r'<commerceIndicator>internet</commerceIndicator>', request_body
true
end.returns(successful_purchase_response)
options = @options.merge({
stored_credential: {
initiator: 'merchant',
reason_type: 'unscheduled',
network_transaction_id: '016150703802094'
}
})
assert response = @gateway.purchase(@amount, @apple_pay, options)
assert_success response
end
def test_successful_apple_pay_purchase_subsequent_auth_mastercard
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_not_match %r'<authenticationData>', request_body
assert_match %r'<commerceIndicator>internet</commerceIndicator>', request_body
true
end.returns(successful_purchase_response)
credit_card = network_tokenization_credit_card('5555555555554444',
brand: 'master',
transaction_id: '123',
eci: '05',
payment_cryptogram: '111111111100cryptogram',
source: :apple_pay)
options = @options.merge({
stored_credential: {
initiator: 'merchant',
reason_type: 'unscheduled',
network_transaction_id: '016150703802094'
}
})
assert response = @gateway.purchase(@amount, credit_card, options)
assert_success response
end
def test_successful_apple_pay_purchase_subsequent_auth_discover
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_match %r'<cavv>', request_body
assert_match %r'<commerceIndicator>dipb</commerceIndicator>', request_body
true
end.returns(successful_purchase_response)
options = @options.merge(enable_cybs_discover_apple_pay: true)
assert response = @gateway.purchase(@amount, @apple_pay_discover, options)
assert_success response
end
def test_successful_apple_pay_purchase_with_master
@gateway.expects(:ssl_post).with do |_host, request_body|
assert_not_match %r'<cavv>', request_body
assert_not_match %r'<xid>', request_body
assert_match %r'<commerceIndicator>spa</commerceIndicator>', request_body
true
end.returns(successful_purchase_response)
assert response = @gateway.purchase(@amount, @apple_pay_master, @options)
assert_success response
end
def test_successful_reference_purchase
@gateway.stubs(:ssl_post).returns(successful_create_subscription_response, successful_purchase_response)
assert_success(response = @gateway.store(@credit_card, @subscription_options))
assert_success(@gateway.purchase(@amount, response.authorization, @options))
assert response.test?
end
def test_unsuccessful_authorization
@gateway.expects(:ssl_post).returns(unsuccessful_authorization_response)
assert response = @gateway.purchase(@amount, @credit_card, @options)
refute_equal 'Successful transaction', response.message
assert_instance_of Response, response
assert_failure response
end
def test_unsuccessful_authorization_with_reply
@gateway.expects(:ssl_post).returns(unsuccessful_authorization_response_with_reply)
assert response = @gateway.purchase(@amount, @credit_card, @options)
refute_equal 'Successful transaction', response.message
assert_equal '481', response.params['reasonCode']
assert_instance_of Response, response
assert_failure response
end
def test_successful_auth_request
@gateway.stubs(:ssl_post).returns(successful_authorization_response)
assert response = @gateway.authorize(@amount, @credit_card, @options)
assert_equal Response, response.class
assert response.success?
assert response.test?
end
def test_successful_reconciliation_id_2
@gateway.stubs(:ssl_post).returns(successful_purchase_and_capture_response)
assert response = @gateway.purchase(@amount, @credit_card, @options)
assert_equal response.params['reconciliationID'], 'abcdf'
assert_equal response.params['reconciliationID2'], '31159291T3XM2B13'
assert response.success?
assert response.test?
end
def test_successful_authorization_without_reconciliation_id_2
@gateway.stubs(:ssl_post).returns(successful_authorization_response)
assert response = @gateway.authorize(@amount, @credit_card, @options)
assert_equal response.params['reconciliationID2'], nil
assert_equal response.params['reconciliationID'], '23439130C40VZ2FB'
assert response.success?
assert response.test?
end
def test_successful_auth_with_elo_request
@gateway.stubs(:ssl_post).returns(successful_authorization_response)
assert response = @gateway.authorize(@amount, @elo_credit_card, @options)
assert_equal Response, response.class
assert response.success?
assert response.test?
end
def test_successful_credit_card_tax_request
@gateway.stubs(:ssl_post).returns(successful_tax_response)
assert response = @gateway.calculate_tax(@credit_card, @options)
assert_equal Response, response.class
assert response.success?
assert response.test?
end
def test_successful_credit_card_tax_request_with_amounts
stub_comms do
@gateway.calculate_tax(@credit_card, @options)
end.check_request do |_endpoint, data, _headers|
doc = REXML::Document.new(data)
REXML::XPath.each(doc, '//item') do |item|
request_item = @options[:line_items][item.attributes['id'].to_i]
assert_match(request_item[:tax_amount], item.get_elements('taxAmount')[0].text)
assert_match(request_item[:national_tax], item.get_elements('nationalTax')[0].text)
end
end.respond_with(successful_tax_response)
end
def test_successful_credit_card_authorize_request_with_line_items
stub_comms do
@gateway.authorize(@amount, @credit_card, @options)
end.check_request do |_endpoint, data, _headers|
doc = REXML::Document.new(data)
REXML::XPath.each(doc, '//item') do |item|
request_item = @options[:line_items][item.attributes['id'].to_i]
assert_match(request_item[:tax_amount], item.get_elements('taxAmount')[0].text)
assert_match(request_item[:national_tax], item.get_elements('nationalTax')[0].text)
end
end.respond_with(successful_tax_response)
end
def test_successful_credit_card_capture_request
@gateway.stubs(:ssl_post).returns(successful_authorization_response, successful_capture_response)
assert response = @gateway.authorize(@amount, @credit_card, @options)
assert response.success?
assert response.test?
assert response_capture = @gateway.capture(@amount, response.authorization)
assert response_capture.success?
assert response_capture.test?
end
def test_capture_includes_local_tax_amount
stub_comms do
@gateway.capture(100, '1842651133440156177166', local_tax_amount: '0.17')
end.check_request do |_endpoint, data, _headers|
assert_match(/<otherTax>\s+<localTaxAmount>0.17<\/localTaxAmount>\s+<\/otherTax>/, data)
end.respond_with(successful_capture_response)
end
def test_capture_includes_national_tax_amount
stub_comms do
@gateway.capture(100, '1842651133440156177166', national_tax_amount: '0.05')
end.check_request do |_endpoint, data, _headers|
assert_match(/<otherTax>\s+<nationalTaxAmount>0.05<\/nationalTaxAmount>\s+<\/otherTax>/, data)
end.respond_with(successful_capture_response)
end
def test_capture_with_additional_tax_fields
stub_comms do
@gateway.capture(100, '1842651133440156177166', user_po: 'ABC123', taxable: true, national_tax_indicator: 1)
end.check_request do |_endpoint, data, _headers|
assert_match(/<userPO>ABC123<\/userPO>/, data)
assert_match(/<taxable>true<\/taxable>/, data)
assert_match(/<nationalTaxIndicator>1<\/nationalTaxIndicator>/, data)
end.respond_with(successful_capture_response)
end
def test_capture_includes_gratuity_amount
stub_comms do
@gateway.capture(100, '1842651133440156177166', gratuity_amount: '3.05')
end.check_request do |_endpoint, data, _headers|
assert_match(/<gratuityAmount>3.05<\/gratuityAmount>/, data)
end.respond_with(successful_capture_response)
end
def test_successful_credit_card_capture_with_elo_request
@gateway.stubs(:ssl_post).returns(successful_authorization_response, successful_capture_response)
assert response = @gateway.authorize(@amount, @elo_credit_card, @options)
assert response.success?
assert response.test?
assert response_capture = @gateway.capture(@amount, response.authorization)
assert response_capture.success?
assert response_capture.test?
end
def test_capture_includes_mdd_fields
stub_comms do
@gateway.capture(100, '1846925324700976124593', order_id: '1', mdd_field_2: 'CustomValue2', mdd_field_3: 'CustomValue3')
end.check_request do |_endpoint, data, _headers|
assert_match(/<mddField id=\"2\">CustomValue2</m, data)
end.respond_with(successful_capture_response)
end
def test_successful_capture_with_merchant_category_code
stub_comms do
@gateway.capture(100, '1846925324700976124593', @options.merge!(merchant_category_code: '1111'))
end.check_request do |_endpoint, data, _headers|
assert_match(/<merchantCategoryCode>1111<\/merchantCategoryCode>/, data)
end.respond_with(successful_capture_response)
end
def test_successful_credit_card_purchase_request
@gateway.stubs(:ssl_post).returns(successful_capture_response)
assert response = @gateway.purchase(@amount, @credit_card, @options)
assert response.success?
assert response.test?
end
def test_successful_credit_card_purchase_request_with_line_items
stub_comms do
@gateway.purchase(@amount, @credit_card, @options)
end.check_request do |_endpoint, data, _headers|
doc = REXML::Document.new(data)
REXML::XPath.each(doc, '//item') do |item|
request_item = @options[:line_items][item.attributes['id'].to_i]
assert_match(request_item[:tax_amount], item.get_elements('taxAmount')[0].text)
assert_match(request_item[:national_tax], item.get_elements('nationalTax')[0].text)
end
end.respond_with(successful_purchase_response)
end
def test_successful_credit_card_purchase_with_elo_request
@gateway.stubs(:ssl_post).returns(successful_capture_response)
assert response = @gateway.purchase(@amount, @elo_credit_card, @options)
assert response.success?
assert response.test?
end
def test_successful_check_purchase_request
@gateway.stubs(:ssl_post).returns(successful_capture_response)
assert response = @gateway.purchase(@amount, @check, @options)
assert response.success?
assert response.test?
end
def test_requires_error_on_tax_calculation_without_line_items
assert_raise(ArgumentError) { @gateway.calculate_tax(@credit_card, @options.delete_if { |key, _val| key == :line_items }) }
end
def test_default_currency
assert_equal 'USD', CyberSourceGateway.default_currency
end
def test_successful_credit_card_store_request
@gateway.stubs(:ssl_post).returns(successful_create_subscription_response)
assert response = @gateway.store(@credit_card, @subscription_options)
assert response.success?
assert response.test?
end
def test_successful_credit_card_update_request
@gateway.stubs(:ssl_post).returns(successful_create_subscription_response, successful_update_subscription_response)
assert response = @gateway.store(@credit_card, @subscription_options)
assert response.success?
assert response.test?
assert response = @gateway.update(response.authorization, @credit_card, @subscription_options)
assert response.success?
assert response.test?
end
def test_successful_credit_card_unstore_request
@gateway.stubs(:ssl_post).returns(successful_create_subscription_response, successful_delete_subscription_response)
assert response = @gateway.store(@credit_card, @subscription_options)
assert response.success?
assert response.test?
assert response = @gateway.unstore(response.authorization, order_id: generate_unique_id)
assert response.success?
assert response.test?
end
def test_successful_credit_card_retrieve_request
@gateway.stubs(:ssl_post).returns(successful_create_subscription_response, successful_retrieve_subscription_response)
assert response = @gateway.store(@credit_card, @subscription_options)
assert response.success?
assert response.test?
assert response = @gateway.retrieve(response.authorization, order_id: generate_unique_id)
assert response.success?
assert response.test?
end
def test_avs_result
@gateway.expects(:ssl_post).returns(successful_purchase_response)
response = @gateway.purchase(@amount, @credit_card, @options)
assert_equal 'Y', response.avs_result['code']
end
def test_cvv_result
@gateway.expects(:ssl_post).returns(successful_purchase_response)
response = @gateway.purchase(@amount, @credit_card, @options)
assert_equal 'M', response.cvv_result['code']
end
def test_successful_refund_request
@gateway.stubs(:ssl_post).returns(successful_capture_response, successful_refund_response)
assert_success(response = @gateway.purchase(@amount, @credit_card, @options))
assert_success(@gateway.refund(@amount, response.authorization))
end
def test_successful_refund_with_elo_request
@gateway.stubs(:ssl_post).returns(successful_capture_response, successful_refund_response)
assert_success(response = @gateway.purchase(@amount, @elo_credit_card, @options))
assert_success(@gateway.refund(@amount, response.authorization))
end
def test_successful_refund_with_merchant_category_code
stub_comms do
@gateway.refund(100, 'test;12345', @options.merge!(merchant_category_code: '1111'))
end.check_request do |_endpoint, data, _headers|
assert_match(/<merchantCategoryCode>1111<\/merchantCategoryCode>/, data)
end.respond_with(successful_refund_response)
end
def test_successful_credit_to_card_request
@gateway.stubs(:ssl_post).returns(successful_card_credit_response)
assert_success(@gateway.credit(@amount, @credit_card, @options))
end
def test_successful_adjust_auth_request
@gateway.stubs(:ssl_post).returns(successful_incremental_auth_response)
assert_success(response = @gateway.authorize(@amount, @credit_card, @options))
assert_success(@gateway.adjust(@amount, response.authorization, @options))
end
def test_authorization_under_review_request
@gateway.stubs(:ssl_post).returns(authorization_review_response)
assert_failure(response = @gateway.authorize(@amount, @credit_card, @options))
assert response.fraud_review?
assert_equal(response.authorization, "#{@options[:order_id]};#{response.params['requestID']};#{response.params['requestToken']};authorize;100;USD;;")
end
def test_successful_credit_to_subscription_request
@gateway.stubs(:ssl_post).returns(successful_create_subscription_response, successful_subscription_credit_response)
assert response = @gateway.store(@credit_card, @subscription_options)
assert response.success?
assert response.test?
assert_success(@gateway.credit(@amount, response.authorization, @options))
end
def test_credit_includes_merchant_descriptor
stub_comms do
@gateway.credit(@amount, @credit_card, merchant_descriptor: 'Spreedly')
end.check_request do |_endpoint, data, _headers|
assert_match(/<merchantDescriptor>Spreedly<\/merchantDescriptor>/, data)
end.respond_with(successful_card_credit_response)
end
def test_credit_includes_issuer_additional_data
stub_comms do
@gateway.credit(@amount, @credit_card, issuer_additional_data: @issuer_additional_data)
end.check_request do |_endpoint, data, _headers|
assert_match(/<issuer>\s+<additionalData>#{@issuer_additional_data}<\/additionalData>\s+<\/issuer>/m, data)
end.respond_with(successful_card_credit_response)
end
def test_credit_includes_mdd_fields
stub_comms do
@gateway.credit(@amount, @credit_card, mdd_field_2: 'CustomValue2', mdd_field_3: 'CustomValue3')
end.check_request do |_endpoint, data, _headers|
assert_match(/<mddField id=\"2\">CustomValue2</m, data)
end.respond_with(successful_card_credit_response)
end
def test_successful_void_purchase_request
purchase = '1000;1842651133440156177166;AP4JY+Or4xRonEAOERAyMzQzOTEzMEM0MFZaNUZCBgDH3fgJ8AEGAMfd+AnwAwzRpAAA7RT/;purchase;100;USD;'
stub_comms do
@gateway.void(purchase, @options)
end.check_request do |_endpoint, data, _headers|
assert_match(%r(<voidService run=\"true\"), data)
end.respond_with(successful_void_response)
end
def test_successful_void_capture_request
capture = '1000;1842651133440156177166;AP4JY+Or4xRonEAOERAyMzQzOTEzMEM0MFZaNUZCBgDH3fgJ8AEGAMfd+AnwAwzRpAAA7RT/;capture;100;USD;'
stub_comms do
@gateway.void(capture, @options)
end.check_request do |_endpoint, data, _headers|
assert_match(%r(<voidService run=\"true\"), data)
end.respond_with(successful_void_response)
end
def test_successful_void_authorization_request
authorization = '1000;1842651133440156177166;AP4JY+Or4xRonEAOERAyMzQzOTEzMEM0MFZaNUZCBgDH3fgJ8AEGAMfd+AnwAwzRpAAA7RT/;authorize;100;USD;'
stub_comms do
@gateway.void(authorization, @options)
end.check_request do |_endpoint, data, _headers|
assert_match(%r(<ccAuthReversalService run=\"true\"), data)
end.respond_with(successful_auth_reversal_response)
end
def test_successful_void_with_issuer_additional_data
authorization = '1000;1842651133440156177166;AP4JY+Or4xRonEAOERAyMzQzOTEzMEM0MFZaNUZCBgDH3fgJ8AEGAMfd+AnwAwzRpAAA7RT/;authorize;100;USD;'
stub_comms do
@gateway.void(authorization, issuer_additional_data: @issuer_additional_data)
end.check_request do |_endpoint, data, _headers|
assert_match(/<issuer>\s+<additionalData>#{@issuer_additional_data}<\/additionalData>\s+<\/issuer>/m, data)
end.respond_with(successful_void_response)
end
def test_void_includes_mdd_fields
authorization = '1000;1842651133440156177166;AP4JY+Or4xRonEAOERAyMzQzOTEzMEM0MFZaNUZCBgDH3fgJ8AEGAMfd+AnwAwzRpAAA7RT/;authorize;100;USD;'
stub_comms do