gocardless_pro 2.24.0 → 2.25.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bac8c2899f9fd824c6dd5f94bcfa506694dc9f97839cb86a1b64149ccfc92e48
4
- data.tar.gz: f12c865ed4d9eaa0def7d20fea5e2b15e2c225898de92bee5758d9a6757e476c
3
+ metadata.gz: 1e1d21b1955fd8db168185745bd1afbd46fd7c3fa434006abf5c0e838cd7507e
4
+ data.tar.gz: ef057338fc37aa2e791ef3dcd7536a50ff55711e7d8fa5762a51d6797395cab6
5
5
  SHA512:
6
- metadata.gz: f3c76aad9bbaace084b8449366edc8b1f8b33f0b705e22050137bc3d2c31b43228addbea11943b337e8ac6c0a7da52117979dea6ee8e1e425dbca026d159e467
7
- data.tar.gz: '097fd8d09e04a316d2e37010eb78d36b8278e7ec8e9cbed867c772ccf3b522957d1fae644c4357a2b560917a16e90b6284841679c79e8525f8f3324795715139'
6
+ metadata.gz: 7c1daafd49f92798ff81c407809ecf888429f2c723801c42b49dfe4d7d7351ae818925c46d897784f798c7d4ee2c7089959594cec38ab0fc6df3472f481d0207
7
+ data.tar.gz: 78eb4cd3154aa14fe50e445cd94605fe39f91893564650948717e84356fb7d374357ecbfad08784be2c21221fc096530e85407c8a1a3eb794b2ab5d40d0024d1
@@ -77,6 +77,9 @@ require_relative 'gocardless_pro/services/mandate_import_entries_service'
77
77
  require_relative 'gocardless_pro/resources/mandate_pdf'
78
78
  require_relative 'gocardless_pro/services/mandate_pdfs_service'
79
79
 
80
+ require_relative 'gocardless_pro/resources/payer_authorisation'
81
+ require_relative 'gocardless_pro/services/payer_authorisations_service'
82
+
80
83
  require_relative 'gocardless_pro/resources/payment'
81
84
  require_relative 'gocardless_pro/services/payments_service'
82
85
 
@@ -68,6 +68,11 @@ module GoCardlessPro
68
68
  @mandate_pdfs ||= Services::MandatePdfsService.new(@api_service)
69
69
  end
70
70
 
71
+ # Access to the service for payer_authorisation to make API calls
72
+ def payer_authorisations
73
+ @payer_authorisations ||= Services::PayerAuthorisationsService.new(@api_service)
74
+ end
75
+
71
76
  # Access to the service for payment to make API calls
72
77
  def payments
73
78
  @payments ||= Services::PaymentsService.new(@api_service)
@@ -153,7 +158,7 @@ module GoCardlessPro
153
158
  'User-Agent' => user_agent.to_s,
154
159
  'Content-Type' => 'application/json',
155
160
  'GoCardless-Client-Library' => 'gocardless-pro-ruby',
156
- 'GoCardless-Client-Version' => '2.24.0',
161
+ 'GoCardless-Client-Version' => '2.25.0',
157
162
  },
158
163
  }
159
164
  end
