Class: Charging::Invoice

Inherits:
Base
  • Object
show all
Defined in:
lib/charging/invoice.rb

Constant Summary collapse

ATTRIBUTES =
[
  :kind, :amount, :document_number, :drawee, :due_date, :portfolio_code,
  :charging_features, :supplier_name, :discount, :interest, :rebate,
  :ticket, :protest_code, :protest_days, :instructions, :demonstrative,
  :our_number
]
READ_ONLY_ATTRIBUTES =
[ :document_date, :paid ]

Constants inherited from Base

Base::COMMON_ATTRIBUTES, Base::DEFAULT_LIMIT, Base::DEFAULT_PAGE

Instance Attribute Summary

Attributes inherited from Base

#errors, #last_response

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#attributes, #deleted?, #normalize_etag!, #persisted?, #unpersisted?, validate_attributes!

Constructor Details

#initialize(attributes, domain, charge_account, response = nil) ⇒ Invoice

Returns a new instance of Invoice.



17
18
19
20
21
# File 'lib/charging/invoice.rb', line 17

def initialize(attributes, domain, , response = nil)
  super(attributes, response)
  @domain = domain
  @charge_account = 
end

Class Method Details

.find_by_uuid(domain, uuid) ⇒ Object

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



120
121
122
123
124
125
126
127
128
# 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

.kinds(domain, page = DEFAULT_PAGE, limit = DEFAULT_LIMIT) ⇒ Object

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



138
139
140
141
142
143
144
145
146
# 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

.load_persisted_invoice(attributes, response, domain, charge_account = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/charging/invoice.rb', line 148

def self.load_persisted_invoice(attributes, response, domain,  = nil)
   = attributes.delete("charge_account").to_s
  
  if .nil? && .start_with?('http')
    begin
       = ChargeAccount.find_by_uri(domain, )
    rescue Http::LastResponseError
    end
  end
  
  validate_attributes!(attributes)
  
  Invoice.new(attributes, domain, , response)
end

Instance Method Details

#billet_urlObject

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



99
100
101
102
103
104
105
106
107
108
109
# 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

#create!Object

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



28
29
30
31
32
33
34
35
36
37
# 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, , attributes)
  end
  
  reload_attributes!(Helpers.extract_uuid(last_response.headers[:location]) || uuid)
end

#destroy!Object

Deletes the invoice at API

API method: DELETE /invoices/:uuid/

API documentation: charging.financeconnect.com.br/static/docs/charges.html#delete-invoices-uuid



44
45
46
47
48
# File 'lib/charging/invoice.rb', line 44

def destroy!
  super do
    Http.delete("/invoices/#{uuid}/", domain.token, etag)
  end
end

#pay!(payment_data = {}) ⇒ Object

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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

#paymentsObject

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



84
85
86
87
88
89
90
91
92
# 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