class Affirm::Charge
Attributes
Public Class Methods
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
# File lib/affirm/charge.rb, line 72 def initialize(attrs: {}, client: Affirm::API.client) @client = client set_attrs(attrs) end
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
- (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
# File lib/affirm/charge.rb, line 77 def refresh response = @client.make_request("/charges/#{id}", :get) set_attrs(response.body) self end
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
- (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
# File lib/affirm/charge.rb, line 44 def void api_request("/charges/#{id}/void", :post) end
# File lib/affirm/charge.rb, line 85 def void? @void end
Private Instance Methods
# 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
# 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
# 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