Search code examples
ruby-on-railsquickbooks-online

How do I add a customer to Quickbooks automatically when they sign into my site?


I have already implemented minimul's guide to getting started with Quickbooks Online. My administrator can authenticate with Quickbooks and perform any number of tasks.

However, I want to generate an automated task that creates a customer entry in my company's Quickbooks' application as soon as they sign up. When they choose an item to purchase I want an invoice created in Quickbooks. And so on.

Obviously I don't want my users to have to authenticate with Quickbooks in order to do this. The users of the site should have no knowledge of Quickbooks; only the proprietor should access it directly. Is there a way to do this?


Solution

  • I am assuming that when you say "My administrator can authenticate with QuickBooks [Online] (QBO) and perform any number of tasks" that this connection is to your company's QBO account.

    Therefore, when there is a new sign up you create the customer on the QBO side by using your company's OAuth tokens, which should be persisted in some fashion (DB or .yaml file). In the example below the OAuth information is persisted to the Account model as the following attributes: qb_token, qb_secret, and qb_realm_id. The $qb_oauth_consumer global comes from config/initializer/quickeebooks.rb.

    account = Account.find(1) # your company's account
    oauth_client = OAuth::AccessToken.new($qb_oauth_consumer, account.qb_token, account.qb_secret)
    service = Quickeebooks::Online::Service::Customer.new
    service.access_token = oauth_client
    service.realm_id = account.qb_realm_id
    # Map the new user to QuickBooks Customer
    qb_customer = Quickeebooks::Online::Model::Customer.new
    qb_customer.name = params[:new_signup_name]
    qb_customer.email_address = params[:new_signup_email]
    # etc.
    service.create(qb_customer)
    

    This goes for invoices, purchases, and whatever else. You use your company's OAuth connection to QBO.