class GoCardlessPro::Services::MandateImportsService

Service for making requests to the MandateImport endpoints

Public Instance Methods

cancel(identity, options = {}) click to toggle source

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

@param identity # Unique identifier, beginning with “IM”. @param options [Hash] parameters as a hash, under a params key.

# File lib/gocardless_pro/services/mandate_imports_service.rb, line 127
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)
      end
    end

    raise e
  end

  return if response.body.nil?

  Resources::MandateImport.new(unenvelope_body(response.body), response)
end
create(options = {}) click to toggle source

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 @param options [Hash] parameters as a hash, under a params key.

# File lib/gocardless_pro/services/mandate_imports_service.rb, line 20
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)
      end
    end

    raise e
  end

  return if response.body.nil?

  Resources::MandateImport.new(unenvelope_body(response.body), response)
end
get(identity, options = {}) click to toggle source

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

@param identity # Unique identifier, beginning with “IM”. @param options [Hash] parameters as a hash, under a params key.

# File lib/gocardless_pro/services/mandate_imports_service.rb, line 57
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 = {}) click to toggle source

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

@param identity # Unique identifier, beginning with “IM”. @param options [Hash] parameters as a hash, under a params key.

# File lib/gocardless_pro/services/mandate_imports_service.rb, line 84
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)
      end
    end

    raise e
  end

  return if response.body.nil?

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

Private Instance Methods

envelope_key() click to toggle source

return the key which API responses will envelope data under

# File lib/gocardless_pro/services/mandate_imports_service.rb, line 169
def envelope_key
  'mandate_imports'
end
unenvelope_body(body) click to toggle source

Unenvelope the response of the body using the service's `envelope_key`

@param body [Hash]

# File lib/gocardless_pro/services/mandate_imports_service.rb, line 164
def unenvelope_body(body)
  body[envelope_key] || body['data']
end