Class: Strike::Charge

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

Constant Summary collapse

PATH =
"/api/v1/charges".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Charge

Returns a new instance of Charge.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/strike/charge.rb', line 31

def initialize(options)
  self.id = options["id"]
  self.amount = options["amount"]
  self.amount_satoshi = options["amount_satoshi"]
  self.currency = options["currency"]
  self.description = options["description"]
  self.customer_id = options["customer_id"]
  self.payment_request = options["payment_request"]
  self.paid = options["paid"]
  self.created = options["created"]
  self.updated = options["updated"]
  self.code = options["code"]
  self.message = options["message"]
end

Instance Attribute Details

#amountObject

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def amount
  @amount
end

#amount_satoshiObject

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def amount_satoshi
  @amount_satoshi
end

#codeObject

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def code
  @code
end

#createdObject

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def created
  @created
end

#currencyObject

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def currency
  @currency
end

#customer_idObject

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def customer_id
  @customer_id
end

#descriptionObject

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def description
  @description
end

#errorObject (readonly)

Returns the value of attribute error.



29
30
31
# File 'lib/strike/charge.rb', line 29

def error
  @error
end

#idObject

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def id
  @id
end

#messageObject

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def message
  @message
end

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def paid
  @paid
end

#payment_requestObject

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def payment_request
  @payment_request
end

#updatedObject

String Unique identifier of the charge



16
17
18
# File 'lib/strike/charge.rb', line 16

def updated
  @updated
end

Class Method Details

.all(page: 1, per_page: 30) ⇒ Object

Fetch a charge by id

returns [Strike::Charge]

Parameters:

  • page (Integer) (defaults to: 1)

    page of results to display

  • per_page (Integer) (defaults to: 30)

    number of results per page



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/strike/charge.rb', line 60

def self.all(page: 1, per_page: 30)
  response =
    conn.get do |req|
      req.url api_url
      req.params['page'] = (page - 1).to_i
      req.params['size'] = per_page.to_i
    end
  handle_errors(response)

  JSON.parse(response.body).map { |attributes| new(attributes) }
end

.api_urlObject



8
9
10
# File 'lib/strike/charge.rb', line 8

def self.api_url
  @api_url ||= PATH
end

.connObject



12
13
14
# File 'lib/strike/charge.rb', line 12

def self.conn
  Client.conn
end

.create(amount:, currency:, description:, customer_id:) ⇒ Object

Create a new Strike charge

returns [Strike::Charge]

Parameters:

  • amount (Integer)

    Positive integer in the smallest unit in the currency used. If the currency is btc, the amount must be in satoshi, and the maximum amount is 4,294,967 satoshis.

  • currency (String)

    Currency code associated with the amount. Only btc is supported currently.

  • description (String)

    Discretionary description of the charge. Must not exceed 256 characters.

  • customer_id (String)

    This field is optional and can be used to attach an identifier of your choice to the charge. Must not exceed 64 characters.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/strike/charge.rb', line 92

def self.create(amount:, currency:, description:, customer_id:)
  body = {
    amount: amount,
    currency: currency,
    description: description,
    customer_id: customer_id
  }

  response =
    conn.post do |req|
      req.url api_url
      req.headers['Content-Type'] = 'application/json'
      req.body = body.to_json
    end

  handle_errors(response)

  new(JSON.parse(response.body))
end

.find(id) ⇒ Object

Fetch a charge by id

returns [Strike::Charge]

Parameters:

  • id (String)

    ID of Strike charge record



77
78
79
80
81
82
# File 'lib/strike/charge.rb', line 77

def self.find(id)
  response = conn.get("#{api_url}/#{id}")
  handle_errors(response)

  new(JSON.parse(response.body))
end

.handle_errors(response) ⇒ Object

Raises:



112
113
114
115
# File 'lib/strike/charge.rb', line 112

def self.handle_errors(response)
  error = Strike::Errors::ERROR_MAP[response.status]
  raise error.new if error
end

Instance Method Details

#fail?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/strike/charge.rb', line 50

def fail?
  !success?
end

#success?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/strike/charge.rb', line 46

def success?
  code.nil? || code == 200
end