gocardless_pro 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +2 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +146 -0
  6. data/circle.yml +3 -0
  7. data/demo.rb +9 -0
  8. data/gocardless_pro.gemspec +26 -0
  9. data/lib/gocardless_pro.rb +73 -0
  10. data/lib/gocardless_pro/api_service.rb +58 -0
  11. data/lib/gocardless_pro/client.rb +135 -0
  12. data/lib/gocardless_pro/error.rb +42 -0
  13. data/lib/gocardless_pro/error/gocardless_error.rb +5 -0
  14. data/lib/gocardless_pro/error/invalid_api_usage_error.rb +5 -0
  15. data/lib/gocardless_pro/error/invalid_state_error.rb +5 -0
  16. data/lib/gocardless_pro/error/validation_error.rb +5 -0
  17. data/lib/gocardless_pro/list_response.rb +29 -0
  18. data/lib/gocardless_pro/paginator.rb +43 -0
  19. data/lib/gocardless_pro/request.rb +69 -0
  20. data/lib/gocardless_pro/resources/creditor.rb +84 -0
  21. data/lib/gocardless_pro/resources/creditor_bank_account.rb +78 -0
  22. data/lib/gocardless_pro/resources/customer.rb +75 -0
  23. data/lib/gocardless_pro/resources/customer_bank_account.rb +80 -0
  24. data/lib/gocardless_pro/resources/event.rb +75 -0
  25. data/lib/gocardless_pro/resources/helper.rb +29 -0
  26. data/lib/gocardless_pro/resources/mandate.rb +70 -0
  27. data/lib/gocardless_pro/resources/payment.rb +87 -0
  28. data/lib/gocardless_pro/resources/payout.rb +66 -0
  29. data/lib/gocardless_pro/resources/redirect_flow.rb +106 -0
  30. data/lib/gocardless_pro/resources/refund.rb +71 -0
  31. data/lib/gocardless_pro/resources/subscription.rb +155 -0
  32. data/lib/gocardless_pro/response.rb +77 -0
  33. data/lib/gocardless_pro/services/base_service.rb +28 -0
  34. data/lib/gocardless_pro/services/creditor_bank_accounts_service.rb +119 -0
  35. data/lib/gocardless_pro/services/creditors_service.rb +113 -0
  36. data/lib/gocardless_pro/services/customer_bank_accounts_service.rb +154 -0
  37. data/lib/gocardless_pro/services/customers_service.rb +113 -0
  38. data/lib/gocardless_pro/services/events_service.rb +80 -0
  39. data/lib/gocardless_pro/services/helpers_service.rb +99 -0
  40. data/lib/gocardless_pro/services/mandates_service.rb +173 -0
  41. data/lib/gocardless_pro/services/payments_service.rb +168 -0
  42. data/lib/gocardless_pro/services/payouts_service.rb +82 -0
  43. data/lib/gocardless_pro/services/redirect_flows_service.rb +98 -0
  44. data/lib/gocardless_pro/services/refunds_service.rb +132 -0
  45. data/lib/gocardless_pro/services/subscriptions_service.rb +134 -0
  46. data/lib/gocardless_pro/version.rb +8 -0
  47. data/spec/api_service_spec.rb +73 -0
  48. data/spec/client_spec.rb +19 -0
  49. data/spec/error_spec.rb +44 -0
  50. data/spec/resources/creditor_bank_account_spec.rb +109 -0
  51. data/spec/resources/creditor_spec.rb +125 -0
  52. data/spec/resources/customer_bank_account_spec.rb +109 -0
  53. data/spec/resources/customer_spec.rb +135 -0
  54. data/spec/resources/event_spec.rb +113 -0
  55. data/spec/resources/helper_spec.rb +23 -0
  56. data/spec/resources/mandate_spec.rb +97 -0
  57. data/spec/resources/payment_spec.rb +129 -0
  58. data/spec/resources/payout_spec.rb +89 -0
  59. data/spec/resources/redirect_flow_spec.rb +97 -0
  60. data/spec/resources/refund_spec.rb +77 -0
  61. data/spec/resources/subscription_spec.rb +165 -0
  62. data/spec/response_spec.rb +89 -0
  63. data/spec/services/creditor_bank_accounts_service_spec.rb +413 -0
  64. data/spec/services/creditors_service_spec.rb +388 -0
  65. data/spec/services/customer_bank_accounts_service_spec.rb +452 -0
  66. data/spec/services/customers_service_spec.rb +429 -0
  67. data/spec/services/events_service_spec.rb +217 -0
  68. data/spec/services/helpers_service_spec.rb +122 -0
  69. data/spec/services/mandates_service_spec.rb +495 -0
  70. data/spec/services/payments_service_spec.rb +546 -0
  71. data/spec/services/payouts_service_spec.rb +217 -0
  72. data/spec/services/redirect_flows_service_spec.rb +254 -0
  73. data/spec/services/refunds_service_spec.rb +323 -0
  74. data/spec/services/subscriptions_service_spec.rb +557 -0
  75. data/spec/spec_helper.rb +91 -0
  76. metadata +224 -0
