Go back to the main page

Get started integrating Rails 4 and QuickBooks Online with the Quickeebooks Gem: Part 3 : Disconnecting with QuickBooks


Part 3: Disconnect the Rails app from QB0.

  1. Let's start by updating the view, app/views/layouts/application.html.erb, to show a disconnect link
  2.   <% unless session[:token] %>
      <% else %>
          <%= link_to 'Disconnect from QuickBooks', '', data: { confirm: 'Are you sure you want to disconnect?' 
      <% end %>
  3. Reload
  4. Simple disconnect link to get the ball rolling. We'll come back later and add the href target.
    DISCUSSION: We need a disconnect action but the vendors_controller is getting crowded, therefore, let's create the quickbooks_controller. This is also a perfect place for the authenticate and oauth_callback actions so we are going to move them before creating the disconnect action.
  5. Make a new quickbooks_controller and move the authenticate and oauth_callback actions from the vendors_controller to here.
    • The new controller at app/controllers/quickbooks_controller.rb should look like the code below. Notice the highlighted sections that also need to be modified.
    • class QuickbooksController < ApplicationController
        def authenticate
          callback = oauth_callback_quickbooks_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
        def oauth_callback
          at = session[:qb_request_token].get_access_token(:oauth_verifier => params[:oauth_verifier])
          session[:token] = at.token
          session[:secret] = at.secret
          session[:realm_id] = params['realmId']
          flash.notice = "Your QuickBooks account has been successfully linked."
          @msg = 'Redirecting. Please wait.'
          @url = vendors_path
          render 'vendors/close_and_redirect', layout: false  end
    • Modify config/routes.rb:
    • Billtastic::Application.routes.draw do
        resources :vendors
        resources :quickbooks do
          collection do
            get :authenticate
            get :oauth_callback
    • Then modify the following line in app/views/layouts/application.html.erb to reflect the new authenticate route.
    •   <script>
            intuit.ipp.anywhere.setup({menuProxy: '/path/to/blue-dot', grantUrl: '<%= authenticate_quickbooks_url %>'});
    • Finally, make sure you issue a touch tmp/restart.txt to restart the app as the routes have changed.
    DISCUSSION: We are still not done with the refactoring. The disconnect action uses a different Quickeebooks service then from the one used in the vendors controller so let's make the set_qb_service method more flexibility.
  6. Allow set_qb_service to create additional services.
    • Remove the set_qb_service from vendors_controller and move it to application_controller and modify it as such:
    • class ApplicationController < ActionController::Base
        # Prevent CSRF attacks by raising an exception.
        # For APIs, you may want to use :null_session instead.
        protect_from_forgery with: :exception
          def set_qb_service(type = 'Vendor')
            oauth_client = OAuth::AccessToken.new($qb_oauth_consumer, session[:token], session[:secret])
            @service = "Quickeebooks::Online::Service::#{type}".constantize.new
            @service.access_token = oauth_client
            @service.realm_id = session[:realm_id]
    • Next, modify the vendors_controller instance vars of @vendor_service to be just @service
  7. Reload billtastic.dev/vendors to test that the refactorings have taken effect with no errors.
  8. Add the disconnect route.
  9. Billtastic::Application.routes.draw do
      resources :vendors
      resources :quickbooks do
        collection do
          get :authenticate
          get :oauth_callback
          get :disconnect
  10. Add the disconnect action to the quickbooks_controller
      def disconnect
        result = @service.disconnect
        if result.error_code == '270'
          msg = 'Disconnect failed as OAuth token is invalid. Try connecting again.'
          msg = 'Successfully disconnected from QuickBooks'
        session[:token] = nil
        session[:secret] = nil
        session[:realm_id] = nil
        redirect_to vendors_path, notice: msg
    DISCUSSION: The disconnect action uses the AccessToken module/service from Quickeebooks. It provides a disconnect instance method that makes an API call requesting the disconnect. Intuit states that an error_code of 0 indicates a successful request but I have found that an error_code of '22' worked fine and the connection was properly disconnected. The session's are cleared in all cases because even on a disconnect failure this indicates that the current credentials are invalid and creating a new connection may be the solution.
    IMPORTANT: There are many points were the Intuit v2 API doesn't work exactly as stated in the docs as in the above example were I received an error code of '22' but the disconnect worked fine. Don't beat yourself up because you are not obtaining responses precisely as described in the documentation. Just shrug your shoulders and move on.
  12. Before we test disconnecting, let's log into the Intuit Partner Platform like we did in Part 1. From there navigate to My Apps->Manage My Apps and then to the Billtastic app.
    • Notice that you should have at least one live API connection
    • We will use this view to confirm that we successfully disconnected.
    • While we are here let's also change the disconnect url to billtastic.dev/quickbooks/disconnect.
    • Before
      NOTE: This url change is not needed for this tutorial and is only used for when a disconnect action is initiated from the Intuit App Center. We want to make the change now while we are here and the new disconnect route is fresh in our minds.
    • Let's go back to the Billtastic.app at billtastic.dev/vendors and click on the 'Disconnect from QuickBooks' link.
    • Click thru the confirmation dialog.
    • Yeah! A successfully disconnect.
    • The 'Connect to QuickBooks' button also correctly displays now that the session[:token] is nil.
    • Let's make sure the disconnect occurred by checking over at the Intuit Partner Platform.
    • Ok, that confirms it. There was 1 connection last time we checked (see Fig. 2).

Implementing Single Sign On

Stay tuned to Part 4 in where I will hook up the Billtastic app with the ability to use your Intuit QBO credentials to sign on. This is referred to as "Single Sign On" by Intuit and is needed if you desire to have your application listed on their App Center.