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,893 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe GoCardlessPro::Services::MandatesService 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.mandates.create(params: new_resource) }
14
- context 'with a valid request' do
15
- let(:new_resource) do
16
- {
17
-
18
- 'authorisation_source' => 'authorisation_source-input',
19
- 'consent_parameters' => 'consent_parameters-input',
20
- 'created_at' => 'created_at-input',
21
- 'id' => 'id-input',
22
- 'links' => 'links-input',
23
- 'metadata' => 'metadata-input',
24
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
25
- 'payments_require_approval' => 'payments_require_approval-input',
26
- 'reference' => 'reference-input',
27
- 'scheme' => 'scheme-input',
28
- 'status' => 'status-input',
29
- 'verified_at' => 'verified_at-input',
30
- }
31
- end
32
-
33
- before do
34
- stub_request(:post, %r{.*api.gocardless.com/mandates}).
35
- with(
36
- body: {
37
- 'mandates' => {
38
-
39
- 'authorisation_source' => 'authorisation_source-input',
40
- 'consent_parameters' => 'consent_parameters-input',
41
- 'created_at' => 'created_at-input',
42
- 'id' => 'id-input',
43
- 'links' => 'links-input',
44
- 'metadata' => 'metadata-input',
45
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
46
- 'payments_require_approval' => 'payments_require_approval-input',
47
- 'reference' => 'reference-input',
48
- 'scheme' => 'scheme-input',
49
- 'status' => 'status-input',
50
- 'verified_at' => 'verified_at-input',
51
- },
52
- }
53
- ).
54
- to_return(
55
- body: {
56
- 'mandates' =>
57
-
58
- {
59
-
60
- 'authorisation_source' => 'authorisation_source-input',
61
- 'consent_parameters' => 'consent_parameters-input',
62
- 'created_at' => 'created_at-input',
63
- 'id' => 'id-input',
64
- 'links' => 'links-input',
65
- 'metadata' => 'metadata-input',
66
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
67
- 'payments_require_approval' => 'payments_require_approval-input',
68
- 'reference' => 'reference-input',
69
- 'scheme' => 'scheme-input',
70
- 'status' => 'status-input',
71
- 'verified_at' => 'verified_at-input',
72
- },
73
-
74
- }.to_json,
75
- headers: response_headers
76
- )
77
- end
78
-
79
- it 'creates and returns the resource' do
80
- expect(post_create_response).to be_a(GoCardlessPro::Resources::Mandate)
81
- end
82
-
83
- describe 'retry behaviour' do
84
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
85
-
86
- it 'retries timeouts' do
87
- stub = stub_request(:post, %r{.*api.gocardless.com/mandates}).
88
- to_timeout.then.to_return(status: 200, headers: response_headers)
89
-
90
- post_create_response
91
- expect(stub).to have_been_requested.twice
92
- end
93
-
94
- it 'retries 5XX errors' do
95
- stub = stub_request(:post, %r{.*api.gocardless.com/mandates}).
96
- to_return(status: 502,
97
- headers: { 'Content-Type' => 'text/html' },
98
- body: '<html><body>Response from Cloudflare</body></html>').
99
- then.to_return(status: 200, headers: response_headers)
100
-
101
- post_create_response
102
- expect(stub).to have_been_requested.twice
103
- end
104
- end
105
- end
106
-
107
- context 'with a request that returns a validation error' do
108
- let(:new_resource) { {} }
109
-
110
- before do
111
- stub_request(:post, %r{.*api.gocardless.com/mandates}).to_return(
112
- body: {
113
- error: {
114
- type: 'validation_failed',
115
- code: 422,
116
- errors: [
117
- { message: 'test error message', field: 'test_field' },
118
- ],
119
- },
120
- }.to_json,
121
- headers: response_headers,
122
- status: 422
123
- )
124
- end
125
-
126
- it 'throws the correct error' do
127
- expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
128
- end
129
- end
130
-
131
- context 'with a request that returns an idempotent creation conflict error' do
132
- let(:id) { 'ID123' }
133
-
134
- let(:new_resource) do
135
- {
136
-
137
- 'authorisation_source' => 'authorisation_source-input',
138
- 'consent_parameters' => 'consent_parameters-input',
139
- 'created_at' => 'created_at-input',
140
- 'id' => 'id-input',
141
- 'links' => 'links-input',
142
- 'metadata' => 'metadata-input',
143
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
144
- 'payments_require_approval' => 'payments_require_approval-input',
145
- 'reference' => 'reference-input',
146
- 'scheme' => 'scheme-input',
147
- 'status' => 'status-input',
148
- 'verified_at' => 'verified_at-input',
149
- }
150
- end
151
-
152
- let!(:post_stub) do
153
- stub_request(:post, %r{.*api.gocardless.com/mandates}).to_return(
154
- body: {
155
- error: {
156
- type: 'invalid_state',
157
- code: 409,
158
- errors: [
159
- {
160
- message: 'A resource has already been created with this idempotency key',
161
- reason: 'idempotent_creation_conflict',
162
- links: {
163
- conflicting_resource_id: id,
164
- },
165
- },
166
- ],
167
- },
168
- }.to_json,
169
- headers: response_headers,
170
- status: 409
171
- )
172
- end
173
-
174
- let!(:get_stub) do
175
- stub_url = "/mandates/#{id}"
176
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).
177
- to_return(
178
- body: {
179
- 'mandates' => {
180
-
181
- 'authorisation_source' => 'authorisation_source-input',
182
- 'consent_parameters' => 'consent_parameters-input',
183
- 'created_at' => 'created_at-input',
184
- 'id' => 'id-input',
185
- 'links' => 'links-input',
186
- 'metadata' => 'metadata-input',
187
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
188
- 'payments_require_approval' => 'payments_require_approval-input',
189
- 'reference' => 'reference-input',
190
- 'scheme' => 'scheme-input',
191
- 'status' => 'status-input',
192
- 'verified_at' => 'verified_at-input',
193
- },
194
- }.to_json,
195
- headers: response_headers
196
- )
197
- end
198
-
199
- context 'with default behaviour' do
200
- it 'fetches the already-created resource' do
201
- post_create_response
202
- expect(post_stub).to have_been_requested
203
- expect(get_stub).to have_been_requested
204
- end
205
- end
206
-
207
- context 'with on_idempotency_conflict: :raise' do
208
- let(:client) do
209
- GoCardlessPro::Client.new(
210
- access_token: 'SECRET_TOKEN',
211
- on_idempotency_conflict: :raise
212
- )
213
- end
214
-
215
- it 'raises an IdempotencyConflict error' do
216
- expect { post_create_response }.
217
- to raise_error(GoCardlessPro::IdempotencyConflict)
218
- end
219
- end
220
- end
221
- end
222
-
223
- describe '#list' do
224
- describe 'with no filters' do
225
- subject(:get_list_response) { client.mandates.list }
226
-
227
- let(:body) do
228
- {
229
- 'mandates' => [{
230
-
231
- 'authorisation_source' => 'authorisation_source-input',
232
- 'consent_parameters' => 'consent_parameters-input',
233
- 'created_at' => 'created_at-input',
234
- 'id' => 'id-input',
235
- 'links' => 'links-input',
236
- 'metadata' => 'metadata-input',
237
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
238
- 'payments_require_approval' => 'payments_require_approval-input',
239
- 'reference' => 'reference-input',
240
- 'scheme' => 'scheme-input',
241
- 'status' => 'status-input',
242
- 'verified_at' => 'verified_at-input',
243
- }],
244
- meta: {
245
- cursors: {
246
- before: nil,
247
- after: 'ABC123',
248
- },
249
- },
250
- }.to_json
251
- end
252
-
253
- before do
254
- stub_request(:get, %r{.*api.gocardless.com/mandates}).to_return(
255
- body: body,
256
- headers: response_headers
257
- )
258
- end
259
-
260
- it 'wraps each item in the resource class' do
261
- expect(get_list_response.records.map(&:class).uniq.first).to eq(GoCardlessPro::Resources::Mandate)
262
-
263
- expect(get_list_response.records.first.authorisation_source).to eq('authorisation_source-input')
264
-
265
- expect(get_list_response.records.first.consent_parameters).to eq('consent_parameters-input')
266
-
267
- expect(get_list_response.records.first.created_at).to eq('created_at-input')
268
-
269
- expect(get_list_response.records.first.id).to eq('id-input')
270
-
271
- expect(get_list_response.records.first.metadata).to eq('metadata-input')
272
-
273
- expect(get_list_response.records.first.next_possible_charge_date).to eq('next_possible_charge_date-input')
274
-
275
- expect(get_list_response.records.first.payments_require_approval).to eq('payments_require_approval-input')
276
-
277
- expect(get_list_response.records.first.reference).to eq('reference-input')
278
-
279
- expect(get_list_response.records.first.scheme).to eq('scheme-input')
280
-
281
- expect(get_list_response.records.first.status).to eq('status-input')
282
-
283
- expect(get_list_response.records.first.verified_at).to eq('verified_at-input')
284
- end
285
-
286
- it 'exposes the cursors for before and after' do
287
- expect(get_list_response.before).to eq(nil)
288
- expect(get_list_response.after).to eq('ABC123')
289
- end
290
-
291
- specify { expect(get_list_response.api_response.headers).to eql('content-type' => 'application/json') }
292
-
293
- describe 'retry behaviour' do
294
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
295
-
296
- it 'retries timeouts' do
297
- stub = stub_request(:get, %r{.*api.gocardless.com/mandates}).
298
- to_timeout.then.to_return(status: 200, headers: response_headers, body: body)
299
-
300
- get_list_response
301
- expect(stub).to have_been_requested.twice
302
- end
303
-
304
- it 'retries 5XX errors' do
305
- stub = stub_request(:get, %r{.*api.gocardless.com/mandates}).
306
- to_return(status: 502,
307
- headers: { 'Content-Type' => 'text/html' },
308
- body: '<html><body>Response from Cloudflare</body></html>').
309
- then.to_return(status: 200, headers: response_headers, body: body)
310
-
311
- get_list_response
312
- expect(stub).to have_been_requested.twice
313
- end
314
- end
315
- end
316
- end
317
-
318
- describe '#all' do
319
- let!(:first_response_stub) do
320
- stub_request(:get, %r{.*api.gocardless.com/mandates$}).to_return(
321
- body: {
322
- 'mandates' => [{
323
-
324
- 'authorisation_source' => 'authorisation_source-input',
325
- 'consent_parameters' => 'consent_parameters-input',
326
- 'created_at' => 'created_at-input',
327
- 'id' => 'id-input',
328
- 'links' => 'links-input',
329
- 'metadata' => 'metadata-input',
330
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
331
- 'payments_require_approval' => 'payments_require_approval-input',
332
- 'reference' => 'reference-input',
333
- 'scheme' => 'scheme-input',
334
- 'status' => 'status-input',
335
- 'verified_at' => 'verified_at-input',
336
- }],
337
- meta: {
338
- cursors: { after: 'AB345' },
339
- limit: 1,
340
- },
341
- }.to_json,
342
- headers: response_headers
343
- )
344
- end
345
-
346
- let!(:second_response_stub) do
347
- stub_request(:get, %r{.*api.gocardless.com/mandates\?after=AB345}).to_return(
348
- body: {
349
- 'mandates' => [{
350
-
351
- 'authorisation_source' => 'authorisation_source-input',
352
- 'consent_parameters' => 'consent_parameters-input',
353
- 'created_at' => 'created_at-input',
354
- 'id' => 'id-input',
355
- 'links' => 'links-input',
356
- 'metadata' => 'metadata-input',
357
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
358
- 'payments_require_approval' => 'payments_require_approval-input',
359
- 'reference' => 'reference-input',
360
- 'scheme' => 'scheme-input',
361
- 'status' => 'status-input',
362
- 'verified_at' => 'verified_at-input',
363
- }],
364
- meta: {
365
- limit: 2,
366
- cursors: {},
367
- },
368
- }.to_json,
369
- headers: response_headers
370
- )
371
- end
372
-
373
- it 'automatically makes the extra requests' do
374
- expect(client.mandates.all.to_a.length).to eq(2)
375
- expect(first_response_stub).to have_been_requested
376
- expect(second_response_stub).to have_been_requested
377
- end
378
-
379
- describe 'retry behaviour' do
380
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
381
-
382
- it 'retries timeouts' do
383
- first_response_stub = stub_request(:get, %r{.*api.gocardless.com/mandates$}).to_return(
384
- body: {
385
- 'mandates' => [{
386
-
387
- 'authorisation_source' => 'authorisation_source-input',
388
- 'consent_parameters' => 'consent_parameters-input',
389
- 'created_at' => 'created_at-input',
390
- 'id' => 'id-input',
391
- 'links' => 'links-input',
392
- 'metadata' => 'metadata-input',
393
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
394
- 'payments_require_approval' => 'payments_require_approval-input',
395
- 'reference' => 'reference-input',
396
- 'scheme' => 'scheme-input',
397
- 'status' => 'status-input',
398
- 'verified_at' => 'verified_at-input',
399
- }],
400
- meta: {
401
- cursors: { after: 'AB345' },
402
- limit: 1,
403
- },
404
- }.to_json,
405
- headers: response_headers
406
- )
407
-
408
- second_response_stub = stub_request(:get, %r{.*api.gocardless.com/mandates\?after=AB345}).
409
- to_timeout.then.
410
- to_return(
411
- body: {
412
- 'mandates' => [{
413
-
414
- 'authorisation_source' => 'authorisation_source-input',
415
- 'consent_parameters' => 'consent_parameters-input',
416
- 'created_at' => 'created_at-input',
417
- 'id' => 'id-input',
418
- 'links' => 'links-input',
419
- 'metadata' => 'metadata-input',
420
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
421
- 'payments_require_approval' => 'payments_require_approval-input',
422
- 'reference' => 'reference-input',
423
- 'scheme' => 'scheme-input',
424
- 'status' => 'status-input',
425
- 'verified_at' => 'verified_at-input',
426
- }],
427
- meta: {
428
- limit: 2,
429
- cursors: {},
430
- },
431
- }.to_json,
432
- headers: response_headers
433
- )
434
-
435
- client.mandates.all.to_a
436
-
437
- expect(first_response_stub).to have_been_requested
438
- expect(second_response_stub).to have_been_requested.twice
439
- end
440
-
441
- it 'retries 5XX errors' do
442
- first_response_stub = stub_request(:get, %r{.*api.gocardless.com/mandates$}).to_return(
443
- body: {
444
- 'mandates' => [{
445
-
446
- 'authorisation_source' => 'authorisation_source-input',
447
- 'consent_parameters' => 'consent_parameters-input',
448
- 'created_at' => 'created_at-input',
449
- 'id' => 'id-input',
450
- 'links' => 'links-input',
451
- 'metadata' => 'metadata-input',
452
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
453
- 'payments_require_approval' => 'payments_require_approval-input',
454
- 'reference' => 'reference-input',
455
- 'scheme' => 'scheme-input',
456
- 'status' => 'status-input',
457
- 'verified_at' => 'verified_at-input',
458
- }],
459
- meta: {
460
- cursors: { after: 'AB345' },
461
- limit: 1,
462
- },
463
- }.to_json,
464
- headers: response_headers
465
- )
466
-
467
- second_response_stub = stub_request(:get, %r{.*api.gocardless.com/mandates\?after=AB345}).
468
- to_return(
469
- status: 502,
470
- body: '<html><body>Response from Cloudflare</body></html>',
471
- headers: { 'Content-Type' => 'text/html' }
472
- ).then.to_return(
473
- body: {
474
- 'mandates' => [{
475
-
476
- 'authorisation_source' => 'authorisation_source-input',
477
- 'consent_parameters' => 'consent_parameters-input',
478
- 'created_at' => 'created_at-input',
479
- 'id' => 'id-input',
480
- 'links' => 'links-input',
481
- 'metadata' => 'metadata-input',
482
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
483
- 'payments_require_approval' => 'payments_require_approval-input',
484
- 'reference' => 'reference-input',
485
- 'scheme' => 'scheme-input',
486
- 'status' => 'status-input',
487
- 'verified_at' => 'verified_at-input',
488
- }],
489
- meta: {
490
- limit: 2,
491
- cursors: {},
492
- },
493
- }.to_json,
494
- headers: response_headers
495
- )
496
-
497
- client.mandates.all.to_a
498
-
499
- expect(first_response_stub).to have_been_requested
500
- expect(second_response_stub).to have_been_requested.twice
501
- end
502
- end
503
- end
504
-
505
- describe '#get' do
506
- let(:id) { 'ID123' }
507
-
508
- subject(:get_response) { client.mandates.get(id) }
509
-
510
- context 'passing in a custom header' do
511
- let!(:stub) do
512
- stub_url = '/mandates/:identity'.gsub(':identity', id)
513
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).
514
- with(headers: { 'Foo' => 'Bar' }).
515
- to_return(
516
- body: {
517
- 'mandates' => {
518
-
519
- 'authorisation_source' => 'authorisation_source-input',
520
- 'consent_parameters' => 'consent_parameters-input',
521
- 'created_at' => 'created_at-input',
522
- 'id' => 'id-input',
523
- 'links' => 'links-input',
524
- 'metadata' => 'metadata-input',
525
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
526
- 'payments_require_approval' => 'payments_require_approval-input',
527
- 'reference' => 'reference-input',
528
- 'scheme' => 'scheme-input',
529
- 'status' => 'status-input',
530
- 'verified_at' => 'verified_at-input',
531
- },
532
- }.to_json,
533
- headers: response_headers
534
- )
535
- end
536
-
537
- subject(:get_response) do
538
- client.mandates.get(id, headers: {
539
- 'Foo' => 'Bar',
540
- })
541
- end
542
-
543
- it 'includes the header' do
544
- get_response
545
- expect(stub).to have_been_requested
546
- end
547
- end
548
-
549
- context 'when there is a mandate to return' do
550
- before do
551
- stub_url = '/mandates/:identity'.gsub(':identity', id)
552
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
553
- body: {
554
- 'mandates' => {
555
-
556
- 'authorisation_source' => 'authorisation_source-input',
557
- 'consent_parameters' => 'consent_parameters-input',
558
- 'created_at' => 'created_at-input',
559
- 'id' => 'id-input',
560
- 'links' => 'links-input',
561
- 'metadata' => 'metadata-input',
562
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
563
- 'payments_require_approval' => 'payments_require_approval-input',
564
- 'reference' => 'reference-input',
565
- 'scheme' => 'scheme-input',
566
- 'status' => 'status-input',
567
- 'verified_at' => 'verified_at-input',
568
- },
569
- }.to_json,
570
- headers: response_headers
571
- )
572
- end
573
-
574
- it 'wraps the response in a resource' do
575
- expect(get_response).to be_a(GoCardlessPro::Resources::Mandate)
576
- end
577
- end
578
-
579
- context 'when nothing is returned' do
580
- before do
581
- stub_url = '/mandates/:identity'.gsub(':identity', id)
582
- stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
583
- body: '',
584
- headers: response_headers
585
- )
586
- end
587
-
588
- it 'returns nil' do
589
- expect(get_response).to be_nil
590
- end
591
- end
592
-
593
- context "when an ID is specified which can't be included in a valid URI" do
594
- let(:id) { '`' }
595
-
596
- it "doesn't raise an error" do
597
- expect { get_response }.to_not raise_error(/bad URI/)
598
- end
599
- end
600
-
601
- describe 'retry behaviour' do
602
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
603
-
604
- it 'retries timeouts' do
605
- stub_url = '/mandates/:identity'.gsub(':identity', id)
606
-
607
- stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
608
- to_timeout.then.to_return(status: 200, headers: response_headers)
609
-
610
- get_response
611
- expect(stub).to have_been_requested.twice
612
- end
613
-
614
- it 'retries 5XX errors, other than 500s' do
615
- stub_url = '/mandates/:identity'.gsub(':identity', id)
616
-
617
- stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
618
- to_return(status: 502,
619
- headers: { 'Content-Type' => 'text/html' },
620
- body: '<html><body>Response from Cloudflare</body></html>').
621
- then.to_return(status: 200, headers: response_headers)
622
-
623
- get_response
624
- expect(stub).to have_been_requested.twice
625
- end
626
-
627
- it 'retries 500 errors returned by the API' do
628
- stub_url = '/mandates/:identity'.gsub(':identity', id)
629
-
630
- gocardless_error = {
631
- 'error' => {
632
- 'message' => 'Internal server error',
633
- 'documentation_url' => 'https://developer.gocardless.com/#gocardless',
634
- 'errors' => [{
635
- 'message' => 'Internal server error',
636
- 'reason' => 'internal_server_error',
637
- }],
638
- 'type' => 'gocardless',
639
- 'code' => 500,
640
- 'request_id' => 'dummy_request_id',
641
- 'id' => 'dummy_exception_id',
642
- },
643
- }
644
-
645
- stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
646
- to_return(status: 500,
647
- headers: response_headers,
648
- body: gocardless_error.to_json).
649
- then.to_return(status: 200, headers: response_headers)
650
-
651
- get_response
652
- expect(stub).to have_been_requested.twice
653
- end
654
- end
655
- end
656
-
657
- describe '#update' do
658
- subject(:put_update_response) { client.mandates.update(id, params: update_params) }
659
- let(:id) { 'ABC123' }
660
-
661
- context 'with a valid request' do
662
- let(:update_params) { { 'hello' => 'world' } }
663
-
664
- let!(:stub) do
665
- stub_url = '/mandates/:identity'.gsub(':identity', id)
666
- stub_request(:put, /.*api.gocardless.com#{stub_url}/).to_return(
667
- body: {
668
- 'mandates' => {
669
-
670
- 'authorisation_source' => 'authorisation_source-input',
671
- 'consent_parameters' => 'consent_parameters-input',
672
- 'created_at' => 'created_at-input',
673
- 'id' => 'id-input',
674
- 'links' => 'links-input',
675
- 'metadata' => 'metadata-input',
676
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
677
- 'payments_require_approval' => 'payments_require_approval-input',
678
- 'reference' => 'reference-input',
679
- 'scheme' => 'scheme-input',
680
- 'status' => 'status-input',
681
- 'verified_at' => 'verified_at-input',
682
- },
683
- }.to_json,
684
- headers: response_headers
685
- )
686
- end
687
-
688
- it 'updates and returns the resource' do
689
- expect(put_update_response).to be_a(GoCardlessPro::Resources::Mandate)
690
- expect(stub).to have_been_requested
691
- end
692
-
693
- describe 'retry behaviour' do
694
- before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
695
-
696
- it 'retries timeouts' do
697
- stub_url = '/mandates/:identity'.gsub(':identity', id)
698
- stub = stub_request(:put, /.*api.gocardless.com#{stub_url}/).
699
- to_timeout.then.to_return(status: 200, headers: response_headers)
700
-
701
- put_update_response
702
- expect(stub).to have_been_requested.twice
703
- end
704
-
705
- it 'retries 5XX errors' do
706
- stub_url = '/mandates/:identity'.gsub(':identity', id)
707
- stub = stub_request(:put, /.*api.gocardless.com#{stub_url}/).
708
- to_return(status: 502,
709
- headers: { 'Content-Type' => 'text/html' },
710
- body: '<html><body>Response from Cloudflare</body></html>').
711
- then.to_return(status: 200, headers: response_headers)
712
-
713
- put_update_response
714
- expect(stub).to have_been_requested.twice
715
- end
716
- end
717
- end
718
- end
719
-
720
- describe '#cancel' do
721
- subject(:post_response) { client.mandates.cancel(resource_id) }
722
-
723
- let(:resource_id) { 'ABC123' }
724
-
725
- let!(:stub) do
726
- # /mandates/%v/actions/cancel
727
- stub_url = '/mandates/:identity/actions/cancel'.gsub(':identity', resource_id)
728
- stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
729
-
730
- body: {
731
- 'mandates' => {
732
-
733
- 'authorisation_source' => 'authorisation_source-input',
734
- 'consent_parameters' => 'consent_parameters-input',
735
- 'created_at' => 'created_at-input',
736
- 'id' => 'id-input',
737
- 'links' => 'links-input',
738
- 'metadata' => 'metadata-input',
739
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
740
- 'payments_require_approval' => 'payments_require_approval-input',
741
- 'reference' => 'reference-input',
742
- 'scheme' => 'scheme-input',
743
- 'status' => 'status-input',
744
- 'verified_at' => 'verified_at-input',
745
- },
746
- }.to_json,
747
-
748
- headers: response_headers
749
- )
750
- end
751
-
752
- it 'wraps the response and calls the right endpoint' do
753
- expect(post_response).to be_a(GoCardlessPro::Resources::Mandate)
754
-
755
- expect(stub).to have_been_requested
756
- end
757
-
758
- describe 'retry behaviour' do
759
- it "doesn't retry errors" do
760
- stub_url = '/mandates/:identity/actions/cancel'.gsub(':identity', resource_id)
761
- stub = stub_request(:post, /.*api.gocardless.com#{stub_url}/).
762
- to_timeout
763
-
764
- expect { post_response }.to raise_error(Faraday::ConnectionFailed)
765
- expect(stub).to have_been_requested
766
- end
767
- end
768
-
769
- context 'when the request needs a body and custom header' do
770
- let(:body) { { foo: 'bar' } }
771
- let(:headers) { { 'Foo' => 'Bar' } }
772
- subject(:post_response) { client.mandates.cancel(resource_id, body, headers) }
773
-
774
- let(:resource_id) { 'ABC123' }
775
-
776
- let!(:stub) do
777
- # /mandates/%v/actions/cancel
778
- stub_url = '/mandates/:identity/actions/cancel'.gsub(':identity', resource_id)
779
- stub_request(:post, /.*api.gocardless.com#{stub_url}/).
780
- with(
781
- body: { foo: 'bar' },
782
- headers: { 'Foo' => 'Bar' }
783
- ).to_return(
784
- body: {
785
- 'mandates' => {
786
-
787
- 'authorisation_source' => 'authorisation_source-input',
788
- 'consent_parameters' => 'consent_parameters-input',
789
- 'created_at' => 'created_at-input',
790
- 'id' => 'id-input',
791
- 'links' => 'links-input',
792
- 'metadata' => 'metadata-input',
793
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
794
- 'payments_require_approval' => 'payments_require_approval-input',
795
- 'reference' => 'reference-input',
796
- 'scheme' => 'scheme-input',
797
- 'status' => 'status-input',
798
- 'verified_at' => 'verified_at-input',
799
- },
800
- }.to_json,
801
- headers: response_headers
802
- )
803
- end
804
- end
805
- end
806
-
807
- describe '#reinstate' do
808
- subject(:post_response) { client.mandates.reinstate(resource_id) }
809
-
810
- let(:resource_id) { 'ABC123' }
811
-
812
- let!(:stub) do
813
- # /mandates/%v/actions/reinstate
814
- stub_url = '/mandates/:identity/actions/reinstate'.gsub(':identity', resource_id)
815
- stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
816
-
817
- body: {
818
- 'mandates' => {
819
-
820
- 'authorisation_source' => 'authorisation_source-input',
821
- 'consent_parameters' => 'consent_parameters-input',
822
- 'created_at' => 'created_at-input',
823
- 'id' => 'id-input',
824
- 'links' => 'links-input',
825
- 'metadata' => 'metadata-input',
826
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
827
- 'payments_require_approval' => 'payments_require_approval-input',
828
- 'reference' => 'reference-input',
829
- 'scheme' => 'scheme-input',
830
- 'status' => 'status-input',
831
- 'verified_at' => 'verified_at-input',
832
- },
833
- }.to_json,
834
-
835
- headers: response_headers
836
- )
837
- end
838
-
839
- it 'wraps the response and calls the right endpoint' do
840
- expect(post_response).to be_a(GoCardlessPro::Resources::Mandate)
841
-
842
- expect(stub).to have_been_requested
843
- end
844
-
845
- describe 'retry behaviour' do
846
- it "doesn't retry errors" do
847
- stub_url = '/mandates/:identity/actions/reinstate'.gsub(':identity', resource_id)
848
- stub = stub_request(:post, /.*api.gocardless.com#{stub_url}/).
849
- to_timeout
850
-
851
- expect { post_response }.to raise_error(Faraday::ConnectionFailed)
852
- expect(stub).to have_been_requested
853
- end
854
- end
855
-
856
- context 'when the request needs a body and custom header' do
857
- let(:body) { { foo: 'bar' } }
858
- let(:headers) { { 'Foo' => 'Bar' } }
859
- subject(:post_response) { client.mandates.reinstate(resource_id, body, headers) }
860
-
861
- let(:resource_id) { 'ABC123' }
862
-
863
- let!(:stub) do
864
- # /mandates/%v/actions/reinstate
865
- stub_url = '/mandates/:identity/actions/reinstate'.gsub(':identity', resource_id)
866
- stub_request(:post, /.*api.gocardless.com#{stub_url}/).
867
- with(
868
- body: { foo: 'bar' },
869
- headers: { 'Foo' => 'Bar' }
870
- ).to_return(
871
- body: {
872
- 'mandates' => {
873
-
874
- 'authorisation_source' => 'authorisation_source-input',
875
- 'consent_parameters' => 'consent_parameters-input',
876
- 'created_at' => 'created_at-input',
877
- 'id' => 'id-input',
878
- 'links' => 'links-input',
879
- 'metadata' => 'metadata-input',
880
- 'next_possible_charge_date' => 'next_possible_charge_date-input',
881
- 'payments_require_approval' => 'payments_require_approval-input',
882
- 'reference' => 'reference-input',
883
- 'scheme' => 'scheme-input',
884
- 'status' => 'status-input',
885
- 'verified_at' => 'verified_at-input',
886
- },
887
- }.to_json,
888
- headers: response_headers
889
- )
890
- end
891
- end
892
- end
893
- end