class GoCardless::Services::RefundService

Service for making requests to the Refund endpoints

Public Instance Methods

all(options = {}) click to toggle source

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

@param options [Hash] parameters as a hash. If the request is a GET, these will be converted to query parameters. Otherwise they will be the body of the request.

# File lib/gocardless-pro/services/refund_service.rb, line 63
def all(options = {})
  Paginator.new(
    service: self,
    path: '/refunds',
    options: options
  ).enumerator
end
create(options = {}, custom_headers = {}) click to toggle source

Creates a new refund object.

This fails with:<a name=“refund_payment_invalid_state”></a><a name=“total_amount_confirmation_invalid”></a>

- ‘refund_payment_invalid_state` error if the linked [payment](developer.gocardless.com/pro/#api-endpoints-payments) isn’t either ‘confirmed` or `paid_out`.

  • ‘total_amount_confirmation_invalid`

if the confirmation amount doesn’t match the total amount refunded for the payment. This safeguard is there to prevent two processes from creating refunds without awareness of each other.

Example URL: /refunds @param options [Hash] parameters as a hash. If the request is a GET, these will be converted to query parameters. Else, they will be the body of the request.

# File lib/gocardless-pro/services/refund_service.rb, line 32
def create(options = {}, custom_headers = {})
  path = '/refunds'
  new_options = {}
  new_options[envelope_key] = options
  options = new_options
  response = make_request(:post, path, options, custom_headers)

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

Retrieves all details for a single refund Example URL: /refunds/:identity

@param identity # Unique identifier, beginning with “RF” @param options [Hash] parameters as a hash. If the request is a GET, these will be converted to query parameters. Else, they will be the body of the request.

# File lib/gocardless-pro/services/refund_service.rb, line 77
def get(identity, options = {}, custom_headers = {})
  path = sub_url('/refunds/:identity',           'identity' => identity)

  response = make_request(:get, path, options, custom_headers)

  Resources::Refund.new(unenvelope_body(response.body))
end
list(options = {}, custom_headers = {}) click to toggle source

Returns a [cursor-paginated](developer.gocardless.com/pro/#overview-cursor-pagination) list of your refunds. Example URL: /refunds @param options [Hash] parameters as a hash. If the request is a GET, these will be converted to query parameters. Else, they will be the body of the request.

# File lib/gocardless-pro/services/refund_service.rb, line 48
def list(options = {}, custom_headers = {})
  path = '/refunds'

  response = make_request(:get, path, options, custom_headers)
  ListResponse.new(
    raw_response: response,
    unenveloped_body: unenvelope_body(response.body),
    resource_class: Resources::Refund
  )
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/refund_service.rb, line 105
def unenvelope_body(body)
  body[envelope_key] || body['data']
end
update(identity, options = {}, custom_headers = {}) click to toggle source

Updates a refund object. Example URL: /refunds/:identity

@param identity # Unique identifier, beginning with “RF” @param options [Hash] parameters as a hash. If the request is a GET, these will be converted to query parameters. Else, they will be the body of the request.

# File lib/gocardless-pro/services/refund_service.rb, line 91
def update(identity, options = {}, custom_headers = {})
  path = sub_url('/refunds/:identity',           'identity' => identity)

  new_options = {}
  new_options[envelope_key] = options
  options = new_options
  response = make_request(:put, path, options, custom_headers)

  Resources::Refund.new(unenvelope_body(response.body))
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/refund_service.rb, line 112
def envelope_key
  'refunds'
end
sub_url(url, param_map) click to toggle source

take a URL with placeholder params and substitute them out for the acutal value @param url [String] the URL with placeholders in @param param_map [Hash] a hash of placeholders and their actual values

# File lib/gocardless-pro/services/refund_service.rb, line 119
def sub_url(url, param_map)
  param_map.reduce(url) do |new_url, (param, value)|
    new_url.gsub(":#{param}", value)
  end
end