2017 © Pedro Peláez
 

library taxjar-php

Sales Tax API Client for PHP 5.5+

image

taxjar/taxjar-php

Sales Tax API Client for PHP 5.5+

  • Thursday, May 3, 2018
  • by fastdivision
  • Repository
  • 10 Watchers
  • 29 Stars
  • 147,904 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 7 Forks
  • 0 Open issues
  • 21 Versions
  • 18 % Grown

The README.md

TaxJar Sales Tax API for PHP Packagist Build Status

Official PHP client for Sales Tax API v2. For the REST documentation, please visit https://developers.taxjar.com/api., (*1)


Requirements
Installation
Authentication
Usage
Custom Options
Sandbox Environment
Error Handling
Testing, (*2)


Requirements

  • PHP 8.0 and later.
  • Guzzle (included via Composer).

Installation

Use Composer and add taxjar-php as a dependency:, (*3)

composer require taxjar/taxjar-php
{
  "require": {
    "taxjar/taxjar-php": "^2.0"
  }
}

If you get an error with composer require, update your composer.json directly and run composer update., (*4)

Authentication

require __DIR__ . '/vendor/autoload.php';
$client = TaxJar\Client::withApiKey($_ENV['TAXJAR_API_KEY']);

You're now ready to use TaxJar! Check out our quickstart guide to get up and running quickly., (*5)

Usage

categories - List all tax categories
taxForOrder - Calculate sales tax for an order
listOrders - List order transactions
showOrder - Show order transaction
createOrder - Create order transaction
updateOrder - Update order transaction
deleteOrder - Delete order transaction
listRefunds - List refund transactions
showRefund - Show refund transaction
createRefund - Create refund transaction
updateRefund - Update refund transaction
deleteRefund - Delete refund transaction
listCustomers - List customers
showCustomer - Show customer
createCustomer - Create customer
updateCustomer - Update customer
deleteCustomer - Delete customer
ratesForLocation - List tax rates for a location (by zip/postal code)
nexusRegions - List nexus regions
validateAddress - Validate an address
validate - Validate a VAT number
summaryRates - Summarize tax rates for all regions, (*6)


List all tax categories _(API docs)_

The TaxJar API provides product-level tax rules for a subset of product categories. These categories are to be used for products that are either exempt from sales tax in some jurisdictions or are taxed at reduced rates. You need not pass in a product tax code for sales tax calculations on product that is fully taxable. Simply leave that parameter out., (*7)

$categories = $client->categories();

Calculate sales tax for an order _(API docs)_

Shows the sales tax that should be collected for a given order., (*8)

$order_taxes = $client->taxForOrder([
  'from_country' => 'US',
  'from_zip' => '07001',
  'from_state' => 'NJ',
  'from_city' => 'Avenel',
  'from_street' => '305 W Village Dr',
  'to_country' => 'US',
  'to_zip' => '07446',
  'to_state' => 'NJ',
  'to_city' => 'Ramsey',
  'to_street' => '63 W Main St',
  'amount' => 16.50,
  'shipping' => 1.5,
  'line_items' => [
    [
      'id' => '1',
      'quantity' => 1,
      'product_tax_code' => '31000',
      'unit_price' => 15.0,
      'discount' => 0
    ]
  ]
]);

echo $order_taxes->amount_to_collect;
// 1.26

List order transactions _(API docs)_

Lists existing order transactions created through the API., (*9)

$orders = $client->listOrders([
  'from_transaction_date' => '2015/05/01',
  'to_transaction_date' => '2015/05/31'
]);

Show order transaction _(API docs)_

Shows an existing order transaction created through the API., (*10)

$order = $client->showOrder('123');

Create order transaction _(API docs)_

Creates a new order transaction., (*11)

