How to Integrate Quickbooks in Rails Application
If you are looking for some help regarding the integration of Quickbooks in the Rails Application, then you have come to the right place. We have a step-by-step guide below to help you down below:
Step 1:- Join the Intuit Partner Platform:
You need to have a Quickbooks online account (http://quickbooks.intuit.com/online), and you can use that same credentials to login into Quickbooks developers account (https://developer.intuit.com).
Step 2:- Create a new app in Quickbooks.
Step 3:- Add the gem “quickbooks-ruby” to your gemfile.
Step 4:- Create a quickbooks.rb initializer file under your config/initializers folder
QB_KEY = “your apps Intuit App Key”
QB_SECRET = “your apps Intuit Secret Key”
$qb_oauth_consumer = OAuth::Consumer.new(QB_KEY, QB_SECRET, {
:site => “https://oauth.intuit.com”,
:request_token_path => “/oauth/v1/get_request_token”,
:authorize_url => “https://appcenter.intuit.com/Connect/Begin”,
:access_token_path => “/oauth/v1/get_access_token”
})
Step 5:- To start the authentication flow with Intuit you include the Intuit Javascript and on a page of your choosing you present the “Connect to Quickbooks” button by including this XHTML:
- a) you need to include this javascript in your application.rb.
<script type=”text/javascript” src=”https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere.js”></script>
- b) ‘grantUrl’ is a URL in your application which kicks off the flow
<script>
intuit.ipp.anywhere.setup({menuProxy: ‘/path/to/blue-dot’, grantUrl: ‘/path/to/your-flow-start’});
</script>
- c) To display the button on Quickbooks which we kick start the flow, add this button on the page you want to connect to Quickbooks.
<ipp:connectToIntuit></ipp:connectToIntuit>
Step 6:- Create a controller action on the grantUrl above should look like this:
def authenticate
callback = quickbooks_oauth_callback_url
token = $qb_oauth_consumer.get_request_token(:oauth_callback => callback)
session[:qb_request_token] = token
redirect_to(“https://appcenter.intuit.com/Connect/Begin?oauth_token=#{token.token}”) and return
end
Step 7:- Where quickbooks_oauth_callback_url is the absolute URL of your application that Intuit should send the user when authentication succeeds. That action should look like:
def oauth_callback
at = session[:qb_request_token].get_access_token(:oauth_verifier => params[:oauth_verifier])
token = at.token
secret = at.secret
realm_id = params[‘realmId’]
# store the token, secret & RealmID somewhere for this user, you will need all 3 to work with Quickbooks-Ruby
end
Now we are connected to Quickbooks, it’s time to make entries to Quickbooks.
Step 8:- Once you have your users OAuth Token & Secret you can initialize your OAuth Consumer and create an OAuth Client using the $qb_oauth_consumer you created earlier in your Rails initializer:
qb_access_token = OAuth::AccessToken.new($qb_oauth_consumer, access_token, access_secret)
Step 9 :- Querying the quickbooks:
qb_customer_service = Quickbooks::Service::Customer.new
qb_customer_service.company_id = realm_id
qb_customer_service.access_token = token
util = Quickbooks::Util::QueryBuilder.new
begin
clause1 = util.clause(“DisplayName”, “LIKE”, “abc”)
search = qb_customer_service.query(“SELECT * FROM Customer WHERE #{clause1}”)
rescue
return
end
Step 10:- Create a new customer in quickbooks:
qb_customer = Quickbooks::Model::Customer.new
qb_customer.display_name = “abc”
qb_customer.email_address = “abc@gmail.com”
qb_customer.primary_phone = “1234567890”
created_customer = qb_customer_service.create(qb_customer)
created_customer_id = created_customer.id
created_customer_name = created_customer.display_name
Step 11:- Create a new payment in quickbooks:
qb_payment = Quickbooks::Model::Payment.new
qb_payment.txn_date = Date.today
qb_payment.private_note = “abc”
qb_payment.total = 100
qb_payment.customer_id = created_customer_id
qb_payment.customer_ref = Quickbooks::Model::BaseReference.new
qb_payment.customer_ref.name = created_customer_name
qb_payment.customer_ref.value = created_customer_id
qb_payment.currency_ref = Quickbooks::Model::BaseReference.new
qb_payment.currency_ref.value = “usd”
qb_payment_service = Quickbooks::Service::Payment.new
qb_payment_service.company_id = realm_id
qb_payment_service.access_token = access_token
qb_payment.deposit_to_account_ref = Quickbooks::Model::BaseReference.new
qb_payment.deposit_to_account_ref.name = “ABC”
qb_payment.deposit_to_account_ref.value = 98
end
Step 12:- Creating a invoice for any particular payment:
qb_invoice = Quickbooks::Model::Invoice.new
qb_invoice.txn_date = Date.today
qb_invoice.balance = qb_payment.total
qb_invoice.private_note = “abc”
qb_invoice.billing_email_address = “abc@gmail.com”
qb_invoice.customer_id = created_customer_id
qb_invoice.customer_ref = Quickbooks::Model::BaseReference.new
qb_invoice.customer_ref.name = created_customer_name
qb_invoice.customer_ref.value = created_customer_id
qb_invoice.billing_address = Quickbooks::Model::PhysicalAddress.new
qb_invoice.billing_address.line1 = “abc”
qb_invoice.billing_address.city = “abc”
qb_invoice.billing_address.country = “abc”
qb_invoice.billing_address.postal_code = 12345
line_item = Quickbooks::Model::InvoiceLineItem.new
line_item.amount = qb_payment.total
qb_payment.private_note = “abc”
line_item.sales_item! do |detail|
detail.unit_price = qb_payment.total
detail.quantity = 1
end
qb_invoice.line_items << line_item
qb_invoice_service = Quickbooks::Service::Invoice.new
qb_invoice_service.company_id = realm_id
qb_invoice_service.access_token = access_token
qb_invoice_service = qb_invoice_service.create(qb_invoice)
created_invoice_id = qb_invoice_service.id
line_item = Quickbooks::Model::Line.new
line_item.line_num = 1
line_item.amount = qb_payment.total
line_item.invoice_id = created_invoice_id
line_item.sales_item! do |detail|
detail.unit_price = qb_payment.total
detail.quantity = 1
end
qb_payment.line_items = []
qb_payment.line_items << line_item
created_payment = qb_payment_service.create(qb_payment)
created_payment_id = created_payment.id
Closure:
We hope that this blog helped you in some manner. If you have any suggestions or perspectives to share, please write them in the comments section down below. But if you have any further questions then feel free to connect to our experts.
References:
- https://github.com/ruckus/quickbooks-ruby
- http://minimul.com/get-started-integrating-rails-4-and-quickbooks-online-with-the-quickeebooks-gem-part-1.html
This blog was originally published by OnGraph Technologies, dated Nov. 11, 2014