solidus_afterpay 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +9 -0
  3. data/Gemfile +6 -3
  4. data/README.md +3 -0
  5. data/app/controllers/solidus_afterpay/callbacks_controller.rb +5 -1
  6. data/app/controllers/solidus_afterpay/express_callbacks_controller.rb +5 -1
  7. data/app/models/solidus_afterpay/order_component_builder.rb +1 -1
  8. data/bin/sandbox +1 -1
  9. data/lib/solidus_afterpay/configuration.rb +9 -2
  10. data/lib/solidus_afterpay/engine.rb +1 -1
  11. data/lib/solidus_afterpay/testing_support/factories.rb +1 -1
  12. data/lib/solidus_afterpay/version.rb +1 -1
  13. data/solidus_afterpay.gemspec +4 -4
  14. metadata +11 -99
  15. data/spec/fixtures/vcr_casettes/create_checkout/invalid.yml +0 -65
  16. data/spec/fixtures/vcr_casettes/create_checkout/valid.yml +0 -64
  17. data/spec/fixtures/vcr_casettes/credit/invalid.yml +0 -61
  18. data/spec/fixtures/vcr_casettes/credit/valid.yml +0 -63
  19. data/spec/fixtures/vcr_casettes/deferred/authorize/declined_payment.yml +0 -120
  20. data/spec/fixtures/vcr_casettes/deferred/authorize/invalid.yml +0 -61
  21. data/spec/fixtures/vcr_casettes/deferred/authorize/valid.yml +0 -120
  22. data/spec/fixtures/vcr_casettes/deferred/capture/invalid.yml +0 -61
  23. data/spec/fixtures/vcr_casettes/deferred/capture/valid.yml +0 -140
  24. data/spec/fixtures/vcr_casettes/deferred/void/invalid.yml +0 -61
  25. data/spec/fixtures/vcr_casettes/deferred/void/valid.yml +0 -137
  26. data/spec/fixtures/vcr_casettes/find_order/invalid.yml +0 -64
  27. data/spec/fixtures/vcr_casettes/find_order/valid.yml +0 -120
  28. data/spec/fixtures/vcr_casettes/find_payment/invalid.yml +0 -61
  29. data/spec/fixtures/vcr_casettes/find_payment/valid.yml +0 -140
  30. data/spec/fixtures/vcr_casettes/immediate/capture/declined_payment.yml +0 -120
  31. data/spec/fixtures/vcr_casettes/immediate/capture/invalid.yml +0 -61
  32. data/spec/fixtures/vcr_casettes/immediate/capture/valid.yml +0 -134
  33. data/spec/fixtures/vcr_casettes/retrieve_configuration/valid.yml +0 -67
  34. data/spec/helpers/solidus_afterpay/afterpay_helper_spec.rb +0 -39
  35. data/spec/models/solidus_afterpay/gateway_spec.rb +0 -492
  36. data/spec/models/solidus_afterpay/order_component_builder_spec.rb +0 -198
  37. data/spec/models/solidus_afterpay/payment_method_spec.rb +0 -207
  38. data/spec/models/solidus_afterpay/payment_source_spec.rb +0 -61
  39. data/spec/models/solidus_afterpay/user_agent_generator_spec.rb +0 -22
  40. data/spec/models/spree/order_spec.rb +0 -158
  41. data/spec/presenters/solidus_afterpay/order_presenter_spec.rb +0 -34
  42. data/spec/presenters/solidus_afterpay/shipping_rate_presenter_spec.rb +0 -28
  43. data/spec/requests/solidus_afterpay/callbacks_controller_spec.rb +0 -127
  44. data/spec/requests/solidus_afterpay/checkouts_controller_spec.rb +0 -258
  45. data/spec/requests/solidus_afterpay/express_callbacks_controller_spec.rb +0 -167
  46. data/spec/services/solidus_afterpay/base_service_spec.rb +0 -13
  47. data/spec/services/solidus_afterpay/shipping_rate_builder_service_spec.rb +0 -34
  48. data/spec/services/solidus_afterpay/update_order_addresses_service_spec.rb +0 -82
  49. data/spec/services/solidus_afterpay/update_order_attributes_service_spec.rb +0 -58
  50. data/spec/spec_helper.rb +0 -31
  51. data/spec/support/auth.rb +0 -15
  52. data/spec/support/cache.rb +0 -5
  53. data/spec/support/preferences.rb +0 -33
  54. data/spec/support/solidus.rb +0 -1
  55. data/spec/support/vcr.rb +0 -18
  56. data/spec/views/solidus_afterpay/express_checkout_button_spec.rb +0 -33
  57. data/spec/views/spree/shared/afterpay_messaging_spec.rb +0 -44
