gocardless_pro 2.24.0 → 2.29.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +23 -4
  3. data/lib/gocardless_pro/api_service.rb +4 -0
  4. data/lib/gocardless_pro/client.rb +46 -1
  5. data/lib/gocardless_pro/error/authentication_error.rb +4 -0
  6. data/lib/gocardless_pro/error/permission_error.rb +4 -0
  7. data/lib/gocardless_pro/error/rate_limit_error.rb +4 -0
  8. data/lib/gocardless_pro/middlewares/raise_gocardless_errors.rb +12 -1
  9. data/lib/gocardless_pro/resources/bank_authorisation.rb +81 -0
  10. data/lib/gocardless_pro/resources/billing_request.rb +108 -0
  11. data/lib/gocardless_pro/resources/billing_request_flow.rb +72 -0
  12. data/lib/gocardless_pro/resources/billing_request_template.rb +68 -0
  13. data/lib/gocardless_pro/resources/block.rb +66 -0
  14. data/lib/gocardless_pro/resources/creditor.rb +2 -3
  15. data/lib/gocardless_pro/resources/event.rb +20 -0
  16. data/lib/gocardless_pro/resources/institution.rb +47 -0
  17. data/lib/gocardless_pro/resources/payer_authorisation.rb +131 -0
  18. data/lib/gocardless_pro/resources/payout_item.rb +4 -0
  19. data/lib/gocardless_pro/resources/redirect_flow.rb +2 -0
  20. data/lib/gocardless_pro/resources/scenario_simulator.rb +42 -0
  21. data/lib/gocardless_pro/resources/webhook.rb +62 -0
  22. data/lib/gocardless_pro/services/bank_authorisations_service.rb +80 -0
  23. data/lib/gocardless_pro/services/billing_request_flows_service.rb +70 -0
  24. data/lib/gocardless_pro/services/billing_request_templates_service.rb +131 -0
  25. data/lib/gocardless_pro/services/billing_requests_service.rb +352 -0
  26. data/lib/gocardless_pro/services/blocks_service.rb +223 -0
  27. data/lib/gocardless_pro/services/creditor_bank_accounts_service.rb +1 -5
  28. data/lib/gocardless_pro/services/creditors_service.rb +1 -3
  29. data/lib/gocardless_pro/services/currency_exchange_rates_service.rb +1 -1
  30. data/lib/gocardless_pro/services/customer_bank_accounts_service.rb +1 -5
  31. data/lib/gocardless_pro/services/customers_service.rb +1 -3
  32. data/lib/gocardless_pro/services/events_service.rb +1 -1
  33. data/lib/gocardless_pro/services/instalment_schedules_service.rb +1 -7
  34. data/lib/gocardless_pro/services/institutions_service.rb +56 -0
  35. data/lib/gocardless_pro/services/mandate_import_entries_service.rb +1 -1
  36. data/lib/gocardless_pro/services/mandate_imports_service.rb +0 -6
  37. data/lib/gocardless_pro/services/mandates_service.rb +1 -7
  38. data/lib/gocardless_pro/services/payer_authorisations_service.rb +202 -0
  39. data/lib/gocardless_pro/services/payments_service.rb +1 -7
  40. data/lib/gocardless_pro/services/payout_items_service.rb +1 -1
  41. data/lib/gocardless_pro/services/payouts_service.rb +1 -1
  42. data/lib/gocardless_pro/services/redirect_flows_service.rb +0 -4
  43. data/lib/gocardless_pro/services/refunds_service.rb +1 -3
  44. data/lib/gocardless_pro/services/scenario_simulators_service.rb +170 -0
  45. data/lib/gocardless_pro/services/subscriptions_service.rb +10 -13
  46. data/lib/gocardless_pro/services/tax_rates_service.rb +1 -1
  47. data/lib/gocardless_pro/services/webhooks_service.rb +111 -0
  48. data/lib/gocardless_pro/version.rb +1 -1
  49. data/lib/gocardless_pro.rb +30 -0
  50. data/spec/api_service_spec.rb +12 -1
  51. data/spec/middlewares/raise_gocardless_errors_spec.rb +30 -0
  52. data/spec/resources/bank_authorisation_spec.rb +259 -0
  53. data/spec/resources/billing_request_flow_spec.rb +219 -0
  54. data/spec/resources/billing_request_spec.rb +782 -0
  55. data/spec/resources/billing_request_template_spec.rb +502 -0
  56. data/spec/resources/block_spec.rb +560 -0
  57. data/spec/resources/institution_spec.rb +108 -0
  58. data/spec/resources/payer_authorisation_spec.rb +418 -0
  59. data/spec/resources/redirect_flow_spec.rb +9 -0
  60. data/spec/resources/scenario_simulator_spec.rb +63 -0
  61. data/spec/resources/webhook_spec.rb +323 -0
  62. data/spec/services/bank_authorisations_service_spec.rb +353 -0
  63. data/spec/services/billing_request_flows_service_spec.rb +253 -0
  64. data/spec/services/billing_request_templates_service_spec.rb +789 -0
  65. data/spec/services/billing_requests_service_spec.rb +1082 -0
  66. data/spec/services/blocks_service_spec.rb +823 -0
  67. data/spec/services/creditor_bank_accounts_service_spec.rb +0 -13
  68. data/spec/services/creditors_service_spec.rb +0 -13
  69. data/spec/services/customer_bank_accounts_service_spec.rb +0 -13
  70. data/spec/services/customers_service_spec.rb +0 -13
  71. data/spec/services/instalment_schedules_service_spec.rb +0 -26
  72. data/spec/services/institutions_service_spec.rb +232 -0
  73. data/spec/services/mandate_imports_service_spec.rb +0 -13
  74. data/spec/services/mandates_service_spec.rb +0 -13
  75. data/spec/services/payer_authorisations_service_spec.rb +559 -0
  76. data/spec/services/payments_service_spec.rb +0 -13
  77. data/spec/services/redirect_flows_service_spec.rb +9 -13
  78. data/spec/services/refunds_service_spec.rb +0 -13
  79. data/spec/services/scenario_simulators_service_spec.rb +74 -0
  80. data/spec/services/subscriptions_service_spec.rb +0 -13
  81. data/spec/services/webhooks_service_spec.rb +545 -0
  82. metadata +64 -7
