class Affirm::Charge

Attributes

amount[R]
auth_hold[R]
created[R]
currency[R]
details[R]
events[R]
id[R]
order_id[R]
payable[R]

Public Class Methods

create(checkout_token, client: Affirm::API.client) click to toggle source

CREATE / AUTHORIZE

checkout_token - (required) string. The charge token passed through the confirmation response.

# File lib/affirm/charge.rb, line 17
def self.create(checkout_token, client: Affirm::API.client)
  response = client.make_request("/charges", :post, checkout_token: checkout_token)

  if response.success?
    new(attrs: response.body, client: client)
  else
    raise ChargeError.from_response(response)
  end
end
new(attrs: {}, client: Affirm::API.client) click to toggle source
# File lib/affirm/charge.rb, line 72
def initialize(attrs: {}, client: Affirm::API.client)
  @client = client
  set_attrs(attrs)
end
retrieve(id, client: Affirm::API.client) click to toggle source

RETRIEVE

id - (required) string. The charge id, in format ‘XXXX-XXXX’

# File lib/affirm/charge.rb, line 9
def self.retrieve(id, client: Affirm::API.client)
  new(attrs: {"id" => id}, client: client).refresh
end

Public Instance Methods

capture(order_id: nil, shipping_carrier: nil, shipping_confirmation: nil) click to toggle source

CAPTURE

order_id - (optional) string. Your internal order id. This is stored for your own future reference. shipping_carrier - (optional) string. The shipping carrier used to ship the items in the charge. shipping_confirmation - (optional) string. The shipping confirmation for the shipment.

# File lib/affirm/charge.rb, line 33
def capture(order_id: nil, shipping_carrier: nil, shipping_confirmation: nil)
  api_request("/charges/#{id}/capture", :post, {
    order_id: order_id,
    shipping_carrier: shipping_carrier,
    shipping_confirmation: shipping_confirmation
  })
end
refresh() click to toggle source
# File lib/affirm/charge.rb, line 77
def refresh
  response = @client.make_request("/charges/#{id}", :get)

  set_attrs(response.body)

  self
end
refund(amount: nil) click to toggle source

REFUND

amount - (optional) integer or null. The amount to refund in cents. The default amount is the remaining balance on the charge.

# File lib/affirm/charge.rb, line 52
def refund(amount: nil)
  api_request("/charges/#{id}/refund", :post, {
    amount: amount
  })
end
update(order_id: nil, shipping_carrier: nil, shipping_confirmation: nil) click to toggle source

UPDATE

order_id - (optional) string. Your internal order id. This is stored for your own future reference. shipping_carrier - (optional) string. The shipping carrier used to ship the items in the charge. shipping_confirmation - (optional) string. The shipping confirmation for the shipment.

# File lib/affirm/charge.rb, line 64
def update(order_id: nil, shipping_carrier: nil, shipping_confirmation: nil)
  api_request("/charges/#{id}/update", :post, {
    order_id: order_id,
    shipping_carrier: shipping_carrier,
    shipping_confirmation: shipping_confirmation
  })
end
void() click to toggle source

VOID

# File lib/affirm/charge.rb, line 44
def void
  api_request("/charges/#{id}/void", :post)
end
void?() click to toggle source
# File lib/affirm/charge.rb, line 85
def void?
  @void
end

Private Instance Methods

api_request(url, method, params={}) click to toggle source
# File lib/affirm/charge.rb, line 112
def api_request(url, method, params={})
  response = @client.make_request(url, method, params)
  if response.success?
    event = ChargeEvent.new(response.body)
    @events << event
    event
  else
    raise ChargeError.from_response(response)
  end
end
parse_events(events_attrs) click to toggle source
# File lib/affirm/charge.rb, line 104
def parse_events(events_attrs)
  if events_attrs
    events_attrs.map { |event| ChargeEvent.new(event) }
  else
    []
  end
end
set_attrs(attrs) click to toggle source
# File lib/affirm/charge.rb, line 91
def set_attrs(attrs)
  @id        = attrs["id"]
  @amount    = attrs["amount"]
  @created   = attrs["created"]
  @currency  = attrs["currency"]
  @auth_hold = attrs["auth_hold"]
  @payable   = attrs["payable"]
  @void      = attrs["void"]
  @order_id  = attrs["order_id"]
  @details   = attrs["details"]
  @events    = parse_events(attrs["events"])
end