@@ -1,492 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'spec_helper'
4
-
5
- RSpec.describe SolidusAfterpay::Gateway do
6
- let(:gateway) { described_class.new(options) }
7
- let(:options) do
8
- {
9
- merchant_id: ENV.fetch('AFTERPAY_MERCHANT_ID', 'dummy_merchant_id'),
10
- secret_key: ENV.fetch('AFTERPAY_SECRET_KEY', 'dummy_secret_key'),
11
- test_mode: true
12
- }
13
- end
14
-
15
- describe '#authorize' do
16
- subject(:response) { gateway.authorize(amount, payment_source, gateway_options) }
17
-
18
- let(:order_token) { '002.m6d9jkrtv1p0j4jqslklhfq9k4nl54jo2530d58kf6snpqq1' }
19
- let(:auto_capture) { true }
20
- let(:payment_method) { build(:afterpay_payment_method, auto_capture: auto_capture) }
21
-
22
- let(:amount) { 1000 }
23
- let(:payment_source) { build(:afterpay_payment_source, token: order_token, payment_method: payment_method) }
24
- let(:gateway_options) { {} }
25
-
26
- context 'with the immediate flow' do
27
- it 'returns a successful response' do
28
- is_expected.to be_success
29
- end
30
- end
31
-
32
- context 'with the deferred flow' do
33
- let(:auto_capture) { false }
34
-
35
- context 'with valid params', vcr: 'deferred/authorize/valid' do
36
- it 'authorize the afterpay payment with the order_token' do
37
- is_expected.to be_success
38
- end
39
- end
40
-
41
- context 'with an invalid token', vcr: 'deferred/authorize/invalid' do
42
- let(:order_token) { 'INVALID_TOKEN' }
43
-
44
- it 'returns an unsuccesfull response' do
45
- is_expected.not_to be_success
46
- end
47
-
48
- it 'returns the error message from Afterpay in the response' do
49
- expect(response.message).to eq('Cannot complete payment, expired or invalid token.')
50
- end
51
-
52
- it 'returns the error_code from Afterpay in the response' do
53
- expect(response.error_code).to eq('invalid_token')
54
- end
55
- end
56
-
57
- context 'with an invalid credit card', vcr: 'deferred/authorize/declined_payment' do
58
- let(:order_token) { '002.ijlqnvko1o4ou45uabplrl9pqao8u2v52njs2972r24hje65' }
59
-
60
- it 'returns an unsuccesfull response' do
61
- is_expected.not_to be_success
62
- end
63
-
64
- it 'returns the error message from Afterpay in the response' do
65
- expect(response.message).to eq(
66
- 'Payment declined. Please contact the Afterpay Customer Service team for more information.'
67
- )
68
- end
69
-
70
- it 'returns the error_code from Afterpay in the response' do
71
- expect(response.error_code).to eq('payment_declined')
72
- end
73
- end
74
-
75
- context 'with a timeout error' do
76
- before do
77
- allow(::Afterpay::API::Payment::Auth).to receive(:call).and_raise(
78
- ::Afterpay::RequestTimeoutError.new('request_timeout'), 'Request Timeout'
79
- )
80
- allow(::Afterpay::API::Payment::Reversal).to receive(:call)
81
- end
82
-
83
- it 'returns an unsuccesfull response' do
84
- is_expected.not_to be_success
85
- end
86
-
87
- it 'returns the error message from Afterpay in the response' do
88
- expect(response.message).to eq('Request Timeout')
89
- end
90
-
91
- it 'returns the error_code from Afterpay in the response' do
92
- expect(response.error_code).to eq('request_timeout')
93
- end
94
-
95
- it 'calls the ::Afterpay::API::Payment::Reversal with order token' do
96
- response
97
- expect(::Afterpay::API::Payment::Reversal).to have_received(:call).with(token: order_token)
98
- end
99
- end
100
- end
101
- end
102
-
103
- describe '#capture' do
104
- subject(:response) { gateway.capture(amount, response_code, gateway_options) }
105
-
106
- let(:order_token) { '002.nt7e0ioqj00fh0ua1nbqcj6vcn9obtfsglqvrj9ijpo3edfc' }
107
- let(:auto_capture) { true }
108
- let(:payment_source) { build(:afterpay_payment_source, token: order_token) }
109
- let(:payment_method) { build(:afterpay_payment_method, auto_capture: auto_capture) }
110
- let(:payment) { build(:afterpay_payment, source: payment_source, payment_method: payment_method) }
111
-
112
- let(:amount) { 1000 }
113
- let(:response_code) { '100101782114' }
114
- let(:gateway_options) { { originator: payment, currency: 'USD' } }
115
-
116
- context 'with the immediate flow' do
117
- context 'with valid params', vcr: 'immediate/capture/valid' do
118
- it 'captures the afterpay payment with the order_token' do
119
- is_expected.to be_success
120
- end
121
- end
122
-
123
- context 'with an invalid token', vcr: 'immediate/capture/invalid' do
124
- let(:order_token) { 'INVALID_TOKEN' }
125
-
126
- it 'returns an unsuccesfull response' do
127
- is_expected.not_to be_success
128
- end
129
-
130
- it 'returns the error message from Afterpay in the response' do
131
- expect(response.message).to eq('Cannot complete payment, expired or invalid token.')
132
- end
133
-
134
- it 'returns the error_code from Afterpay in the response' do
135
- expect(response.error_code).to eq('invalid_token')
136
- end
137
- end
138
-
139
- context 'with an invalid credit card', vcr: 'immediate/capture/declined_payment' do
140
- let(:order_token) { '002.kj16plsn63eqfacueg767cp7l34e9ph5tms4ql14o2iid7l1' }
141
-
142
- it 'returns an unsuccesfull response' do
143
- is_expected.not_to be_success
144
- end
145
-
146
- it 'returns the error message from Afterpay in the response' do
147
- expect(response.message).to eq(
148
- 'Payment declined. Please contact the Afterpay Customer Service team for more information.'
149
- )
150
- end
151
-
152
- it 'returns the error_code from Afterpay in the response' do
153
- expect(response.error_code).to eq('payment_declined')
154
- end
155
- end
156
- end
157
-
158
- context 'with the deferred flow' do
159
- let(:auto_capture) { false }
160
-
161
- context 'with valid params', vcr: 'deferred/capture/valid' do
162
- it 'captures the afterpay payment with the order_id' do
163
- is_expected.to be_success
164
- end
165
- end
166
-
167
- context 'with an invalid payment ID', vcr: 'deferred/capture/invalid' do
168
- let(:response_code) { 'INVALID_RESPONSE_CODE' }
169
-
170
- it 'returns an unsuccesfull response' do
171
- is_expected.not_to be_success
172
- end
173
-
174
- it 'returns the error message from Afterpay in the response' do
175
- expect(response.message).to eq('Afterpay payment ID not found.')
176
- end
177
-
178
- it 'returns the error_code from Afterpay in the response' do
179
- expect(response.error_code).to eq('not_found')
180
- end
181
- end
182
-
183
- context 'with a timeout error' do
184
- before do
185
- allow(::Afterpay::API::Payment::DeferredCapture).to receive(:call).and_raise(
186
- ::Afterpay::RequestTimeoutError.new('request_timeout'), 'Request Timeout'
187
- )
188
- allow(::Afterpay::API::Payment::Reversal).to receive(:call)
189
- end
190
-
191
- it 'returns an unsuccesfull response' do
192
- is_expected.not_to be_success
193
- end
194
-
195
- it 'returns the error message from Afterpay in the response' do
196
- expect(response.message).to eq('Request Timeout')
197
- end
198
-
199
- it 'returns the error_code from Afterpay in the response' do
200
- expect(response.error_code).to eq('request_timeout')
201
- end
202
-
203
- it 'calls the ::Afterpay::API::Payment::Reversal with order token' do
204
- response
205
- expect(::Afterpay::API::Payment::Reversal).to have_received(:call).with(token: order_token)
206
- end
207
- end
208
- end
209
- end
210
-
211
- describe '#purchase' do
212
- subject(:response) { gateway.purchase(amount, payment_source, gateway_options) }
213
-
214
- let(:order_token) { '002.nt7e0ioqj00fh0ua1nbqcj6vcn9obtfsglqvrj9ijpo3edfc' }
215
- let(:auto_capture) { true }
216
- let(:payment_method) { build(:afterpay_payment_method, auto_capture: auto_capture) }
217
- let(:payment) { build(:afterpay_payment, source: payment_source, payment_method: payment_method) }
218
-
219
- let(:amount) { 1000 }
220
- let(:payment_source) { build(:afterpay_payment_source, token: order_token, payment_method: payment_method) }
221
- let(:gateway_options) { { originator: payment, currency: 'USD' } }
222
-
223
- context 'with the immediate flow' do
224
- context 'with valid params', vcr: 'immediate/capture/valid' do
225
- it 'authorize and captures the afterpay payment with the order_token' do
226
- is_expected.to be_success
227
- end
228
- end
229
-
230
- context 'with an invalid token', vcr: 'immediate/capture/invalid' do
231
- let(:order_token) { 'INVALID_TOKEN' }
232
-
233
- it 'returns an unsuccesfull response' do
234
- is_expected.not_to be_success
235
- end
236
-
237
- it 'returns the error message from Afterpay in the response' do
238
- expect(response.message).to eq('Cannot complete payment, expired or invalid token.')
239
- end
240
-
241
- it 'returns the error_code from Afterpay in the response' do
242
- expect(response.error_code).to eq('invalid_token')
243
- end
244
- end
245
-
246
- context 'with an invalid credit card', vcr: 'immediate/capture/declined_payment' do
247
- let(:order_token) { '002.kj16plsn63eqfacueg767cp7l34e9ph5tms4ql14o2iid7l1' }
248
-
249
- it 'returns an unsuccesfull response' do
250
- is_expected.not_to be_success
251
- end
252
-
253
- it 'returns the error message from Afterpay in the response' do
254
- expect(response.message).to eq(
255
- 'Payment declined. Please contact the Afterpay Customer Service team for more information.'
256
- )
257
- end
258
-
259
- it 'returns the error_code from Afterpay in the response' do
260
- expect(response.error_code).to eq('payment_declined')
261
- end
262
- end
263
- end
264
-
265
- context 'with the deferred flow' do
266
- let(:auto_capture) { false }
267
-
268
- context 'with valid params', vcr: 'deferred/authorize/valid' do
269
- it 'authorize and captures the afterpay payment with the order_token' do
270
- VCR.use_cassette('deferred/capture/valid') do
271
- is_expected.to be_success
272
- end
273
- end
274
- end
275
-
276
- context 'with an invalid token', vcr: 'deferred/authorize/invalid' do
277
- let(:order_token) { 'INVALID_TOKEN' }
278
-
279
- it 'returns an unsuccesfull response' do
280
- is_expected.not_to be_success
281
- end
282
-
283
- it 'returns the error message from Afterpay in the response' do
284
- expect(response.message).to eq('Cannot complete payment, expired or invalid token.')
285
- end
286
-
287
- it 'returns the error_code from Afterpay in the response' do
288
- expect(response.error_code).to eq('invalid_token')
289
- end
290
- end
291
-
292
- context 'with an invalid credit card', vcr: 'deferred/authorize/declined_payment' do
293
- let(:order_token) { '002.kj16plsn63eqfacueg767cp7l34e9ph5tms4ql14o2iid7l1' }
294
-
295
- it 'returns an unsuccesfull response' do
296
- is_expected.not_to be_success
297
- end
298
-
299
- it 'returns the error message from Afterpay in the response' do
300
- expect(response.message).to eq(
301
- 'Payment declined. Please contact the Afterpay Customer Service team for more information.'
302
- )
303
- end
304
-
305
- it 'returns the error_code from Afterpay in the response' do
306
- expect(response.error_code).to eq('payment_declined')
307
- end
308
- end
309
- end
310
- end
311
-
312
- describe '#credit' do
313
- subject(:response) { gateway.credit(amount, response_code, gateway_options) }
314
-
315
- let(:payment) { build(:afterpay_payment) }
316
- let(:refund) { build(:refund, payment: payment) }
317
-
318
- let(:amount) { 1000 }
319
- let(:response_code) { '100101768366' }
320
- let(:gateway_options) { { originator: refund } }
321
-
322
- context 'with valid params', vcr: 'credit/valid' do
323
- it 'refunds the amount using the response_code' do
324
- is_expected.to be_success
325
- end
326
- end
327
-
328
- context 'with an invalid response_code', vcr: 'credit/invalid' do
329
- let(:response_code) { 'INVALID_RESPONSE_CODE' }
330
-
331
- it 'returns an unsuccesfull response' do
332
- is_expected.not_to be_success
333
- end
334
-
335
- it 'returns the error message from Afterpay in the response' do
336
- expect(response.message).to eq('Afterpay payment ID not found.')
337
- end
338
-
339
- it 'returns the error_code from Afterpay in the response' do
340
- expect(response.error_code).to eq('not_found')
341
- end
342
- end
343
- end
344
-
345
- describe '#void' do
346
- subject(:response) { gateway.void(response_code, gateway_options) }
347
-
348
- let(:auto_capture) { true }
349
- let(:amount) { 10 }
350
- let(:payment_method) { build(:afterpay_payment_method, auto_capture: auto_capture) }
351
- let(:payment) { build(:afterpay_payment, payment_method: payment_method, amount: amount) }
352
-
353
- let(:response_code) { '100101785223' }
354
- let(:gateway_options) { { originator: payment, currency: 'USD' } }
355
-
356
- context 'with the immediate flow' do
357
- it 'returns an unsuccessful response' do
358
- is_expected.not_to be_success
359
- end
360
-
361
- it 'returns the error message from Afterpay in the response' do
362
- expect(response.message).to eq("Transaction can't be voided")
363
- end
364
-
365
- it 'returns the error_code from Afterpay in the response' do
366
- expect(response.error_code).to eq('void_not_allowed')
367
- end
368
- end
369
-
370
- context 'with the deferred flow' do
371
- let(:auto_capture) { false }
372
-
373
- context 'with valid params', vcr: 'deferred/void/valid' do
374
- it 'voids the payment using the response_code' do
375
- is_expected.to be_success
376
- end
377
- end
378
-
379
- context 'with an invalid response_code', vcr: 'deferred/void/invalid' do
380
- let(:response_code) { 'INVALID_RESPONSE_CODE' }
381
-
382
- it 'returns an unsuccesfull response' do
383
- is_expected.not_to be_success
384
- end
385
-
386
- it 'returns the error message from Afterpay in the response' do
387
- expect(response.message).to eq('Afterpay payment ID not found.')
388
- end
389
-
390
- it 'returns the error_code from Afterpay in the response' do
391
- expect(response.error_code).to eq('not_found')
392
- end
393
- end
394
- end
395
- end
396
-
397
- describe '#create_checkout' do
398
- subject(:response) { gateway.create_checkout(order, gateway_options) }
399
-
400
- let(:redirect_confirm_url) { 'https://merchantsite.com/confirm' }
401
- let(:redirect_cancel_url) { 'https://merchantsite.com/cancel' }
402
-
403
- let(:order) { build(:order_with_line_items) }
404
- let(:gateway_options) { { redirect_confirm_url: redirect_confirm_url, redirect_cancel_url: redirect_cancel_url } }
405
-
406
- context 'with valid params', vcr: 'create_checkout/valid' do
407
- it 'creates the checkout' do
408
- is_expected.to be_success
409
- end
410
-
411
- it 'returns the order_token' do
412
- expect(response.params).to include('token')
413
- end
414
- end
415
-
416
- context 'with an invalid params', vcr: 'create_checkout/invalid' do
417
- let(:redirect_confirm_url) { 'INVALID_URL' }
418
-
419
- it 'returns an unsuccesfull response' do
420
- is_expected.not_to be_success
421
- end
422
-
423
- it 'returns the error message from Afterpay in the response' do
424
- expect(response.message).to eq('merchant.redirectConfirmUrl must be a valid URL')
425
- end
426
-
427
- it 'returns the error_code from Afterpay in the response' do
428
- expect(response.error_code).to eq('invalid_object')
429
- end
430
- end
431
- end
432
-
433
- describe '#find_payment' do
434
- subject(:response) { gateway.find_payment(order_id: order_id) }
435
-
436
- let(:order_id) { '100101785223' }
437
-
438
- context 'with valid params', vcr: 'find_payment/valid' do
439
- it 'retrieves the Afterpay payment' do
440
- expect(response).to include('id' => order_id)
441
- end
442
- end
443
-
444
- context 'with an invalid params', vcr: 'find_payment/invalid' do
445
- let(:order_id) { 'INVALID_ORDER_ID' }
446
-
447
- it 'returns nil' do
448
- is_expected.to be_nil
449
- end
450
- end
451
- end
452
-
453
- describe '#find_order' do
454
- subject(:response) { gateway.find_order(token: token) }
455
-
456
- let(:token) { '002.cb9qevbs1o4el3adh817hqkotkbv4b8u1jkekofd3nb2m8lu' }
457
-
458
- context 'with valid params', vcr: 'find_order/valid' do
459
- it 'retrieves the Afterpay order' do
460
- expect(response).to include(token: token)
461
- end
462
- end
463
-
464
- context 'with an invalid params', vcr: 'find_order/invalid' do
465
- let(:token) { 'INVALID_TOKEN' }
466
-
467
- it 'returns nil' do
468
- is_expected.to be_nil
469
- end
470
- end
471
- end
472
-
473
- describe '#retrieve_configuration' do
474
- subject(:response) { gateway.retrieve_configuration }
475
-
476
- context 'with valid response', vcr: 'retrieve_configuration/valid' do
477
- it 'retrieves the afterpay configuration' do
478
- expect(response).to include('minimumAmount', 'maximumAmount')
479
- end
480
- end
481
-
482
- context 'with invalid response' do
483
- before do
484
- allow(::Afterpay::API::Configuration::Retrieve).to receive(:call).and_raise(::Afterpay::BaseError.new(nil))
485
- end
486
-
487
- it 'retrieves the afterpay configuration' do
488
- expect(response).to be_nil
489
- end
490
- end
491
- end
492
- end
@@ -1,198 +0,0 @@
1
- require 'spec_helper'
2
-
3
- RSpec.describe SolidusAfterpay::OrderComponentBuilder do
4
- let(:order) { create(:order_with_product_property) }
5
- let(:redirect_confirm_url) { 'https://merchantsite.com/confirm' }
6
- let(:redirect_cancel_url) { 'https://merchantsite.com/cancel' }
7
-
8
- let(:builder) do
9
- described_class.new(
10
- order: order,
11
- redirect_confirm_url: redirect_confirm_url,
12
- redirect_cancel_url: redirect_cancel_url,
13
- mode: nil,
14
- popup_origin_url: nil
15
- )
16
- end
17
-
18
- describe '#call' do
19
- subject(:result) { builder.call }
20
-
21
- let(:expected_result_not_combined) do
22
- Afterpay::Components::Order.new(
23
- amount: Afterpay::Components::Money.new(amount: '110.0', currency: 'USD'),
24
- billing: Afterpay::Components::Contact.new(
25
- area1: 'Herndon',
26
- area2: nil,
27
- country_code: nil,
28
- line1: 'PO Box 1337',
29
- line2: 'Northwest',
30
- name: 'John',
31
- phone_number: '555-555-0199',
32
- postcode: order.billing_address.zipcode,
33
- region: 'AL'
34
- ),
35
- consumer: Afterpay::Components::Consumer.new(
36
- email: order.user.email,
37
- given_names: 'John',
38
- phone_number: nil,
39
- surname: nil
40
- ),
41
- courier: nil,
42
- discounts: nil,
43
- items: [
44
- Afterpay::Components::Item.new(
45
- preorder: true,
46
- estimated_shipment_date: "2025-10-25",
47
- name: order.line_items.first.name,
48
- price: Afterpay::Components::Money.new(
49
- amount: '10.0',
50
- currency: 'USD'
51
- ),
52
- quantity: 1,
53
- sku: order.line_items.first.sku
54
- )
55
- ],
56
- merchant: Afterpay::Components::Merchant.new(
57
- redirect_confirm_url: 'https://merchantsite.com/confirm',
58
- redirect_cancel_url: 'https://merchantsite.com/cancel'
59
- ),
60
- merchant_reference: order.number,
61
- payment_type: nil,
62
- shipping: Afterpay::Components::Contact.new(
63
- area1: 'Herndon',
64
- area2: nil,
65
- country_code: nil,
66
- line1: 'A Different Road',
67
- line2: 'Northwest',
68
- name: 'John',
69
- phone_number: '555-555-0199',
70
- postcode: order.shipping_address.zipcode,
71
- region: 'AL'
72
- ),
73
- shipping_amount: nil,
74
- tax_amount: nil
75
- )
76
- end
77
-
78
- let(:expected_result_combined) do
79
- Afterpay::Components::Order.new(
80
- amount: Afterpay::Components::Money.new(amount: '110.0', currency: 'USD'),
81
- billing: Afterpay::Components::Contact.new(
82
- area1: 'Herndon',
83
- area2: nil,
84
- country_code: nil,
85
- line1: 'PO Box 1337',
86
- line2: 'Northwest',
87
- name: 'John Von Doe',
88
- phone_number: '555-555-0199',
89
- postcode: order.billing_address.zipcode,
90
- region: 'AL'
91
- ),
92
- consumer: Afterpay::Components::Consumer.new(
93
- email: order.user.email,
94
- given_names: 'John',
95
- phone_number: nil,
96
- surname: 'Von Doe'
97
- ),
98
- courier: nil,
99
- discounts: nil,
100
- items: [
101
- Afterpay::Components::Item.new(
102
- estimated_shipment_date: "2025-10-25",
103
- preorder: true,
104
- name: order.line_items.first.name,
105
- price: Afterpay::Components::Money.new(
106
- amount: '10.0',
107
- currency: 'USD'
108
- ),
109
- quantity: 1,
110
- sku: order.line_items.first.sku
111
- )
112
- ],
113
- merchant: Afterpay::Components::Merchant.new(
114
- redirect_confirm_url: 'https://merchantsite.com/confirm',
115
- redirect_cancel_url: 'https://merchantsite.com/cancel'
116
- ),
117
- merchant_reference: order.number,
118
- payment_type: nil,
119
- shipping: Afterpay::Components::Contact.new(
120
- area1: 'Herndon',
121
- area2: nil,
122
- country_code: nil,
123
- line1: 'A Different Road',
124
- line2: 'Northwest',
125
- name: 'John Von Doe',
126
- phone_number: '555-555-0199',
127
- postcode: order.shipping_address.zipcode,
128
- region: 'AL'
129
- ),
130
- shipping_amount: nil,
131
- tax_amount: nil
132
- )
133
- end
134
-
135
- context 'when solidus combines first and last name' do
136
- before do
137
- allow(SolidusSupport)
138
- .to receive(:combined_first_and_last_name_in_address?)
139
- .and_return(true)
140
- end
141
-
142
- it 'returns the correct payload' do
143
- expect(result.as_json).to eq(expected_result_combined.as_json)
144
- end
145
- end
146
-
147
- context 'when solidus does not combine first and last name' do
148
- before do
149
- allow(SolidusSupport)
150
- .to receive(:combined_first_and_last_name_in_address?)
151
- .and_return(false)
152
-
153
- # rubocop:disable RSpec/AnyInstance
154
- allow_any_instance_of(Spree::Address)
155
- .to receive(:first_name)
156
- .and_return('John')
157
-
158
- allow_any_instance_of(Spree::Address)
159
- .to receive(:last_name)
160
- .and_return(nil)
161
- # rubocop:enable RSpec/AnyInstance
162
- end
163
-
164
- it 'returns the correct payload' do
165
- expect(result.as_json).to eq(expected_result_not_combined.as_json)
166
- end
167
- end
168
-
169
- context "when provided an estimated shipment date" do
170
- context "when provided a product estimated shipment date" do
171
- it "returns the correct estimated shipment date" do
172
- expect(result.items.first.estimated_shipment_date).to match("2025-10-25")
173
- end
174
-
175
- it "returns true if the estimated shipping date didn't pass yet" do
176
- expect(result.items.first.preorder).to be_truthy
177
- end
178
- end
179
-
180
- context "when provided a variant estimated shipment date" do
181
- let(:order) { create(:order_with_variant_property) }
182
-
183
- it 'contains the correct estimated shipment date' do
184
- expect(result.items.first.estimated_shipment_date).to match("2021-09-19")
185
- end
186
-
187
- it 'returns the variant estimated shipment date when product and variant properties are set' do
188
- order.line_items.first.product.set_property("estimatedShipmentDate", "2025-10-25")
189
- expect(result.items.first.estimated_shipment_date).to match("2021-09-19")
190
- end
191
-
192
- it "returns false when the estimated shipping date passed" do
193
- expect(result.items.first.preorder).to be_falsey
194
- end
195
- end
196
- end
197
- end
198
- end