@@ -0,0 +1,119 @@
1
+ # encoding: utf-8
2
+
3
+ #
4
+ # This client is automatically generated from a template and JSON schema definition.
5
+ # See https://github.com/gocardless/gocardless-pro-ruby#contributing before editing.
6
+ #
7
+
8
+ require 'uri'
9
+
10
+ module GoCardlessPro
11
+ # A module containing classes for each of the resources in the GC Api
12
+ module Resources
13
+ # Represents an instance of a payer_authorisation resource returned from the API
14
+
15
+ # Payer Authorisation resource acts as a wrapper for creating customer, bank
16
+ # account and mandate details in a single request.
17
+ # PayerAuthorisation API enables the integrators to build their own custom
18
+ # payment pages.
19
+ #
20
+ # The process to use the Payer Authorisation API is as follows:
21
+ #
22
+ # 1. Create a Payer Authorisation, either empty or with already available
23
+ # information
24
+ # 2. Update the authorisation with additional information or fix any
25
+ # mistakes
26
+ # 3. Submit the authorisation, after the payer has reviewed their
27
+ # information
28
+ # 4. [coming soon] Redirect the payer to the verification mechanisms from
29
+ # the response of the Submit request (this will be introduced as a
30
+ # non-breaking change)
31
+ # 5. Confirm the authorisation to indicate that the resources can be
32
+ # created
33
+ #
34
+ # After the Payer Authorisation is confirmed, resources will eventually be
35
+ # created as it's an asynchronous process.
36
+ #
37
+ # To retrieve the status and ID of the linked resources you can do one of
38
+ # the following:
39
+ # <ol>
40
+ # <li> Listen to <code> payer_authorisation_completed </code> <a
41
+ # href="#appendix-webhooks"> webhook</a> (recommended)</li>
42
+ # <li> Poll the GET <a
43
+ # href="#payer-authorisations-get-a-single-payer-authorisation">
44
+ # endpoint</a></li>
45
+ # <li> Poll the GET events API
46
+ # <code>https://api.gocardless.com/events?payer_authorisation={id}&action=completed</code>
47
+ # </li>
48
+ # </ol>
49
+ #
50
+ # <p class="notice">
51
+ # Note that the `create` and `update` endpoints behave differently than
52
+ # other existing `create` and `update` endpoints. The Payer Authorisation
53
+ # is still saved if incomplete data is provided.
54
+ # We return the list of incomplete data in the `incomplete_fields` along
55
+ # with the resources in the body of the response.
56
+ # <br><br>
57
+ # The API is designed to be flexible and allows you to collect information
58
+ # in multiple steps without storing any sensitive data in the browser or in
59
+ # your servers.
60
+ # </p>
61
+ class PayerAuthorisation
62
+ attr_reader :bank_account
63
+ attr_reader :created_at
64
+ attr_reader :customer
65
+ attr_reader :id
66
+ attr_reader :incomplete_fields
67
+ attr_reader :mandate
68
+ attr_reader :status
69
+
70
+ # Initialize a payer_authorisation resource instance
71
+ # @param object [Hash] an object returned from the API
72
+ def initialize(object, response = nil)
73
+ @object = object
74
+
75
+ @bank_account = object['bank_account']
76
+ @created_at = object['created_at']
77
+ @customer = object['customer']
78
+ @id = object['id']
79
+ @incomplete_fields = object['incomplete_fields']
80
+ @links = object['links']
81
+ @mandate = object['mandate']
82
+ @status = object['status']
83
+ @response = response
84
+ end
85
+
86
+ def api_response
87
+ ApiResponse.new(@response)
88
+ end
89
+
90
+ # Return the links that the resource has
91
+ def links
92
+ @payer_authorisation_links ||= Links.new(@links)
93
+ end
94
+
95
+ # Provides the payer_authorisation resource as a hash of all its readable attributes
96
+ def to_h
97
+ @object
98
+ end
99
+
100
+ class Links
101
+ def initialize(links)
102
+ @links = links || {}
103
+ end
104
+
105
+ def bank_account
106
+ @links['bank_account']
107
+ end
108
+
109
+ def customer
110
+ @links['customer']
111
+ end
112
+
113
+ def mandate
114
+ @links['mandate']
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -73,6 +73,10 @@ module GoCardlessPro
73
73
  def payment
74
74
  @links['payment']
75
75
  end
76
+
77
+ def refund
78
+ @links['refund']
79
+ end
76
80
  end
77
81
  end
78
82
  end
