Class: GoCardlessPro::Services::BillingRequestsService

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

Overview

Service for making requests to the BillingRequest 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.



373
374
375
376
377
378
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 373

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

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

Immediately cancels a billing request, causing all billing request flows to expire. Example URL: /billing_requests/:identity/actions/cancel

Parameters:

  • identity

    # Unique identifier, beginning with “BRQ”.

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

    parameters as a hash, under a params key.



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 317

def cancel(identity, options = {})
  path = sub_url('/billing_requests/: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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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

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

This will allow for the updating of the currency and subsequently the scheme if needed for a Billing Request. This will only be available for mandate only flows which do not have the lock_currency flag set to true on the Billing Request Flow. It will also not support any request which has a payments request. Example URL: /billing_requests/:identity/actions/choose_currency

Parameters:

  • identity

    # Unique identifier, beginning with “BRQ”.

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

    parameters as a hash, under a params key.



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 494

def choose_currency(identity, options = {})
  path = sub_url('/billing_requests/:identity/actions/choose_currency', {
                   '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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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

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

If the billing request has a pending collect_bank_account action, this endpoint can be used to collect the details in order to complete it.

The endpoint takes the same payload as Customer Bank Accounts, but check the bank account is valid for the billing request scheme before creating and attaching it.

If the scheme is PayTo and the pay_id is available, this can be included in the payload along with the country_code.

_ACH scheme_ For compliance reasons, an extra validation step is done using a third-party provider to make sure the customer’s bank account can accept Direct Debit. If a bank account is discovered to be closed or invalid, the customer is requested to adjust the account number/routing number and succeed in this check to continue with the flow.

_BACS scheme_ [Payer Name Verification](hub.gocardless.com/s/article/Introduction-to-Payer-Name-Verification?language=en_GB) is enabled by default for UK based bank accounts, meaning we verify the account holder name and bank account number match the details held by the relevant bank. Example URL: /billing_requests/:identity/actions/collect_bank_account

Parameters:

  • identity

    # Unique identifier, beginning with “BRQ”.

  • 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/billing_requests_service.rb', line 196

def (identity, options = {})
  path = sub_url('/billing_requests/:identity/actions/collect_bank_account', {
                   '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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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

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

If the billing request has a pending collect_customer_details action, this endpoint can be used to collect the details in order to complete it.

The endpoint takes the same payload as Customers, but checks that the customer fields are populated correctly for the billing request scheme.

Whatever is provided to this endpoint is used to update the referenced customer, and will take effect immediately after the request is successful. Example URL: /billing_requests/:identity/actions/collect_customer_details

Parameters:

  • identity

    # Unique identifier, beginning with “BRQ”.

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

    parameters as a hash, under a params key.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 135

def collect_customer_details(identity, options = {})
  path = sub_url('/billing_requests/:identity/actions/collect_customer_details', {
                   '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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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

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

This is needed when you have a mandate request. As a scheme compliance rule we are required to allow the payer to crosscheck the details entered by them and confirm it. Example URL: /billing_requests/:identity/actions/confirm_payer_details

Parameters:

  • identity

    # Unique identifier, beginning with “BRQ”.

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

    parameters as a hash, under a params key.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 237

def confirm_payer_details(identity, options = {})
  path = sub_url('/billing_requests/:identity/actions/confirm_payer_details', {
                   '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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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

#create(options = {}) ⇒ Object

<p class=“notice”><strong>Important</strong>: All properties associated with ‘subscription_request` are only supported for ACH and PAD schemes.</p> Example URL: /billing_requests

Parameters:

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

    parameters as a hash, under a params key.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 17

def create(options = {})
  path = '/billing_requests'

  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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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

#create_with_instalments_with_dates(options = {}) ⇒ Object

<p class=“notice”><strong>Important</strong>: All properties associated with ‘instalment_schedule_request` are only supported for ACH and PAD schemes.</p> Example URL: /billing_requests

Parameters:

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

    parameters as a hash, under a params key.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 53

def create_with_instalments_with_dates(options = {})
  path = '/billing_requests'

  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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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

#create_with_instalments_with_schedule(options = {}) ⇒ Object

<p class=“notice”><strong>Important</strong>: All properties associated with ‘instalment_schedule_request` are only supported for ACH and PAD schemes.</p> Example URL: /billing_requests

Parameters:

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

    parameters as a hash, under a params key.



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
114
115
116
117
118
119
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 89

def create_with_instalments_with_schedule(options = {})
  path = '/billing_requests'

  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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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

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

Triggers a fallback from the open-banking flow to direct debit. Note, the billing request must have fallback enabled. Example URL: /billing_requests/:identity/actions/fallback

Parameters:

  • identity

    # Unique identifier, beginning with “BRQ”.

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

    parameters as a hash, under a params key.



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 449

def fallback(identity, options = {})
  path = sub_url('/billing_requests/:identity/actions/fallback', {
                   '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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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

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

If a billing request is ready to be fulfilled, call this endpoint to cause it to fulfil, executing the payment. Example URL: /billing_requests/:identity/actions/fulfil

Parameters:

  • identity

    # Unique identifier, beginning with “BRQ”.

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

    parameters as a hash, under a params key.



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 277

def fulfil(identity, options = {})
  path = sub_url('/billing_requests/:identity/actions/fulfil', {
                   '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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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

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

Fetches a billing request Example URL: /billing_requests/:identity

Parameters:

  • identity

    # Unique identifier, beginning with “BRQ”.

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

    parameters as a hash, under a params key.



385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 385

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

  options[:retry_failures] = true

  response = make_request(:get, path, options)

  return if response.body.nil?

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

#list(options = {}) ⇒ Object

Returns a [cursor-paginated](#api-usage-cursor-pagination) list of your billing requests. Example URL: /billing_requests

Parameters:

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

    parameters as a hash, under a params key.



355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 355

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

  options[:retry_failures] = true

  response = make_request(:get, path, options)

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

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

Notifies the customer linked to the billing request, asking them to authorise it. Currently, the customer can only be notified by email.

This endpoint is currently supported only for Instant Bank Pay Billing Requests. Example URL: /billing_requests/:identity/actions/notify

Parameters:

  • identity

    # Unique identifier, beginning with “BRQ”.

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

    parameters as a hash, under a params key.



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 409

def notify(identity, options = {})
  path = sub_url('/billing_requests/:identity/actions/notify', {
                   '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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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

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

Creates an Institution object and attaches it to the Billing Request Example URL: /billing_requests/:identity/actions/select_institution

Parameters:

  • identity

    # Unique identifier, beginning with “BRQ”.

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

    parameters as a hash, under a params key.



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/gocardless_pro/services/billing_requests_service.rb', line 533

def select_institution(identity, options = {})
  path = sub_url('/billing_requests/:identity/actions/select_institution', {
                   '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, e.error
      when :fetch
        return get(e.conflicting_resource_id)
      end
    end

    raise e
  end

  return if response.body.nil?

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