Avalara Developer Network Developer avaTax

Avalara AvaTax with Stripe.js

The new AvaTax.js library makes it easy to calculate taxes with browser-based shopping carts and other web commerce environments – providing real-time tax calculation results during the checkout process. Those accurate, on-demand tax calculations can be consumed at time of checkout, allowing you to display the results to your shoppers or use them in your payment processing. This diagram is a typical process flow for an E-commerce scenario.

E-Commerce Workflow with Stripe integration

Let’s take a look at an easy example where AvaTax.js can be used in conjunction with Stripe.js to calculate tax and create a payment authorization token.

You’ll need to include the core files (AvaTax.php and AvaTax.js) available here in your site directory.

  1. Enter your AvaTax account credentials in the AvaTax.php proxy file. If you don't have an AvaTax account yet, you can sign up for a free trial account.
    $accountNumber = "1234567890";
    $licenseKey = "A1B2C3D4E5F6G7H8";
  2. Include the AvaTax.js reference in your page.
    <script type="text/javascript" src="AvaTax.js"></script>;
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
  3. Call the methods provided to calculate tax. The request parameters in your actual call should be values drawn from the cart on which you want to determine tax.
    var getTaxRequest = {
    customerCode: "ABC4335",
    docDate: "2014-01-01",
    companyCode: "APITrialCompany",
    client: "AvaTaxSample",
    docCode: "INV001",
    addresses: [
    {
    addressCode: "01",
    line1: "45 Fremont Street",
    city: "San Francisco",
    region: "CA" }],
    lines: [
    {
    lineNo: "01",
    itemCode: "N543",
    qty: "1",
    amount: "10",
    originCode: "01",
    destinationCode: "01",
    description: "Red Size 7 Widget"}]};
  4. Consume the calculation resultsIf the order has not yet been placed, and you’re just estimating the tax amount prior to confirmation, you might have something like this:
    GetTax(getTaxRequest, displayTaxToUser(getTaxResult));
    Otherwise, if you're recording an order to AvaTax that has been confirmed, and you'd like to charge the card as well, you might do something more like this:
    GetTax(getTaxRequest, function(getTaxResult) { chargeCardViaStripe((getTaxResult.TotalTax + getTaxResult.TotalAmount) *100 )});

And you’re done!  That one GetTax call can be used to calculate tax on a cart and/or to record a finalized order in the AvaTax™ system for reporting and filing. For more examples of calls using the AvaTax.js library, take a look at the test html files available in the JavaScript sample repository.

Subscribe via RSS!

Back to posts