Integration of Quickbooks in Rails App | Part 2 | Inkoop Blog

Quickbooks is a software which is mainly used to organize the expenses of a small scale or medium scaled business. We talk about how to use Quickbooks ruby gem to manage invoices on Quickbooks.

Posted by Ameena on 28 Mar 2017
Integration of Quickbooks in Rails App | Part 2 | Inkoop Blog

Check the previous blog for creating the Quickbooks customer.

How to generate invoices

Like Quickbooks customer creation, invoice generation also has two cases:

  1. If there is no invoice for the Quickbooks customer, generate the invoice.

  2. If there is already an invoice on which you need to make changes, update the invoice.

Case 1:

Consider customer id as 21, Refer the previous blog to generate customer_id.

def build_invoice
  service =
  service.access_token = access_token
  service.company_id = company_id
  quickbooks_invoice =
  quickbooks_invoice = set_invoice_data quickbooks_invoice

def set_invoice_data quickbooks_invoice
  quickbooks_invoice.customer_id = 21
  quickbooks_invoice.txn_date = Date.civil(2017, 3, 29)
  quickbooks_invoice.doc_number = 11
  line_item =
  line_item.amount = 300
  line_item.description = "Item"
  line_item.sales_item! do |detail|
    detail.unit_price = 50
    detail.quantity = 6
    detail.item_id = 1
  quickbooks_invoice.line_items << line_item
  • In the method build_invoice, create an invoice by using service object on Invoice. Consider, the id of the invoice that is created as 130.

  • For setting the invoice data, first set the customer_id on the Quickbooks's invoice object. Each invoice need not necessarily have only one item hence an invoice line item object is created. Make sure the amount specified will be quantity * unit_price.

Case 2:

Consider you already have an invoice say 130 and you want to change the invoice by either adding items or deleting the items or completely change the items present in the invoice. To do so first get the invoice you want to change and then make changes and call update to see the changes in Quickbooks invoice.

def update_invoice
  service =
  service.access_token = access_token
  service.company_id = company_id
  quickbooks_invoice = service.fetch_by_id(130)
  quickbooks_invoice = set_invoice_data quickbooks_invoice

One word of warning before you get started with Quickbooks is to remember about refreshing the access token. Because the access credentials will be valid only for 180 days once created. But, it can be renewed 30 days prior to its expiry. Keeping track of renewal and managing the renewal process should be done manually by the user.

Hence, instantiate service object on AccessToken and call renewal on the service object which will be a new token.

def refresh_token
  service =
  service.access_token = access_token
  service.company_id = company_id
  new_token = service.renew
  case new_token.error_code
  when "0"
    # Update the values on success
      access_token: new_token.token,
      access_secret: new_token.secret,
      token_expires_at: 180.days.from_now.utc,
  when "270"
    # Discard any saved credentials or call destroy on the object when access token is expired.
      access_token: nil,
      access_secret: nil,
      token_expires_at: nil,
  when "212"
    # Tried to renew it more than 30 days before expiration
    puts "Renewal ignored, tried too soon"
    puts "Renewal failed, code: #{new_token.error_code} message: #{new_token.error_message}"

Keep Coding !!!


Related Services.

Hire ReactJS Developers
Hire Gatsby Developers
Hire NextJS Developers

We support the Open Source community.

Have a Project in mind?