Taxamo PHP API Client Documentation

Everything you need to know about using the Tectalic Taxamo PHP API Client.

Introduction

The Tectalic Taxamo REST API Client is a package that provides a convenient and straightforward way to interact with the Taxamo API from your PHP application.

You can purchase this package from https://tectalic.com/apis/taxamo.

Installation

System Requirements

  • PHP version 7.2.5 or newer (including PHP 8.0 and 8.1)
  • PHP JSON extension installed if using PHP 7.x. As of PHP 8.0, this extension became a core PHP extension so is always enabled.
  • A PSR-18 compatible HTTP client such as ‘Guzzle’ or the ‘Symfony HTTP Client’.

Composer Installation

To install this package into your PHP project, we recommend using Composer.

Please see the detailed instructions on configuring your project to access the Tectalic Composer repository.

You will need to log into the Tectalic account that purchased the Tectalic Taxamo REST API Client package to access these instructions.

Once you have followed the above instructions, install the package into your project:

composer require tectalic/taxamo

Manual Installation

If you aren’t using Composer in your PHP project, you can choose to Download the latest release and install it into your PHP project manually.

If doing this, you will need to ensure that all dependencies listed in the package’s composer.json file are also installed.

Usage

After installing the Tectalic Taxamo REST API Client package into your project, ensure you also have a compatible PSR-18 HTTP client such as ‘Guzzle’ or the Symfony ‘HTTP Client’.

You can use the following code sample and customize it to suit your application.

// Load your project's composer autoloader (if you aren't already doing so).
require_once(__DIR__ . '/vendor/autoload.php');
use Symfony\Component\HttpClient\Psr18Client;
use Tectalic\Taxamo\Client;
use Tectalic\Taxamo\Manager;
 
// Build a Tectalic Taxamo REST API Client globally.
$httpClient = new Psr18Client();
Manager::build($httpClient);
 
// or
 
// Build a Tectalic Taxamo REST API Client manually.
$httpClient = new Psr18Client();
$client = new Client($httpClient, Manager::BASE_URI);

Client Class

The primary class you will interact with is the Client class (Tectalic\Taxamo\Client).

This Client class also contains the helper methods that let you quickly access the 27 API Handlers.

Please see below for a complete list of supported handlers and methods.

Supported API Handlers and Methods

This package supports 36 API Methods, which are grouped into 27 API Handlers.

See the table below for a full list of API Handlers and Methods.

