Integration of Quickbook in Rails App | Part 1 | Inkoop Blog

Quickbook 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 quickbook ruby gem to manage customers on Quickbook.

Posted by Ameena on 20 Mar 2017

Quickbook is a software which is mainly used to organize the expenses of a small scale or medium scaled business.

  • Install gems omniauth and omniauth-quickbooks to authenticate with quickbooks.
  • By default, the gem quickbooks-ruby will be in production mode but you can run it in other mode (development or testing).
  • Create a rails initializer and set the OAUTHCONSUMERKEY and OAUTHCONSUMERSECRET properly and set the callback in the routes in order to inform rails about the request.
  • You can set the OAuth access credentials with four fields accesstoken, accesssecret, companyid, tokenexpires_at to avoid connecting to Quickbook often.

After, all the important and basic setup is done now its time to learn how to generate invoices for the user. Without an user generating invoices makes no sense. Hence, the first thing is to know the user. There are two possible cases for this:

  1. If the user does not exist in quickbook, create the user and generate the invoice.
  2. If the user already exists in quickbook, update the user and generate the invoice.

Case 1:

If the user does not exist then instantiate a Service object on the Customer and create it.

...
def build_user_information
  service = Quickbooks::Service::Customer.new
  service.access_token = access_token
  service.company_id = company_id
  quickbook_customer = Quickbooks::Model::Customer.new
  quickbook_customer = set_user_information quickbook_customer
  service.create(quickbook_customer)
end

def set_user_information quickbook_customer
  # Name
  quickbook_customer.given_name = quickbook_customer.display_name =
  quickbook_customer.print_on_check_name =
  quickbook_customer.fully_qualified_name = "First Last"
  # Email
  quickbook_email = Quickbooks::Model::EmailAddress.new
  quickbook_email.address = "firstlast@abc.com"
  quickbook_customer.primary_email_address = quickbook_email
  # Phone Number
  quickbook_phone_number = Quickbooks::Model::TelephoneNumber.new
  quickbook_phone_number.free_form_number = 9876543210
  quickbook_customer.primary_phone = quickbook_phone_number
  # Address
  quickbook_address = Quickbooks::Model::PhysicalAddress.new
  quickbook_address.line1 = "line1"
  quickbook_address.line2 = "line2"
  quickbook_address.city = "city"
  quickbook_address.country = "country"
  quickbook_customer.billing_address = quickbook_address
  quickbook_customer
end

The above piece of code has two methods,

  • The first method is to create the user in the quickbook.
  • The second method is to set the data using an object of Quickbook's customer.

service.create(quickbook_customer) will create a new quickbook customer with an id as its primary key, say 21 in this case.

Case 2:

If the user already exists in quickbook then get the customer and update it.

...
def update_user_information
  service = Quickbooks::Service::Customer.new
  service.access_token = access_token
  service.company_id = company_id
  quickbook_customer = service.fetch_by_id(21)
  quickbook_customer = set_user_information quickbook_customer
  service.update(quickbook_customer)
end
  • updateuserinformation is to update the data of the existing customer.

There are couples of things that you can play with the service object

users = service.all

users will be an array with all the objects of the service(customer in our case)

users = service.query()

It will be a SQL like structure to retrieve objects.

In the next post there will be a procedure to generate the invoices for the created customer.

Click here to check part 2.

Ameena


Have a Project in mind?