$order = $client->createOrder([
  'transaction_id' => '123',
  'transaction_date' => '2015/05/14',
  'from_country' => 'US',
  'from_zip' => '92093',
  'from_state' => 'CA',
  'from_city' => 'La Jolla',
  'from_street' => '9500 Gilman Drive',
  'to_country' => 'US',
  'to_zip' => '90002',
  'to_state' => 'CA',
  'to_city' => 'Los Angeles',
  'to_street' => '123 Palm Grove Ln',
  'amount' => 17.45,
  'shipping' => 1.5,
  'sales_tax' => 0.95,
  'line_items' => [
    [
      'id' => '1',
      'quantity' => 1,
      'product_identifier' => '12-34243-9',
      'description' => 'Fuzzy Widget',
      'unit_price' => 15.0,
      'discount': 0,
      'sales_tax' => 0.95
    ]
  ]
]);

Update order transaction _(API docs)_

Updates an existing order transaction created through the API., (*12)

$order = $client->updateOrder([
  'transaction_id' => '123',
  'amount' => 17.95,
  'shipping' => 2.0,
  'line_items' => [
    [
      'quantity' => 1,
      'product_identifier' => '12-34243-0',
      'description' => 'Heavy Widget',
      'unit_price' => 15.0,
      'discount' => 0.0,
      'sales_tax' => 0.95
    ]
  ]
]);

Delete order transaction _(API docs)_

Deletes an existing order transaction created through the API., (*13)

$client->deleteOrder('123');

List refund transactions _(API docs)_

Lists existing refund transactions created through the API., (*14)

$refunds = $client->listRefunds([
  'from_transaction_date' => '2015/05/01',
  'to_transaction_date' => '2015/05/31'
]);

Show refund transaction _(API docs)_

Shows an existing refund transaction created through the API., (*15)

$refund = $client->showRefund('123-refund');

Create refund transaction _(API docs)_

Creates a new refund transaction., (*16)

$refund = $client->createRefund([
  'transaction_id' => '123-refund',
  'transaction_reference_id' => '123',
  'transaction_date' => '2015/05/14',
  'from_country' => 'US',
  'from_zip' => '92093',
  'from_state' => 'CA',
  'from_city' => 'La Jolla',
  'from_street' => '9500 Gilman Drive',
  'to_country' => 'US',
  'to_zip' => '90002',
  'to_state' => 'CA',
  'to_city' => 'Los Angeles',
  'to_street' => '123 Palm Grove Ln',
  'amount' => -17.45,
  'shipping' => -1.5,
  'sales_tax' => -0.95,
  'line_items' => [
    [
      'id' => '1',
      'quantity' => 1,
      'product_identifier' => '12-34243-9',
      'description' => 'Fuzzy Widget',
      'unit_price' => -15.0,
      'discount' => -0,
      'sales_tax' => -0.95
    ]
  ]
]);

Update refund transaction _(API docs)_

Updates an existing refund transaction created through the API., (*17)

$refund = $client->updateRefund([
  'transaction_id' => '123-refund',
  'transaction_reference_id' => '123',
  'amount' => -17.95,
  'shipping' => -2.0,
  'line_items' => [
    [
      'id' => '1',
      'quantity' => 1,
      'product_identifier' => '12-34243-0',
      'description' => 'Heavy Widget',
      'unit_price' => -15.0,
      'discount' => -0,
      'sales_tax' => -0.95
    ]
  ]
]);

Delete refund transaction _(API docs)_

Deletes an existing refund transaction created through the API., (*18)

$client->deleteRefund('123-refund');

List customers _(API docs)_

Lists existing customers created through the API., (*19)

$customers = $client->listCustomers();

Show customer _(API docs)_

Shows an existing customer created through the API., (*20)

$customer = $client->showCustomer('123');

Create customer _(API docs)_

Creates a new customer., (*21)

$customer = $client->createCustomer([
  'customer_id' => '123',
  'exemption_type' => 'wholesale',
  'name' => 'Dunder Mifflin Paper Company',
  'exempt_regions' => [
    [
      'country' => 'US',
      'state' => 'FL'
    ],
    [
      'country' => 'US',
      'state' => 'PA'
    ]
  ],
  'country' => 'US',
  'state' => 'PA',
  'zip' => '18504',
  'city' => 'Scranton',
  'street' => '1725 Slough Avenue'
]);

Update customer _(API docs)_

Updates an existing customer created through the API., (*22)

