Class: Affirm::Charge

Inherits:
Object
  • Object
show all
Defined in:
lib/affirm/charge.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs: {}, client: Affirm::API.client) ⇒ Charge

Returns a new instance of Charge.



72
73
74
75
# File 'lib/affirm/charge.rb', line 72

def initialize(attrs: {}, client: Affirm::API.client)
  @client = client
  set_attrs(attrs)
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



3
4
5
# File 'lib/affirm/charge.rb', line 3

def amount
  @amount
end

#auth_holdObject (readonly)

Returns the value of attribute auth_hold.



3
4
5
# File 'lib/affirm/charge.rb', line 3

def auth_hold
  @auth_hold
end

#createdObject (readonly)

Returns the value of attribute created.



3
4
5
# File 'lib/affirm/charge.rb', line 3

def created
  @created
end

#currencyObject (readonly)

Returns the value of attribute currency.



3
4
5
# File 'lib/affirm/charge.rb', line 3

def currency
  @currency
end

#detailsObject (readonly)

Returns the value of attribute details.



3
4
5
# File 'lib/affirm/charge.rb', line 3

def details
  @details
end

#eventsObject (readonly)

Returns the value of attribute events.



3
4
5
# File 'lib/affirm/charge.rb', line 3

def events
  @events
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/affirm/charge.rb', line 3

def id
  @id
end

#order_idObject (readonly)

Returns the value of attribute order_id.



3
4
5
# File 'lib/affirm/charge.rb', line 3

def order_id
  @order_id
end

#payableObject (readonly)

Returns the value of attribute payable.



3
4
5
# File 'lib/affirm/charge.rb', line 3

def payable
  @payable
end

Class Method Details

.create(checkout_token, client: Affirm::API.client) ⇒ Object

CREATE / AUTHORIZE

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



17
18
19
20
21
22
23
24
25
# 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

.retrieve(id, client: Affirm::API.client) ⇒ Object

RETRIEVE

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



9
10
11
# File 'lib/affirm/charge.rb', line 9

def self.retrieve(id, client: Affirm::API.client)
  new(attrs: {"id" => id}, client: client).refresh
end

Instance Method Details

#capture(order_id: nil, shipping_carrier: nil, shipping_confirmation: nil) ⇒ Object

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.



33
34
35
36
37
38
39
# 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

#refreshObject



77
78
79
80
81
82
83
# 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) ⇒ Object

REFUND

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



52
53
54
55
56
# 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) ⇒ Object

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.



64
65
66
67
68
69
70
# 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

#voidObject

VOID



44
45
46
# File 'lib/affirm/charge.rb', line 44

def void
  api_request("/charges/#{id}/void", :post)
end

#void?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/affirm/charge.rb', line 85

def void?
  @void
end