gocardless_pro 2.25.0 → 2.27.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +3 -3
  3. data/lib/gocardless_pro.rb +18 -0
  4. data/lib/gocardless_pro/client.rb +31 -1
  5. data/lib/gocardless_pro/resources/bank_authorisation.rb +87 -0
  6. data/lib/gocardless_pro/resources/billing_request.rb +86 -0
  7. data/lib/gocardless_pro/resources/billing_request_flow.rb +62 -0
  8. data/lib/gocardless_pro/resources/creditor.rb +2 -3
  9. data/lib/gocardless_pro/resources/institution.rb +45 -0
  10. data/lib/gocardless_pro/resources/payer_authorisation.rb +3 -0
  11. data/lib/gocardless_pro/resources/scenario_simulator.rb +42 -0
  12. data/lib/gocardless_pro/resources/webhook.rb +62 -0
  13. data/lib/gocardless_pro/services/bank_authorisations_service.rb +82 -0
  14. data/lib/gocardless_pro/services/billing_request_flows_service.rb +47 -0
  15. data/lib/gocardless_pro/services/billing_requests_service.rb +325 -0
  16. data/lib/gocardless_pro/services/institutions_service.rb +56 -0
  17. data/lib/gocardless_pro/services/payer_authorisations_service.rb +5 -5
  18. data/lib/gocardless_pro/services/scenario_simulators_service.rb +148 -0
  19. data/lib/gocardless_pro/services/subscriptions_service.rb +8 -3
  20. data/lib/gocardless_pro/services/webhooks_service.rb +113 -0
  21. data/lib/gocardless_pro/version.rb +1 -1
  22. data/spec/resources/bank_authorisation_spec.rb +259 -0
  23. data/spec/resources/billing_request_flow_spec.rb +129 -0
  24. data/spec/resources/billing_request_spec.rb +736 -0
  25. data/spec/resources/institution_spec.rb +103 -0
  26. data/spec/resources/scenario_simulator_spec.rb +63 -0
  27. data/spec/resources/webhook_spec.rb +323 -0
  28. data/spec/services/bank_authorisations_service_spec.rb +366 -0
  29. data/spec/services/billing_request_flows_service_spec.rb +152 -0
  30. data/spec/services/billing_requests_service_spec.rb +1042 -0
  31. data/spec/services/institutions_service_spec.rb +223 -0
  32. data/spec/services/scenario_simulators_service_spec.rb +74 -0
  33. data/spec/services/webhooks_service_spec.rb +545 -0
  34. metadata +39 -3
