/
remote_cyber_source_rest_test.rb
541 lines (458 loc) · 18.9 KB
/
remote_cyber_source_rest_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
require 'test_helper'
class RemoteCyberSourceRestTest < Test::Unit::TestCase
def setup
@gateway = CyberSourceRestGateway.new(fixtures(:cybersource_rest))
@amount = 10221
@card_without_funds = credit_card('42423482938483873')
@bank_account = check(account_number: '4100', routing_number: '121042882')
@declined_bank_account = check(account_number: '550111', routing_number: '121107882')
@visa_card = credit_card('4111111111111111', verification_value: '987', month: 12, year: 2031)
@master_card = credit_card('2222420000001113', brand: 'master')
@discover_card = credit_card('6011111111111117', brand: 'discover')
@apple_pay = network_tokenization_credit_card(
'4111111111111111',
payment_cryptogram: 'AceY+igABPs3jdwNaDg3MAACAAA=',
month: '11',
year: Time.now.year + 1,
source: :apple_pay,
verification_value: 569
)
@google_pay = network_tokenization_credit_card(
'4111111111111111',
payment_cryptogram: 'YwAAAAAABaYcCMX/OhNRQAAAAAA=',
month: '11',
year: Time.now.year + 1,
source: :google_pay,
verification_value: 569
)
@google_pay_master = network_tokenization_credit_card(
'5555555555554444',
payment_cryptogram: 'YwAAAAAABaYcCMX/OhNRQAAAAAA=',
month: '11',
year: Time.now.year + 1,
source: :google_pay,
verification_value: 569,
brand: 'master'
)
@apple_pay_jcb = network_tokenization_credit_card(
'3566111111111113',
payment_cryptogram: 'YwAAAAAABaYcCMX/OhNRQAAAAAA=',
month: '11',
year: Time.now.year + 1,
source: :apple_pay,
verification_value: 569,
brand: 'jcb'
)
@apple_pay_american_express = network_tokenization_credit_card(
'378282246310005',
payment_cryptogram: 'YwAAAAAABaYcCMX/OhNRQAAAAAA=',
month: '11',
year: Time.now.year + 1,
source: :apple_pay,
verification_value: 569,
brand: 'american_express'
)
@google_pay_discover = network_tokenization_credit_card(
'6011111111111117',
payment_cryptogram: 'YwAAAAAABaYcCMX/OhNRQAAAAAA=',
month: '11',
year: Time.now.year + 1,
source: :google_pay,
verification_value: 569,
brand: 'discover'
)
@billing_address = {
name: 'John Doe',
address1: '1 Market St',
city: 'san francisco',
state: 'CA',
zip: '94105',
country: 'US',
phone: '4158880000'
}
@options = {
order_id: generate_unique_id,
currency: 'USD',
email: 'test@cybs.com',
billing_address: {
name: 'John Doe',
address1: '1 Market St',
city: 'san francisco',
state: 'CA',
zip: '94105',
country: 'US',
phone: '4158880000'
}
}
end
def test_handle_credentials_error
gateway = CyberSourceRestGateway.new({ merchant_id: 'abc123', public_key: 'abc456', private_key: 'def789' })
response = gateway.authorize(@amount, @visa_card, @options)
assert_equal('Authentication Failed', response.message)
end
def test_successful_authorize
response = @gateway.authorize(@amount, @visa_card, @options)
assert_success response
assert response.test?
assert_equal 'AUTHORIZED', response.message
refute_empty response.params['_links']['capture']
end
def test_successful_authorize_with_billing_address
@options[:billing_address] = @billing_address
response = @gateway.authorize(@amount, @visa_card, @options)
assert_success response
assert response.test?
assert_equal 'AUTHORIZED', response.message
refute_empty response.params['_links']['capture']
end
def test_failure_authorize_with_declined_credit_card
response = @gateway.authorize(@amount, @card_without_funds, @options)
assert_failure response
assert_match %r{Invalid account}, response.message
assert_equal 'INVALID_ACCOUNT', response.error_code
end
def test_successful_capture
authorize = @gateway.authorize(@amount, @visa_card, @options)
response = @gateway.capture(@amount, authorize.authorization, @options)
assert_success response
assert_equal 'PENDING', response.message
end
def test_successful_capture_with_partial_amount
authorize = @gateway.authorize(@amount, @visa_card, @options)
response = @gateway.capture(@amount - 10, authorize.authorization, @options)
assert_success response
assert_equal 'PENDING', response.message
end
def test_failure_capture_with_higher_amount
authorize = @gateway.authorize(@amount, @visa_card, @options)
response = @gateway.capture(@amount + 10, authorize.authorization, @options)
assert_failure response
assert_match(/exceeds/, response.params['message'])
end
def test_successful_purchase
response = @gateway.purchase(@amount, @visa_card, @options)
assert_success response
assert response.test?
assert_equal 'AUTHORIZED', response.message
assert_nil response.params['_links']['capture']
end
def test_successful_purchase_with_credit_card_ignore_avs
@options[:ignore_avs] = 'true'
response = @gateway.purchase(@amount, @visa_card, @options)
assert_success response
assert response.test?
assert_equal 'AUTHORIZED', response.message
assert_nil response.params['_links']['capture']
end
def test_successful_purchase_with_network_token_ignore_avs
@options[:ignore_avs] = 'true'
response = @gateway.purchase(@amount, @apple_pay, @options)
assert_success response
assert response.test?
assert_equal 'AUTHORIZED', response.message
assert_nil response.params['_links']['capture']
end
def test_successful_purchase_with_credit_card_ignore_cvv
@options[:ignore_cvv] = 'true'
response = @gateway.purchase(@amount, @visa_card, @options)
assert_success response
assert response.test?
assert_equal 'AUTHORIZED', response.message
assert_nil response.params['_links']['capture']
end
def test_successful_purchase_with_network_token_ignore_cvv
@options[:ignore_cvv] = 'true'
response = @gateway.purchase(@amount, @apple_pay, @options)
assert_success response
assert response.test?
assert_equal 'AUTHORIZED', response.message
assert_nil response.params['_links']['capture']
end
def test_successful_refund
purchase = @gateway.purchase(@amount, @visa_card, @options)
response = @gateway.refund(@amount, purchase.authorization, @options)
assert_success response
assert response.test?
assert_equal 'PENDING', response.message
assert response.params['id'].present?
assert response.params['_links']['void'].present?
end
def test_failure_refund
purchase = @gateway.purchase(@amount, @card_without_funds, @options)
response = @gateway.refund(@amount, purchase.authorization, @options)
assert_failure response
assert response.test?
assert_match %r{Declined - One or more fields in the request contains invalid data}, response.params['message']
assert_equal 'INVALID_DATA', response.params['reason']
end
def test_successful_partial_refund
purchase = @gateway.purchase(@amount, @visa_card, @options)
response = @gateway.refund(@amount / 2, purchase.authorization, @options)
assert_success response
assert response.test?
assert_equal 'PENDING', response.message
assert response.params['id'].present?
assert response.params['_links']['void'].present?
end
def test_successful_repeat_refund_transaction
purchase = @gateway.purchase(@amount, @visa_card, @options)
response1 = @gateway.refund(@amount, purchase.authorization, @options)
assert_success response1
assert response1.test?
assert_equal 'PENDING', response1.message
assert response1.params['id'].present?
assert response1.params['_links']['void']
response2 = @gateway.refund(@amount, purchase.authorization, @options)
assert_success response2
assert response2.test?
assert_equal 'PENDING', response2.message
assert response2.params['id'].present?
assert response2.params['_links']['void']
assert_not_equal response1.params['_links']['void'], response2.params['_links']['void']
end
def test_successful_credit
response = @gateway.credit(@amount, @visa_card, @options)
assert_success response
assert response.test?
assert_equal 'PENDING', response.message
assert response.params['id'].present?
assert_nil response.params['_links']['capture']
end
def test_failure_credit
response = @gateway.credit(@amount, @card_without_funds, @options)
assert_failure response
assert response.test?
assert_match %r{Decline - Invalid account number}, response.message
assert_equal 'INVALID_ACCOUNT', response.error_code
end
def test_successful_void
authorize = @gateway.authorize(@amount, @visa_card, @options)
response = @gateway.void(authorize.authorization, @options)
assert_success response
assert response.params['id'].present?
assert_equal 'REVERSED', response.message
assert_nil response.params['_links']['capture']
end
def test_failure_void_using_card_without_funds
authorize = @gateway.authorize(@amount, @card_without_funds, @options)
response = @gateway.void(authorize.authorization, @options)
assert_failure response
assert_match %r{Declined - The request is missing one or more fields}, response.params['message']
assert_equal 'INVALID_REQUEST', response.params['status']
end
def test_successful_verify
response = @gateway.verify(@visa_card, @options)
assert_success response
assert response.params['id'].present?
assert_equal 'AUTHORIZED', response.message
refute_empty response.params['_links']['capture']
end
def test_failure_verify
response = @gateway.verify(@card_without_funds, @options)
assert_failure response
assert_match %r{Decline - Invalid account number}, response.message
assert_equal 'INVALID_ACCOUNT', response.error_code
end
def test_successful_authorize_with_apple_pay
response = @gateway.authorize(@amount, @apple_pay, @options)
assert_success response
assert_equal 'AUTHORIZED', response.message
refute_empty response.params['_links']['capture']
end
def test_successful_authorize_with_google_pay
response = @gateway.authorize(@amount, @apple_pay, @options)
assert_success response
assert_equal 'AUTHORIZED', response.message
refute_empty response.params['_links']['capture']
end
def test_successful_purchase_with_apple_pay_jcb
response = @gateway.purchase(@amount, @apple_pay_jcb, @options)
assert_success response
assert_equal 'AUTHORIZED', response.message
end
def test_successful_purchase_with_apple_pay_american_express
response = @gateway.purchase(@amount, @apple_pay_american_express, @options)
assert_success response
assert_equal 'AUTHORIZED', response.message
end
def test_successful_purchase_with_google_pay_master
response = @gateway.purchase(@amount, @google_pay_master, @options)
assert_success response
assert_equal 'AUTHORIZED', response.message
end
def test_successful_authorize_with_google_pay_discover
response = @gateway.purchase(@amount, @google_pay_discover, @options)
assert_success response
assert_equal 'AUTHORIZED', response.message
end
def test_transcript_scrubbing
transcript = capture_transcript(@gateway) do
@gateway.authorize(@amount, @visa_card, @options)
end
transcript = @gateway.scrub(transcript)
assert_scrubbed(@visa_card.number, transcript)
assert_scrubbed(@visa_card.verification_value, transcript)
end
def test_transcript_scrubbing_bank
@options[:billing_address] = @billing_address
transcript = capture_transcript(@gateway) do
@gateway.purchase(@amount, @bank_account, @options)
end
transcript = @gateway.scrub(transcript)
assert_scrubbed(@bank_account.account_number, transcript)
assert_scrubbed(@bank_account.routing_number, transcript)
end
def test_successful_authorize_with_bank_account
@options[:billing_address] = @billing_address
response = @gateway.authorize(@amount, @bank_account, @options)
assert_success response
assert_equal 'PENDING', response.message
end
def test_successful_purchase_with_bank_account
@options[:billing_address] = @billing_address
response = @gateway.purchase(@amount, @bank_account, @options)
assert_success response
assert_equal 'PENDING', response.message
end
def test_failed_authorize_with_bank_account
@options[:billing_address] = @billing_address
response = @gateway.authorize(@amount, @declined_bank_account, @options)
assert_failure response
assert_equal 'Decline - General decline by the processor.', response.message
end
def test_failed_authorize_with_bank_account_missing_country_code
response = @gateway.authorize(@amount, @bank_account, @options.except(:billing_address))
assert_failure response
assert_equal 'Declined - The request is missing one or more fields', response.params['message']
end
def stored_credential_options(*args, ntid: nil)
@options.merge(stored_credential: stored_credential(*args, network_transaction_id: ntid))
end
def test_purchase_using_stored_credential_initial_mit
options = stored_credential_options(:merchant, :internet, :initial)
options[:reason_code] = '4'
assert auth = @gateway.authorize(@amount, @visa_card, options)
assert_success auth
assert purchase = @gateway.purchase(@amount, @visa_card, options)
assert_success purchase
end
def test_purchase_using_stored_credential_recurring_cit
options = stored_credential_options(:cardholder, :recurring, :initial)
options[:reason_code] = '4'
assert auth = @gateway.authorize(@amount, @visa_card, options)
assert_success auth
used_store_credentials = stored_credential_options(:cardholder, :recurring, ntid: auth.network_transaction_id)
used_store_credentials[:reason_code] = '4'
assert purchase = @gateway.purchase(@amount, @visa_card, used_store_credentials)
assert_success purchase
end
def test_purchase_using_stored_credential_recurring_mit
options = stored_credential_options(:merchant, :recurring, :initial)
options[:reason_code] = '4'
assert auth = @gateway.authorize(@amount, @visa_card, options)
assert_success auth
used_store_credentials = stored_credential_options(:merchant, :recurring, ntid: auth.network_transaction_id)
used_store_credentials[:reason_code] = '4'
assert purchase = @gateway.purchase(@amount, @visa_card, used_store_credentials)
assert_success purchase
end
def test_purchase_using_stored_credential_installment_cit
options = stored_credential_options(:cardholder, :installment, :initial)
options[:reason_code] = '4'
assert auth = @gateway.authorize(@amount, @visa_card, options)
assert_success auth
used_store_credentials = stored_credential_options(:cardholder, :installment, ntid: auth.network_transaction_id)
used_store_credentials[:reason_code] = '4'
assert purchase = @gateway.purchase(@amount, @visa_card, used_store_credentials)
assert_success purchase
end
def test_purchase_using_stored_credential_installment_mit
options = stored_credential_options(:merchant, :installment, :initial)
options[:reason_code] = '4'
assert auth = @gateway.authorize(@amount, @visa_card, options)
assert_success auth
used_store_credentials = stored_credential_options(:merchant, :installment, ntid: auth.network_transaction_id)
used_store_credentials[:reason_code] = '4'
assert purchase = @gateway.purchase(@amount, @visa_card, used_store_credentials)
assert_success purchase
end
def test_failure_stored_credential_invalid_reason_code
options = stored_credential_options(:cardholder, :internet, :initial)
assert auth = @gateway.authorize(@amount, @master_card, options)
assert_equal(auth.params['status'], 'INVALID_REQUEST')
assert_equal(auth.params['message'], 'Declined - One or more fields in the request contains invalid data')
assert_equal(auth.params['details'].first['field'], 'processingInformation.authorizationOptions.initiator.merchantInitiatedTransaction.reason')
end
def test_auth_and_purchase_with_network_txn_id
options = stored_credential_options(:merchant, :recurring, :initial)
options[:reason_code] = '4'
assert auth = @gateway.authorize(@amount, @visa_card, options)
assert purchase = @gateway.purchase(@amount, @visa_card, options.merge(network_transaction_id: auth.network_transaction_id))
assert_success purchase
end
def test_successful_purchase_with_reconciliation_id
options = @options.merge(reconciliation_id: '1936831')
assert response = @gateway.purchase(@amount, @visa_card, options)
assert_success response
end
def test_successful_authorization_with_reconciliation_id
options = @options.merge(reconciliation_id: '1936831')
assert response = @gateway.authorize(@amount, @visa_card, options)
assert_success response
assert !response.authorization.blank?
end
def test_successful_verify_zero_amount
@options[:zero_amount_auth] = true
response = @gateway.verify(@visa_card, @options)
assert_success response
assert_match '0.00', response.params['orderInformation']['amountDetails']['authorizedAmount']
assert_equal 'AUTHORIZED', response.message
end
def test_successful_bank_account_purchase_with_sec_code
options = @options.merge(sec_code: 'WEB')
response = @gateway.purchase(@amount, @bank_account, options)
assert_success response
assert_equal 'PENDING', response.message
end
def test_successful_purchase_with_solution_id
ActiveMerchant::Billing::CyberSourceRestGateway.application_id = 'A1000000'
assert response = @gateway.purchase(@amount, @visa_card, @options)
assert_success response
assert !response.authorization.blank?
ensure
ActiveMerchant::Billing::CyberSourceGateway.application_id = nil
end
def test_successful_purchase_in_australian_dollars
@options[:currency] = 'AUD'
response = @gateway.purchase(@amount, @visa_card, @options)
assert_success response
assert response.test?
assert_equal 'AUTHORIZED', response.message
assert_nil response.params['_links']['capture']
assert_equal 'AUD', response.params['orderInformation']['amountDetails']['currency']
end
def test_successful_authorize_with_3ds2_visa
@options[:three_d_secure] = {
version: '2.2.0',
cavv: '3q2+78r+ur7erb7vyv66vv\/\/\/\/8=',
eci: '05',
ds_transaction_id: 'ODUzNTYzOTcwODU5NzY3Qw==',
enrolled: 'true',
authentication_response_status: 'Y'
}
auth = @gateway.authorize(@amount, @visa_card, @options)
assert_success auth
end
def test_successful_authorize_with_3ds2_mastercard
@options[:three_d_secure] = {
version: '2.2.0',
cavv: '3q2+78r+ur7erb7vyv66vv\/\/\/\/8=',
eci: '05',
ds_transaction_id: 'ODUzNTYzOTcwODU5NzY3Qw==',
enrolled: 'true',
authentication_response_status: 'Y'
}
auth = @gateway.authorize(@amount, @master_card, @options)
assert_success auth
end
end