Go back to the main page

Integrating Rails and QuickBooks Online via the version 3 API: Part 1

 

 

Minimul says —

IMPORTANT: Intuit has introduced and requires developing against sandboxes for U.S. QBO.

See my new article, Connect Rails and QuickBooks Online via Sandbox, which would come in around step 4 or so for this article.


Tutorial and screencast use Rails 4, Ruby 1.9.3, and Pow. Be sure to watch the screencast above as I unveil more details and tidbits than can be found in the article itself.

  • Code from screencast is available on Github
    1. If you haven't already, sign up at Intuit Partner Platform
    2. If you already have a QBO account you can use your username and password to join as a developer. If you are a developer desiring a test account then email Intuit @ ippdevelopersupport@intuit.com and request a developer subscription to QBO.
    3. Create a new app.
    4. Select 'Quickbooks API'.
      Fill in the form like in this figure and "Save". Note: the minimulcasts.io is provided because a real domain is needed to get past the validations.
      Next, your app tokens and keys are generated.
    5. Create a new Rails app and hook it up with Pow.
    6. $ cd ~/www/labs # example
      $ rails new minimulcasts
      $ ln -s ~/www/labs/minimulcasts ~/.pow
      $ curl -L minimulcasts.dev | grep "Welcome" # Test POW
      $ cd minimulcasts
      
    7. Add the quickbooks-ruby and oauth-plugin gems.
    8. # Gemfile -> add these two gems:
      gem 'quickbooks-ruby'
      gem 'oauth-plugin'
      $ bundle install
      
    9. Beget Vendor scaffolding
    10. $ rails g scaffold Vendor name
      $ bundle exec rake db:migrate
      
    11. Create config/initializers/quickeebooks.rb.
    12. 
      QB_KEY = "<copy from developer.intuit.com>"
      QB_SECRET = "<copy from developer.intuit.com>"
      
      $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"
      })
      
    13. Modify config/routes.rb.
    14. Minimulcasts::Application.routes.draw do
        resources :vendors do
          collection do
            get :authenticate
            get :oauth_callback
          end
        end
      
        root to: 'vendors#index'
      end
      
    15. Make sure application level changes take by reloading with a touch tmp/restart.txt
    16. Next, hook actions up to those new routes app/controllers/vendors_controller.rb.
    17. Minimul says —

      Pay attention to the Rails 4.1 and greater notes in the next 2 methods.

        # app/controllers/vendors_controller.rb
        # .. code omitted
      
        def authenticate
          callback = oauth_callback_vendors_url
          token = $qb_oauth_consumer.get_request_token(:oauth_callback => callback)
          session[:qb_request_token] = token
          # If Rails >= 4.1 you need to do this => session[:qb_request_token] = Marshal.dump(token)
          redirect_to("https://appcenter.intuit.com/Connect/Begin?oauth_token=#{token.token}") and return
        end
      
        def oauth_callback
          at = session[:qb_request_token].get_access_token(:oauth_verifier => params[:oauth_verifier])
          # If Rails >= 4.1 you need to do this =>  at = Marshal.load(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']
          redirect_to root_url, notice: "Your QuickBooks account has been successfully linked."
        end
      
        private
      
        # .. code omitted
      
      You will persist the token, secret & realmId to the database. Those 3 items are needed to communicate with QBO. For brevity, sessions are used for persistence.
    18. Add Intuit Connect button to application layout at app/view/layouts/application.html.erb.
    19.   .. omitted
        <body>
          <!-- REPLACE THE BODY of the default application.html.erb with this -->
      
          <% unless session[:token] %>
            <ipp:connectToIntuit></ipp:connectToIntuit>
          <% end %>
      
          <% if notice %>
            <div style="padding: 10px;background: gainsboro;font-weight: 900;width: 50%;"><%= notice %></div>
          <% end %>
      
          <%= yield %>
      
          <script type="text/javascript" src="https://appcenter.intuit.com/Content/IA/intuit.ipp.anywhere.js"></script>
      
          <script>
              intuit.ipp.anywhere.setup({menuProxy: '/path/to/blue-dot', grantUrl: '<%= authenticate_vendors_url %>'});
          </script>
        </body>
      
      
    20. In your browser go to: http://minimulcasts.dev
    21. Connect with QuickBooks button is generated from Intuit.
    22. Click on the 'Connect with QuickBooks' button
    23. Put in your QuickBooks Online username and password.
      Then click the 'Authorize' button.
    24. Once authenticated Intuit will return a GET request to the /vendors/oauth_callback.
    25. We are now authorized to communicate with QuickBooks Online.
    26. Finally, double check that the QBO connection was successful by logging into QuickBooks Online then go to the 'Company' tab and then to the 'Activity Log' submenu.
    27. That confirms it.

    Next, creating a vendor in Rails and QuickBooks Online

    In Part 2 I demonstrate actually creating an entity over at QBO.