solidus_braintree 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/.github/dependabot.yml +7 -0
  3. data/.github/workflows/lint.yml +25 -0
  4. data/.github/workflows/test.yml +77 -0
  5. data/.rubocop.yml +5 -84
  6. data/.standard.yml +6 -0
  7. data/Gemfile +12 -17
  8. data/README.md +7 -5
  9. data/Rakefile +7 -7
  10. data/app/helpers/solidus_braintree/braintree_admin_helper.rb +4 -4
  11. data/app/models/solidus_braintree/avs_result.rb +34 -34
  12. data/app/models/solidus_braintree/base_record.rb +1 -1
  13. data/app/models/solidus_braintree/configuration.rb +11 -11
  14. data/app/models/solidus_braintree/gateway.rb +14 -16
  15. data/app/models/solidus_braintree/response.rb +9 -9
  16. data/app/models/solidus_braintree/source.rb +5 -5
  17. data/app/models/solidus_braintree/transaction.rb +2 -2
  18. data/app/models/solidus_braintree/transaction_address.rb +5 -5
  19. data/app/models/solidus_braintree/transaction_import.rb +2 -2
  20. data/app/{decorators/models/solidus_braintree/spree_store_decorator.rb → patches/models/solidus_braintree/spree_store_patch.rb} +1 -1
  21. data/app/{decorators/models/solidus_braintree/spree_user_decorator.rb → patches/models/solidus_braintree/spree_user_patch.rb} +2 -2
  22. data/bin/dummy-app +22 -3
  23. data/bin/rails-sandbox +4 -4
  24. data/bin/rspec +6 -0
  25. data/bin/sandbox +55 -31
  26. data/db/migrate/20161114231422_create_solidus_paypal_braintree_configurations.rb +2 -2
  27. data/db/migrate/20170508085402_add_not_null_constraint_to_sources_payment_type.rb +2 -2
  28. data/db/migrate/20230109080950_rename_solidus_paypal_braintree_source_type.rb +3 -3
  29. data/lib/controllers/api/solidus_braintree/client_tokens_controller.rb +2 -2
  30. data/lib/controllers/backend/solidus_braintree/configurations_controller.rb +4 -4
  31. data/lib/generators/solidus_braintree/install/install_generator.rb +48 -40
  32. data/lib/generators/solidus_braintree/install/templates/app/controllers/solidus_braintree/checkouts_controller.rb +7 -7
  33. data/lib/generators/solidus_braintree/install/templates/app/controllers/solidus_braintree/transactions_controller.rb +7 -7
  34. data/lib/generators/solidus_braintree/install/templates/app/helpers/solidus_braintree/braintree_checkout_helper.rb +5 -5
  35. data/{app/decorators/controllers/solidus_braintree/admin_payments_controller_decorator.rb → lib/patches/backend/controllers/solidus_braintree/admin_payments_controller_patch.rb} +1 -1
  36. data/lib/solidus_braintree/engine.rb +13 -24
  37. data/lib/solidus_braintree/extension_configuration.rb +1 -1
  38. data/lib/solidus_braintree/request_protection.rb +2 -2
  39. data/lib/solidus_braintree/version.rb +1 -1
  40. data/lib/solidus_braintree.rb +8 -8
  41. data/lib/solidus_paypal_braintree.rb +2 -2
  42. data/mise.toml +2 -0
  43. data/solidus_braintree.gemspec +23 -22
  44. metadata +59 -147
  45. data/.circleci/config.yml +0 -79
  46. data/lib/views/backend_v1.2/spree/admin/payments/source_forms/_braintree.html.erb +0 -16
  47. data/lib/views/backend_v2.4/spree/admin/shared/preference_fields/_hash.html.erb +0 -12
  48. data/spec/controllers/solidus_braintree/checkouts_controller_spec.rb +0 -99
  49. data/spec/controllers/solidus_braintree/client_tokens_controller_spec.rb +0 -55
  50. data/spec/controllers/solidus_braintree/configurations_controller_spec.rb +0 -73
  51. data/spec/controllers/solidus_braintree/transactions_controller_spec.rb +0 -183
  52. data/spec/fixtures/cassettes/admin/invalid_credit_card.yml +0 -63
  53. data/spec/fixtures/cassettes/admin/resubmit_credit_card.yml +0 -352
  54. data/spec/fixtures/cassettes/admin/valid_credit_card.yml +0 -412
  55. data/spec/fixtures/cassettes/braintree/create_profile.yml +0 -71
  56. data/spec/fixtures/cassettes/braintree/generate_token.yml +0 -63
  57. data/spec/fixtures/cassettes/braintree/token.yml +0 -63
  58. data/spec/fixtures/cassettes/checkout/invalid_credit_card.yml +0 -63
  59. data/spec/fixtures/cassettes/checkout/resubmit_credit_card.yml +0 -216
  60. data/spec/fixtures/cassettes/checkout/update.yml +0 -71
  61. data/spec/fixtures/cassettes/checkout/valid_credit_card.yml +0 -171
  62. data/spec/fixtures/cassettes/checkout/valid_venmo_transaction.yml +0 -599
  63. data/spec/fixtures/cassettes/gateway/authorize/credit_card/address.yml +0 -86
  64. data/spec/fixtures/cassettes/gateway/authorize/merchant_account/EUR.yml +0 -154
  65. data/spec/fixtures/cassettes/gateway/authorize/paypal/EUR.yml +0 -90
  66. data/spec/fixtures/cassettes/gateway/authorize/paypal/address.yml +0 -90
  67. data/spec/fixtures/cassettes/gateway/authorize.yml +0 -86
  68. data/spec/fixtures/cassettes/gateway/authorized_transaction.yml +0 -73
  69. data/spec/fixtures/cassettes/gateway/cancel/missing.yml +0 -63
  70. data/spec/fixtures/cassettes/gateway/cancel/refunds.yml +0 -272
  71. data/spec/fixtures/cassettes/gateway/cancel/void.yml +0 -201
  72. data/spec/fixtures/cassettes/gateway/capture.yml +0 -141
  73. data/spec/fixtures/cassettes/gateway/complete.yml +0 -157
  74. data/spec/fixtures/cassettes/gateway/credit.yml +0 -208
  75. data/spec/fixtures/cassettes/gateway/customer.yml +0 -79
  76. data/spec/fixtures/cassettes/gateway/purchase.yml +0 -87
  77. data/spec/fixtures/cassettes/gateway/settled_transaction.yml +0 -140
  78. data/spec/fixtures/cassettes/gateway/void.yml +0 -137
  79. data/spec/fixtures/cassettes/source/bin.yml +0 -295
  80. data/spec/fixtures/cassettes/source/card_type.yml +0 -267
  81. data/spec/fixtures/cassettes/source/last4.yml +0 -267
  82. data/spec/fixtures/cassettes/transaction/import/valid/capture.yml +0 -224
  83. data/spec/fixtures/cassettes/transaction/import/valid.yml +0 -71
  84. data/spec/fixtures/views/carts/_cart_footer.html.erb +0 -18
  85. data/spec/helpers/solidus_braintree/braintree_admin_helper_spec.rb +0 -17
  86. data/spec/helpers/solidus_braintree/braintree_checkout_helper_spec.rb +0 -70
  87. data/spec/models/solidus_braintree/address_spec.rb +0 -71
  88. data/spec/models/solidus_braintree/avs_result_spec.rb +0 -317
  89. data/spec/models/solidus_braintree/gateway_spec.rb +0 -774
  90. data/spec/models/solidus_braintree/response_spec.rb +0 -280
  91. data/spec/models/solidus_braintree/source_spec.rb +0 -555
  92. data/spec/models/solidus_braintree/transaction_address_spec.rb +0 -235
  93. data/spec/models/solidus_braintree/transaction_import_spec.rb +0 -302
  94. data/spec/models/solidus_braintree/transaction_spec.rb +0 -86
  95. data/spec/models/spree/store_spec.rb +0 -14
  96. data/spec/requests/spree/api/orders_controller_spec.rb +0 -36
  97. data/spec/solidus_braintree_spec_helper.rb +0 -9
  98. data/spec/support/solidus_braintree/capybara.rb +0 -7
  99. data/spec/support/solidus_braintree/factories.rb +0 -55
  100. data/spec/support/solidus_braintree/gateway_helpers.rb +0 -29
  101. data/spec/support/solidus_braintree/order_ready_for_payment.rb +0 -44
  102. data/spec/support/solidus_braintree/order_walkthrough.rb +0 -87
  103. data/spec/support/solidus_braintree/vcr.rb +0 -38
  104. data/spec/support/solidus_braintree/with_prepended_view_fixtures.rb +0 -19
  105. data/spec/system/backend/configuration_spec.rb +0 -23
  106. data/spec/system/backend/new_payment_spec.rb +0 -136
  107. data/spec/system/frontend/braintree_credit_card_checkout_spec.rb +0 -199
  108. data/spec/system/frontend/paypal_checkout_spec.rb +0 -168
  109. data/spec/system/frontend/venmo_checkout_spec.rb +0 -193