@@ -0,0 +1,208 @@
1
+ require_relative './base_service'
2
+
3
+ # encoding: utf-8
4
+ #
5
+ # This client is automatically generated from a template and JSON schema definition.
6
+ # See https://github.com/gocardless/gocardless-pro-ruby#contributing before editing.
7
+ #
8
+
9
+ module GoCardlessPro
10
+ module Services
11
+ # Service for making requests to the PayerAuthorisation endpoints
12
+ class PayerAuthorisationsService < BaseService
13
+ # Retrieves the details of a single existing Payer Authorisation. It can be used
14
+ # for polling the status of a Payer Authorisation.
15
+ # Example URL: /payer_authorisations/:identity
16
+ #
17
+ # @param identity # Unique identifier, beginning with "PA".
18
+ # @param options [Hash] parameters as a hash, under a params key.
19
+ def get(identity, options = {})
20
+ path = sub_url('/payer_authorisations/:identity', 'identity' => identity)
21
+
22
+ options[:retry_failures] = true
23
+
24
+ response = make_request(:get, path, options)
25
+
26
+ return if response.body.nil?
27
+
28
+ Resources::PayerAuthorisation.new(unenvelope_body(response.body), response)
29
+ end
30
+
31
+ # Creates a Payer Authorisation. The resource is saved to the database even if
32
+ # incomplete. An empty array of incomplete_fields means that the resource is
33
+ # valid. The ID of the resource is used for the other actions. This endpoint has
34
+ # been designed this way so you do not need to save any payer data on your
35
+ # servers or the browser while still being able to implement a progressive
36
+ # solution, such as a multi-step form.
37
+ # Example URL: /payer_authorisations
38
+ # @param options [Hash] parameters as a hash, under a params key.
39
+ def create(options = {})
40
+ path = '/payer_authorisations'
41
+
42
+ params = options.delete(:params) || {}
43
+ options[:params] = {}
44
+ options[:params][envelope_key] = params
45
+
46
+ options[:retry_failures] = true
47
+
48
+ begin
49
+ response = make_request(:post, path, options)
50
+
51
+ # Response doesn't raise any errors until #body is called
52
+ response.tap(&:body)
53
+ rescue InvalidStateError => e
54
+ if e.idempotent_creation_conflict?
55
+ case @api_service.on_idempotency_conflict
56
+ when :raise
57
+ raise IdempotencyConflict, e.error
58
+ when :fetch
59
+ return get(e.conflicting_resource_id)
60
+ else
61
+ raise ArgumentError, 'Unknown mode for :on_idempotency_conflict'
62
+ end
63
+ end
64
+
65
+ raise e
66
+ end
67
+
68
+ return if response.body.nil?
69
+
70
+ Resources::PayerAuthorisation.new(unenvelope_body(response.body), response)
71
+ end
72
+
73
+ # Updates a Payer Authorisation. Updates the Payer Authorisation with the
74
+ # request data.Can be invoked as many times as needed. Only fields present in
75
+ # the request will be modified. An empty array of incomplete_fields means that
76
+ # the resource is valid. This endpoint has been designed this way so you do not
77
+ # need to save any payer data on your servers or the browser while still being
78
+ # able to implement a progressive solution, such a multi-step form. <p
79
+ # class="notice"> Note that in order to update the `metadata` attribute values
80
+ # it must be sent completely as it overrides the previously existing values.
81
+ # </p>
82
+ # Example URL: /payer_authorisations/:identity
83
+ #
84
+ # @param identity # Unique identifier, beginning with "PA".
85
+ # @param options [Hash] parameters as a hash, under a params key.
86
+ def update(identity, options = {})
87
+ path = sub_url('/payer_authorisations/:identity', 'identity' => identity)
88
+
89
+ params = options.delete(:params) || {}
90
+ options[:params] = {}
91
+ options[:params][envelope_key] = params
92
+
93
+ options[:retry_failures] = true
94
+
95
+ response = make_request(:put, path, options)
96
+
97
+ return if response.body.nil?
98
+
99
+ Resources::PayerAuthorisation.new(unenvelope_body(response.body), response)
100
+ end
101
+
102
+ # Submits all the data previously pushed to this PayerAuthorisation for
103
+ # verification.This time, a 200 HTTP status is returned if the resource is valid
104
+ # and a 422 error response in case of validation errors. After it is
105
+ # successfully submitted, the Payer Authorisation can no longer be edited.
106
+ # Example URL: /payer_authorisations/:identity/actions/submit
107
+ #
108
+ # @param identity # Unique identifier, beginning with "PA".
109
+ # @param options [Hash] parameters as a hash, under a params key.
110
+ def submit(identity, options = {})
111
+ path = sub_url('/payer_authorisations/:identity/actions/submit', 'identity' => identity)
112
+
113
+ params = options.delete(:params) || {}
114
+ options[:params] = {}
115
+ options[:params]['data'] = params
116
+
117
+ options[:retry_failures] = false
118
+
119
+ begin
120
+ response = make_request(:post, path, options)
121
+
122
+ # Response doesn't raise any errors until #body is called
123
+ response.tap(&:body)
124
+ rescue InvalidStateError => e
125
+ if e.idempotent_creation_conflict?
126
+ case @api_service.on_idempotency_conflict
127
+ when :raise
128
+ raise IdempotencyConflict, e.error
129
+ when :fetch
130
+ return get(e.conflicting_resource_id)
131
+ else
132
+ raise ArgumentError, 'Unknown mode for :on_idempotency_conflict'
133
+ end
134
+ end
135
+
136
+ raise e
137
+ end
138
+
139
+ return if response.body.nil?
140
+
141
+ Resources::PayerAuthorisation.new(unenvelope_body(response.body), response)
142
+ end
143
+
144
+ # Confirms the Payer Authorisation, indicating that the resources are ready to
145
+ # be created.
146
+ # A Payer Authorisation cannot be confirmed if it hasn't been submitted yet.
147
+ #
148
+ # <p class="notice">
149
+ # The main use of the confirm endpoint is to enable integrators to acknowledge
150
+ # the end of the setup process.
151
+ # They might want to make the payers go through some other steps after they go
152
+ # through our flow or make them go through the necessary verification
153
+ # mechanism(upcomming feature).
154
+ # </p>
155
+ # Example URL: /payer_authorisations/:identity/actions/confirm
156
+ #
157
+ # @param identity # Unique identifier, beginning with "PA".
158
+ # @param options [Hash] parameters as a hash, under a params key.
159
+ def confirm(identity, options = {})
160
+ path = sub_url('/payer_authorisations/:identity/actions/confirm', 'identity' => identity)
161
+
162
+ params = options.delete(:params) || {}
163
+ options[:params] = {}
164
+ options[:params]['data'] = params
165
+
166
+ options[:retry_failures] = false
167
+
168
+ begin
169
+ response = make_request(:post, path, options)
170
+
171
+ # Response doesn't raise any errors until #body is called
172
+ response.tap(&:body)
173
+ rescue InvalidStateError => e
174
+ if e.idempotent_creation_conflict?
175
+ case @api_service.on_idempotency_conflict
176
+ when :raise
177
+ raise IdempotencyConflict, e.error
178
+ when :fetch
179
+ return get(e.conflicting_resource_id)
180
+ else
181
+ raise ArgumentError, 'Unknown mode for :on_idempotency_conflict'
182
+ end
183
+ end
184
+
185
+ raise e
186
+ end
187
+
188
+ return if response.body.nil?
189
+
190
+ Resources::PayerAuthorisation.new(unenvelope_body(response.body), response)
191
+ end
192
+
193
+ private
194
+
195
+ # Unenvelope the response of the body using the service's `envelope_key`
196
+ #
197
+ # @param body [Hash]
198
+ def unenvelope_body(body)
199
+ body[envelope_key] || body['data']
200
+ end
201
+
202
+ # return the key which API responses will envelope data under
203
+ def envelope_key
204
+ 'payer_authorisations'
205
+ end
206
+ end
207
+ end
208
+ end
@@ -4,5 +4,5 @@ end
4
4
 
