Invoicing on Kapture with QuickBooks

Among other things, Kapture CRM facilitates accounting and invoicing for businesses via third-party APIs. Embedding an accounting system such as QuickBooks on Kapture keeps your books up to date. The primary application for any business is to easily push invoices from Kapture’s dashboard to the vendor app using a simple integration function.

The process is straightforward. To get started, an app is to be created on Intuit developer with Accounting as the domain. Next, we are directed to the development section where we can get a unique Client ID and Client Secret, which look something like:

Client ID: ABj4jpQUDvaNKx421f9jvRVBe46uUASRrj5Xq2x3LoKpXOPlkl

Client Secret: IVfKBr1hf4YHH25H2vIix5YjneTVHzqmVkODpGNaSrz6T

  • Authentication

Once this is done, webhooks are selected based on accounting needs. You can choose the functionalities you want; for example, Account, Customer, Invoice, Item, Payment, Purchase, Vendor, etc.

An OpenID connect token is to be obtained for authentication of users. It can be done so by granting permissions for the application under User Consent. The resulting authorization code can be used to obtain the access token and ID token.

  • Authorization

I. Creating an authorization request based on the parameters on the client library for OAuth 2.0 is done using:

//Prepare the config
OAuth2Config oauth2Config = new OAuth2Config.OAuth2ConfigBuilder("clientId", "clientSecret")
        .callDiscoveryAPI(Environment.SANDBOX) .buildConfig();

//Generate the CSRF token
String csrf = oauth2Config.generateCSRFToken();

//Prepare scopes
List<Scope> scopes = new ArrayList<Scope>();
scopes.add(Scope.OpenIdAll);

//Get the authorization URL
String url = oauth2Config.prepareUrl(scopes, redirectUri, csrf); //redirectUri - pass the callback url

II. After redirecting to Intuit’s OAuth 2.0 server, the authorization process is kickstarted. This step is required when the application first needs to access user’s data. In the case of incremental authorization, this step also occurs when your application first needs to access additional resources that it does not yet have permission to access.

Use standard url redirect-
resp.sendRedirect(url);

Then, user consent is prompted by Intuit to grant the application the requested access. A consent window pop-up lets you connect Kapture with Quickbooks.

III. The OAuth 2.0 server responds to the application’s access request by using the redirect_uri specified in the request. If the user approves the access request, then the response contains an authorization code as shown below:


https://www.mydemoapp.com/oauth-redirect?state=security_token%3D138r5719ru3e1%26url

%3Dhttps://www.mydemoapp.com/oauth-redirect&code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&realmId=1231434565226279

The authorization code is returned to the server whose state has to be confirmed.

IV. Once this is done, we arrive at exchanging the authorization code to obtain the ID token and access token.

//Prepare OAuth2PlatformClient
OAuth2PlatformClient client  = new OAuth2PlatformClient(oauth2Config);

//Get the bearer token (OAuth2 tokens)
BearerTokenResponse bearerTokenResponse = client.retrieveBearerTokens(authCode, redirectUri);

//retrieve the token using the variables below
bearerTokenResponse.getAccessToken()
bearerTokenResponse.getIdToken()

V. What is left if to validate the token ID and we are set.

boolean valid = client.validateIDToken(bearerTokenResponse.getIdToken());

Since Kapture focuses primarily on invoicing, a set of rules are to be defined for an invoice constitutes.

To start, we define basic (among others) URL parameters such as:

  • Item
  • Customer Reference
  • Doc Number
  • Address

The invoice object can be coded as:

{
  "Invoice": {
    "TxnDate": "2019-09-19", 
    "domain": "QBO", 
    "PrintStatus": "NeedToPrint", 
    "SalesTermRef": {
      "value": "3"
    }, 
    "TotalAmt": 362.07, 
    "Line": [
      {
        "Description": "Rock Fountain", 
        "DetailType": "SalesItemLineDetail", 
        "SalesItemLineDetail": {
          "TaxCodeRef": {
            "value": "TAX"
          }, 
          "Qty": 1, 
          "UnitPrice": 275, 
          "ItemRef": {
            "name": "Rock Fountain", 
            "value": "5"
          }
        }, 
        "LineNum": 1, 
        "Amount": 275.0, 
        "Id": "1"
      }, 
      "DetailType": "SubTotalLineDetail", 
        "Amount": 335.25, 
        "SubTotalLineDetail": {}
      }
    ], 
    "DueDate": "2019-10-19", 
    "ApplyTaxAfterDiscount": false, 
    "DocNumber": "1037", 
    "sparse": false, 
    "CustomerMemo": {
      "value": "Thank you for your business and have a great day!"
    }, 
    "Deposit": 0, 
    "Balance": 362.07, 
    "CustomerRef": {
      "name": "Sonnenschein Family Store", 
      "value": "24"
    }, 
    "TxnTaxDetail": {
      "TxnTaxCodeRef": {
        "value": "2"
      }, 
      "TotalTax": 26.82, 
      "TaxLine": [
        {
          "DetailType": "TaxLineDetail", 
          "Amount": 26.82, 
          "TaxLineDetail": {
            "NetAmountTaxable": 335.25, 
            "TaxPercent": 8, 
            "TaxRateRef": {
              "value": "3"
            }, 
            "PercentBased": true
          }
        }
      ]
    }, 
    "SyncToken": "0", 
    "LinkedTxn": [
      {
        "TxnId": "100", 
        "TxnType": "Estimate"
      }
    ], 
    "BillEmail": {
      "Address": "Familiystore@intuit.com"
    }, 
    "ShipAddr": {
      "City": "Middlefield", 
      "Line1": "5647 Cypress Hill Ave.", 
      "PostalCode": "94303", 
      "Lat": "37.4238562", 
      "Long": "-122.1141681", 
      "CountrySubDivisionCode": "CA", 
      "Id": "25"
    }, 
    "MetaData": {
      "CreateTime": "2019-09-19T13:16:17-07:00", 
      "LastUpdatedTime": "2019-09-19T13:16:17-07:00"
    }, 
    "CustomField": [
      {
        "DefinitionId": "1", 
        "StringValue": "102", 
        "Type": "StringType", 
        "Name": "Crew #"
      }
    ], 
    "Id": "130"
  }, 
  "time": "2020-07-24T10:48:27.082-07:00"
}

Additional codes can be defined for creating, deleting, reading and sending invoices.

Leave a Reply

Your email address will not be published. Required fields are marked *