$customer = $client->updateCustomer([
  'customer_id' => '123',
  'exemption_type' => 'wholesale',
  'name' => 'Sterling Cooper',
  'exempt_regions' => [
    [
      'country' => 'US',
      'state' => 'NY'
    ]
  ],
  'country' => 'US',
  'state' => 'NY',
  'zip' => '10010',
  'city' => 'New York',
  'street' => '405 Madison Ave'
]);

Delete customer _(API docs)_

Deletes an existing customer created through the API., (*23)

$client->deleteCustomer('123');

List tax rates for a location (by zip/postal code) _(API docs)_

Shows the sales tax rates for a given location., (*24)

Please note this method only returns the full combined rate for a given location. It does not support nexus determination, sourcing based on a ship from and ship to address, shipping taxability, product exemptions, customer exemptions, or sales tax holidays. We recommend using taxForOrder to accurately calculate sales tax for an order., (*25)

$rates = $client->ratesForLocation(90002, [
  'city' => 'LOS ANGELES',
  'country' => 'US'
]);

echo $rates->combined_rate;
// 0.09

List nexus regions _(API docs)_

Lists existing nexus locations for a TaxJar account., (*26)

$nexus_regions = $client->nexusRegions();

Validate an address _(API docs)_

Validates a customer address and returns back a collection of address matches. Address validation requires a TaxJar Plus subscription., (*27)

$addresses = $client->validateAddress([
  'country' => 'US',
  'state' => 'AZ',
  'zip' => '85297',
  'city' => 'Gilbert',
  'street' => '3301 South Greenfield Rd'
]);

Validate a VAT number _(API docs)_

Validates an existing VAT identification number against VIES., (*28)

$validation = $client->validate([
  'vat' => 'FR40303265045'
]);

Summarize tax rates for all regions _(API docs)_

Retrieve minimum and average sales tax rates by region as a backup., (*29)

This method is useful for periodically pulling down rates to use if the TaxJar API is unavailable. However, it does not support nexus determination, sourcing based on a ship from and ship to address, shipping taxability, product exemptions, customer exemptions, or sales tax holidays. We recommend using taxForOrder to accurately calculate sales tax for an order., (*30)

$summarized_rates = $client->summaryRates();

Sandbox Environment

You can easily configure the client to use the TaxJar Sandbox:, (*31)

require __DIR__ . '/vendor/autoload.php';
$client = TaxJar\Client::withApiKey($_ENV['TAXJAR_SANDBOX_API_KEY']);
$client->setApiConfig('api_url', TaxJar\Client::SANDBOX_API_URL);

For testing specific error response codes, pass the custom X-TJ-Expected-Response header:, (*32)

$client->setApiConfig('headers', [
  'X-TJ-Expected-Response' => 422
]);

Custom Options

Timeout

This package utilizes Guzzle which defaults to a request timeout value of 0s, allowing requests to remain pending for an indefinite period of time., (*33)

You can modify this behavior by configuring the client with a timeout value in seconds., (*34)

$client->setApiConfig('timeout', 30);

API Version

By default, TaxJar's API will respond to requests with the latest API version when a version header is not present on the request., (*35)

To request a specific API version, include the x-api-version header with the desired version string., (*36)

$client->setApiConfig('headers', [
  'x-api-version' => '2020-08-07'
]);

Error Handling

When invalid data is sent to TaxJar or we encounter an error, we’ll throw a TaxJar\Exception with the HTTP status code and error message. To catch these exceptions, refer to the example below:, (*37)

require __DIR__ . '/vendor/autoload.php';
$client = TaxJar\Client::withApiKey($_ENV['TAXJAR_API_KEY']);

try {
  // Invalid request
  $order = $client->createOrder([
    'transaction_date' => '2015/05/14',
    'from_country' => 'US',
    'from_zip' => '07001',
    'from_state' => 'NJ',
    'from_city' => 'Avenel',
    'from_street' => '305 W Village Dr',
    'to_country' => 'US',
    'to_zip' => '90002',
    'to_state' => 'CA',
    'to_city' => 'Ramsey',
    'to_street' => '63 W Main St',
    'amount' => 16.5,
    'shipping' => 1.5,
    'sales_tax' => 0.95,
    'line_items' => [
      [
        'id' => '1',
        'quantity' => 1,
        'product_tax_code' => '31000',
        'unit_price' => 15,
        'discount' => 0,
        'sales_tax' => 0.95
      ]
    ]
  ]);
} catch (TaxJar\Exception $e) {
  // 406 Not Acceptable – transaction_id is missing
  echo $e->getMessage();

  // 406
  echo $e->getStatusCode();
}