@@ -0,0 +1,122 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardlessPro::Services::HelpersService do
4
+ let(:client) do
5
+ GoCardlessPro::Client.new(
6
+ access_token: "SECRET_TOKEN"
7
+ )
8
+ end
9
+
10
+
11
+
12
+
13
+
14
+
15
+ describe "#mandate" do
16
+
17
+
18
+ subject(:post_response) { client.helpers.mandate }
19
+
20
+ let(:resource_id) { "ABC123" }
21
+
22
+ let!(:stub) do
23
+ # /helpers/mandate
24
+ stub_url = "/helpers/mandate".gsub(':identity', resource_id)
25
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
26
+ body: {
27
+ helpers: {
28
+
29
+ }
30
+ }.to_json,
31
+ headers: {'Content-Type' => 'application/json'},
32
+ )
33
+ end
34
+
35
+ it "wraps the response and calls the right endpoint" do
36
+ expect(post_response).to be_a(GoCardlessPro::Resources::Helper)
37
+
38
+ expect(stub).to have_been_requested
39
+ end
40
+
41
+ context "when the request needs a body and custom header" do
42
+
43
+ subject(:post_response) { client.helpers.mandate(body, headers) }
44
+
45
+ let(:resource_id) { "ABC123" }
46
+
47
+ let!(:stub) do
48
+ # /helpers/mandate
49
+ stub_url = "/helpers/mandate".gsub(':identity', resource_id)
50
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
51
+ with(
52
+ body: { foo: 'bar' },
53
+ headers: { 'Foo' => 'Bar' }
54
+ ).to_return(
55
+ body: {
56
+ helpers: {
57
+
58
+ }
59
+ }.to_json,
60
+ headers: {'Content-Type' => 'application/json'},
61
+ )
62
+ end
63
+ end
64
+ end
65
+
66
+
67
+
68
+
69
+
70
+ describe "#modulus_check" do
71
+
72
+
73
+ subject(:post_response) { client.helpers.modulus_check }
74
+
75
+ let(:resource_id) { "ABC123" }
76
+
77
+ let!(:stub) do
78
+ # /helpers/modulus_check
79
+ stub_url = "/helpers/modulus_check".gsub(':identity', resource_id)
80
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
81
+ body: {
82
+ helpers: {
83
+
84
+ }
85
+ }.to_json,
86
+ headers: {'Content-Type' => 'application/json'},
87
+ )
88
+ end
89
+
90
+ it "wraps the response and calls the right endpoint" do
91
+ expect(post_response).to be_a(GoCardlessPro::Resources::Helper)
92
+
93
+ expect(stub).to have_been_requested
94
+ end
95
+
96
+ context "when the request needs a body and custom header" do
97
+
98
+ subject(:post_response) { client.helpers.modulus_check(body, headers) }
99
+
100
+ let(:resource_id) { "ABC123" }
101
+
102
+ let!(:stub) do
103
+ # /helpers/modulus_check
104
+ stub_url = "/helpers/modulus_check".gsub(':identity', resource_id)
105
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
106
+ with(
107
+ body: { foo: 'bar' },
108
+ headers: { 'Foo' => 'Bar' }
109
+ ).to_return(
110
+ body: {
111
+ helpers: {
112
+
113
+ }
114
+ }.to_json,
115
+ headers: {'Content-Type' => 'application/json'},
116
+ )
117
+ end
118
+ end
119
+ end
120
+
121
+
122
+ end
@@ -0,0 +1,495 @@
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
+
11
+
12
+
13
+
14
+
15
+ describe "#create" do
16
+ subject(:post_create_response) { client.mandates.create(params: new_resource) }
17
+ context "with a valid request" do
18
+ let(:new_resource) do
19
+ {
20
+
21
+ "created_at" => "created_at-input",
22
+ "id" => "id-input",
23
+ "links" => "links-input",
24
+ "metadata" => "metadata-input",
25
+ "next_possible_charge_date" => "next_possible_charge_date-input",
26
+ "reference" => "reference-input",
27
+ "scheme" => "scheme-input",
28
+ "status" => "status-input",
29
+ }
30
+ end
31
+
32
+ before do
33
+ stub_request(:post, /.*api.gocardless.com\/mandates/).
34
+ with(
35
+ body: {
36
+ mandates: {
37
+
38
+ "created_at" => "created_at-input",
39
+ "id" => "id-input",
40
+ "links" => "links-input",
41
+ "metadata" => "metadata-input",
42
+ "next_possible_charge_date" => "next_possible_charge_date-input",
43
+ "reference" => "reference-input",
44
+ "scheme" => "scheme-input",
45
+ "status" => "status-input",
46
+ }
47
+ }
48
+ ).
49
+ to_return(
50
+ body: {
51
+ mandates: {
52
+
53
+ "created_at" => "created_at-input",
54
+ "id" => "id-input",
55
+ "links" => "links-input",
56
+ "metadata" => "metadata-input",
57
+ "next_possible_charge_date" => "next_possible_charge_date-input",
58
+ "reference" => "reference-input",
59
+ "scheme" => "scheme-input",
60
+ "status" => "status-input",
61
+ }
62
+ }.to_json,
63
+ :headers => {'Content-Type' => 'application/json'}
64
+ )
65
+ end
66
+
67
+ it "creates and returns the resource" do
68
+ expect(post_create_response).to be_a(GoCardlessPro::Resources::Mandate)
69
+ end
70
+ end
71
+
72
+ context "with a request that returns a validation error" do
73
+ let(:new_resource) { {} }
74
+
75
+ before do
76
+ stub_request(:post, /.*api.gocardless.com\/mandates/).to_return(
77
+ body: {
78
+ error: {
79
+ type: 'validation_failed',
80
+ code: 422,
81
+ errors: [
82
+ { message: 'test error message', field: 'test_field' }
83
+ ]
84
+ }
85
+ }.to_json,
86
+ headers: {'Content-Type' => 'application/json'},
87
+ status: 422
88
+ )
89
+ end
90
+
91
+ it "throws the correct error" do
92
+ expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
93
+ end
94
+ end
95
+ end
96
+
97
+
98
+
99
+
100
+
101
+ describe "#list" do
102
+ describe "with no filters" do
103
+ subject(:get_list_response) { client.mandates.list }
104
+
105
+ before do
106
+ stub_request(:get, /.*api.gocardless.com\/mandates/).to_return(
107
+ body: {
108
+ mandates: [{
109
+
110
+ "created_at" => "created_at-input",
111
+ "id" => "id-input",
112
+ "links" => "links-input",
113
+ "metadata" => "metadata-input",
114
+ "next_possible_charge_date" => "next_possible_charge_date-input",
115
+ "reference" => "reference-input",
116
+ "scheme" => "scheme-input",
117
+ "status" => "status-input",
118
+ }],
119
+ meta: {
120
+ cursors: {
121
+ before: nil,
122
+ after: "ABC123"
123
+ }
124
+ }
125
+ }.to_json,
126
+ :headers => {'Content-Type' => 'application/json'}
127
+ )
128
+ end
129
+
130
+ it "wraps each item in the resource class" do
131
+ expect(get_list_response.records.map { |x| x.class }.uniq.first).to eq(GoCardlessPro::Resources::Mandate)
132
+
133
+
134
+
135
+ expect(get_list_response.records.first.created_at).to eq("created_at-input")
136
+
137
+
138
+
139
+ expect(get_list_response.records.first.id).to eq("id-input")
140
+
141
+
142
+
143
+
144
+
145
+ expect(get_list_response.records.first.metadata).to eq("metadata-input")
146
+
147
+
148
+
149
+ expect(get_list_response.records.first.next_possible_charge_date).to eq("next_possible_charge_date-input")
150
+
151
+
152
+
153
+ expect(get_list_response.records.first.reference).to eq("reference-input")
154
+
155
+
156
+
157
+ expect(get_list_response.records.first.scheme).to eq("scheme-input")
158
+
159
+
160
+
161
+ expect(get_list_response.records.first.status).to eq("status-input")
162
+
163
+
164
+ end
165
+
166
+ it "exposes the cursors for before and after" do
167
+ expect(get_list_response.before).to eq(nil)
168
+ expect(get_list_response.after).to eq("ABC123")
169
+ end
170
+ end
171
+ end
172
+
173
+ describe "#all" do
174
+ let!(:first_response_stub) do
175
+ stub_request(:get, /.*api.gocardless.com\/mandates$/).to_return(
176
+ body: {
177
+ mandates: [{
178
+
179
+ "created_at" => "created_at-input",
180
+ "id" => "id-input",
181
+ "links" => "links-input",
182
+ "metadata" => "metadata-input",
183
+ "next_possible_charge_date" => "next_possible_charge_date-input",
184
+ "reference" => "reference-input",
185
+ "scheme" => "scheme-input",
186
+ "status" => "status-input",
187
+ }],
188
+ meta: {
189
+ cursors: { after: 'AB345' },
190
+ limit: 1
191
+ }
192
+ }.to_json,
193
+ :headers => {'Content-Type' => 'application/json'}
194
+ )
195
+ end
196
+
197
+ let!(:second_response_stub) do
198
+ stub_request(:get, /.*api.gocardless.com\/mandates\?after=AB345/).to_return(
199
+ body: {
200
+ mandates: [{
201
+
202
+ "created_at" => "created_at-input",
203
+ "id" => "id-input",
204
+ "links" => "links-input",
205
+ "metadata" => "metadata-input",
206
+ "next_possible_charge_date" => "next_possible_charge_date-input",
207
+ "reference" => "reference-input",
208
+ "scheme" => "scheme-input",
209
+ "status" => "status-input",
210
+ }],
211
+ meta: {
212
+ limit: 2,
213
+ cursors: {}
214
+ }
215
+ }.to_json,
216
+ :headers => {'Content-Type' => 'application/json'}
217
+ )
218
+ end
219
+
220
+ it "automatically makes the extra requests" do
221
+ expect(client.mandates.all.to_a.length).to eq(2)
222
+ expect(first_response_stub).to have_been_requested
223
+ expect(second_response_stub).to have_been_requested
224
+ end
225
+ end
226
+
227
+
228
+
229
+
230
+
231
+
232
+ describe "#get" do
233
+ let(:id) { "ID123" }
234
+
235
+ subject(:get_response) { client.mandates.get(id) }
236
+
237
+ context "passing in a custom header" do
238
+ let!(:stub) do
239
+ stub_request(:get, /.*api.gocardless.com\/mandates\/ID123/)
240
+ .with(headers: { 'Foo' => 'Bar' })
241
+ .to_return(
242
+ body: {
243
+ mandates: {
244
+
245
+ "created_at" => "created_at-input",
246
+ "id" => "id-input",
247
+ "links" => "links-input",
248
+ "metadata" => "metadata-input",
249
+ "next_possible_charge_date" => "next_possible_charge_date-input",
250
+ "reference" => "reference-input",
251
+ "scheme" => "scheme-input",
252
+ "status" => "status-input",
253
+ }
254
+ }.to_json,
255
+ :headers => {'Content-Type' => 'application/json'}
256
+ )
257
+ end
258
+
259
+ subject(:get_response) do
260
+ client.mandates.get(id, headers: {
261
+ 'Foo' => 'Bar'
262
+ })
263
+ end
264
+
265
+ it "includes the header" do
266
+ get_response
267
+ expect(stub).to have_been_requested
268
+ end
269
+ end
270
+
271
+ context "when there is a mandate to return" do
272
+ before do
273
+ stub_request(:get, /.*api.gocardless.com\/mandates\/ID123/).to_return(
274
+ body: {
275
+ mandates: {
276
+
277
+ "created_at" => "created_at-input",
278
+ "id" => "id-input",
279
+ "links" => "links-input",
280
+ "metadata" => "metadata-input",
281
+ "next_possible_charge_date" => "next_possible_charge_date-input",
282
+ "reference" => "reference-input",
283
+ "scheme" => "scheme-input",
284
+ "status" => "status-input",
285
+ }
286
+ }.to_json,
287
+ :headers => {'Content-Type' => 'application/json'}
288
+ )
289
+ end
290
+
291
+ it "wraps the response in a resource" do
292
+ expect(get_response).to be_a(GoCardlessPro::Resources::Mandate)
293
+ end
294
+ end
295
+
296
+ context "when nothing is returned" do
297
+ before do
298
+ stub_request(:get, /.*api.gocardless.com\/mandates\/ID123/).to_return(
299
+ body: "",
300
+ headers: { 'Content-Type' => 'application/json' }
301
+ )
302
+ end
303
+
304
+ it "returns nil" do
305
+ expect(get_response).to be_nil
306
+ end
307
+ end
308
+ end
309
+
310
+
311
+
312
+
313
+
314
+
315
+ describe "#update" do
316
+ subject(:put_update_response) { client.mandates.update(id, params: update_params) }
317
+ let(:id) { "ABC123" }
318
+
319
+ context "with a valid request" do
320
+ let(:update_params) { { "hello" => "world" } }
321
+
322
+ let!(:stub) do
323
+ stub_request(:put, /.*api.gocardless.com\/mandates\/ABC123/).to_return(
324
+ body: {
325
+ mandates: {
326
+
327
+ "created_at" => "created_at-input",
328
+ "id" => "id-input",
329
+ "links" => "links-input",
330
+ "metadata" => "metadata-input",
331
+ "next_possible_charge_date" => "next_possible_charge_date-input",
332
+ "reference" => "reference-input",
333
+ "scheme" => "scheme-input",
334
+ "status" => "status-input",
335
+ }
336
+ }.to_json,
337
+ :headers => {'Content-Type' => 'application/json'}
338
+ )
339
+ end
340
+
341
+ it "updates and returns the resource" do
342
+ expect(put_update_response).to be_a(GoCardlessPro::Resources::Mandate)
343
+ expect(stub).to have_been_requested
344
+ end
345
+ end
346
+ end
347
+
348
+
349
+
350
+
351
+
352
+ describe "#cancel" do
353
+
354
+
355
+ subject(:post_response) { client.mandates.cancel(resource_id) }
356
+
357
+ let(:resource_id) { "ABC123" }
358
+
359
+ let!(:stub) do
360
+ # /mandates/%v/actions/cancel
361
+ stub_url = "/mandates/:identity/actions/cancel".gsub(':identity', resource_id)
362
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
363
+ body: {
364
+ mandates: {
365
+
366
+ "created_at" => "created_at-input",
367
+ "id" => "id-input",
368
+ "links" => "links-input",
369
+ "metadata" => "metadata-input",
370
+ "next_possible_charge_date" => "next_possible_charge_date-input",
371
+ "reference" => "reference-input",
372
+ "scheme" => "scheme-input",
373
+ "status" => "status-input",
374
+ }
375
+ }.to_json,
376
+ headers: {'Content-Type' => 'application/json'},
377
+ )
378
+ end
379
+
380
+ it "wraps the response and calls the right endpoint" do
381
+ expect(post_response).to be_a(GoCardlessPro::Resources::Mandate)
382
+
383
+ expect(stub).to have_been_requested
384
+ end
385
+
386
+ context "when the request needs a body and custom header" do
387
+
388
+ let(:body) { { foo: 'bar' } }
389
+ let(:headers) { { 'Foo' => 'Bar' } }
390
+ subject(:post_response) { client.mandates.cancel(resource_id, body, headers) }
391
+
392
+ let(:resource_id) { "ABC123" }
393
+
394
+ let!(:stub) do
395
+ # /mandates/%v/actions/cancel
396
+ stub_url = "/mandates/:identity/actions/cancel".gsub(':identity', resource_id)
397
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
398
+ with(
399
+ body: { foo: 'bar' },
400
+ headers: { 'Foo' => 'Bar' }
401
+ ).to_return(
402
+ body: {
403
+ mandates: {
404
+
405
+ "created_at" => "created_at-input",
406
+ "id" => "id-input",
407
+ "links" => "links-input",
408
+ "metadata" => "metadata-input",
409
+ "next_possible_charge_date" => "next_possible_charge_date-input",
410
+ "reference" => "reference-input",
411
+ "scheme" => "scheme-input",
412
+ "status" => "status-input",
413
+ }
414
+ }.to_json,
415
+ headers: {'Content-Type' => 'application/json'},
416
+ )
417
+ end
418
+ end
419
+ end
420
+
421
+
422
+
423
+
424
+
425
+ describe "#reinstate" do
426
+
427
+
428
+ subject(:post_response) { client.mandates.reinstate(resource_id) }
429
+
430
+ let(:resource_id) { "ABC123" }
431
+
432
+ let!(:stub) do
433
+ # /mandates/%v/actions/reinstate
434
+ stub_url = "/mandates/:identity/actions/reinstate".gsub(':identity', resource_id)
435
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
436
+ body: {
437
+ mandates: {
438
+
439
+ "created_at" => "created_at-input",
440
+ "id" => "id-input",
441
+ "links" => "links-input",
442
+ "metadata" => "metadata-input",
443
+ "next_possible_charge_date" => "next_possible_charge_date-input",
444
+ "reference" => "reference-input",
445
+ "scheme" => "scheme-input",
446
+ "status" => "status-input",
447
+ }
448
+ }.to_json,
449
+ headers: {'Content-Type' => 'application/json'},
450
+ )
451
+ end
452
+
453
+ it "wraps the response and calls the right endpoint" do
454
+ expect(post_response).to be_a(GoCardlessPro::Resources::Mandate)
455
+
456
+ expect(stub).to have_been_requested
457
+ end
458
+
459
+ context "when the request needs a body and custom header" do
460
+
461
+ let(:body) { { foo: 'bar' } }
462
+ let(:headers) { { 'Foo' => 'Bar' } }
463
+ subject(:post_response) { client.mandates.reinstate(resource_id, body, headers) }
464
+
465
+ let(:resource_id) { "ABC123" }
466
+
467
+ let!(:stub) do
468
+ # /mandates/%v/actions/reinstate
469
+ stub_url = "/mandates/:identity/actions/reinstate".gsub(':identity', resource_id)
470
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
471
+ with(
472
+ body: { foo: 'bar' },
473
+ headers: { 'Foo' => 'Bar' }
474
+ ).to_return(
475
+ body: {
476
+ mandates: {
477
+
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
+ "reference" => "reference-input",
484
+ "scheme" => "scheme-input",
485
+ "status" => "status-input",
486
+ }
487
+ }.to_json,
488
+ headers: {'Content-Type' => 'application/json'},
489
+ )
490
+ end
491
+ end
492
+ end
493
+
494
+
495
+ end