class Charging::Invoice
Constants
- ATTRIBUTES
- READ_ONLY_ATTRIBUTES
Public Class Methods
Finds an invoice by uuid. It requites an domain
and a uuid
.
Returns an Invoice
instance or raises a Http::LastResponseError
if something went wrong, like unauthorized request, not found.
API method: GET /invoices/:uuid/
API documentation: charging.financeconnect.com.br/static/docs/charges.html#get-invoices-uuid
# File lib/charging/invoice.rb, line 120 def self.find_by_uuid(domain, uuid) Helpers.required_arguments!(domain: domain, uuid: uuid) response = Invoice.get_invoice(domain, uuid) raise_last_response_unless 200, response load_persisted_invoice(MultiJson.decode(response.body), response, domain) end
Returns a list of kind of invoices available for current domain. You SHOULD pass a domain
instance. You MAY pass a page
and limit
for pagination.
API method: <tt>GET /invoices/kinds?page=:page&limit=:limit
API documentation: charging.financeconnect.com.br/static/docs/charges.html#get-invoices-kinds-limit-limit-page-page
# File lib/charging/invoice.rb, line 138 def self.kinds(domain, page = DEFAULT_PAGE, limit = DEFAULT_LIMIT) Helpers.required_arguments!(domain: domain) response = Http.get("/invoices/kinds/?page=#{Integer(page)}&limit=#{Integer(limit)}", domain.token) raise_last_response_unless 200, response MultiJson.decode(response) end
# File lib/charging/invoice.rb, line 148 def self.load_persisted_invoice(attributes, response, domain, charge_account = nil) charge_account_uri = attributes.delete("charge_account").to_s if charge_account.nil? && charge_account_uri.start_with?('http') begin charge_account = ChargeAccount.find_by_uri(domain, charge_account_uri) rescue Http::LastResponseError end end validate_attributes!(attributes) Invoice.new(attributes, domain, charge_account, response) end
Charging::Base::new
# File lib/charging/invoice.rb, line 17 def initialize(attributes, domain, charge_account, response = nil) super(attributes, response) @domain = domain @charge_account = charge_account end
Private Class Methods
# File lib/charging/invoice.rb, line 175 def self.get_invoice(domain, uuid) Http.get("/invoices/#{uuid}/", domain.token) end
# File lib/charging/invoice.rb, line 179 def self.post_charge_accounts_invoices(domain, charge_account, attributes) Http.post("/charge-accounts/#{charge_account.uuid}/invoices/", domain.token, MultiJson.encode(attributes)) end
Public Instance Methods
Returns a String with the temporary URL for print current invoice.
API method: GET /invoices/:uuid/billet/
API documentation: charging.financeconnect.com.br/static/docs/charges.html#get-invoices-uuid-billet
# File lib/charging/invoice.rb, line 99 def billet_url return if unpersisted? response = Http.get("/invoices/#{uuid}/billet/", domain.token) return if response.code != 200 MultiJson.decode(response.body)["billet"] rescue nil end
Creates current invoice at API.
API method: POST /charge-accounts/:uuid/invoices/
API documentation: charging.financeconnect.com.br/static/docs/charges.html#post-charge-accounts-uuid-invoices
Charging::Base#create!
# File lib/charging/invoice.rb, line 28 def create! super do raise 'can not create without a domain' if invalid_domain? raise 'can not create wihtout a charge account' if invalid_charge_account? Invoice.post_charge_accounts_invoices(domain, charge_account, attributes) end reload_attributes!(Helpers.extract_uuid(last_response.headers[:location]) || uuid) end
Deletes the invoice at API
API method: DELETE /invoices/:uuid/
API documentation: charging.financeconnect.com.br/static/docs/charges.html#delete-invoices-uuid
Charging::Base#destroy!
# File lib/charging/invoice.rb, line 44 def destroy! super do Http.delete("/invoices/#{uuid}/", domain.token, etag) end end
Pays current invoice at API. You can pass paid_amount
, payment_date
and note
about payment. Default values:
-
amount
: amount -
date
: Time.now.strftime('%Y-%m-%d')
API method: POST /invoices/:uuid/pay/
API documentation: charging.financeconnect.com.br/static/docs/charges.html#post-invoices-uuid-pay
# File lib/charging/invoice.rb, line 59 def pay!(payment_data = {}) reset_errors! attributes = { amount: self.amount, date: Time.now.strftime('%Y-%m-%d') }.merge(payment_data) @last_response = Http.post("/invoices/#{uuid}/pay/", domain.token, MultiJson.encode(attributes), etag: self.etag) raise_last_response_unless 201 reload_attributes!(uuid) ensure if $ERROR_INFO @last_response = $ERROR_INFO.last_response if $ERROR_INFO.kind_of?(Http::LastResponseError) @errors = [$ERROR_INFO.message] end end
List all payments for an invoice
API method: GET /invoices/:uuid/payments/
API documentation: charging.financeconnect.com.br/static/docs/charges.html#get-invoices-uuid-payments
# File lib/charging/invoice.rb, line 84 def payments reset_errors! response = Http.get("/invoices/#{uuid}/payments/", domain.token) return [] if response.code != 200 MultiJson.decode(response.body) end
Private Instance Methods
# File lib/charging/invoice.rb, line 187 def invalid_charge_account? charge_account.nil? end
# File lib/charging/invoice.rb, line 183 def invalid_domain? domain.nil? end
# File lib/charging/invoice.rb, line 165 def reload_attributes!(uuid) new_invoice = self.class.find_by_uuid(domain, uuid) (COMMON_ATTRIBUTES + READ_ONLY_ATTRIBUTES).each do |attribute| instance_variable_set "@#{attribute}", new_invoice.send(attribute) end self end