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,546 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardlessPro::Services::PaymentsService 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.payments.create(params: new_resource) }
17
+ context "with a valid request" do
18
+ let(:new_resource) do
19
+ {
20
+
21
+ "amount" => "amount-input",
22
+ "amount_refunded" => "amount_refunded-input",
23
+ "charge_date" => "charge_date-input",
24
+ "created_at" => "created_at-input",
25
+ "currency" => "currency-input",
26
+ "description" => "description-input",
27
+ "id" => "id-input",
28
+ "links" => "links-input",
29
+ "metadata" => "metadata-input",
30
+ "reference" => "reference-input",
31
+ "status" => "status-input",
32
+ }
33
+ end
34
+
35
+ before do
36
+ stub_request(:post, /.*api.gocardless.com\/payments/).
37
+ with(
38
+ body: {
39
+ payments: {
40
+
41
+ "amount" => "amount-input",
42
+ "amount_refunded" => "amount_refunded-input",
43
+ "charge_date" => "charge_date-input",
44
+ "created_at" => "created_at-input",
45
+ "currency" => "currency-input",
46
+ "description" => "description-input",
47
+ "id" => "id-input",
48
+ "links" => "links-input",
49
+ "metadata" => "metadata-input",
50
+ "reference" => "reference-input",
51
+ "status" => "status-input",
52
+ }
53
+ }
54
+ ).
55
+ to_return(
56
+ body: {
57
+ payments: {
58
+
59
+ "amount" => "amount-input",
60
+ "amount_refunded" => "amount_refunded-input",
61
+ "charge_date" => "charge_date-input",
62
+ "created_at" => "created_at-input",
63
+ "currency" => "currency-input",
64
+ "description" => "description-input",
65
+ "id" => "id-input",
66
+ "links" => "links-input",
67
+ "metadata" => "metadata-input",
68
+ "reference" => "reference-input",
69
+ "status" => "status-input",
70
+ }
71
+ }.to_json,
72
+ :headers => {'Content-Type' => 'application/json'}
73
+ )
74
+ end
75
+
76
+ it "creates and returns the resource" do
77
+ expect(post_create_response).to be_a(GoCardlessPro::Resources::Payment)
78
+ end
79
+ end
80
+
81
+ context "with a request that returns a validation error" do
82
+ let(:new_resource) { {} }
83
+
84
+ before do
85
+ stub_request(:post, /.*api.gocardless.com\/payments/).to_return(
86
+ body: {
87
+ error: {
88
+ type: 'validation_failed',
89
+ code: 422,
90
+ errors: [
91
+ { message: 'test error message', field: 'test_field' }
92
+ ]
93
+ }
94
+ }.to_json,
95
+ headers: {'Content-Type' => 'application/json'},
96
+ status: 422
97
+ )
98
+ end
99
+
100
+ it "throws the correct error" do
101
+ expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
102
+ end
103
+ end
104
+ end
105
+
106
+
107
+
108
+
109
+
110
+ describe "#list" do
111
+ describe "with no filters" do
112
+ subject(:get_list_response) { client.payments.list }
113
+
114
+ before do
115
+ stub_request(:get, /.*api.gocardless.com\/payments/).to_return(
116
+ body: {
117
+ payments: [{
118
+
119
+ "amount" => "amount-input",
120
+ "amount_refunded" => "amount_refunded-input",
121
+ "charge_date" => "charge_date-input",
122
+ "created_at" => "created_at-input",
123
+ "currency" => "currency-input",
124
+ "description" => "description-input",
125
+ "id" => "id-input",
126
+ "links" => "links-input",
127
+ "metadata" => "metadata-input",
128
+ "reference" => "reference-input",
129
+ "status" => "status-input",
130
+ }],
131
+ meta: {
132
+ cursors: {
133
+ before: nil,
134
+ after: "ABC123"
135
+ }
136
+ }
137
+ }.to_json,
138
+ :headers => {'Content-Type' => 'application/json'}
139
+ )
140
+ end
141
+
142
+ it "wraps each item in the resource class" do
143
+ expect(get_list_response.records.map { |x| x.class }.uniq.first).to eq(GoCardlessPro::Resources::Payment)
144
+
145
+
146
+
147
+ expect(get_list_response.records.first.amount).to eq("amount-input")
148
+
149
+
150
+
151
+ expect(get_list_response.records.first.amount_refunded).to eq("amount_refunded-input")
152
+
153
+
154
+
155
+ expect(get_list_response.records.first.charge_date).to eq("charge_date-input")
156
+
157
+
158
+
159
+ expect(get_list_response.records.first.created_at).to eq("created_at-input")
160
+
161
+
162
+
163
+ expect(get_list_response.records.first.currency).to eq("currency-input")
164
+
165
+
166
+
167
+ expect(get_list_response.records.first.description).to eq("description-input")
168
+
169
+
170
+
171
+ expect(get_list_response.records.first.id).to eq("id-input")
172
+
173
+
174
+
175
+
176
+
177
+ expect(get_list_response.records.first.metadata).to eq("metadata-input")
178
+
179
+
180
+
181
+ expect(get_list_response.records.first.reference).to eq("reference-input")
182
+
183
+
184
+
185
+ expect(get_list_response.records.first.status).to eq("status-input")
186
+
187
+
188
+ end
189
+
190
+ it "exposes the cursors for before and after" do
191
+ expect(get_list_response.before).to eq(nil)
192
+ expect(get_list_response.after).to eq("ABC123")
193
+ end
194
+ end
195
+ end
196
+
197
+ describe "#all" do
198
+ let!(:first_response_stub) do
199
+ stub_request(:get, /.*api.gocardless.com\/payments$/).to_return(
200
+ body: {
201
+ payments: [{
202
+
203
+ "amount" => "amount-input",
204
+ "amount_refunded" => "amount_refunded-input",
205
+ "charge_date" => "charge_date-input",
206
+ "created_at" => "created_at-input",
207
+ "currency" => "currency-input",
208
+ "description" => "description-input",
209
+ "id" => "id-input",
210
+ "links" => "links-input",
211
+ "metadata" => "metadata-input",
212
+ "reference" => "reference-input",
213
+ "status" => "status-input",
214
+ }],
215
+ meta: {
216
+ cursors: { after: 'AB345' },
217
+ limit: 1
218
+ }
219
+ }.to_json,
220
+ :headers => {'Content-Type' => 'application/json'}
221
+ )
222
+ end
223
+
224
+ let!(:second_response_stub) do
225
+ stub_request(:get, /.*api.gocardless.com\/payments\?after=AB345/).to_return(
226
+ body: {
227
+ payments: [{
228
+
229
+ "amount" => "amount-input",
230
+ "amount_refunded" => "amount_refunded-input",
231
+ "charge_date" => "charge_date-input",
232
+ "created_at" => "created_at-input",
233
+ "currency" => "currency-input",
234
+ "description" => "description-input",
235
+ "id" => "id-input",
236
+ "links" => "links-input",
237
+ "metadata" => "metadata-input",
238
+ "reference" => "reference-input",
239
+ "status" => "status-input",
240
+ }],
241
+ meta: {
242
+ limit: 2,
243
+ cursors: {}
244
+ }
245
+ }.to_json,
246
+ :headers => {'Content-Type' => 'application/json'}
247
+ )
248
+ end
249
+
250
+ it "automatically makes the extra requests" do
251
+ expect(client.payments.all.to_a.length).to eq(2)
252
+ expect(first_response_stub).to have_been_requested
253
+ expect(second_response_stub).to have_been_requested
254
+ end
255
+ end
256
+
257
+
258
+
259
+
260
+
261
+
262
+ describe "#get" do
263
+ let(:id) { "ID123" }
264
+
265
+ subject(:get_response) { client.payments.get(id) }
266
+
267
+ context "passing in a custom header" do
268
+ let!(:stub) do
269
+ stub_request(:get, /.*api.gocardless.com\/payments\/ID123/)
270
+ .with(headers: { 'Foo' => 'Bar' })
271
+ .to_return(
272
+ body: {
273
+ payments: {
274
+
275
+ "amount" => "amount-input",
276
+ "amount_refunded" => "amount_refunded-input",
277
+ "charge_date" => "charge_date-input",
278
+ "created_at" => "created_at-input",
279
+ "currency" => "currency-input",
280
+ "description" => "description-input",
281
+ "id" => "id-input",
282
+ "links" => "links-input",
283
+ "metadata" => "metadata-input",
284
+ "reference" => "reference-input",
285
+ "status" => "status-input",
286
+ }
287
+ }.to_json,
288
+ :headers => {'Content-Type' => 'application/json'}
289
+ )
290
+ end
291
+
292
+ subject(:get_response) do
293
+ client.payments.get(id, headers: {
294
+ 'Foo' => 'Bar'
295
+ })
296
+ end
297
+
298
+ it "includes the header" do
299
+ get_response
300
+ expect(stub).to have_been_requested
301
+ end
302
+ end
303
+
304
+ context "when there is a payment to return" do
305
+ before do
306
+ stub_request(:get, /.*api.gocardless.com\/payments\/ID123/).to_return(
307
+ body: {
308
+ payments: {
309
+
310
+ "amount" => "amount-input",
311
+ "amount_refunded" => "amount_refunded-input",
312
+ "charge_date" => "charge_date-input",
313
+ "created_at" => "created_at-input",
314
+ "currency" => "currency-input",
315
+ "description" => "description-input",
316
+ "id" => "id-input",
317
+ "links" => "links-input",
318
+ "metadata" => "metadata-input",
319
+ "reference" => "reference-input",
320
+ "status" => "status-input",
321
+ }
322
+ }.to_json,
323
+ :headers => {'Content-Type' => 'application/json'}
324
+ )
325
+ end
326
+
327
+ it "wraps the response in a resource" do
328
+ expect(get_response).to be_a(GoCardlessPro::Resources::Payment)
329
+ end
330
+ end
331
+
332
+ context "when nothing is returned" do
333
+ before do
334
+ stub_request(:get, /.*api.gocardless.com\/payments\/ID123/).to_return(
335
+ body: "",
336
+ headers: { 'Content-Type' => 'application/json' }
337
+ )
338
+ end
339
+
340
+ it "returns nil" do
341
+ expect(get_response).to be_nil
342
+ end
343
+ end
344
+ end
345
+
346
+
347
+
348
+
349
+
350
+
351
+ describe "#update" do
352
+ subject(:put_update_response) { client.payments.update(id, params: update_params) }
353
+ let(:id) { "ABC123" }
354
+
355
+ context "with a valid request" do
356
+ let(:update_params) { { "hello" => "world" } }
357
+
358
+ let!(:stub) do
359
+ stub_request(:put, /.*api.gocardless.com\/payments\/ABC123/).to_return(
360
+ body: {
361
+ payments: {
362
+
363
+ "amount" => "amount-input",
364
+ "amount_refunded" => "amount_refunded-input",
365
+ "charge_date" => "charge_date-input",
366
+ "created_at" => "created_at-input",
367
+ "currency" => "currency-input",
368
+ "description" => "description-input",
369
+ "id" => "id-input",
370
+ "links" => "links-input",
371
+ "metadata" => "metadata-input",
372
+ "reference" => "reference-input",
373
+ "status" => "status-input",
374
+ }
375
+ }.to_json,
376
+ :headers => {'Content-Type' => 'application/json'}
377
+ )
378
+ end
379
+
380
+ it "updates and returns the resource" do
381
+ expect(put_update_response).to be_a(GoCardlessPro::Resources::Payment)
382
+ expect(stub).to have_been_requested
383
+ end
384
+ end
385
+ end
386
+
387
+
388
+
389
+
390
+
391
+ describe "#cancel" do
392
+
393
+
394
+ subject(:post_response) { client.payments.cancel(resource_id) }
395
+
396
+ let(:resource_id) { "ABC123" }
397
+
398
+ let!(:stub) do
399
+ # /payments/%v/actions/cancel
400
+ stub_url = "/payments/:identity/actions/cancel".gsub(':identity', resource_id)
401
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
402
+ body: {
403
+ payments: {
404
+
405
+ "amount" => "amount-input",
406
+ "amount_refunded" => "amount_refunded-input",
407
+ "charge_date" => "charge_date-input",
408
+ "created_at" => "created_at-input",
409
+ "currency" => "currency-input",
410
+ "description" => "description-input",
411
+ "id" => "id-input",
412
+ "links" => "links-input",
413
+ "metadata" => "metadata-input",
414
+ "reference" => "reference-input",
415
+ "status" => "status-input",
416
+ }
417
+ }.to_json,
418
+ headers: {'Content-Type' => 'application/json'},
419
+ )
420
+ end
421
+
422
+ it "wraps the response and calls the right endpoint" do
423
+ expect(post_response).to be_a(GoCardlessPro::Resources::Payment)
424
+
425
+ expect(stub).to have_been_requested
426
+ end
427
+
428
+ context "when the request needs a body and custom header" do
429
+
430
+ let(:body) { { foo: 'bar' } }
431
+ let(:headers) { { 'Foo' => 'Bar' } }
432
+ subject(:post_response) { client.payments.cancel(resource_id, body, headers) }
433
+
434
+ let(:resource_id) { "ABC123" }
435
+
436
+ let!(:stub) do
437
+ # /payments/%v/actions/cancel
438
+ stub_url = "/payments/:identity/actions/cancel".gsub(':identity', resource_id)
439
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
440
+ with(
441
+ body: { foo: 'bar' },
442
+ headers: { 'Foo' => 'Bar' }
443
+ ).to_return(
444
+ body: {
445
+ payments: {
446
+
447
+ "amount" => "amount-input",
448
+ "amount_refunded" => "amount_refunded-input",
449
+ "charge_date" => "charge_date-input",
450
+ "created_at" => "created_at-input",
451
+ "currency" => "currency-input",
452
+ "description" => "description-input",
453
+ "id" => "id-input",
454
+ "links" => "links-input",
455
+ "metadata" => "metadata-input",
456
+ "reference" => "reference-input",
457
+ "status" => "status-input",
458
+ }
459
+ }.to_json,
460
+ headers: {'Content-Type' => 'application/json'},
461
+ )
462
+ end
463
+ end
464
+ end
465
+
466
+
467
+
468
+
469
+
470
+ describe "#retry" do
471
+
472
+
473
+ subject(:post_response) { client.payments.retry(resource_id) }
474
+
475
+ let(:resource_id) { "ABC123" }
476
+
477
+ let!(:stub) do
478
+ # /payments/%v/actions/retry
479
+ stub_url = "/payments/:identity/actions/retry".gsub(':identity', resource_id)
480
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
481
+ body: {
482
+ payments: {
483
+
484
+ "amount" => "amount-input",
485
+ "amount_refunded" => "amount_refunded-input",
486
+ "charge_date" => "charge_date-input",
487
+ "created_at" => "created_at-input",
488
+ "currency" => "currency-input",
489
+ "description" => "description-input",
490
+ "id" => "id-input",
491
+ "links" => "links-input",
492
+ "metadata" => "metadata-input",
493
+ "reference" => "reference-input",
494
+ "status" => "status-input",
495
+ }
496
+ }.to_json,
497
+ headers: {'Content-Type' => 'application/json'},
498
+ )
499
+ end
500
+
501
+ it "wraps the response and calls the right endpoint" do
502
+ expect(post_response).to be_a(GoCardlessPro::Resources::Payment)
503
+
504
+ expect(stub).to have_been_requested
505
+ end
506
+
507
+ context "when the request needs a body and custom header" do
508
+
509
+ let(:body) { { foo: 'bar' } }
510
+ let(:headers) { { 'Foo' => 'Bar' } }
511
+ subject(:post_response) { client.payments.retry(resource_id, body, headers) }
512
+
513
+ let(:resource_id) { "ABC123" }
514
+
515
+ let!(:stub) do
516
+ # /payments/%v/actions/retry
517
+ stub_url = "/payments/:identity/actions/retry".gsub(':identity', resource_id)
518
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
519
+ with(
520
+ body: { foo: 'bar' },
521
+ headers: { 'Foo' => 'Bar' }
522
+ ).to_return(
523
+ body: {
524
+ payments: {
525
+
526
+ "amount" => "amount-input",
527
+ "amount_refunded" => "amount_refunded-input",
528
+ "charge_date" => "charge_date-input",
529
+ "created_at" => "created_at-input",
530
+ "currency" => "currency-input",
531
+ "description" => "description-input",
532
+ "id" => "id-input",
533
+ "links" => "links-input",
534
+ "metadata" => "metadata-input",
535
+ "reference" => "reference-input",
536
+ "status" => "status-input",
537
+ }
538
+ }.to_json,
539
+ headers: {'Content-Type' => 'application/json'},
540
+ )
541
+ end
542
+ end
543
+ end
544
+
545
+
546
+ end