@@ -1,774 +0,0 @@
1
- require 'solidus_braintree_spec_helper'
2
- require 'webmock'
3
- require 'support/solidus_braintree/order_ready_for_payment'
4
-
5
- RSpec.describe SolidusBraintree::Gateway do
6
- let(:gateway) do
7
- new_gateway.tap(&:save)
8
- end
9
-
10
- let(:braintree) { gateway.braintree }
11
-
12
- let(:user) { create :user }
13
-
14
- let(:source) do
15
- SolidusBraintree::Source.create!(
16
- nonce: 'fake-valid-nonce',
17
- user: user,
18
- payment_type: payment_type,
19
- payment_method: gateway,
20
- device_data: 'fake-device-data'
21
- )
22
- end
23
-
24
- let(:payment_type) { SolidusBraintree::Source::PAYPAL }
25
-
26
- describe "saving preference hashes as strings" do
27
- subject { gateway.update(update_params) }
28
-
29
- context "with valid hash syntax" do
30
- let(:update_params) do
31
- {
32
- preferred_merchant_currency_map: '{"EUR" => "test_merchant_account_id"}',
33
- preferred_paypal_payee_email_map: '{"CAD" => "bruce+wayne@example.com"}'
34
- }
35
- end
36
-
37
- it "successfully updates the preference" do
38
- subject
39
- expect(gateway.preferred_merchant_currency_map).to eq({ "EUR" => "test_merchant_account_id" })
40
- expect(gateway.preferred_paypal_payee_email_map).to eq({ "CAD" => "bruce+wayne@example.com" })
41
- end
42
- end
43
-
44
- context "with invalid user input" do
45
- let(:update_params) do
46
- { preferred_merchant_currency_map: '{this_is_not_a_valid_hash}' }
47
- end
48
-
49
- it "raise a JSON parser error" do
50
- expect{ subject }.to raise_error(JSON::ParserError)
51
- end
52
- end
53
- end
54
-
55
- describe 'making a payment on an order', vcr: {
56
- cassette_name: 'gateway/complete',
57
- match_requests_on: [:braintree_uri]
58
- } do
59
- include_context 'when order is ready for payment'
60
-
61
- before do
62
- order.update(number: "ORDER0")
63
- payment.update(number: "PAYMENT0")
64
- end
65
-
66
- let(:payment) do
67
- order.payments.create!(
68
- payment_method: gateway,
69
- source: source,
70
- amount: 55
71
- )
72
- end
73
-
74
- it 'can complete an order' do
75
- order.payments.reset
76
-
77
- expect(order.total).to eq 55
78
-
79
- expect(payment.capture_events.count).to eq 0
80
-
81
- order.next!
82
- expect(order.state).to eq "confirm"
83
-
84
- order.complete!
85
- expect(order.state).to eq "complete"
86
-
87
- expect(order.outstanding_balance).to eq 0.0
88
-
89
- expect(payment.capture_events.count).to eq 1
90
- end
91
- end
92
-
93
- describe "instance methods" do
94
- shared_examples "successful response" do
95
- it 'returns a successful billing response', aggregate_failures: true do
96
- expect(subject).to be_a ActiveMerchant::Billing::Response
97
- expect(subject).to be_success
98
- end
99
- end
100
-
101
- shared_examples "protects against connection errors" do
102
- context 'when a timeout error happens' do
103
- it 'raises ActiveMerchant::ConnectionError' do
104
- expect_any_instance_of(Braintree::TransactionGateway).to receive(gateway_action) do
105
- raise Braintree::BraintreeError
106
- end
107
-
108
- expect { subject }.to raise_error ActiveMerchant::ConnectionError
109
- end
110
- end
111
- end
112
-
113
- let(:authorized_id) do
114
- braintree.transaction.sale(
115
- amount: 40,
116
- payment_method_nonce: source.nonce
117
- ).transaction.id
118
- end
119
-
120
- let(:sale_id) do
121
- braintree.transaction.sale(
122
- amount: 40,
123
- payment_method_nonce: source.nonce,
124
- options: {
125
- submit_for_settlement: true
126
- }
127
- ).transaction.id
128
- end
129
-
130
- let(:settled_id) do
131
- braintree.testing.settle(sale_id).transaction.id
132
- end
133
-
134
- let(:currency) { 'USD' }
135
-
136
- let(:gateway_options) do
137
- {
138
- currency: currency,
139
- shipping_address: {
140
- name: "Bruce Wayne",
141
- address1: "42 Spruce Lane",
142
- address2: "Apt 312",
143
- city: "Gotham",
144
- state: "CA",
145
- zip: "90210",
146
- country: "US"
147
- },
148
- billing_address: {
149
- name: "Dick Grayson",
150
- address1: "15 Robin Walk",
151
- address2: "Apt 123",
152
- city: "Blüdhaven",
153
- state: "CA",
154
- zip: "90210",
155
- country: "US"
156
- }
157
- }
158
- end
159
-
160
- describe "#method_type" do
161
- subject { gateway.method_type }
162
-
163
- it { is_expected.to eq "braintree" }
164
- end
165
-
166
- describe '#gateway_options' do
167
- subject(:gateway_options) { gateway.gateway_options }
168
-
169
- it 'includes http_open_timeout' do
170
- expect(subject).to have_key(:http_open_timeout)
171
- expect(gateway_options[:http_open_timeout]).to eq(60)
172
- end
173
-
174
- it 'includes http_read_timeout' do
175
- expect(subject).to have_key(:http_read_timeout)
176
- expect(gateway_options[:http_read_timeout]).to eq(60)
177
- end
178
- end
179
-
180
- describe '#purchase' do
181
- subject(:purchase) { gateway.purchase(1000, source, gateway_options) }
182
-
183
- context 'with successful purchase', vcr: {
184
- cassette_name: 'gateway/purchase',
185
- match_requests_on: [:braintree_uri]
186
- } do
187
- include_examples "successful response"
188
-
189
- it 'submits the transaction for settlement', aggregate_failures: true do
190
- expect(purchase.message).to eq 'submitted_for_settlement'
191
- expect(purchase.authorization).to be_present
192
- end
193
- end
194
-
195
- include_examples "protects against connection errors" do
196
- let(:gateway_action) { :sale }
197
- end
198
- end
199
-
200
- describe "#authorize" do
201
- subject(:authorize) { gateway.authorize(1000, source, gateway_options) }
202
-
203
- include_examples "protects against connection errors" do
204
- let(:gateway_action) { :sale }
205
- end
206
-
207
- context 'with successful authorization', vcr: {
208
- cassette_name: 'gateway/authorize',
209
- match_requests_on: [:braintree_uri]
210
- } do
211
- include_examples "successful response"
212
-
213
- it 'passes "Solidus" as the channel parameter in the request' do
214
- expect_any_instance_of(Braintree::TransactionGateway).
215
- to receive(:sale).
216
- with(hash_including({ channel: "Solidus" })).and_call_original
217
- authorize
218
- end
219
-
220
- it 'authorizes the transaction', aggregate_failures: true do
221
- expect(authorize.message).to eq 'authorized'
222
- expect(authorize.authorization).to be_present
223
- end
224
-
225
- context 'with available device data' do
226
- it 'passes the device data as a parameter in the request' do
227
- expect_any_instance_of(Braintree::TransactionGateway).
228
- to receive(:sale).
229
- with(hash_including({ device_data: "fake-device-data" })).and_call_original
230
- authorize
231
- end
232
- end
233
-
234
- context 'without device_data' do
235
- let(:source) do
236
- SolidusBraintree::Source.create!(
237
- nonce: 'fake-valid-nonce',
238
- user: user,
239
- payment_type: payment_type,
240
- payment_method: gateway
241
- )
242
- end
243
-
244
- before do
245
- allow_any_instance_of(Braintree::TransactionGateway).to receive(:sale).and_call_original
246
- end
247
-
248
- it 'does not pass any device data in the request' do
249
- expect_any_instance_of(Braintree::TransactionGateway)
250
- .not_to receive(:sale).with(hash_including({ device_data: "" }))
251
-
252
- authorize
253
- end
254
- end
255
- end
256
-
257
- context 'with different merchant account for currency', vcr: {
258
- cassette_name: 'gateway/authorize/merchant_account/EUR',
259
- match_requests_on: [:braintree_uri]
260
- } do
261
- let(:currency) { 'EUR' }
262
-
263
- it 'settles with the correct currency' do
264
- transaction = braintree.transaction.find(authorize.authorization)
265
- expect(transaction.merchant_account_id).to eq 'stembolt_EUR'
266
- end
267
- end
268
-
269
- context 'with different paypal payee email for currency', vcr: {
270
- cassette_name: 'gateway/authorize/paypal/EUR',
271
- match_requests_on: [:braintree_uri]
272
- } do
273
- let(:currency) { 'EUR' }
274
-
275
- it 'uses the correct payee email' do
276
- expect_any_instance_of(Braintree::TransactionGateway).
277
- to receive(:sale).
278
- with(hash_including({
279
- options: {
280
- store_in_vault_on_success: true,
281
- paypal: {
282
- payee_email: ENV.fetch('BRAINTREE_PAYPAL_PAYEE_EMAIL')
283
- }
284
- }
285
- })).and_call_original
286
- authorize
287
- end
288
-
289
- context "with PayPal transaction", vcr: {
290
- cassette_name: 'gateway/authorize/paypal/address',
291
- match_requests_on: [:braintree_uri]
292
- } do
293
- it 'includes the shipping address in the request' do
294
- expect_any_instance_of(Braintree::TransactionGateway).
295
- to receive(:sale).
296
- with(hash_including({
297
- shipping: {
298
- first_name: "Bruce",
299
- last_name: "Wayne",
300
- street_address: "42 Spruce Lane Apt 312",
301
- locality: "Gotham",
302
- postal_code: "90210",
303
- region: "CA",
304
- country_code_alpha2: "US"
305
- }
306
- })).and_call_original
307
- authorize
308
- end
309
- end
310
- end
311
-
312
- context "with CreditCard transaction", vcr: {
313
- cassette_name: 'gateway/authorize/credit_card/address',
314
- match_requests_on: [:braintree_uri]
315
- } do
316
- let(:payment_type) { SolidusBraintree::Source::CREDIT_CARD }
317
-
318
- it 'includes the billing address in the request' do
319
- expect_any_instance_of(Braintree::TransactionGateway).to receive(:sale).
320
- with(hash_including({
321
- billing: {
322
- first_name: "Dick",
323
- last_name: "Grayson",
324
- street_address: "15 Robin Walk Apt 123",
325
- locality: "Blüdhaven",
326
- postal_code: "90210",
327
- region: "CA",
328
- country_code_alpha2: "US"
329
- }
330
- })).and_call_original
331
- authorize
332
- end
333
- end
334
- end
335
-
336
- describe "#capture" do
337
- subject(:capture) { gateway.capture(1000, authorized_id, {}) }
338
-
339
- context 'with successful capture', vcr: {
340
- cassette_name: 'gateway/capture',
341
- match_requests_on: [:braintree_uri]
342
- } do
343
- include_examples "successful response"
344
-
345
- it 'submits the transaction for settlement' do
346
- expect(capture.message).to eq "submitted_for_settlement"
347
- end
348
- end
349
-
350
- context 'with authorized transaction', vcr: {
351
- cassette_name: 'gateway/authorized_transaction',
352
- match_requests_on: [:braintree_uri]
353
- } do
354
- include_examples "protects against connection errors" do
355
- let(:gateway_action) { :submit_for_settlement }
356
- end
357
- end
358
- end
359
-
360
- describe "#credit" do
361
- subject(:credit) { gateway.credit(2000, source, settled_id, {}) }
362
-
363
- context 'with successful credit', vcr: {
364
- cassette_name: 'gateway/credit',
365
- match_requests_on: [:braintree_uri]
366
- } do
367
- include_examples "successful response"
368
-
369
- it 'credits the transaction' do
370
- expect(credit.message).to eq 'submitted_for_settlement'
371
- end
372
- end
373
-
374
- context 'with settled transaction', vcr: {
375
- cassette_name: 'gateway/settled_transaction',
376
- match_requests_on: [:braintree_uri]
377
- } do
378
- include_examples "protects against connection errors" do
379
- let(:gateway_action) { :refund }
380
- end
381
- end
382
- end
383
-
384
- describe "#void" do
385
- subject(:void) { gateway.void(authorized_id, source, {}) }
386
-
387
- context 'when successfully voided', vcr: {
388
- cassette_name: 'gateway/void',
389
- match_requests_on: [:braintree_uri]
390
- } do
391
- include_examples "successful response"
392
-
393
- it 'voids the transaction' do
394
- expect(void.message).to eq 'voided'
395
- end
396
- end
397
-
398
- context 'with authorized transaction', vcr: {
399
- cassette_name: 'gateway/authorized_transaction',
400
- match_requests_on: [:braintree_uri]
401
- } do
402
- include_examples "protects against connection errors" do
403
- let(:gateway_action) { :void }
404
- end
405
- end
406
- end
407
-
408
- describe "#cancel", vcr: {
409
- cassette_name: 'gateway/cancel',
410
- match_requests_on: [:braintree_uri]
411
- } do
412
- subject(:cancel) { gateway.cancel(transaction_id) }
413
-
414
- let(:transaction_id) { "fake_transaction_id" }
415
-
416
- context "when the transaction is found" do
417
- context "when it is voidable", vcr: {
418
- cassette_name: 'gateway/cancel/void',
419
- match_requests_on: [:braintree_uri]
420
- } do
421
- let(:transaction_id) { authorized_id }
422
-
423
- include_examples "successful response"
424
-
425
- it 'voids the transaction' do
426
- expect(cancel.message).to eq 'voided'
427
- end
428
- end
429
-
430
- context "when it is not voidable", vcr: {
431
- cassette_name: 'gateway/cancel/refunds',
432
- match_requests_on: [:braintree_uri]
433
- } do
434
- let(:transaction_id) { settled_id }
435
-
436
- include_examples "successful response"
437
-
438
- it 'refunds the transaction' do
439
- expect(cancel.message).to eq 'submitted_for_settlement'
440
- end
441
- end
442
- end
443
-
444
- context "when the transaction is not found", vcr: {
445
- cassette_name: 'gateway/cancel/missing',
446
- match_requests_on: [:braintree_uri]
447
- } do
448
- it 'raises an error' do
449
- expect{ cancel }.to raise_error ActiveMerchant::ConnectionError
450
- end
451
- end
452
- end
453
-
454
- describe '#try_void' do
455
- subject { gateway.try_void(instance_double(Spree::Payment, response_code: source.token)) }
456
-
457
- let(:transaction_request) do
458
- class_double(Braintree::Transaction,
459
- find: transaction_response)
460
- end
461
-
462
- before do
463
- client = instance_double(Braintree::Gateway)
464
- allow(client).to receive(:transaction) { transaction_request }
465
- allow(gateway).to receive(:braintree) { client }
466
- end
467
-
468
- context 'with voidable payment' do
469
- let(:transaction_response) do
470
- instance_double(Braintree::Transaction,
471
- status: Braintree::Transaction::Status::Authorized)
472
- end
473
-
474
- it 'voids the payment' do
475
- expect(gateway).to receive(:void)
476
- subject
477
- end
478
-
479
- context 'with error response mentioning an unvoidable transaction' do
480
- before do
481
- allow(gateway).to receive(:void) do
482
- raise ActiveMerchant::ConnectionError.new(
483
- 'Transaction can only be voided if status is authorized',
484
- double
485
- )
486
- end
487
- end
488
-
489
- it { is_expected.to be(false) }
490
- end
491
-
492
- context 'with other error response' do
493
- before do
494
- allow(gateway).to receive(:void) do
495
- raise ActiveMerchant::ConnectionError.new(
496
- 'Server unreachable',
497
- double
498
- )
499
- end
500
- end
501
-
502
- it { expect { subject }.to raise_error ActiveMerchant::ConnectionError }
503
- end
504
- end
505
-
506
- context 'with voidable paypal payment' do
507
- let(:transaction_response) do
508
- instance_double(Braintree::Transaction,
509
- status: Braintree::Transaction::Status::SettlementPending)
510
- end
511
-
512
- it 'voids the payment' do
513
- expect(gateway).to receive(:void)
514
- subject
515
- end
516
- end
517
-
518
- context 'with non-voidable payment' do
519
- let(:transaction_response) do
520
- instance_double(Braintree::Transaction,
521
- status: Braintree::Transaction::Status::Settled)
522
- end
523
-
524
- it { is_expected.to be(false) }
525
- end
526
- end
527
-
528
- describe "#create_profile" do
529
- subject(:profile) { gateway.create_profile(payment) }
530
-
531
- let(:payment) do
532
- build(:payment, {
533
- payment_method: gateway,
534
- source: source
535
- })
536
- end
537
-
538
- cassette_options = {
539
- cassette_name: "braintree/create_profile",
540
- match_requests_on: [:braintree_uri]
541
- }
542
- context "with no existing customer profile", vcr: cassette_options do
543
- it 'creates and returns a new customer profile', aggregate_failures: true do
544
- expect(profile).to be_a SolidusBraintree::Customer
545
- expect(profile.sources).to eq [source]
546
- expect(profile.braintree_customer_id).to be_present
547
- end
548
-
549
- it "sets a token on the payment source" do
550
- expect{ subject }.to change(source, :token)
551
- end
552
- end
553
-
554
- context "when the source already has a token" do
555
- before { source.token = "totally-a-valid-token" }
556
-
557
- it "does not create a new customer profile" do
558
- expect(profile).to be_nil
559
- end
560
- end
561
-
562
- context "when the source already has a customer" do
563
- before { source.build_customer }
564
-
565
- it "does not create a new customer profile" do
566
- expect(profile).to be_nil
567
- end
568
- end
569
-
570
- context "when the source has no nonce" do
571
- before { source.nonce = nil }
572
-
573
- it "does not create a new customer profile" do
574
- expect(profile).to be_nil
575
- end
576
- end
577
- end
578
-
579
- describe '#customer_profile_params' do
580
- subject(:params) { gateway.send(:customer_profile_params, payment) }
581
-
582
- let(:payment) do
583
- build(:payment, {
584
- payment_method: gateway,
585
- source: source
586
- })
587
- end
588
-
589
- context 'when payment does not belong to an order' do
590
- before { allow(payment).to receive(:order).and_return(nil) }
591
-
592
- it 'has the email param as nil' do
593
- expect(subject[:email]).to be_nil
594
- end
595
- end
596
-
597
- context 'when payment belongs to an order' do
598
- it 'has no email param' do
599
- expect(subject[:email]).to eq(payment.order.email)
600
- end
601
- end
602
- end
603
-
604
- describe "Braintree Customer" do
605
- subject(:customer) { braintree.customer.create(params).customer }
606
-
607
- let(:params) { gateway.send(:customer_profile_params, payment) }
608
-
609
- let(:payment) do
610
- build(:payment, {
611
- payment_method: gateway,
612
- source: source
613
- })
614
- end
615
-
616
- cassette_options = {
617
- cassette_name: 'gateway/customer',
618
- match_requests_on: [:braintree_uri]
619
- }
620
-
621
- context "with customer", vcr: cassette_options do
622
- it 'saves the customer email correctly' do
623
- allow(payment.order).to receive(:email).and_return('braintree@customers.com')
624
- expect(subject.email).to eq(payment.order.email)
625
- end
626
- end
627
- end
628
-
629
- shared_examples "sources_by_order" do
630
- let(:order) { FactoryBot.create :order, user: user, state: "complete", completed_at: Time.current }
631
- let(:gateway) { new_gateway.tap(&:save!) }
632
-
633
- let(:other_payment_method) { FactoryBot.create(:payment_method) }
634
-
635
- let(:source_without_profile) do
636
- SolidusBraintree::Source.create!(
637
- payment_method_id: gateway.id,
638
- payment_type: payment_type,
639
- user_id: user.id
640
- )
641
- end
642
-
643
- let(:source_with_profile) do
644
- SolidusBraintree::Source.create!(
645
- payment_method_id: gateway.id,
646
- payment_type: payment_type,
647
- user_id: user.id
648
- ).tap do |source|
649
- source.create_customer!(user: user)
650
- source.save!
651
- end
652
- end
653
-
654
- before do
655
- create(:payment, order: order, payment_method_id: payment_method_id, source: source)
656
- end
657
-
658
- context "when the order has payments with the braintree payment method" do
659
- let(:payment_method_id) { gateway.id }
660
-
661
- context "when the payment has a saved source with a profile" do
662
- let(:source) { source_with_profile }
663
-
664
- it "returns the source" do
665
- expect(subject.to_a).to eql([source])
666
- end
667
- end
668
-
669
- context "when the payment has a saved source without a profile" do
670
- let(:source) { source_without_profile }
671
-
672
- it "returns no result" do
673
- expect(subject.to_a).to eql([])
674
- end
675
- end
676
- end
677
-
678
- context "when the order has no payments with the braintree payment method" do
679
- let(:payment_method_id) { other_payment_method.id }
680
- let(:source) { FactoryBot.create :credit_card }
681
-
682
- it "returns no results" do
683
- expect(subject.to_a).to eql([])
684
- end
685
- end
686
- end
687
-
688
- describe "#sources_by_order" do
689
- subject { gateway.sources_by_order(order) }
690
-
691
- let(:gateway) { new_gateway.tap(&:save!) }
692
- let(:order) { FactoryBot.create :order, user: user, state: "complete", completed_at: Time.current }
693
-
694
- include_examples "sources_by_order"
695
- end
696
-
697
- describe "#reusable_sources" do
698
- subject { gateway.reusable_sources(order) }
699
-
700
- let(:order) { FactoryBot.build :order, user: user }
701
- let(:gateway) { new_gateway.tap(&:save!) }
702
-
703
- context "when an order is completed" do
704
- include_examples "sources_by_order"
705
- end
706
-
707
- context "when an order is not completed" do
708
- context "when the order has a user id" do
709
- let(:user) { FactoryBot.create(:user) }
710
-
711
- let!(:source_without_profile) do
712
- SolidusBraintree::Source.create!(
713
- payment_method_id: gateway.id,
714
- payment_type: payment_type,
715
- user_id: user.id
716
- )
717
- end
718
-
719
- let!(:source_with_profile) do
720
- SolidusBraintree::Source.create!(
721
- payment_method_id: gateway.id,
722
- payment_type: payment_type,
723
- user_id: user.id
724
- ).tap do |source|
725
- source.create_customer!(user: user)
726
- source.save!
727
- end
728
- end
729
-
730
- it "includes saved sources with payment profiles" do
731
- expect(subject).to include(source_with_profile)
732
- end
733
-
734
- it "excludes saved sources without payment profiles" do
735
- expect(subject).not_to include(source_without_profile)
736
- end
737
- end
738
-
739
- context "when the order does not have a user" do
740
- let(:user) { nil }
741
-
742
- it "returns no sources for guest users" do
743
- expect(subject).to eql([])
744
- end
745
- end
746
- end
747
- end
748
- end
749
-
750
- describe '.generate_token' do
751
- subject do
752
- # dont VCR ignore generate token request, use the existing cassette
753
- allow(VCR.request_ignorer.hooks).to receive(:[]).with(:ignore_request).and_return([])
754
- gateway.generate_token
755
- end
756
-
757
- context 'with connection enabled', vcr: {
758
- cassette_name: 'braintree/generate_token',
759
- match_requests_on: [:braintree_uri]
760
- } do
761
- it { is_expected.to be_a(String).and be_present }
762
- end
763
-
764
- context 'when token generation is disabled' do
765
- let(:gateway) do
766
- gateway = described_class.create!(name: 'braintree')
767
- gateway.preferred_token_generation_enabled = false
768
- gateway
769
- end
770
-
771
- it { expect { subject }.to raise_error SolidusBraintree::Gateway::TokenGenerationDisabledError }
772
- end
773
- end
774
- end