@@ -0,0 +1,103 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardlessPro::Resources::Institution 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 '#list' do
13
+ describe 'with no filters' do
14
+ subject(:get_list_response) { client.institutions.list }
15
+
16
+ before do
17
+ stub_request(:get, %r{.*api.gocardless.com/institutions}).to_return(
18
+ body: {
19
+ 'institutions' => [{
20
+
21
+ 'icon_url' => 'icon_url-input',
22
+ 'id' => 'id-input',
23
+ 'logo_url' => 'logo_url-input',
24
+ 'name' => 'name-input',
25
+ }],
26
+ meta: {
27
+ cursors: {
28
+ before: nil,
29
+ after: 'ABC123',
30
+ },
31
+ },
32
+ }.to_json,
33
+ headers: response_headers
34
+ )
35
+ end
36
+
37
+ it 'wraps each item in the resource class' do
38
+ expect(get_list_response.records.map(&:class).uniq.first).to eq(GoCardlessPro::Resources::Institution)
39
+
40
+ expect(get_list_response.records.first.icon_url).to eq('icon_url-input')
41
+
42
+ expect(get_list_response.records.first.id).to eq('id-input')
43
+
44
+ expect(get_list_response.records.first.logo_url).to eq('logo_url-input')
45
+
46
+ expect(get_list_response.records.first.name).to eq('name-input')
47
+ end
48
+
49
+ it 'exposes the cursors for before and after' do
50
+ expect(get_list_response.before).to eq(nil)
51
+ expect(get_list_response.after).to eq('ABC123')
52
+ end
53
+
54
+ specify { expect(get_list_response.api_response.headers).to eql('content-type' => 'application/json') }
55
+ end
56
+ end
57
+
58
+ describe '#all' do
59
+ let!(:first_response_stub) do
60
+ stub_request(:get, %r{.*api.gocardless.com/institutions$}).to_return(
61
+ body: {
62
+ 'institutions' => [{
63
+
64
+ 'icon_url' => 'icon_url-input',
65
+ 'id' => 'id-input',
66
+ 'logo_url' => 'logo_url-input',
67
+ 'name' => 'name-input',
68
+ }],
69
+ meta: {
70
+ cursors: { after: 'AB345' },
71
+ limit: 1,
72
+ },
73
+ }.to_json,
74
+ headers: response_headers
75
+ )
76
+ end
77
+
78
+ let!(:second_response_stub) do
79
+ stub_request(:get, %r{.*api.gocardless.com/institutions\?after=AB345}).to_return(
80
+ body: {
81
+ 'institutions' => [{
82
+
83
+ 'icon_url' => 'icon_url-input',
84
+ 'id' => 'id-input',
85
+ 'logo_url' => 'logo_url-input',
86
+ 'name' => 'name-input',
87
+ }],
88
+ meta: {
89
+ limit: 2,
90
+ cursors: {},
91
+ },
92
+ }.to_json,
93
+ headers: response_headers
94
+ )
95
+ end
96
+
97
+ it 'automatically makes the extra requests' do
98
+ expect(client.institutions.all.to_a.length).to eq(2)
99
+ expect(first_response_stub).to have_been_requested
100
+ expect(second_response_stub).to have_been_requested
101
+ end
102
+ end
103
+ end
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardlessPro::Resources::ScenarioSimulator 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 '#run' do
13
+ subject(:post_response) { client.scenario_simulators.run(resource_id) }
14
+
15
+ let(:resource_id) { 'ABC123' }
16
+
17
+ let!(:stub) do
18
+ # /scenario_simulators/%v/actions/run
19
+ stub_url = '/scenario_simulators/:identity/actions/run'.gsub(':identity', resource_id)
20
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
21
+ body: {
22
+ 'scenario_simulators' => {
23
+
24
+ 'id' => 'id-input',
25
+ },
26
+ }.to_json,
27
+ headers: response_headers
28
+ )
29
+ end
30
+
31
+ it 'wraps the response and calls the right endpoint' do
32
+ expect(post_response).to be_a(GoCardlessPro::Resources::ScenarioSimulator)
33
+
34
+ expect(stub).to have_been_requested
35
+ end
36
+
37
+ context 'when the request needs a body and custom header' do
38
+ let(:body) { { foo: 'bar' } }
39
+ let(:headers) { { 'Foo' => 'Bar' } }
40
+ subject(:post_response) { client.scenario_simulators.run(resource_id, body, headers) }
41
+
42
+ let(:resource_id) { 'ABC123' }
43
+
44
+ let!(:stub) do
45
+ # /scenario_simulators/%v/actions/run
46
+ stub_url = '/scenario_simulators/:identity/actions/run'.gsub(':identity', resource_id)
47
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
48
+ with(
49
+ body: { foo: 'bar' },
50
+ headers: { 'Foo' => 'Bar' }
51
+ ).to_return(
52
+ body: {
53
+ 'scenario_simulators' => {
54
+
55
+ 'id' => 'id-input',
56
+ },
57
+ }.to_json,
58
+ headers: response_headers
59
+ )
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,323 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardlessPro::Resources::Webhook 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 '#list' do
13
+ describe 'with no filters' do
14
+ subject(:get_list_response) { client.webhooks.list }
15
+
16
+ before do
17
+ stub_request(:get, %r{.*api.gocardless.com/webhooks}).to_return(
18
+ body: {
19
+ 'webhooks' => [{
20
+
21
+ 'created_at' => 'created_at-input',
22
+ 'id' => 'id-input',
23
+ 'is_test' => 'is_test-input',
24
+ 'request_body' => 'request_body-input',
25
+ 'request_headers' => 'request_headers-input',
26
+ 'response_body' => 'response_body-input',
27
+ 'response_body_truncated' => 'response_body_truncated-input',
28
+ 'response_code' => 'response_code-input',
29
+ 'response_headers' => 'response_headers-input',
30
+ 'response_headers_content_truncated' => 'response_headers_content_truncated-input',
31
+ 'response_headers_count_truncated' => 'response_headers_count_truncated-input',
32
+ 'successful' => 'successful-input',
33
+ 'url' => 'url-input',
34
+ }],
35
+ meta: {
36
+ cursors: {
37
+ before: nil,
38
+ after: 'ABC123',
39
+ },
40
+ },
41
+ }.to_json,
42
+ headers: response_headers
43
+ )
44
+ end
45
+
46
+ it 'wraps each item in the resource class' do
47
+ expect(get_list_response.records.map(&:class).uniq.first).to eq(GoCardlessPro::Resources::Webhook)
48
+
49
+ expect(get_list_response.records.first.created_at).to eq('created_at-input')
50
+
51
+ expect(get_list_response.records.first.id).to eq('id-input')
52
+
53
+ expect(get_list_response.records.first.is_test).to eq('is_test-input')
54
+
55
+ expect(get_list_response.records.first.request_body).to eq('request_body-input')
56
+
57
+ expect(get_list_response.records.first.request_headers).to eq('request_headers-input')
58
+
59
+ expect(get_list_response.records.first.response_body).to eq('response_body-input')
60
+
61
+ expect(get_list_response.records.first.response_body_truncated).to eq('response_body_truncated-input')
62
+
63
+ expect(get_list_response.records.first.response_code).to eq('response_code-input')
64
+
65
+ expect(get_list_response.records.first.response_headers).to eq('response_headers-input')
66
+
67
+ expect(get_list_response.records.first.response_headers_content_truncated).to eq('response_headers_content_truncated-input')
68
+
69
+ expect(get_list_response.records.first.response_headers_count_truncated).to eq('response_headers_count_truncated-input')
70
+
71
+ expect(get_list_response.records.first.successful).to eq('successful-input')
72
+
73
+ expect(get_list_response.records.first.url).to eq('url-input')
74
+ end
75
+
76
+ it 'exposes the cursors for before and after' do
77
+ expect(get_list_response.before).to eq(nil)
78
+ expect(get_list_response.after).to eq('ABC123')
79
+ end
80
+
81
+ specify { expect(get_list_response.api_response.headers).to eql('content-type' => 'application/json') }
82
+ end
83
+ end
84
+
85
+ describe '#all' do
86
+ let!(:first_response_stub) do
87
+ stub_request(:get, %r{.*api.gocardless.com/webhooks$}).to_return(
88
+ body: {
89
+ 'webhooks' => [{
90
+
91
+ 'created_at' => 'created_at-input',
92
+ 'id' => 'id-input',
93
+ 'is_test' => 'is_test-input',
94
+ 'request_body' => 'request_body-input',
95
+ 'request_headers' => 'request_headers-input',
96
+ 'response_body' => 'response_body-input',
97
+ 'response_body_truncated' => 'response_body_truncated-input',
98
+ 'response_code' => 'response_code-input',
99
+ 'response_headers' => 'response_headers-input',
100
+ 'response_headers_content_truncated' => 'response_headers_content_truncated-input',
101
+ 'response_headers_count_truncated' => 'response_headers_count_truncated-input',
102
+ 'successful' => 'successful-input',
103
+ 'url' => 'url-input',
104
+ }],
105
+ meta: {
106
+ cursors: { after: 'AB345' },
107
+ limit: 1,
108
+ },
109
+ }.to_json,
110
+ headers: response_headers
111
+ )
112
+ end
113
+
114
+ let!(:second_response_stub) do
115
+ stub_request(:get, %r{.*api.gocardless.com/webhooks\?after=AB345}).to_return(
116
+ body: {
117
+ 'webhooks' => [{
118
+
119
+ 'created_at' => 'created_at-input',
120
+ 'id' => 'id-input',
121
+ 'is_test' => 'is_test-input',
122
+ 'request_body' => 'request_body-input',
123
+ 'request_headers' => 'request_headers-input',
124
+ 'response_body' => 'response_body-input',
125
+ 'response_body_truncated' => 'response_body_truncated-input',
126
+ 'response_code' => 'response_code-input',
127
+ 'response_headers' => 'response_headers-input',
128
+ 'response_headers_content_truncated' => 'response_headers_content_truncated-input',
129
+ 'response_headers_count_truncated' => 'response_headers_count_truncated-input',
130
+ 'successful' => 'successful-input',
131
+ 'url' => 'url-input',
132
+ }],
133
+ meta: {
134
+ limit: 2,
135
+ cursors: {},
136
+ },
137
+ }.to_json,
138
+ headers: response_headers
139
+ )
140
+ end
141
+
142
+ it 'automatically makes the extra requests' do
143
+ expect(client.webhooks.all.to_a.length).to eq(2)
144
+ expect(first_response_stub).to have_been_requested
145
+ expect(second_response_stub).to have_been_requested
146
+ end
147
+ end
148
+
149
+ describe '#get' do
150
+ let(:id) { 'ID123' }
151
+
152
+ subject(:get_response) { client.webhooks.get(id) }
153
+
154
+ context 'passing in a custom header' do
155
+ let!(:stub) do
156
+ stub_url = '/webhooks/:identity'.gsub(':identity', id)
157
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).
158
+ with(headers: { 'Foo' => 'Bar' }).
159
+ to_return(
160
+ body: {
161
+ 'webhooks' => {
162
+
163
+ 'created_at' => 'created_at-input',
164
+ 'id' => 'id-input',
165
+ 'is_test' => 'is_test-input',
166
+ 'request_body' => 'request_body-input',
167
+ 'request_headers' => 'request_headers-input',
168
+ 'response_body' => 'response_body-input',
169
+ 'response_body_truncated' => 'response_body_truncated-input',
170
+ 'response_code' => 'response_code-input',
171
+ 'response_headers' => 'response_headers-input',
172
+ 'response_headers_content_truncated' => 'response_headers_content_truncated-input',
173
+ 'response_headers_count_truncated' => 'response_headers_count_truncated-input',
174
+ 'successful' => 'successful-input',
175
+ 'url' => 'url-input',
176
+ },
177
+ }.to_json,
178
+ headers: response_headers
179
+ )
180
+ end
181
+
182
+ subject(:get_response) do
183
+ client.webhooks.get(id, headers: {
184
+ 'Foo' => 'Bar',
185
+ })
186
+ end
187
+
188
+ it 'includes the header' do
189
+ get_response
190
+ expect(stub).to have_been_requested
191
+ end
192
+ end
193
+
194
+ context 'when there is a webhook to return' do
195
+ before do
196
+ stub_url = '/webhooks/:identity'.gsub(':identity', id)
197
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
198
+ body: {
199
+ 'webhooks' => {
200
+
201
+ 'created_at' => 'created_at-input',
202
+ 'id' => 'id-input',
203
+ 'is_test' => 'is_test-input',
204
+ 'request_body' => 'request_body-input',
205
+ 'request_headers' => 'request_headers-input',
206
+ 'response_body' => 'response_body-input',
207
+ 'response_body_truncated' => 'response_body_truncated-input',
208
+ 'response_code' => 'response_code-input',
209
+ 'response_headers' => 'response_headers-input',
210
+ 'response_headers_content_truncated' => 'response_headers_content_truncated-input',
211
+ 'response_headers_count_truncated' => 'response_headers_count_truncated-input',
212
+ 'successful' => 'successful-input',
213
+ 'url' => 'url-input',
214
+ },
215
+ }.to_json,
216
+ headers: response_headers
217
+ )
218
+ end
219
+
220
+ it 'wraps the response in a resource' do
221
+ expect(get_response).to be_a(GoCardlessPro::Resources::Webhook)
222
+ end
223
+ end
224
+
225
+ context 'when nothing is returned' do
226
+ before do
227
+ stub_url = '/webhooks/:identity'.gsub(':identity', id)
228
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
229
+ body: '',
230
+ headers: response_headers
231
+ )
232
+ end
233
+
234
+ it 'returns nil' do
235
+ expect(get_response).to be_nil
236
+ end
237
+ end
238
+
239
+ context "when an ID is specified which can't be included in a valid URI" do
240
+ let(:id) { '`' }
241
+
242
+ it "doesn't raise an error" do
243
+ expect { get_response }.to_not raise_error(/bad URI/)
244
+ end
245
+ end
246
+ end
247
+
248
+ describe '#retry' do
249
+ subject(:post_response) { client.webhooks.retry(resource_id) }
250
+
251
+ let(:resource_id) { 'ABC123' }
252
+
253
+ let!(:stub) do
254
+ # /webhooks/%v/actions/retry
255
+ stub_url = '/webhooks/:identity/actions/retry'.gsub(':identity', resource_id)
256
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
257
+ body: {
258
+ 'webhooks' => {
259
+
260
+ 'created_at' => 'created_at-input',
261
+ 'id' => 'id-input',
262
+ 'is_test' => 'is_test-input',
263
+ 'request_body' => 'request_body-input',
264
+ 'request_headers' => 'request_headers-input',
265
+ 'response_body' => 'response_body-input',
266
+ 'response_body_truncated' => 'response_body_truncated-input',
267
+ 'response_code' => 'response_code-input',
268
+ 'response_headers' => 'response_headers-input',
269
+ 'response_headers_content_truncated' => 'response_headers_content_truncated-input',
270
+ 'response_headers_count_truncated' => 'response_headers_count_truncated-input',
271
+ 'successful' => 'successful-input',
272
+ 'url' => 'url-input',
273
+ },
274
+ }.to_json,
275
+ headers: response_headers
276
+ )
277
+ end
278
+
279
+ it 'wraps the response and calls the right endpoint' do
280
+ expect(post_response).to be_a(GoCardlessPro::Resources::Webhook)
281
+
282
+ expect(stub).to have_been_requested
283
+ end
284
+
285
+ context 'when the request needs a body and custom header' do
286
+ let(:body) { { foo: 'bar' } }
287
+ let(:headers) { { 'Foo' => 'Bar' } }
288
+ subject(:post_response) { client.webhooks.retry(resource_id, body, headers) }
289
+
290
+ let(:resource_id) { 'ABC123' }
291
+
292
+ let!(:stub) do
293
+ # /webhooks/%v/actions/retry
294
+ stub_url = '/webhooks/:identity/actions/retry'.gsub(':identity', resource_id)
295
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
296
+ with(
297
+ body: { foo: 'bar' },
298
+ headers: { 'Foo' => 'Bar' }
299
+ ).to_return(
300
+ body: {
301
+ 'webhooks' => {
302
+
303
+ 'created_at' => 'created_at-input',
304
+ 'id' => 'id-input',
305
+ 'is_test' => 'is_test-input',
306
+ 'request_body' => 'request_body-input',
307
+ 'request_headers' => 'request_headers-input',
308
+ 'response_body' => 'response_body-input',
309
+ 'response_body_truncated' => 'response_body_truncated-input',
310
+ 'response_code' => 'response_code-input',
311
+ 'response_headers' => 'response_headers-input',
312
+ 'response_headers_content_truncated' => 'response_headers_content_truncated-input',
313
+ 'response_headers_count_truncated' => 'response_headers_count_truncated-input',
314
+ 'successful' => 'successful-input',
315
+ 'url' => 'url-input',
316
+ },
317
+ }.to_json,
318
+ headers: response_headers
319
+ )
320
+ end
321
+ end
322
+ end
323
+ end