gocardless-pro 0.1.0

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