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,208 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardless::Services::RedirectFlowService 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.redirect_flows.create(new_resource) }
18
+ context "with a valid request" do
19
+ let(:new_resource) do
20
+ {
21
+
22
+ "created_at" => "created_at-input",
23
+ "description" => "description-input",
24
+ "id" => "id-input",
25
+ "links" => "links-input",
26
+ "redirect_url" => "redirect_url-input",
27
+ "scheme" => "scheme-input",
28
+ "session_token" => "session_token-input",
29
+ "success_redirect_url" => "success_redirect_url-input",
30
+ }
31
+ end
32
+
33
+ before do
34
+ stub_request(:post, /.*api.gocardless.com\/redirect_flows/).
35
+ with(
36
+ body: {
37
+ redirect_flows: {
38
+
39
+ "created_at" => "created_at-input",
40
+ "description" => "description-input",
41
+ "id" => "id-input",
42
+ "links" => "links-input",
43
+ "redirect_url" => "redirect_url-input",
44
+ "scheme" => "scheme-input",
45
+ "session_token" => "session_token-input",
46
+ "success_redirect_url" => "success_redirect_url-input",
47
+ }
48
+ }
49
+ ).
50
+ to_return(
51
+ body: {
52
+ redirect_flows: {
53
+
54
+ "created_at" => "created_at-input",
55
+ "description" => "description-input",
56
+ "id" => "id-input",
57
+ "links" => "links-input",
58
+ "redirect_url" => "redirect_url-input",
59
+ "scheme" => "scheme-input",
60
+ "session_token" => "session_token-input",
61
+ "success_redirect_url" => "success_redirect_url-input",
62
+ }
63
+ }.to_json,
64
+ :headers => {'Content-Type' => 'application/json'}
65
+ )
66
+ end
67
+
68
+ it "creates and returns the resource" do
69
+ expect(post_create_response).to be_a(GoCardless::Resources::RedirectFlow)
70
+ end
71
+ end
72
+
73
+ context "with a request that returns a validation error" do
74
+ let(:new_resource) { {} }
75
+
76
+ before do
77
+ stub_request(:post, /.*api.gocardless.com\/redirect_flows/).to_return(
78
+ body: {
79
+ error: {
80
+ type: 'validation_failed',
81
+ code: 422,
82
+ errors: [
83
+ { message: 'test error message', field: 'test_field' }
84
+ ]
85
+ }
86
+ }.to_json,
87
+ headers: {'Content-Type' => 'application/json'},
88
+ status: 422
89
+ )
90
+ end
91
+
92
+ it "throws the correct error" do
93
+ expect { post_create_response }.to raise_error(GoCardless::ValidationError)
94
+ end
95
+ end
96
+ end
97
+
98
+
99
+
100
+
101
+
102
+ describe "#get" do
103
+ let(:id) { "ID123" }
104
+
105
+ subject(:get_response) { client.redirect_flows.get(id) }
106
+
107
+ context "when there is a redirect_flow to return" do
108
+ before do
109
+ stub_request(:get, /.*api.gocardless.com\/redirect_flows\/ID123/).to_return(
110
+ body: {
111
+ redirect_flows: {
112
+
113
+ "created_at" => "created_at-input",
114
+ "description" => "description-input",
115
+ "id" => "id-input",
116
+ "links" => "links-input",
117
+ "redirect_url" => "redirect_url-input",
118
+ "scheme" => "scheme-input",
119
+ "session_token" => "session_token-input",
120
+ "success_redirect_url" => "success_redirect_url-input",
121
+ }
122
+ }.to_json,
123
+ :headers => {'Content-Type' => 'application/json'}
124
+ )
125
+ end
126
+
127
+ it "wraps the response in a resource" do
128
+ expect(get_response).to be_a(GoCardless::Resources::RedirectFlow)
129
+ end
130
+ end
131
+ end
132
+
133
+
134
+
135
+
136
+
137
+
138
+ describe "#complete" do
139
+
140
+
141
+ subject(:post_response) { client.redirect_flows.complete(resource_id) }
142
+
143
+ let(:resource_id) { "ABC123" }
144
+
145
+ let!(:stub) do
146
+ # /redirect_flows/%v/actions/complete
147
+ stub_url = "/redirect_flows/:identity/actions/complete".gsub(':identity', resource_id)
148
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
149
+ body: {
150
+ redirect_flows: {
151
+
152
+ "created_at" => "created_at-input",
153
+ "description" => "description-input",
154
+ "id" => "id-input",
155
+ "links" => "links-input",
156
+ "redirect_url" => "redirect_url-input",
157
+ "scheme" => "scheme-input",
158
+ "session_token" => "session_token-input",
159
+ "success_redirect_url" => "success_redirect_url-input",
160
+ }
161
+ }.to_json,
162
+ headers: {'Content-Type' => 'application/json'},
163
+ )
164
+ end
165
+
166
+ it "wraps the response and calls the right endpoint" do
167
+ expect(post_response).to be_a(GoCardless::Resources::RedirectFlow)
168
+
169
+ expect(stub).to have_been_requested
170
+ end
171
+
172
+ context "when the request needs a body and custom header" do
173
+
174
+ let(:body) { { foo: 'bar' } }
175
+ let(:headers) { { 'Foo' => 'Bar' } }
176
+ subject(:post_response) { client.redirect_flows.complete(resource_id, body, headers) }
177
+
178
+ let(:resource_id) { "ABC123" }
179
+
180
+ let!(:stub) do
181
+ # /redirect_flows/%v/actions/complete
182
+ stub_url = "/redirect_flows/:identity/actions/complete".gsub(':identity', resource_id)
183
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
184
+ with(
185
+ body: { foo: 'bar' },
186
+ headers: { 'Foo' => 'Bar' }
187
+ ).to_return(
188
+ body: {
189
+ redirect_flows: {
190
+
191
+ "created_at" => "created_at-input",
192
+ "description" => "description-input",
193
+ "id" => "id-input",
194
+ "links" => "links-input",
195
+ "redirect_url" => "redirect_url-input",
196
+ "scheme" => "scheme-input",
197
+ "session_token" => "session_token-input",
198
+ "success_redirect_url" => "success_redirect_url-input",
199
+ }
200
+ }.to_json,
201
+ headers: {'Content-Type' => 'application/json'},
202
+ )
203
+ end
204
+ end
205
+ end
206
+
207
+
208
+ end
@@ -0,0 +1,279 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardless::Services::RefundService 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.refunds.create(new_resource) }
18
+ context "with a valid request" do
19
+ let(:new_resource) do
20
+ {
21
+
22
+ "amount" => "amount-input",
23
+ "created_at" => "created_at-input",
24
+ "currency" => "currency-input",
25
+ "id" => "id-input",
26
+ "links" => "links-input",
27
+ "metadata" => "metadata-input",
28
+ }
29
+ end
30
+
31
+ before do
32
+ stub_request(:post, /.*api.gocardless.com\/refunds/).
33
+ with(
34
+ body: {
35
+ refunds: {
36
+
37
+ "amount" => "amount-input",
38
+ "created_at" => "created_at-input",
39
+ "currency" => "currency-input",
40
+ "id" => "id-input",
41
+ "links" => "links-input",
42
+ "metadata" => "metadata-input",
43
+ }
44
+ }
45
+ ).
46
+ to_return(
47
+ body: {
48
+ refunds: {
49
+
50
+ "amount" => "amount-input",
51
+ "created_at" => "created_at-input",
52
+ "currency" => "currency-input",
53
+ "id" => "id-input",
54
+ "links" => "links-input",
55
+ "metadata" => "metadata-input",
56
+ }
57
+ }.to_json,
58
+ :headers => {'Content-Type' => 'application/json'}
59
+ )
60
+ end
61
+
62
+ it "creates and returns the resource" do
63
+ expect(post_create_response).to be_a(GoCardless::Resources::Refund)
64
+ end
65
+ end
66
+
67
+ context "with a request that returns a validation error" do
68
+ let(:new_resource) { {} }
69
+
70
+ before do
71
+ stub_request(:post, /.*api.gocardless.com\/refunds/).to_return(
72
+ body: {
73
+ error: {
74
+ type: 'validation_failed',
75
+ code: 422,
76
+ errors: [
77
+ { message: 'test error message', field: 'test_field' }
78
+ ]
79
+ }
80
+ }.to_json,
81
+ headers: {'Content-Type' => 'application/json'},
82
+ status: 422
83
+ )
84
+ end
85
+
86
+ it "throws the correct error" do
87
+ expect { post_create_response }.to raise_error(GoCardless::ValidationError)
88
+ end
89
+ end
90
+ end
91
+
92
+
93
+
94
+
95
+
96
+ describe "#list" do
97
+ describe "with no filters" do
98
+ subject(:get_list_response) { client.refunds.list }
99
+
100
+ before do
101
+ stub_request(:get, /.*api.gocardless.com\/refunds/).to_return(
102
+ body: {
103
+ refunds: [{
104
+
105
+ "amount" => "amount-input",
106
+ "created_at" => "created_at-input",
107
+ "currency" => "currency-input",
108
+ "id" => "id-input",
109
+ "links" => "links-input",
110
+ "metadata" => "metadata-input",
111
+ }],
112
+ meta: {
113
+ cursors: {
114
+ before: nil,
115
+ after: "ABC123"
116
+ }
117
+ }
118
+ }.to_json,
119
+ :headers => {'Content-Type' => 'application/json'}
120
+ )
121
+ end
122
+
123
+ it "wraps each item in the resource class" do
124
+ expect(get_list_response.map { |x| x.class }.uniq.first).to eq(GoCardless::Resources::Refund)
125
+
126
+
127
+
128
+ expect(get_list_response.first.amount).to eq("amount-input")
129
+
130
+
131
+
132
+ expect(get_list_response.first.created_at).to eq("created_at-input")
133
+
134
+
135
+
136
+ expect(get_list_response.first.currency).to eq("currency-input")
137
+
138
+
139
+
140
+ expect(get_list_response.first.id).to eq("id-input")
141
+
142
+
143
+
144
+
145
+
146
+ expect(get_list_response.first.metadata).to eq("metadata-input")
147
+
148
+
149
+ end
150
+
151
+ it "exposes the cursors for before and after" do
152
+ expect(get_list_response.before).to eq(nil)
153
+ expect(get_list_response.after).to eq("ABC123")
154
+ end
155
+ end
156
+ end
157
+
158
+ describe "#all" do
159
+ let!(:first_response_stub) do
160
+ stub_request(:get, /.*api.gocardless.com\/refunds$/).to_return(
161
+ body: {
162
+ refunds: [{
163
+
164
+ "amount" => "amount-input",
165
+ "created_at" => "created_at-input",
166
+ "currency" => "currency-input",
167
+ "id" => "id-input",
168
+ "links" => "links-input",
169
+ "metadata" => "metadata-input",
170
+ }],
171
+ meta: {
172
+ cursors: { after: 'AB345' },
173
+ limit: 1
174
+ }
175
+ }.to_json,
176
+ :headers => {'Content-Type' => 'application/json'}
177
+ )
178
+ end
179
+
180
+ let!(:second_response_stub) do
181
+ stub_request(:get, /.*api.gocardless.com\/refunds\?after=AB345/).to_return(
182
+ body: {
183
+ refunds: [{
184
+
185
+ "amount" => "amount-input",
186
+ "created_at" => "created_at-input",
187
+ "currency" => "currency-input",
188
+ "id" => "id-input",
189
+ "links" => "links-input",
190
+ "metadata" => "metadata-input",
191
+ }],
192
+ meta: {
193
+ limit: 2,
194
+ cursors: {}
195
+ }
196
+ }.to_json,
197
+ :headers => {'Content-Type' => 'application/json'}
198
+ )
199
+ end
200
+
201
+ it "automatically makes the extra requests" do
202
+ expect(client.refunds.all.to_a.length).to eq(2)
203
+ expect(first_response_stub).to have_been_requested
204
+ expect(second_response_stub).to have_been_requested
205
+ end
206
+ end
207
+
208
+
209
+
210
+
211
+
212
+
213
+ describe "#get" do
214
+ let(:id) { "ID123" }
215
+
216
+ subject(:get_response) { client.refunds.get(id) }
217
+
218
+ context "when there is a refund to return" do
219
+ before do
220
+ stub_request(:get, /.*api.gocardless.com\/refunds\/ID123/).to_return(
221
+ body: {
222
+ refunds: {
223
+
224
+ "amount" => "amount-input",
225
+ "created_at" => "created_at-input",
226
+ "currency" => "currency-input",
227
+ "id" => "id-input",
228
+ "links" => "links-input",
229
+ "metadata" => "metadata-input",
230
+ }
231
+ }.to_json,
232
+ :headers => {'Content-Type' => 'application/json'}
233
+ )
234
+ end
235
+
236
+ it "wraps the response in a resource" do
237
+ expect(get_response).to be_a(GoCardless::Resources::Refund)
238
+ end
239
+ end
240
+ end
241
+
242
+
243
+
244
+
245
+
246
+
247
+ describe "#update" do
248
+ subject(:put_update_response) { client.refunds.update(id, update_params) }
249
+ let(:id) { "ABC123" }
250
+
251
+ context "with a valid request" do
252
+ let(:update_params) { { "hello" => "world" } }
253
+
254
+ let!(:stub) do
255
+ stub_request(:put, /.*api.gocardless.com\/refunds\/ABC123/).to_return(
256
+ body: {
257
+ refunds: {
258
+
259
+ "amount" => "amount-input",
260
+ "created_at" => "created_at-input",
261
+ "currency" => "currency-input",
262
+ "id" => "id-input",
263
+ "links" => "links-input",
264
+ "metadata" => "metadata-input",
265
+ }
266
+ }.to_json,
267
+ :headers => {'Content-Type' => 'application/json'}
268
+ )
269
+ end
270
+
271
+ it "updates and returns the resource" do
272
+ expect(put_update_response).to be_a(GoCardless::Resources::Refund)
273
+ expect(stub).to have_been_requested
274
+ end
275
+ end
276
+ end
277
+
278
+
279
+ end