Class: GoCardlessPro::Services::InstalmentSchedulesService

Inherits:
BaseService
  • Object
show all
Defined in:
lib/gocardless_pro/services/instalment_schedules_service.rb

Overview

Service for making requests to the InstalmentSchedule endpoints

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #make_request, #sub_url

Constructor Details

This class inherits a constructor from GoCardlessPro::Services::BaseService

Instance Method Details

#all(options = {}) ⇒ Object

Get a lazily enumerated list of all the items returned. This is similar to the list method but will paginate for you automatically.

Otherwise they will be the body of the request.

Parameters:

  • options (Hash) (defaults to: {})

    parameters as a hash. If the request is a GET, these will be converted to query parameters.



138
139
140
141
142
143
# File 'lib/gocardless_pro/services/instalment_schedules_service.rb', line 138

def all(options = {})
  Paginator.new(
    service: self,
    options: options
  ).enumerator
end

#cancel(identity, options = {}) ⇒ Object

Immediately cancels an instalment schedule; no further payments will be collected for it.

This will fail with a cancellation_failed error if the instalment schedule is already cancelled or has completed. Example URL: /instalment_schedules/:identity/actions/cancel

Parameters:

  • identity

    Unique identifier, beginning with "IS".

  • options (Hash) (defaults to: {})

    parameters as a hash, under a params key.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/gocardless_pro/services/instalment_schedules_service.rb', line 196

def cancel(identity, options = {})
  path = sub_url('/instalment_schedules/:identity/actions/cancel', {
                   'identity' => identity,
                 })

  params = options.delete(:params) || {}
  options[:params] = {}
  options[:params]['data'] = params

  options[:retry_failures] = false

  begin
    response = make_request(:post, path, options)

    # Response doesn't raise any errors until #body is called
    response.tap(&:body)
  rescue InvalidStateError => e
    if e.idempotent_creation_conflict?
      case @api_service.on_idempotency_conflict
      when :raise
        raise IdempotencyConflict.new(e.error)
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

  Resources::InstalmentSchedule.new(unenvelope_body(response.body), response)
end

#create_with_dates(options = {}) ⇒ Object

Creates a new instalment schedule object, along with the associated payments. This API is recommended if you know the specific dates you wish to charge. Otherwise, please check out the scheduling version (https://developer.gocardless.com/api-reference/#instalment-schedules-create-with-schedule).

The instalments property is an array of payment properties (amount and charge_date).

It can take quite a while to create the associated payments, so the API will return the status as pending initially. When processing has completed, a subsequent GET request for the instalment schedule will either have the status success and link to the created payments, or the status error and detailed information about the failures. Example URL: /instalment_schedules

Parameters:

  • options (Hash) (defaults to: {})

    parameters as a hash, under a params key.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gocardless_pro/services/instalment_schedules_service.rb', line 34

def create_with_dates(options = {})
  path = '/instalment_schedules'

  params = options.delete(:params) || {}
  options[:params] = {}
  options[:params][envelope_key] = params

  options[:retry_failures] = true

  begin
    response = make_request(:post, path, options)

    # Response doesn't raise any errors until #body is called
    response.tap(&:body)
  rescue InvalidStateError => e
    if e.idempotent_creation_conflict?
      case @api_service.on_idempotency_conflict
      when :raise
        raise IdempotencyConflict.new(e.error)
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

  Resources::InstalmentSchedule.new(unenvelope_body(response.body), response)
end

#create_with_schedule(options = {}) ⇒ Object

Creates a new instalment schedule object, along with the associated payments. This API is recommended if you wish to use the GoCardless scheduling logic. For finer control over the individual dates, please check out the alternative version (https://developer.gocardless.com/api-reference/#instalment-schedules-create-with-dates).

It can take quite a while to create the associated payments, so the API will return the status as pending initially. When processing has completed, a subsequent GET request for the instalment schedule will either have the status success and link to the created payments, or the status error and detailed information about the failures. Example URL: /instalment_schedules

Parameters:

  • options (Hash) (defaults to: {})

    parameters as a hash, under a params key.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gocardless_pro/services/instalment_schedules_service.rb', line 83

def create_with_schedule(options = {})
  path = '/instalment_schedules'

  params = options.delete(:params) || {}
  options[:params] = {}
  options[:params][envelope_key] = params

  options[:retry_failures] = true

  begin
    response = make_request(:post, path, options)

    # Response doesn't raise any errors until #body is called
    response.tap(&:body)
  rescue InvalidStateError => e
    if e.idempotent_creation_conflict?
      case @api_service.on_idempotency_conflict
      when :raise
        raise IdempotencyConflict.new(e.error)
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

  Resources::InstalmentSchedule.new(unenvelope_body(response.body), response)
end

#get(identity, options = {}) ⇒ Object

Retrieves the details of an existing instalment schedule. Example URL: /instalment_schedules/:identity

Parameters:

  • identity

    Unique identifier, beginning with "IS".

  • options (Hash) (defaults to: {})

    parameters as a hash, under a params key.



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/gocardless_pro/services/instalment_schedules_service.rb', line 150

def get(identity, options = {})
  path = sub_url('/instalment_schedules/:identity', {
                   'identity' => identity,
                 })

  options[:retry_failures] = true

  response = make_request(:get, path, options)

  return if response.body.nil?

  Resources::InstalmentSchedule.new(unenvelope_body(response.body), response)
end

#list(options = {}) ⇒ Object

Returns a cursor-paginated (https://developer.gocardless.com/api-reference/#api-usage-cursor-pagination) list of your instalment schedules. Example URL: /instalment_schedules

Parameters:

  • options (Hash) (defaults to: {})

    parameters as a hash, under a params key.



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/gocardless_pro/services/instalment_schedules_service.rb', line 120

def list(options = {})
  path = '/instalment_schedules'

  options[:retry_failures] = true

  response = make_request(:get, path, options)

  ListResponse.new(
    response: response,
    unenveloped_body: unenvelope_body(response.body),
    resource_class: Resources::InstalmentSchedule
  )
end

#update(identity, options = {}) ⇒ Object

Updates an instalment schedule. This accepts only the metadata parameter. Example URL: /instalment_schedules/:identity

Parameters:

  • identity

    Unique identifier, beginning with "IS".

  • options (Hash) (defaults to: {})

    parameters as a hash, under a params key.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/gocardless_pro/services/instalment_schedules_service.rb', line 169

def update(identity, options = {})
  path = sub_url('/instalment_schedules/:identity', {
                   'identity' => identity,
                 })

  params = options.delete(:params) || {}
  options[:params] = {}
  options[:params][envelope_key] = params

  options[:retry_failures] = true

  response = make_request(:put, path, options)

  return if response.body.nil?

  Resources::InstalmentSchedule.new(unenvelope_body(response.body), response)
end