@@ -0,0 +1,253 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardlessPro::Services::BillingRequestFlowsService 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.billing_request_flows.create(params: new_resource) }
14
+ context 'with a valid request' do
15
+ let(:new_resource) do
16
+ {
17
+
18
+ 'authorisation_url' => 'authorisation_url-input',
19
+ 'auto_fulfil' => 'auto_fulfil-input',
20
+ 'created_at' => 'created_at-input',
21
+ 'expires_at' => 'expires_at-input',
22
+ 'id' => 'id-input',
23
+ 'links' => 'links-input',
24
+ 'lock_bank_account' => 'lock_bank_account-input',
25
+ 'lock_customer_details' => 'lock_customer_details-input',
26
+ 'redirect_uri' => 'redirect_uri-input',
27
+ 'session_token' => 'session_token-input',
28
+ }
29
+ end
30
+
31
+ before do
32
+ stub_request(:post, %r{.*api.gocardless.com/billing_request_flows}).
33
+ with(
34
+ body: {
35
+ 'billing_request_flows' => {
36
+
37
+ 'authorisation_url' => 'authorisation_url-input',
38
+ 'auto_fulfil' => 'auto_fulfil-input',
39
+ 'created_at' => 'created_at-input',
40
+ 'expires_at' => 'expires_at-input',
41
+ 'id' => 'id-input',
42
+ 'links' => 'links-input',
43
+ 'lock_bank_account' => 'lock_bank_account-input',
44
+ 'lock_customer_details' => 'lock_customer_details-input',
45
+ 'redirect_uri' => 'redirect_uri-input',
46
+ 'session_token' => 'session_token-input',
47
+ },
48
+ }
49
+ ).
50
+ to_return(
51
+ body: {
52
+ 'billing_request_flows' =>
53
+
54
+ {
55
+
56
+ 'authorisation_url' => 'authorisation_url-input',
57
+ 'auto_fulfil' => 'auto_fulfil-input',
58
+ 'created_at' => 'created_at-input',
59
+ 'expires_at' => 'expires_at-input',
60
+ 'id' => 'id-input',
61
+ 'links' => 'links-input',
62
+ 'lock_bank_account' => 'lock_bank_account-input',
63
+ 'lock_customer_details' => 'lock_customer_details-input',
64
+ 'redirect_uri' => 'redirect_uri-input',
65
+ 'session_token' => 'session_token-input',
66
+ },
67
+
68
+ }.to_json,
69
+ headers: response_headers
70
+ )
71
+ end
72
+
73
+ it 'creates and returns the resource' do
74
+ expect(post_create_response).to be_a(GoCardlessPro::Resources::BillingRequestFlow)
75
+ end
76
+
77
+ describe 'retry behaviour' do
78
+ before { allow_any_instance_of(GoCardlessPro::Request).to receive(:sleep) }
79
+
80
+ it 'retries timeouts' do
81
+ stub = stub_request(:post, %r{.*api.gocardless.com/billing_request_flows}).
82
+ to_timeout.then.to_return(status: 200, headers: response_headers)
83
+
84
+ post_create_response
85
+ expect(stub).to have_been_requested.twice
86
+ end
87
+
88
+ it 'retries 5XX errors' do
89
+ stub = stub_request(:post, %r{.*api.gocardless.com/billing_request_flows}).
90
+ to_return(status: 502,
91
+ headers: { 'Content-Type' => 'text/html' },
92
+ body: '<html><body>Response from Cloudflare</body></html>').
93
+ then.to_return(status: 200, headers: response_headers)
94
+
95
+ post_create_response
96
+ expect(stub).to have_been_requested.twice
97
+ end
98
+ end
99
+ end
100
+
101
+ context 'with a request that returns a validation error' do
102
+ let(:new_resource) { {} }
103
+
104
+ before do
105
+ stub_request(:post, %r{.*api.gocardless.com/billing_request_flows}).to_return(
106
+ body: {
107
+ error: {
108
+ type: 'validation_failed',
109
+ code: 422,
110
+ errors: [
111
+ { message: 'test error message', field: 'test_field' },
112
+ ],
113
+ },
114
+ }.to_json,
115
+ headers: response_headers,
116
+ status: 422
117
+ )
118
+ end
119
+
120
+ it 'throws the correct error' do
121
+ expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
122
+ end
123
+ end
124
+
125
+ context 'with a request that returns an idempotent creation conflict error' do
126
+ let(:id) { 'ID123' }
127
+
128
+ let(:new_resource) do
129
+ {
130
+
131
+ 'authorisation_url' => 'authorisation_url-input',
132
+ 'auto_fulfil' => 'auto_fulfil-input',
133
+ 'created_at' => 'created_at-input',
134
+ 'expires_at' => 'expires_at-input',
135
+ 'id' => 'id-input',
136
+ 'links' => 'links-input',
137
+ 'lock_bank_account' => 'lock_bank_account-input',
138
+ 'lock_customer_details' => 'lock_customer_details-input',
139
+ 'redirect_uri' => 'redirect_uri-input',
140
+ 'session_token' => 'session_token-input',
141
+ }
142
+ end
143
+
144
+ let!(:post_stub) do
145
+ stub_request(:post, %r{.*api.gocardless.com/billing_request_flows}).to_return(
146
+ body: {
147
+ error: {
148
+ type: 'invalid_state',
149
+ code: 409,
150
+ errors: [
151
+ {
152
+ message: 'A resource has already been created with this idempotency key',
153
+ reason: 'idempotent_creation_conflict',
154
+ links: {
155
+ conflicting_resource_id: id,
156
+ },
157
+ },
158
+ ],
159
+ },
160
+ }.to_json,
161
+ headers: response_headers,
162
+ status: 409
163
+ )
164
+ end
165
+
166
+ it 'raises an InvalidStateError' do
167
+ expect { post_create_response }.to raise_error(GoCardlessPro::InvalidStateError)
168
+ expect(post_stub).to have_been_requested
169
+ end
170
+ end
171
+ end
172
+
173
+ describe '#initialise' do
174
+ subject(:post_response) { client.billing_request_flows.initialise(resource_id) }
175
+
176
+ let(:resource_id) { 'ABC123' }
177
+
178
+ let!(:stub) do
179
+ # /billing_request_flows/%v/actions/initialise
180
+ stub_url = '/billing_request_flows/:identity/actions/initialise'.gsub(':identity', resource_id)
181
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
182
+ body: {
183
+ 'billing_request_flows' => {
184
+
185
+ 'authorisation_url' => 'authorisation_url-input',
186
+ 'auto_fulfil' => 'auto_fulfil-input',
187
+ 'created_at' => 'created_at-input',
188
+ 'expires_at' => 'expires_at-input',
189
+ 'id' => 'id-input',
190
+ 'links' => 'links-input',
191
+ 'lock_bank_account' => 'lock_bank_account-input',
192
+ 'lock_customer_details' => 'lock_customer_details-input',
193
+ 'redirect_uri' => 'redirect_uri-input',
194
+ 'session_token' => 'session_token-input',
195
+ },
196
+ }.to_json,
197
+ headers: response_headers
198
+ )
199
+ end
200
+
201
+ it 'wraps the response and calls the right endpoint' do
202
+ expect(post_response).to be_a(GoCardlessPro::Resources::BillingRequestFlow)
203
+
204
+ expect(stub).to have_been_requested
205
+ end
206
+
207
+ describe 'retry behaviour' do
208
+ it "doesn't retry errors" do
209
+ stub_url = '/billing_request_flows/:identity/actions/initialise'.gsub(':identity', resource_id)
210
+ stub = stub_request(:post, /.*api.gocardless.com#{stub_url}/).
211
+ to_timeout
212
+
213
+ expect { post_response }.to raise_error(Faraday::ConnectionFailed)
214
+ expect(stub).to have_been_requested
215
+ end
216
+ end
217
+
218
+ context 'when the request needs a body and custom header' do
219
+ let(:body) { { foo: 'bar' } }
220
+ let(:headers) { { 'Foo' => 'Bar' } }
221
+ subject(:post_response) { client.billing_request_flows.initialise(resource_id, body, headers) }
222
+
223
+ let(:resource_id) { 'ABC123' }
224
+
225
+ let!(:stub) do
226
+ # /billing_request_flows/%v/actions/initialise
227
+ stub_url = '/billing_request_flows/:identity/actions/initialise'.gsub(':identity', resource_id)
228
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
229
+ with(
230
+ body: { foo: 'bar' },
231
+ headers: { 'Foo' => 'Bar' }
232
+ ).to_return(
233
+ body: {
234
+ 'billing_request_flows' => {
235
+
236
+ 'authorisation_url' => 'authorisation_url-input',
237
+ 'auto_fulfil' => 'auto_fulfil-input',
238
+ 'created_at' => 'created_at-input',
239
+ 'expires_at' => 'expires_at-input',
240
+ 'id' => 'id-input',
241
+ 'links' => 'links-input',
242
+ 'lock_bank_account' => 'lock_bank_account-input',
243
+ 'lock_customer_details' => 'lock_customer_details-input',
244
+ 'redirect_uri' => 'redirect_uri-input',
245
+ 'session_token' => 'session_token-input',
246
+ },
247
+ }.to_json,
248
+ headers: response_headers
249
+ )
250
+ end
251
+ end
252
+ end
253
+ end