API Handler Class and Method Name Description API Verb and URL
ApiV1DictionariesCountries::getCountriesDict() Countries GET /api/v1/dictionaries/countries
ApiV1DictionariesCurrencies::getCurrenciesDict() Currencies GET /api/v1/dictionaries/currencies
ApiV1DictionariesProductTypes::getProductTypesDict() Product types GET /api/v1/dictionaries/product_types
ApiV1Geoip::locateMyIp() Locate IP GET /api/v1/geoip
ApiV1Geoip::locateGivenIp() Locate provided IP GET /api/v1/geoip/{ip}
ApiV1ReportsDomesticSummary::getDomesticSummary() Calculate domestic summary GET /api/v1/reports/domestic/summary
ApiV1ReportsEuVies::getEuVies() Calculate EU VIES report GET /api/v1/reports/eu/vies
ApiV1Settlement::get() Fetch settlement GET /api/v1/settlement/{quarter}
ApiV1SettlementDetailedRefunds::list() Detailed refunds GET /api/v1/settlement/detailed_refunds
ApiV1SettlementRefunds::list() Fetch refunds GET /api/v1/settlement/refunds
ApiV1SettlementSummary::getSettlement() Fetch summary GET /api/v1/settlement/summary/{quarter}
ApiV1StatsSettlementByCountry::getSettlementStats() Settlement by country GET /api/v1/stats/settlement/by_country
ApiV1StatsSettlementByTaxationType::getSettlementStats() Settlement by tax type GET /api/v1/stats/settlement/by_taxation_type
ApiV1StatsSettlementDaily::list() Settlement stats over time GET /api/v1/stats/settlement/daily
ApiV1StatsTransactions::list() Transaction stats GET /api/v1/stats/transactions
ApiV1StatsTransactionsByCountry::getTransactionsStats() Settlement by country GET /api/v1/stats/transactions/by_country
ApiV1TaxCalculate::simple() Simple tax GET /api/v1/tax/calculate
ApiV1TaxCalculate::calculateTax() Calculate tax POST /api/v1/tax/calculate
ApiV1TaxLocationCalculate::tax() Calculate location GET /api/v1/tax/location/calculate
ApiV1TaxVatNumbersValidate::taxNumber() Validate VAT number GET /api/v1/tax/vat_numbers/{tax_number}/validate
ApiV1Transactions::list() Browse transactions GET /api/v1/transactions
ApiV1Transactions::create() Store transaction POST /api/v1/transactions
ApiV1Transactions::get() Retrieve transaction data. GET /api/v1/transactions/{key}
ApiV1Transactions::update() Update transaction PUT /api/v1/transactions/{key}
ApiV1Transactions::cancel() Delete transaction DELETE /api/v1/transactions/{key}
ApiV1TransactionsConfirm::confirmTransaction() Confirm transaction POST /api/v1/transactions/{key}/confirm
ApiV1TransactionsInvoiceRefundsSendEmail::email() Email credit note POST /api/v1/transactions/{key}/invoice/refunds/{refund_note_number}/send_email
ApiV1TransactionsInvoiceSendEmail::email() Email invoice POST /api/v1/transactions/{key}/invoice/send_email
ApiV1TransactionsPayments::list() List payments GET /api/v1/transactions/{key}/payments
ApiV1TransactionsPayments::create() Register a payment POST /api/v1/transactions/{key}/payments
ApiV1TransactionsPaymentsCapture::capturePayment() Capture payment POST /api/v1/transactions/{key}/payments/capture
ApiV1TransactionsRefunds::list() Get transaction refunds GET /api/v1/transactions/{key}/refunds
ApiV1TransactionsRefunds::create() Create a refund POST /api/v1/transactions/{key}/refunds
ApiV1TransactionsUnconfirm::unconfirmTransaction() Un-confirm the transaction POST /api/v1/transactions/{key}/unconfirm
ApiV1VerificationSms::createSmstoken() Create SMS token POST /api/v1/verification/sms
ApiV1VerificationSms::verifySmstoken() Verify SMS token GET /api/v1/verification/sms/{token}

Making a Request

There are two ways to make a request to the nominated API Handler and API Method:

If you built the client to be accessible globally, you can use the relevant API Handler Class directly:

use Tectalic\Taxamo\Handlers\ApiV1DictionariesCountries;
 
(new ApiV1DictionariesCountries())->getCountriesDict();

Alternatively, you can access all API Handlers from the client class using the Client class:

$client->apiV1DictionariesCountries()->getCountriesDict();

Retrieving the Response

Once you have made a request using one of the two methods outlined above, the next step is to access the response.

You can access the response in different ways. Please choose your preferred one.

Model Responses

Model responses are Data Transfer Object (DTO) style PHP classes, with public properties for each API property.

They offer a structured way of retrieving the response from an API request.

All Response Models are an instance of \Spatie\DataTransferObject\DataTransferObject or \Spatie\DataTransferObject\DataTransferObjectCollection.

After performing the request, use the ->toModel() fluent method to the API Method:

use Tectalic\Taxamo\Handlers\ApiV1DictionariesCountries;
 
$model = (new ApiV1DictionariesCountries())->getCountriesDict()->toModel();

Each API Method’s toModel() call will return the appropriate Model class type for the API Method you have just called.

Associative Array Responses

After performing the request, use the ->toArray() fluent method to the API Method:

use Tectalic\Taxamo\Handlers\ApiV1DictionariesCountries;
 
$array = (new ApiV1DictionariesCountries())->getCountriesDict()->toArray();

In the resulting associative array, the array keys will match the names of the public properties in the relevant Model class.

PSR 7 Response Objects

If you need to access the raw response or inspect the HTTP headers, use the ->getResponse() fluent method to the API Method. It will return a Psr\Http\Message\ResponseInterface:

use Tectalic\Taxamo\Handlers\ApiV1DictionariesCountries;
 
$response = (new ApiV1DictionariesCountries())->getCountriesDict()->getResponse();

Errors

When performing requests with Tectalic Taxamo REST API Client, specific scenarios will cause a Tectalic\Taxamo\Exception\ClientException to be thrown. Please see below for details.

Invalid Usage of the Manager Class

