class Strike::Charge
Constants
- PATH
Attributes
Public Class Methods
Fetch a charge by id
@param page [Integer] page of results to display @param per_page [Integer] number of results per page
returns [Strike::Charge]
# 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
# File lib/strike/charge.rb, line 8 def self.api_url @api_url ||= PATH end
# File lib/strike/charge.rb, line 12 def self.conn Client.conn end
Create a new Strike
charge
@param 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. @param currency [String] Currency code associated with the amount. Only btc is supported currently. @param description [String] Discretionary description of the charge. Must not exceed 256 characters. @param 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.
returns [Strike::Charge]
# 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
Fetch a charge by id
@param id [String] ID of Strike
charge record
returns [Strike::Charge]
# 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
# 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
# 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
Public Instance Methods
# File lib/strike/charge.rb, line 50 def fail? !success? end
# File lib/strike/charge.rb, line 46 def success? code.nil? || code == 200 end