Class: GoCardlessPro::Services::MandateImportsService

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

Overview

Service for making requests to the MandateImport endpoints

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #make_request

Constructor Details

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

Instance Method Details

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

Cancels the mandate import, which aborts the import process and stops the mandates being set up in GoCardless. Once the import has been cancelled, it can no longer have entries added to it. Mandate imports which have already been submitted or processed cannot be cancelled. Example URL: /mandate_imports/:identity/actions/cancel

Parameters:

  • identity

    # Unique identifier, beginning with “IM”.

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

    parameters as a hash, under a params key.



132
133
134
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
# File 'lib/gocardless_pro/services/mandate_imports_service.rb', line 132

def cancel(identity, options = {})
  path = sub_url('/mandate_imports/: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)
      else
        raise ArgumentError, 'Unknown mode for :on_idempotency_conflict'
      end
    end

    raise e
  end

  return if response.body.nil?

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

#create(options = {}) ⇒ Object

Mandate imports are first created, before mandates are added one-at-a-time, so this endpoint merely signals the start of the import process. Once you’ve finished adding entries to an import, you should [submit](#mandate-imports-submit-a-mandate-import) it. Example URL: /mandate_imports

Parameters:

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

    parameters as a hash, under a params key.



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
48
49
50
51
52
53
# File 'lib/gocardless_pro/services/mandate_imports_service.rb', line 21

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

  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)
      else
        raise ArgumentError, 'Unknown mode for :on_idempotency_conflict'
      end
    end

    raise e
  end

  return if response.body.nil?

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

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

Returns a single mandate import. Example URL: /mandate_imports/:identity

Parameters:

  • identity

    # Unique identifier, beginning with “IM”.

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

    parameters as a hash, under a params key.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/gocardless_pro/services/mandate_imports_service.rb', line 60

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

  options[:retry_failures] = true

  response = make_request(:get, path, options)

  return if response.body.nil?

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

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

Submits the mandate import, which allows it to be processed by a member of the GoCardless team. Once the import has been submitted, it can no longer have entries added to it.

In our sandbox environment, to aid development, we automatically process mandate imports approximately 10 seconds after they are submitted. This will allow you to test both the “submitted” response and wait for the webhook to confirm the processing has begun. Example URL: /mandate_imports/:identity/actions/submit

Parameters:

  • identity

    # Unique identifier, beginning with “IM”.

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

    parameters as a hash, under a params key.



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

def submit(identity, options = {})
  path = sub_url('/mandate_imports/:identity/actions/submit', '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)
      else
        raise ArgumentError, 'Unknown mode for :on_idempotency_conflict'
      end
    end

    raise e
  end

  return if response.body.nil?

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