A \LogicException will be thrown if the Manager::build() function is called multiple times, or if Manager::access() is called before calling Manager::build().

Unsuccessful HTTP Response Codes

The Tectalic Taxamo REST API Client depends on a PSR-18 compatible HTTP client, and that HTTP client should not throw an exception for unsuccessful HTTP response codes.

An unsuccessful response code is classified as one that is not in the range 200-299 (inclusive). Examples of unsuccessful response codes include:

  • Informational responses (100-199)
  • Redirection responses (300-399)
  • Client error responses (400-499)
  • Server error responses (500-599)

If an unsuccessful response code does occur:

  • your HTTP Client will not throw an Exception.
  • the API Handler’s toModel() method will throw a ClientException.
  • the API Handler’s toArray() method will return the response body and not throw a ClientException.
  • The API Handler’s getResponse() method will return the raw response and not throw a ClientException.

Below is an example of how you may wish to use a try/catch block when performing a request so that you can detect and handle unexpected errors.

use Tectalic\Taxamo\Client;
use Tectalic\Taxamo\ClientException;
use Tectalic\Taxamo\Manager;
 
// Build a Tectalic Taxamo REST API Client globally.
 
Manager::build($httpClient, $auth);
$handler = new ApiV1DictionariesCountries();
 
// Perform a request
try {
$model = $handler->getCountriesDict()->toModel();
// Do something with the response model...
} catch (ClientException $e) {
// Error response received. Retrieve the HTTP response code and response body.
$responseBody = $handler->toArray();
$rawResponse = $handler->getResponse()->getResponse();
$responseCode = $handler->getResponse()->getStatusCode();
// Handle the error...
}

HTTP Client Exceptions

If your HTTP client of choice throws an exception other than ClientException, the Tectalic Taxamo REST API Client Client and its API Handler classes will let these exceptions bubble up.

Consult your HTTP client’s documentation for more details on exception handling.

Tests

The Tectalic Taxamo REST API Client package includes several types of automated PHPUnit tests to verify the correct operation:

  • Unit Tests
  • Integration Tests

To run these tests, you will need to have installed the Tectalic Taxamo REST API Client package with its dev dependencies (i.e. not using the --no-dev flag when running composer).

Unit Tests

These PHPUnit tests are designed to:

  • confirm that each API Method assembles a valid request that matches the Taxamo API OpenAPI specification.
  • verify the behaviour of other parts of the package, such as the Client and Manager classes.

The unit tests can be run using the following command, which needs to be run from this package’s root directory.

composer test:unit

Unit tests do not perform any real requests against the Taxamo API.

Unit tests are located in the tests/Unit directory.

Integration Tests

Integration tests are located in the tests/Integration directory.

These PHPUnit tests are designed to confirm that each API Method parses a valid response, according to the Taxamo API OpenAPI specification. Out of the box the integration tests are designed to work with the Prism Mock Server.

Using Prism as the Target

Make sure Prism is installed. Please see the Prism documentation for details on how to install Prism.

Once Prism is installed, you can run prism and the integration tests side by side in separate terminal windows, or using the following command, which need to be run from this package’s root directory.

echo "> Starting Prism server"
prism mock tests/openapi.json >/dev/null 2>&1 &
PRISM_PID=$!
sleep 2
echo " => Started"
composer test:integration
kill $PRISM_PID

Those commands will start the Prism mock server, then run the integration tests, and then stop the Prism mock server when the tests are completed.

In this case the integration tests do not perform any real requests against the Taxamo API.

Using a Different Target

By setting the TAXAMO_CLIENT_TEST_BASE_URI environment variable, you can set a different API endpoint target for the integration tests.

For example, instead of using Prism, you can use a different mocking/staging/test server of your choice, or you can use the Taxamo API’s live endpoints.

After your setup is complete simply run the following command.

composer test:integration

We do not recommend running integration tests against the live Taxamo API endpoints. This is because the tests will send example data to all endpoints, which can result in new data being created, or existing data being deleted.

Support

If you have any questions or feedback, you can submit a support request to the Tectalic developers by going to https://tectalic.com/support/taxamo.

License

This software is copyright (c) Tectalic.

For the full copyright and license information, please view the ‘LICENSE’ file distributed with the source code.

Last updated 27 Oct 2022

Tectalic Taxamo PHP API Client