5
5
  module GoCardlessPro
6
6
  # Current version of the GC gem
7
- VERSION = '2.24.0'.freeze
7
+ VERSION = '2.25.0'.freeze
8
8
  end
@@ -0,0 +1,418 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoCardlessPro::Resources::PayerAuthorisation 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 '#get' do
13
+ let(:id) { 'ID123' }
14
+
15
+ subject(:get_response) { client.payer_authorisations.get(id) }
16
+
17
+ context 'passing in a custom header' do
18
+ let!(:stub) do
19
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
20
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).
21
+ with(headers: { 'Foo' => 'Bar' }).
22
+ to_return(
23
+ body: {
24
+ 'payer_authorisations' => {
25
+
26
+ 'bank_account' => 'bank_account-input',
27
+ 'created_at' => 'created_at-input',
28
+ 'customer' => 'customer-input',
29
+ 'id' => 'id-input',
30
+ 'incomplete_fields' => 'incomplete_fields-input',
31
+ 'links' => 'links-input',
32
+ 'mandate' => 'mandate-input',
33
+ 'status' => 'status-input',
34
+ },
35
+ }.to_json,
36
+ headers: response_headers
37
+ )
38
+ end
39
+
40
+ subject(:get_response) do
41
+ client.payer_authorisations.get(id, headers: {
42
+ 'Foo' => 'Bar',
43
+ })
44
+ end
45
+
46
+ it 'includes the header' do
47
+ get_response
48
+ expect(stub).to have_been_requested
49
+ end
50
+ end
51
+
52
+ context 'when there is a payer_authorisation to return' do
53
+ before do
54
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
55
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
56
+ body: {
57
+ 'payer_authorisations' => {
58
+
59
+ 'bank_account' => 'bank_account-input',
60
+ 'created_at' => 'created_at-input',
61
+ 'customer' => 'customer-input',
62
+ 'id' => 'id-input',
63
+ 'incomplete_fields' => 'incomplete_fields-input',
64
+ 'links' => 'links-input',
65
+ 'mandate' => 'mandate-input',
66
+ 'status' => 'status-input',
67
+ },
68
+ }.to_json,
69
+ headers: response_headers
70
+ )
71
+ end
72
+
73
+ it 'wraps the response in a resource' do
74
+ expect(get_response).to be_a(GoCardlessPro::Resources::PayerAuthorisation)
75
+ end
76
+ end
77
+
78
+ context 'when nothing is returned' do
79
+ before do
80
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
81
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
82
+ body: '',
83
+ headers: response_headers
84
+ )
85
+ end
86
+
87
+ it 'returns nil' do
88
+ expect(get_response).to be_nil
89
+ end
90
+ end
91
+
92
+ context "when an ID is specified which can't be included in a valid URI" do
93
+ let(:id) { '`' }
94
+
95
+ it "doesn't raise an error" do
96
+ expect { get_response }.to_not raise_error(/bad URI/)
97
+ end
98
+ end
99
+ end
100
+
101
+ describe '#create' do
102
+ subject(:post_create_response) { client.payer_authorisations.create(params: new_resource) }
103
+ context 'with a valid request' do
104
+ let(:new_resource) do
105
+ {
106
+
107
+ 'bank_account' => 'bank_account-input',
108
+ 'created_at' => 'created_at-input',
109
+ 'customer' => 'customer-input',
110
+ 'id' => 'id-input',
111
+ 'incomplete_fields' => 'incomplete_fields-input',
112
+ 'links' => 'links-input',
113
+ 'mandate' => 'mandate-input',
114
+ 'status' => 'status-input',
115
+ }
116
+ end
117
+
118
+ before do
119
+ stub_request(:post, %r{.*api.gocardless.com/payer_authorisations}).
120
+ with(
121
+ body: {
122
+ 'payer_authorisations' => {
123
+
124
+ 'bank_account' => 'bank_account-input',
125
+ 'created_at' => 'created_at-input',
126
+ 'customer' => 'customer-input',
127
+ 'id' => 'id-input',
128
+ 'incomplete_fields' => 'incomplete_fields-input',
129
+ 'links' => 'links-input',
130
+ 'mandate' => 'mandate-input',
131
+ 'status' => 'status-input',
132
+ },
133
+ }
134
+ ).
135
+ to_return(
136
+ body: {
137
+ 'payer_authorisations' =>
138
+
139
+ {
140
+
141
+ 'bank_account' => 'bank_account-input',
142
+ 'created_at' => 'created_at-input',
143
+ 'customer' => 'customer-input',
144
+ 'id' => 'id-input',
145
+ 'incomplete_fields' => 'incomplete_fields-input',
146
+ 'links' => 'links-input',
147
+ 'mandate' => 'mandate-input',
148
+ 'status' => 'status-input',
149
+ },
150
+
151
+ }.to_json,
152
+ headers: response_headers
153
+ )
154
+ end
155
+
156
+ it 'creates and returns the resource' do
157
+ expect(post_create_response).to be_a(GoCardlessPro::Resources::PayerAuthorisation)
158
+ end
159
+ end
160
+
161
+ context 'with a request that returns a validation error' do
162
+ let(:new_resource) { {} }
163
+
164
+ before do
165
+ stub_request(:post, %r{.*api.gocardless.com/payer_authorisations}).to_return(
166
+ body: {
167
+ error: {
168
+ type: 'validation_failed',
169
+ code: 422,
170
+ errors: [
171
+ { message: 'test error message', field: 'test_field' },
172
+ ],
173
+ },
174
+ }.to_json,
175
+ headers: response_headers,
176
+ status: 422
177
+ )
178
+ end
179
+
180
+ it 'throws the correct error' do
181
+ expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError)
182
+ end
183
+ end
184
+
185
+ context 'with a request that returns an idempotent creation conflict error' do
186
+ let(:id) { 'ID123' }
187
+
188
+ let(:new_resource) do
189
+ {
190
+
191
+ 'bank_account' => 'bank_account-input',
192
+ 'created_at' => 'created_at-input',
193
+ 'customer' => 'customer-input',
194
+ 'id' => 'id-input',
195
+ 'incomplete_fields' => 'incomplete_fields-input',
196
+ 'links' => 'links-input',
197
+ 'mandate' => 'mandate-input',
198
+ 'status' => 'status-input',
199
+ }
200
+ end
201
+
202
+ let!(:post_stub) do
203
+ stub_request(:post, %r{.*api.gocardless.com/payer_authorisations}).to_return(
204
+ body: {
205
+ error: {
206
+ type: 'invalid_state',
207
+ code: 409,
208
+ errors: [
209
+ {
210
+ message: 'A resource has already been created with this idempotency key',
211
+ reason: 'idempotent_creation_conflict',
212
+ links: {
213
+ conflicting_resource_id: id,
214
+ },
215
+ },
216
+ ],
217
+ },
218
+ }.to_json,
219
+ headers: response_headers,
220
+ status: 409
221
+ )
222
+ end
223
+
224
+ let!(:get_stub) do
225
+ stub_url = "/payer_authorisations/#{id}"
226
+ stub_request(:get, /.*api.gocardless.com#{stub_url}/).
227
+ to_return(
228
+ body: {
229
+ 'payer_authorisations' => {
230
+
231
+ 'bank_account' => 'bank_account-input',
232
+ 'created_at' => 'created_at-input',
233
+ 'customer' => 'customer-input',
234
+ 'id' => 'id-input',
235
+ 'incomplete_fields' => 'incomplete_fields-input',
236
+ 'links' => 'links-input',
237
+ 'mandate' => 'mandate-input',
238
+ 'status' => 'status-input',
239
+ },
240
+ }.to_json,
241
+ headers: response_headers
242
+ )
243
+ end
244
+
245
+ it 'fetches the already-created resource' do
246
+ post_create_response
247
+ expect(post_stub).to have_been_requested
248
+ expect(get_stub).to have_been_requested
249
+ end
250
+ end
251
+ end
252
+
253
+ describe '#update' do
254
+ subject(:put_update_response) { client.payer_authorisations.update(id, params: update_params) }
255
+ let(:id) { 'ABC123' }
256
+
257
+ context 'with a valid request' do
258
+ let(:update_params) { { 'hello' => 'world' } }
259
+
260
+ let!(:stub) do
261
+ stub_url = '/payer_authorisations/:identity'.gsub(':identity', id)
262
+ stub_request(:put, /.*api.gocardless.com#{stub_url}/).to_return(
263
+ body: {
264
+ 'payer_authorisations' => {
265
+
266
+ 'bank_account' => 'bank_account-input',
267
+ 'created_at' => 'created_at-input',
268
+ 'customer' => 'customer-input',
269
+ 'id' => 'id-input',
270
+ 'incomplete_fields' => 'incomplete_fields-input',
271
+ 'links' => 'links-input',
272
+ 'mandate' => 'mandate-input',
273
+ 'status' => 'status-input',
274
+ },
275
+ }.to_json,
276
+ headers: response_headers
277
+ )
278
+ end
279
+
280
+ it 'updates and returns the resource' do
281
+ expect(put_update_response).to be_a(GoCardlessPro::Resources::PayerAuthorisation)
282
+ expect(stub).to have_been_requested
283
+ end
284
+ end
285
+ end
286
+
287
+ describe '#submit' do
288
+ subject(:post_response) { client.payer_authorisations.submit(resource_id) }
289
+
290
+ let(:resource_id) { 'ABC123' }
291
+
292
+ let!(:stub) do
293
+ # /payer_authorisations/%v/actions/submit
294
+ stub_url = '/payer_authorisations/:identity/actions/submit'.gsub(':identity', resource_id)
295
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
296
+ body: {
297
+ 'payer_authorisations' => {
298
+
299
+ 'bank_account' => 'bank_account-input',
300
+ 'created_at' => 'created_at-input',
301
+ 'customer' => 'customer-input',
302
+ 'id' => 'id-input',
303
+ 'incomplete_fields' => 'incomplete_fields-input',
304
+ 'links' => 'links-input',
305
+ 'mandate' => 'mandate-input',
306
+ 'status' => 'status-input',
307
+ },
308
+ }.to_json,
309
+ headers: response_headers
310
+ )
311
+ end
312
+
313
+ it 'wraps the response and calls the right endpoint' do
314
+ expect(post_response).to be_a(GoCardlessPro::Resources::PayerAuthorisation)
315
+
316
+ expect(stub).to have_been_requested
317
+ end
318
+
319
+ context 'when the request needs a body and custom header' do
320
+ let(:body) { { foo: 'bar' } }
321
+ let(:headers) { { 'Foo' => 'Bar' } }
322
+ subject(:post_response) { client.payer_authorisations.submit(resource_id, body, headers) }
323
+
324
+ let(:resource_id) { 'ABC123' }
325
+
326
+ let!(:stub) do
327
+ # /payer_authorisations/%v/actions/submit
328
+ stub_url = '/payer_authorisations/:identity/actions/submit'.gsub(':identity', resource_id)
329
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
330
+ with(
331
+ body: { foo: 'bar' },
332
+ headers: { 'Foo' => 'Bar' }
333
+ ).to_return(
334
+ body: {
335
+ 'payer_authorisations' => {
336
+
337
+ 'bank_account' => 'bank_account-input',
338
+ 'created_at' => 'created_at-input',
339
+ 'customer' => 'customer-input',
340
+ 'id' => 'id-input',
341
+ 'incomplete_fields' => 'incomplete_fields-input',
342
+ 'links' => 'links-input',
343
+ 'mandate' => 'mandate-input',
344
+ 'status' => 'status-input',
345
+ },
346
+ }.to_json,
347
+ headers: response_headers
348
+ )
349
+ end
350
+ end
351
+ end
352
+
353
+ describe '#confirm' do
354
+ subject(:post_response) { client.payer_authorisations.confirm(resource_id) }
355
+
356
+ let(:resource_id) { 'ABC123' }
357
+
358
+ let!(:stub) do
359
+ # /payer_authorisations/%v/actions/confirm
360
+ stub_url = '/payer_authorisations/:identity/actions/confirm'.gsub(':identity', resource_id)
361
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).to_return(
362
+ body: {
363
+ 'payer_authorisations' => {
364
+
365
+ 'bank_account' => 'bank_account-input',
366
+ 'created_at' => 'created_at-input',
367
+ 'customer' => 'customer-input',
368
+ 'id' => 'id-input',
369
+ 'incomplete_fields' => 'incomplete_fields-input',
370
+ 'links' => 'links-input',
371
+ 'mandate' => 'mandate-input',
372
+ 'status' => 'status-input',
373
+ },
374
+ }.to_json,
375
+ headers: response_headers
376
+ )
377
+ end
378
+
379
+ it 'wraps the response and calls the right endpoint' do
380
+ expect(post_response).to be_a(GoCardlessPro::Resources::PayerAuthorisation)
381
+
382
+ expect(stub).to have_been_requested
383
+ end
384
+
385
+ context 'when the request needs a body and custom header' do
386
+ let(:body) { { foo: 'bar' } }
387
+ let(:headers) { { 'Foo' => 'Bar' } }
388
+ subject(:post_response) { client.payer_authorisations.confirm(resource_id, body, headers) }
389
+
390
+ let(:resource_id) { 'ABC123' }
391
+
392
+ let!(:stub) do
393
+ # /payer_authorisations/%v/actions/confirm
394
+ stub_url = '/payer_authorisations/:identity/actions/confirm'.gsub(':identity', resource_id)
395
+ stub_request(:post, /.*api.gocardless.com#{stub_url}/).
396
+ with(
397
+ body: { foo: 'bar' },
398
+ headers: { 'Foo' => 'Bar' }
399
+ ).to_return(
400
+ body: {
401
+ 'payer_authorisations' => {
402
+
403
+ 'bank_account' => 'bank_account-input',
404
+ 'created_at' => 'created_at-input',
405
+ 'customer' => 'customer-input',
406
+ 'id' => 'id-input',
407
+ 'incomplete_fields' => 'incomplete_fields-input',
408
+ 'links' => 'links-input',
409
+ 'mandate' => 'mandate-input',
410
+ 'status' => 'status-input',
411
+ },
412
+ }.to_json,
413
+ headers: response_headers
414
+ )
415
+ end
416
+ end
417
+ end
418
+ end