PHP code example of taxjar / taxjar-php

1. Go to this page and download the library: Download taxjar/taxjar-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

taxjar / taxjar-php example snippets



$client = TaxJar\Client::withApiKey($_ENV['TAXJAR_API_KEY']);

$categories = $client->categories();

$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

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

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

$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
    ]
  ]
]);

$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
    ]
  ]
]);

$client->deleteOrder('123');

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

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

$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
    ]
  ]
]);

$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
    ]
  ]
]);

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

$customers = $client->listCustomers();

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

$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'
]);

$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'
]);

$client->deleteCustomer('123');

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

echo $rates->combined_rate;
// 0.09

$nexus_regions = $client->nexusRegions();

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

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

$summarized_rates = $client->summaryRates();


$client = TaxJar\Client::withApiKey($_ENV['TAXJAR_SANDBOX_API_KEY']);
$client->setApiConfig('api_url', TaxJar\Client::SANDBOX_API_URL);

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

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

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


$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();
}

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

composer 

php vendor/bin/phpunit test/specs/.