For a full list of error codes, click here., (*38)

Testing

Make sure PHPUnit is installed via composer install and run the following:, (*39)

php vendor/bin/phpunit test/specs/.

To enable debug mode, set the following config parameter after authenticating:, (*40)

$client->setApiConfig('debug', true);

The Versions

03/05 2018

dev-master

9999999-dev https://www.taxjar.com/

Sales Tax API Client for PHP 5.5+

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar and contributors
by TaxJar

api taxjar sales tax

03/05 2018

v1.7.0

1.7.0.0 https://www.taxjar.com/

Sales Tax API Client for PHP 5.5+

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar

api taxjar sales tax

03/05 2018

dev-travis-test

dev-travis-test https://www.taxjar.com/

Sales Tax API Client for PHP 5.5+

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar

api taxjar sales tax

13/04 2018

dev-customer-exemptions

dev-customer-exemptions https://www.taxjar.com/

Sales Tax API Client for PHP 5.5+

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar

api taxjar sales tax

21/03 2018

v1.6.0

1.6.0.0 https://www.taxjar.com/

Sales Tax API Client for PHP 5.5+

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar

api taxjar sales tax

21/03 2018

dev-sandbox-environment

dev-sandbox-environment https://www.taxjar.com/

Sales Tax API Client for PHP 5.5+

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar

api taxjar sales tax

20/06 2017

v1.5.0

1.5.0.0 https://www.taxjar.com/

Sales Tax API Client for PHP 5.5+

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar

api taxjar sales tax

20/06 2017

dev-exception-handling

dev-exception-handling http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar and contributors

api taxjar sales tax

05/10 2016

v1.4.0

1.4.0.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar and contributors

api taxjar sales tax

07/04 2016

v1.3.1

1.3.1.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar and contributors

api taxjar sales tax

04/03 2016

v1.3.0

1.3.0.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar and contributors

api taxjar sales tax

29/12 2015

v1.2.4

1.2.4.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar and contributors

api taxjar sales tax

17/10 2015

v1.2.3

1.2.3.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar and contributors

api taxjar sales tax

28/09 2015

v1.2.2

1.2.2.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar and contributors

api taxjar sales tax

24/09 2015

v1.2.1

1.2.1.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

 

The Development Requires

by TaxJar and contributors

api taxjar sales tax

24/09 2015

v1.1.4

1.1.4.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

  • ext-curl *
  • ext-json *
  • ext-mbstring *
  • php >=5.3.3

 

by TaxJar and contributors

api taxjar sales tax

01/09 2015

v1.1.3

1.1.3.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

  • ext-curl *
  • ext-json *
  • ext-mbstring *
  • php >=5.3.3

 

by TaxJar and contributors

api taxjar sales tax

26/08 2015

v1.1.2

1.1.2.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

  • ext-curl *
  • ext-json *
  • ext-mbstring *
  • php >=5.3.3

 

by TaxJar and contributors

api taxjar sales tax

18/08 2015

v1.1.1

1.1.1.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

  • ext-curl *
  • ext-json *
  • ext-mbstring *
  • php >=5.3.3

 

by TaxJar and contributors

api taxjar sales tax

28/07 2015

v1.1.0

1.1.0.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

  • ext-curl *
  • ext-json *
  • ext-mbstring *
  • php >=5.3.3

 

by TaxJar and contributors

api taxjar sales tax

15/07 2015

v1.0.0

1.0.0.0 http://taxjar.com/

TaxJar PHP Library

  Sources   Download

MIT

The Requires

  • ext-curl *
  • ext-json *
  • ext-mbstring *
  • php >=5.3.3

 

by TaxJar and contributors

api taxjar sales tax