Creating invoices with the API
There are three REST calls in this example: first you will get information about your account in invoicefu, then you will use the API to populate a JSON object with the default values for your account (general account information, currency, invoice number, taxes...). You will finally set values for your client and for the services/goods being invoiced and issue a POST to create the invoice.
require 'rubygems'
require 'restclient'
require 'json'
#read your invoicefu credentials from heroku environment
auth_params = {:app_platform=>ENV['INVOICEFU_APP_PLATFORM'], :app_id=> ENV['INVOICEFU_APP_ID'], :api_key=>ENV['INVOICEFU_API_KEY']}
#get information about your invoicefu account. It will provide the endpoints for viewing/managing clients and invoices
remote_account = RestClient::Resource.new( "https://invoicefu.com/api/v1/accounts.json" ).get :params => auth_params, :content_type => :json
account = JSON.parse( remote_account.to_s )['account']
#get an empty JSON template with the default values for a new invoice
invoice_template_url = account['links'].detect{|l| l['rel']=='new_invoice'}['uri']
remote_template = RestClient::Resource.new( invoice_template_url ).get :params => auth_params, :content_type => :json
invoice = JSON.parse( remote_template.to_s )['invoice']
#fill in the data for your invoice
invoice['cl_name'] = 'aspgems'
invoice['cl_reference_code'] = 'CL01' #for your convenience, you can provide a unique code that identifies this client in your system
invoice['cl_address'] = 'sextante 9'
invoice['cl_city'] = 'madrid'
invoice['cl_postal_code'] = '28023'
invoice['reference_code'] = '0199902' #for your convenience, you can provide a unique code that identifies this in your system, for example the order number
#now we add the lines of the invoice providing amount, description and price for each line
invoice['invoice_lines'] << { :amount=>1, :price=>99.99, :description=>'invoicefu yearly premium subscription' }
#and we are ready for sending the JSON to invoicefu so the invoice will be created
invoices_url = account['links'].detect{|l| l['rel']=='invoices'}['uri']
remote_invoice = RestClient::Resource.new( invoices_url ).post invoice.merge(auth_params).to_json, :content_type => :json