The example.rb
file has examples for performing various tasks on the API, like creating customers, invoicing, and recording payments.
The invoiced
gem needs to be installed first with:
gem install invoiced
require 'invoiced' | |
invoiced = Invoiced::Client.new('{YOUR_API_KEY}') | |
## | |
## Customers | |
## | |
# Create a Customer | |
p "Creating customer:" | |
customer = invoiced.Customer.create({ | |
:name => 'Pied Piper' | |
}) | |
p customer | |
# Update the newly created Customer | |
p "Updating customer #{customer.id}:" | |
customer.payment_terms = 'NET 30' | |
p customer.save | |
# And then delete the Customer | |
p "Deleting customer #{customer.id}:" | |
p customer.delete | |
# List the 3 newest Customers | |
p "Listing customers:" | |
customers, metadata = invoiced.Customer.list({:per_page => 3, :sort => "created_at DESC"}) | |
p metadata, customers | |
# Retrieve a Customer by ID | |
id = customers.first.id | |
customer = invoiced.Customer.retrieve(id) | |
p "Retrieved customer #{id}:", customer | |
## | |
## Invoices | |
## | |
# Create an Invoice | |
p "Creating invoice:" | |
invoice = invoiced.Invoice.create({ | |
:customer => customer.id, | |
:items => [ | |
{ | |
:name => "Line Item Title", | |
:description => "More detailed description", | |
:quantity => 5, | |
:unit_cost => 100 | |
} | |
] | |
}) | |
p invoice | |
# Mark the newly created Invoice as sent | |
p "Updating invoice:" | |
invoice.sent = true | |
p invoice.save | |
# And then delete the Invoice | |
p "Deleting invoice:" | |
p invoice.delete | |
# List the 3 most recent unpaid Invoices | |
p "Listing invoices:" | |
invoices, metadata = invoiced.Invoice.list({ | |
:per_page => 3, | |
:sort => "created_at desc", | |
:filter => {:paid => false, :closed => false}}) | |
p metadata, invoices | |
# Retrieve an Invoice by ID | |
id = invoices.first.id | |
invoice = invoiced.Invoice.retrieve(id) | |
p "Retrieved invoice #{id}:", invoice | |
## | |
## Transactions | |
## | |
# Mark one of the outstanding invoices from earlier as paid | |
# by creating a transaction matching the balance on the invoice | |
p "Creating transaction:" | |
transaction = invoiced.Transaction.create( | |
:invoice => invoice.id, | |
:method => "check", | |
:amount => invoice.balance | |
) | |
p transaction | |
# Update the newly created Transaction | |
p "Updating transaction:" | |
transaction.notes = "Test notes" | |
p transaction.save | |
# Delete the Transaction (also unmarks the invoice as paid) | |
p "Deleting transaction:" | |
p transaction.delete | |
# List the 3 most recent Transactions | |
p "Listing transactions:" | |
transactions, metadata = invoiced.Transaction.list({:per_page => 3, :sort => "created_at DESC"}) | |
p metadata, transactions | |
# Retrieve a Transaction by ID | |
id = transactions.first.id | |
transaction = invoiced.Transaction.retrieve(id) | |
p "Retrieved transaction #{id}:", transaction |