gocardless_pro 2.41.0 → 2.41.1

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 (144) hide show
  1. checksums.yaml +4 -4
  2. data/lib/gocardless_pro/api_service.rb +4 -5
  3. data/lib/gocardless_pro/client.rb +3 -3
  4. data/lib/gocardless_pro/error/invalid_state_error.rb +1 -0
  5. data/lib/gocardless_pro/error.rb +1 -0
  6. data/lib/gocardless_pro/middlewares/raise_gocardless_errors.rb +7 -9
  7. data/lib/gocardless_pro/request.rb +5 -7
  8. data/lib/gocardless_pro/resources/bank_authorisation.rb +0 -2
  9. data/lib/gocardless_pro/resources/bank_details_lookup.rb +0 -2
  10. data/lib/gocardless_pro/resources/billing_request.rb +0 -2
  11. data/lib/gocardless_pro/resources/billing_request_flow.rb +0 -2
  12. data/lib/gocardless_pro/resources/billing_request_template.rb +0 -2
  13. data/lib/gocardless_pro/resources/block.rb +0 -2
  14. data/lib/gocardless_pro/resources/creditor.rb +0 -2
  15. data/lib/gocardless_pro/resources/creditor_bank_account.rb +0 -2
  16. data/lib/gocardless_pro/resources/currency_exchange_rate.rb +0 -2
  17. data/lib/gocardless_pro/resources/customer.rb +0 -2
  18. data/lib/gocardless_pro/resources/customer_bank_account.rb +0 -2
  19. data/lib/gocardless_pro/resources/customer_notification.rb +0 -2
  20. data/lib/gocardless_pro/resources/event.rb +0 -2
  21. data/lib/gocardless_pro/resources/instalment_schedule.rb +0 -2
  22. data/lib/gocardless_pro/resources/institution.rb +0 -2
  23. data/lib/gocardless_pro/resources/mandate.rb +0 -2
  24. data/lib/gocardless_pro/resources/mandate_import.rb +0 -2
  25. data/lib/gocardless_pro/resources/mandate_import_entry.rb +0 -2
  26. data/lib/gocardless_pro/resources/mandate_pdf.rb +0 -2
  27. data/lib/gocardless_pro/resources/payer_authorisation.rb +0 -2
  28. data/lib/gocardless_pro/resources/payment.rb +0 -2
  29. data/lib/gocardless_pro/resources/payout.rb +0 -2
  30. data/lib/gocardless_pro/resources/payout_item.rb +0 -2
  31. data/lib/gocardless_pro/resources/redirect_flow.rb +0 -2
  32. data/lib/gocardless_pro/resources/refund.rb +0 -2
  33. data/lib/gocardless_pro/resources/scenario_simulator.rb +0 -2
  34. data/lib/gocardless_pro/resources/scheme_identifier.rb +0 -2
  35. data/lib/gocardless_pro/resources/subscription.rb +0 -2
  36. data/lib/gocardless_pro/resources/tax_rate.rb +0 -2
  37. data/lib/gocardless_pro/resources/verification_detail.rb +0 -2
  38. data/lib/gocardless_pro/resources/webhook.rb +0 -2
  39. data/lib/gocardless_pro/services/bank_authorisations_service.rb +3 -1
  40. data/lib/gocardless_pro/services/billing_request_flows_service.rb +3 -1
  41. data/lib/gocardless_pro/services/billing_request_templates_service.rb +6 -2
  42. data/lib/gocardless_pro/services/billing_requests_service.rb +27 -9
  43. data/lib/gocardless_pro/services/blocks_service.rb +9 -3
  44. data/lib/gocardless_pro/services/creditor_bank_accounts_service.rb +6 -2
  45. data/lib/gocardless_pro/services/creditors_service.rb +9 -3
  46. data/lib/gocardless_pro/services/customer_bank_accounts_service.rb +9 -3
  47. data/lib/gocardless_pro/services/customer_notifications_service.rb +3 -1
  48. data/lib/gocardless_pro/services/customers_service.rb +9 -3
  49. data/lib/gocardless_pro/services/events_service.rb +3 -1
  50. data/lib/gocardless_pro/services/instalment_schedules_service.rb +9 -3
  51. data/lib/gocardless_pro/services/mandate_imports_service.rb +9 -3
  52. data/lib/gocardless_pro/services/mandates_service.rb +12 -4
  53. data/lib/gocardless_pro/services/payer_authorisations_service.rb +12 -4
  54. data/lib/gocardless_pro/services/payments_service.rb +12 -4
  55. data/lib/gocardless_pro/services/payouts_service.rb +6 -2
  56. data/lib/gocardless_pro/services/redirect_flows_service.rb +6 -2
  57. data/lib/gocardless_pro/services/refunds_service.rb +6 -2
  58. data/lib/gocardless_pro/services/scenario_simulators_service.rb +3 -1
  59. data/lib/gocardless_pro/services/scheme_identifiers_service.rb +3 -1
  60. data/lib/gocardless_pro/services/subscriptions_service.rb +15 -5
  61. data/lib/gocardless_pro/services/tax_rates_service.rb +3 -1
  62. data/lib/gocardless_pro/services/webhooks_service.rb +6 -2
  63. data/lib/gocardless_pro/version.rb +1 -2
  64. data/lib/gocardless_pro/webhook.rb +5 -4
  65. data/lib/gocardless_pro.rb +2 -4
  66. metadata +5 -195
  67. data/.github/workflows/tests.yml +0 -40
  68. data/.gitignore +0 -1
  69. data/.rspec +0 -2
  70. data/.rubocop.yml +0 -5
  71. data/.travis.yml +0 -10
  72. data/LICENSE.txt +0 -22
  73. data/demo.rb +0 -9
  74. data/gocardless_pro.gemspec +0 -39
  75. data/spec/api_response_spec.rb +0 -28
  76. data/spec/api_service_spec.rb +0 -188
  77. data/spec/client_spec.rb +0 -19
  78. data/spec/error_spec.rb +0 -53
  79. data/spec/middlewares/raise_gocardless_errors_spec.rb +0 -127
  80. data/spec/resources/bank_authorisation_spec.rb +0 -259
  81. data/spec/resources/bank_details_lookup_spec.rb +0 -121
  82. data/spec/resources/billing_request_flow_spec.rb +0 -257
  83. data/spec/resources/billing_request_spec.rb +0 -962
  84. data/spec/resources/billing_request_template_spec.rb +0 -515
  85. data/spec/resources/block_spec.rb +0 -577
  86. data/spec/resources/creditor_bank_account_spec.rb +0 -472
  87. data/spec/resources/creditor_spec.rb +0 -659
  88. data/spec/resources/currency_exchange_rate_spec.rb +0 -103
  89. data/spec/resources/customer_bank_account_spec.rb +0 -509
  90. data/spec/resources/customer_notification_spec.rb +0 -75
  91. data/spec/resources/customer_spec.rb +0 -616
  92. data/spec/resources/event_spec.rb +0 -210
  93. data/spec/resources/instalment_schedule_spec.rb +0 -636
  94. data/spec/resources/institution_spec.rb +0 -113
  95. data/spec/resources/mandate_import_entry_spec.rb +0 -206
  96. data/spec/resources/mandate_import_spec.rb +0 -344
  97. data/spec/resources/mandate_pdf_spec.rb +0 -117
  98. data/spec/resources/mandate_spec.rb +0 -600
  99. data/spec/resources/payer_authorisation_spec.rb +0 -422
  100. data/spec/resources/payment_spec.rb +0 -617
  101. data/spec/resources/payout_item_spec.rb +0 -101
  102. data/spec/resources/payout_spec.rb +0 -284
  103. data/spec/resources/redirect_flow_spec.rb +0 -347
  104. data/spec/resources/refund_spec.rb +0 -409
  105. data/spec/resources/scenario_simulator_spec.rb +0 -65
  106. data/spec/resources/scheme_identifier_spec.rb +0 -484
  107. data/spec/resources/subscription_spec.rb +0 -828
  108. data/spec/resources/tax_rate_spec.rb +0 -198
  109. data/spec/resources/verification_detail_spec.rb +0 -145
  110. data/spec/resources/webhook_spec.rb +0 -325
  111. data/spec/response_spec.rb +0 -34
  112. data/spec/services/bank_authorisations_service_spec.rb +0 -353
  113. data/spec/services/bank_details_lookups_service_spec.rb +0 -144
  114. data/spec/services/billing_request_flows_service_spec.rb +0 -291
  115. data/spec/services/billing_request_templates_service_spec.rb +0 -806
  116. data/spec/services/billing_requests_service_spec.rb +0 -1288
  117. data/spec/services/blocks_service_spec.rb +0 -840
  118. data/spec/services/creditor_bank_accounts_service_spec.rb +0 -725
  119. data/spec/services/creditors_service_spec.rb +0 -977
  120. data/spec/services/currency_exchange_rates_service_spec.rb +0 -223
  121. data/spec/services/customer_bank_accounts_service_spec.rb +0 -787
  122. data/spec/services/customer_notifications_service_spec.rb +0 -86
  123. data/spec/services/customers_service_spec.rb +0 -922
  124. data/spec/services/events_service_spec.rb +0 -401
  125. data/spec/services/instalment_schedules_service_spec.rb +0 -945
  126. data/spec/services/institutions_service_spec.rb +0 -241
  127. data/spec/services/mandate_import_entries_service_spec.rb +0 -345
  128. data/spec/services/mandate_imports_service_spec.rb +0 -460
  129. data/spec/services/mandate_pdfs_service_spec.rb +0 -140
  130. data/spec/services/mandates_service_spec.rb +0 -893
  131. data/spec/services/payer_authorisations_service_spec.rb +0 -563
  132. data/spec/services/payments_service_spec.rb +0 -914
  133. data/spec/services/payout_items_service_spec.rb +0 -221
  134. data/spec/services/payouts_service_spec.rb +0 -520
  135. data/spec/services/redirect_flows_service_spec.rb +0 -452
  136. data/spec/services/refunds_service_spec.rb +0 -668
  137. data/spec/services/scenario_simulators_service_spec.rb +0 -76
  138. data/spec/services/scheme_identifiers_service_spec.rb +0 -754
  139. data/spec/services/subscriptions_service_spec.rb +0 -1164
  140. data/spec/services/tax_rates_service_spec.rb +0 -381
  141. data/spec/services/verification_details_service_spec.rb +0 -168
  142. data/spec/services/webhooks_service_spec.rb +0 -547
  143. data/spec/spec_helper.rb +0 -90
  144. data/spec/webhook_spec.rb +0 -122
@@ -1,668 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe GoCardlessPro::Services::RefundsService do
4
- let(:client) do
5
- GoCardlessPro::Client.new(
6
- access_token: 'SECRET_TOKEN'
7
- )
8
- end
9
-
10
- let(:response_headers) { { 'Content-Type' => 'application/json' } }
11
-
12
- describe '#create' do
13
- subject(:post_create_response) { client.refunds.create(params: new_resource) }
14
- context 'with a valid request' do
15
- let(:new_resource) do
16
- {
17
-
18
- 'amount' => 'amount-input',
19
- 'created_at' => 'created_at-input',
20
- 'currency' => 'currency-input',
21
- 'fx' => 'fx-input',
22
- 'id' => 'id-input',
23
- 'links' => 'links-input',
24
- 'metadata' => 'metadata-input',
25
- 'reference' => 'reference-input',
26
- 'status' => 'status-input',
27
- }
28
- end
29
-
30
- before do
31
- stub_request(:post, %r{.*api.gocardless.com/refunds}).
32
- with(
33
- body: {
34
- 'refunds' => {
35
-
36
- 'amount' => 'amount-input',
37
- 'created_at' => 'created_at-input',
38
- 'currency' => 'currency-input',
39
- 'fx' => 'fx-input',
40
- 'id' => 'id-input',
41
- 'links' => 'links-input',
42
- 'metadata' => 'metadata-input',
43
- 'reference' => 'reference-input',
44
- 'status' => 'status-input',
45
- },
46
- }
47
- ).
48
- to_return(
49
- body: {
50
- 'refunds' =>
51
-
52
- {
53
-
54
- 'amount' => 'amount-input',
55
- 'created_at' => 'created_at-input',
56
- 'currency' => 'currency-input',
57
- 'fx' => 'fx-input',
58
- 'id' => 'id-input',
59
- 'links' => 'links-input',
60
- 'metadata' => 'metadata-input',
61
- 'reference' => 'reference-input',
62
- 'status' => 'status-input',
63
- },
64
-
65
- }.to_json,
66
- headers: response_headers
67
- )
68
- end
69
-
70
- it 'creates and returns the resource' do
71
- expect(post_create_response).to be_a(GoCardlessPro::Resources::Refund)
72
- end
73
-
74
- describe 'retry behaviour' do
75
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
76
-
77
- it 'retries timeouts' do
78
- stub = stub_request(:post, %r{.*api.gocardless.com/refunds}).
79
- to_timeout.then.to_return(status: 200, headers: response_headers)
80
-
81
- post_create_response
82
- expect(stub).to have_been_requested.twice
83
- end
84
-
85
- it 'retries 5XX errors' do
86
- stub = stub_request(:post, %r{.*api.gocardless.com/refunds}).
87
- to_return(status: 502,
88
- headers: { 'Content-Type' => 'text/html' },
89
- body: '<html><body>Response from Cloudflare</body></html>').
90
- then.to_return(status: 200, headers: response_headers)
91
-
92
- post_create_response
93
- expect(stub).to have_been_requested.twice
94
- end
95
- end
96
- end
97
-
98
- context 'with a request that returns a validation error' do
99
- let(:new_resource) { {} }
100
-
101
- before do
102
- stub_request(:post, %r{.*api.gocardless.com/refunds}).to_return(
103
- body: {
104
- error: {
105
- type: 'validation_failed',
106
- code: 422,
107
- errors: [
108
- { message: 'test error message', field: 'test_field' },
109
- ],
110
- },
111
- }.to_json,
112
- headers: response_headers,
113
- status: 422
114
- )
115
- end
116
-
117
- it 'throws the correct error' do
118
- expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
119
- end
120
- end
121
-
122
- context 'with a request that returns an idempotent creation conflict error' do
123
- let(:id) { 'ID123' }
124
-
125
- let(:new_resource) do
126
- {
127
-
128
- 'amount' => 'amount-input',
129
- 'created_at' => 'created_at-input',
130
- 'currency' => 'currency-input',
131
- 'fx' => 'fx-input',
132
- 'id' => 'id-input',
133
- 'links' => 'links-input',
134
- 'metadata' => 'metadata-input',
135
- 'reference' => 'reference-input',
136
- 'status' => 'status-input',
137
- }
138
- end
139
-
140
- let!(:post_stub) do
141
- stub_request(:post, %r{.*api.gocardless.com/refunds}).to_return(
142
- body: {
143
- error: {
144
- type: 'invalid_state',
145
- code: 409,
146
- errors: [
147
- {
148
- message: 'A resource has already been created with this idempotency key',
149
- reason: 'idempotent_creation_conflict',
150
- links: {
151
- conflicting_resource_id: id,
152
- },
153
- },
154
- ],
155
- },
156
- }.to_json,
157
- headers: response_headers,
158
- status: 409
159
- )
160
- end
161
-
162
- let!(:get_stub) do
163
- stub_url = "/refunds/#{id}"
164
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).
165
- to_return(
166
- body: {
167
- 'refunds' => {
168
-
169
- 'amount' => 'amount-input',
170
- 'created_at' => 'created_at-input',
171
- 'currency' => 'currency-input',
172
- 'fx' => 'fx-input',
173
- 'id' => 'id-input',
174
- 'links' => 'links-input',
175
- 'metadata' => 'metadata-input',
176
- 'reference' => 'reference-input',
177
- 'status' => 'status-input',
178
- },
179
- }.to_json,
180
- headers: response_headers
181
- )
182
- end
183
-
184
- context 'with default behaviour' do
185
- it 'fetches the already-created resource' do
186
- post_create_response
187
- expect(post_stub).to have_been_requested
188
- expect(get_stub).to have_been_requested
189
- end
190
- end
191
-
192
- context 'with on_idempotency_conflict: :raise' do
193
- let(:client) do
194
- GoCardlessPro::Client.new(
195
- access_token: 'SECRET_TOKEN',
196
- on_idempotency_conflict: :raise
197
- )
198
- end
199
-
200
- it 'raises an IdempotencyConflict error' do
201
- expect { post_create_response }.
202
- to raise_error(GoCardlessPro::IdempotencyConflict)
203
- end
204
- end
205
- end
206
- end
207
-
208
- describe '#list' do
209
- describe 'with no filters' do
210
- subject(:get_list_response) { client.refunds.list }
211
-
212
- let(:body) do
213
- {
214
- 'refunds' => [{
215
-
216
- 'amount' => 'amount-input',
217
- 'created_at' => 'created_at-input',
218
- 'currency' => 'currency-input',
219
- 'fx' => 'fx-input',
220
- 'id' => 'id-input',
221
- 'links' => 'links-input',
222
- 'metadata' => 'metadata-input',
223
- 'reference' => 'reference-input',
224
- 'status' => 'status-input',
225
- }],
226
- meta: {
227
- cursors: {
228
- before: nil,
229
- after: 'ABC123',
230
- },
231
- },
232
- }.to_json
233
- end
234
-
235
- before do
236
- stub_request(:get, %r{.*api.gocardless.com/refunds}).to_return(
237
- body: body,
238
- headers: response_headers
239
- )
240
- end
241
-
242
- it 'wraps each item in the resource class' do
243
- expect(get_list_response.records.map(&:class).uniq.first).to eq(GoCardlessPro::Resources::Refund)
244
-
245
- expect(get_list_response.records.first.amount).to eq('amount-input')
246
-
247
- expect(get_list_response.records.first.created_at).to eq('created_at-input')
248
-
249
- expect(get_list_response.records.first.currency).to eq('currency-input')
250
-
251
- expect(get_list_response.records.first.fx).to eq('fx-input')
252
-
253
- expect(get_list_response.records.first.id).to eq('id-input')
254
-
255
- expect(get_list_response.records.first.metadata).to eq('metadata-input')
256
-
257
- expect(get_list_response.records.first.reference).to eq('reference-input')
258
-
259
- expect(get_list_response.records.first.status).to eq('status-input')
260
- end
261
-
262
- it 'exposes the cursors for before and after' do
263
- expect(get_list_response.before).to eq(nil)
264
- expect(get_list_response.after).to eq('ABC123')
265
- end
266
-
267
- specify { expect(get_list_response.api_response.headers).to eql('content-type' => 'application/json') }
268
-
269
- describe 'retry behaviour' do
270
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
271
-
272
- it 'retries timeouts' do
273
- stub = stub_request(:get, %r{.*api.gocardless.com/refunds}).
274
- to_timeout.then.to_return(status: 200, headers: response_headers, body: body)
275
-
276
- get_list_response
277
- expect(stub).to have_been_requested.twice
278
- end
279
-
280
- it 'retries 5XX errors' do
281
- stub = stub_request(:get, %r{.*api.gocardless.com/refunds}).
282
- to_return(status: 502,
283
- headers: { 'Content-Type' => 'text/html' },
284
- body: '<html><body>Response from Cloudflare</body></html>').
285
- then.to_return(status: 200, headers: response_headers, body: body)
286
-
287
- get_list_response
288
- expect(stub).to have_been_requested.twice
289
- end
290
- end
291
- end
292
- end
293
-
294
- describe '#all' do
295
- let!(:first_response_stub) do
296
- stub_request(:get, %r{.*api.gocardless.com/refunds$}).to_return(
297
- body: {
298
- 'refunds' => [{
299
-
300
- 'amount' => 'amount-input',
301
- 'created_at' => 'created_at-input',
302
- 'currency' => 'currency-input',
303
- 'fx' => 'fx-input',
304
- 'id' => 'id-input',
305
- 'links' => 'links-input',
306
- 'metadata' => 'metadata-input',
307
- 'reference' => 'reference-input',
308
- 'status' => 'status-input',
309
- }],
310
- meta: {
311
- cursors: { after: 'AB345' },
312
- limit: 1,
313
- },
314
- }.to_json,
315
- headers: response_headers
316
- )
317
- end
318
-
319
- let!(:second_response_stub) do
320
- stub_request(:get, %r{.*api.gocardless.com/refunds\?after=AB345}).to_return(
321
- body: {
322
- 'refunds' => [{
323
-
324
- 'amount' => 'amount-input',
325
- 'created_at' => 'created_at-input',
326
- 'currency' => 'currency-input',
327
- 'fx' => 'fx-input',
328
- 'id' => 'id-input',
329
- 'links' => 'links-input',
330
- 'metadata' => 'metadata-input',
331
- 'reference' => 'reference-input',
332
- 'status' => 'status-input',
333
- }],
334
- meta: {
335
- limit: 2,
336
- cursors: {},
337
- },
338
- }.to_json,
339
- headers: response_headers
340
- )
341
- end
342
-
343
- it 'automatically makes the extra requests' do
344
- expect(client.refunds.all.to_a.length).to eq(2)
345
- expect(first_response_stub).to have_been_requested
346
- expect(second_response_stub).to have_been_requested
347
- end
348
-
349
- describe 'retry behaviour' do
350
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
351
-
352
- it 'retries timeouts' do
353
- first_response_stub = stub_request(:get, %r{.*api.gocardless.com/refunds$}).to_return(
354
- body: {
355
- 'refunds' => [{
356
-
357
- 'amount' => 'amount-input',
358
- 'created_at' => 'created_at-input',
359
- 'currency' => 'currency-input',
360
- 'fx' => 'fx-input',
361
- 'id' => 'id-input',
362
- 'links' => 'links-input',
363
- 'metadata' => 'metadata-input',
364
- 'reference' => 'reference-input',
365
- 'status' => 'status-input',
366
- }],
367
- meta: {
368
- cursors: { after: 'AB345' },
369
- limit: 1,
370
- },
371
- }.to_json,
372
- headers: response_headers
373
- )
374
-
375
- second_response_stub = stub_request(:get, %r{.*api.gocardless.com/refunds\?after=AB345}).
376
- to_timeout.then.
377
- to_return(
378
- body: {
379
- 'refunds' => [{
380
-
381
- 'amount' => 'amount-input',
382
- 'created_at' => 'created_at-input',
383
- 'currency' => 'currency-input',
384
- 'fx' => 'fx-input',
385
- 'id' => 'id-input',
386
- 'links' => 'links-input',
387
- 'metadata' => 'metadata-input',
388
- 'reference' => 'reference-input',
389
- 'status' => 'status-input',
390
- }],
391
- meta: {
392
- limit: 2,
393
- cursors: {},
394
- },
395
- }.to_json,
396
- headers: response_headers
397
- )
398
-
399
- client.refunds.all.to_a
400
-
401
- expect(first_response_stub).to have_been_requested
402
- expect(second_response_stub).to have_been_requested.twice
403
- end
404
-
405
- it 'retries 5XX errors' do
406
- first_response_stub = stub_request(:get, %r{.*api.gocardless.com/refunds$}).to_return(
407
- body: {
408
- 'refunds' => [{
409
-
410
- 'amount' => 'amount-input',
411
- 'created_at' => 'created_at-input',
412
- 'currency' => 'currency-input',
413
- 'fx' => 'fx-input',
414
- 'id' => 'id-input',
415
- 'links' => 'links-input',
416
- 'metadata' => 'metadata-input',
417
- 'reference' => 'reference-input',
418
- 'status' => 'status-input',
419
- }],
420
- meta: {
421
- cursors: { after: 'AB345' },
422
- limit: 1,
423
- },
424
- }.to_json,
425
- headers: response_headers
426
- )
427
-
428
- second_response_stub = stub_request(:get, %r{.*api.gocardless.com/refunds\?after=AB345}).
429
- to_return(
430
- status: 502,
431
- body: '<html><body>Response from Cloudflare</body></html>',
432
- headers: { 'Content-Type' => 'text/html' }
433
- ).then.to_return(
434
- body: {
435
- 'refunds' => [{
436
-
437
- 'amount' => 'amount-input',
438
- 'created_at' => 'created_at-input',
439
- 'currency' => 'currency-input',
440
- 'fx' => 'fx-input',
441
- 'id' => 'id-input',
442
- 'links' => 'links-input',
443
- 'metadata' => 'metadata-input',
444
- 'reference' => 'reference-input',
445
- 'status' => 'status-input',
446
- }],
447
- meta: {
448
- limit: 2,
449
- cursors: {},
450
- },
451
- }.to_json,
452
- headers: response_headers
453
- )
454
-
455
- client.refunds.all.to_a
456
-
457
- expect(first_response_stub).to have_been_requested
458
- expect(second_response_stub).to have_been_requested.twice
459
- end
460
- end
461
- end
462
-
463
- describe '#get' do
464
- let(:id) { 'ID123' }
465
-
466
- subject(:get_response) { client.refunds.get(id) }
467
-
468
- context 'passing in a custom header' do
469
- let!(:stub) do
470
- stub_url = '/refunds/:identity'.gsub(':identity', id)
471
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).
472
- with(headers: { 'Foo' => 'Bar' }).
473
- to_return(
474
- body: {
475
- 'refunds' => {
476
-
477
- 'amount' => 'amount-input',
478
- 'created_at' => 'created_at-input',
479
- 'currency' => 'currency-input',
480
- 'fx' => 'fx-input',
481
- 'id' => 'id-input',
482
- 'links' => 'links-input',
483
- 'metadata' => 'metadata-input',
484
- 'reference' => 'reference-input',
485
- 'status' => 'status-input',
486
- },
487
- }.to_json,
488
- headers: response_headers
489
- )
490
- end
491
-
492
- subject(:get_response) do
493
- client.refunds.get(id, headers: {
494
- 'Foo' => 'Bar',
495
- })
496
- end
497
-
498
- it 'includes the header' do
499
- get_response
500
- expect(stub).to have_been_requested
501
- end
502
- end
503
-
504
- context 'when there is a refund to return' do
505
- before do
506
- stub_url = '/refunds/:identity'.gsub(':identity', id)
507
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
508
- body: {
509
- 'refunds' => {
510
-
511
- 'amount' => 'amount-input',
512
- 'created_at' => 'created_at-input',
513
- 'currency' => 'currency-input',
514
- 'fx' => 'fx-input',
515
- 'id' => 'id-input',
516
- 'links' => 'links-input',
517
- 'metadata' => 'metadata-input',
518
- 'reference' => 'reference-input',
519
- 'status' => 'status-input',
520
- },
521
- }.to_json,
522
- headers: response_headers
523
- )
524
- end
525
-
526
- it 'wraps the response in a resource' do
527
- expect(get_response).to be_a(GoCardlessPro::Resources::Refund)
528
- end
529
- end
530
-
531
- context 'when nothing is returned' do
532
- before do
533
- stub_url = '/refunds/:identity'.gsub(':identity', id)
534
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
535
- body: '',
536
- headers: response_headers
537
- )
538
- end
539
-
540
- it 'returns nil' do
541
- expect(get_response).to be_nil
542
- end
543
- end
544
-
545
- context "when an ID is specified which can't be included in a valid URI" do
546
- let(:id) { '`' }
547
-
548
- it "doesn't raise an error" do
549
- expect { get_response }.to_not raise_error(/bad URI/)
550
- end
551
- end
552
-
553
- describe 'retry behaviour' do
554
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
555
-
556
- it 'retries timeouts' do
557
- stub_url = '/refunds/:identity'.gsub(':identity', id)
558
-
559
- stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
560
- to_timeout.then.to_return(status: 200, headers: response_headers)
561
-
562
- get_response
563
- expect(stub).to have_been_requested.twice
564
- end
565
-
566
- it 'retries 5XX errors, other than 500s' do
567
- stub_url = '/refunds/:identity'.gsub(':identity', id)
568
-
569
- stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
570
- to_return(status: 502,
571
- headers: { 'Content-Type' => 'text/html' },
572
- body: '<html><body>Response from Cloudflare</body></html>').
573
- then.to_return(status: 200, headers: response_headers)
574
-
575
- get_response
576
- expect(stub).to have_been_requested.twice
577
- end
578
-
579
- it 'retries 500 errors returned by the API' do
580
- stub_url = '/refunds/:identity'.gsub(':identity', id)
581
-
582
- gocardless_error = {
583
- 'error' => {
584
- 'message' => 'Internal server error',
585
- 'documentation_url' => 'https://developer.gocardless.com/#gocardless',
586
- 'errors' => [{
587
- 'message' => 'Internal server error',
588
- 'reason' => 'internal_server_error',
589
- }],
590
- 'type' => 'gocardless',
591
- 'code' => 500,
592
- 'request_id' => 'dummy_request_id',
593
- 'id' => 'dummy_exception_id',
594
- },
595
- }
596
-
597
- stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
598
- to_return(status: 500,
599
- headers: response_headers,
600
- body: gocardless_error.to_json).
601
- then.to_return(status: 200, headers: response_headers)
602
-
603
- get_response
604
- expect(stub).to have_been_requested.twice
605
- end
606
- end
607
- end
608
-
609
- describe '#update' do
610
- subject(:put_update_response) { client.refunds.update(id, params: update_params) }
611
- let(:id) { 'ABC123' }
612
-
613
- context 'with a valid request' do
614
- let(:update_params) { { 'hello' => 'world' } }
615
-
616
- let!(:stub) do
617
- stub_url = '/refunds/:identity'.gsub(':identity', id)
618
- stub_request(:put, /.*api.gocardless.com#{stub_url}/).to_return(
619
- body: {
620
- 'refunds' => {
621
-
622
- 'amount' => 'amount-input',
623
- 'created_at' => 'created_at-input',
624
- 'currency' => 'currency-input',
625
- 'fx' => 'fx-input',
626
- 'id' => 'id-input',
627
- 'links' => 'links-input',
628
- 'metadata' => 'metadata-input',
629
- 'reference' => 'reference-input',
630
- 'status' => 'status-input',
631
- },
632
- }.to_json,
633
- headers: response_headers
634
- )
635
- end
636
-
637
- it 'updates and returns the resource' do
638
- expect(put_update_response).to be_a(GoCardlessPro::Resources::Refund)
639
- expect(stub).to have_been_requested
640
- end
641
-
642
- describe 'retry behaviour' do
643
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
644
-
645
- it 'retries timeouts' do
646
- stub_url = '/refunds/:identity'.gsub(':identity', id)
647
- stub = stub_request(:put, /.*api.gocardless.com#{stub_url}/).
648
- to_timeout.then.to_return(status: 200, headers: response_headers)
649
-
650
- put_update_response
651
- expect(stub).to have_been_requested.twice
652
- end
653
-
654
- it 'retries 5XX errors' do
655
- stub_url = '/refunds/:identity'.gsub(':identity', id)
656
- stub = stub_request(:put, /.*api.gocardless.com#{stub_url}/).
657
- to_return(status: 502,
658
- headers: { 'Content-Type' => 'text/html' },
659
- body: '<html><body>Response from Cloudflare</body></html>').
660
- then.to_return(status: 200, headers: response_headers)
661
-
662
- put_update_response
663
- expect(stub).to have_been_requested.twice
664
- end
665
- end
666
- end
667
- end
668
- end