Author: Dave Smith
Posted on: 2016-09-28
Package: PHP VAT Number Check
If you or your clients need to add value-added taxes (VAT) within the European Union or validate VAT numbers, then there is a service available to get the latest rates for each country including rates within reduced rate categories.
You can also use the service to calculate VAT compliant prices for you, or reverse the calculation to get an amount without VAT if it has already been included.
Read this article to learn more about how you can use this service to help determine the VAT taxes to use on different purchases using PHP applications.
Contents
Introduction
Getting Started
Available Endpoints
Validating a VAT Number
Getting the VAT Rate
Getting VAT Rates for all EU Countries
Getting VAT Compliant Price Calculations
The List of Reduced Categories
Conclusion
Introduction
VAT, which stands for value-added tax, is used within the European Union to implement a consumption tax on goods and services. It is certainly not one of the most popular subjects for business owners or consumers, however it is a reality that governments need their share of the pie to operate.
The PHP VAT Number Check class queries the online service offered by vatLayer to retrieve the current rates based on the country and type of goods or service being purchased.
Getting Started
To start using the service, we must first get an API access key from the provider by signing up at https://vatlayer.com/product. Of the several subscription plans to choose from, we will be using the Free plan in this article which allows 100 requests per month, which is plenty for our purposes.
Once you have signed up, you will be given a unique access key which you enter in the vatlayer.class.php file.
protected $apiKey = 'YOUR_API_KEY_HERE';
Replace YOUR_API_KEY_HERE with the your unique key, making sure it is the only text between the single quotes.
I will be using the code provided in the example.php file to help demonstrate how the needs of any business can be met.
We will start by instantiating the class to make it ready for us to use.
include('vatlayer.class.php');
$vatLayer = new vatLayer();
Available Endpoints
An endpoint within an API is the location for accessing data from the different services provided. vatLayer has 5 endpoints available which are...
1) validate - Checks that a given VAT number is valid.
2) rate - Returns the rate for a given country.
3) rate_list - Returns the rates for all EU member countries.
4) price - Returns the sale price for a given commodity or service.
5) types - Returns a list of all reduced rate categories for each country.
Validating a VAT Number
To validate a VAT number, which is a unique number assigned to identify the business providing the commodity or service, we need to supply the VAT number to the validate endpoint.
$vatLayer->setEndPoint('validate');
$vatLayer->setParam('vat_number','LU26375245');
$vatLayer->getResponse();
In this code we are setting our endpoint to 'validate', setting the vat_number parameter to LU26375245 and finally getting the response from the service.
We can then process the response which will be contained in the $vatLayer->response property.
if( $vatLayer->response->database == 'ok' ) {
//manage the booleans
$valid = ( $vatLayer->response->valid ) ? 'Yes' : 'No';
$formatValid = ( $vatLayer->response->format_valid ) ? 'Yes' : 'No';
echo 'VAT number: '.$vatLayer->response->vat_number.'<br>';
echo 'Correct format: '.$formatValid.'<br>';
echo 'Valid: '.$valid.'<br>';
echo 'Country Code: '.$vatLayer->response->country_code.'<br>';
echo 'Company: '.$vatLayer->response->company_name.'<br>';
echo 'Address: '.$vatLayer->response->company_address.'<br>';
} else {
echo 'sorry, '.$vatLayer->response->country_code.' vat database is offline<br>';
}
We are initially checking to make sure the service was able to access the specific VAT database for the country the VAT number was generated from. The 'database' response will contain the text 'ok' on success, otherwise the database is currently offline.
The response will contain 2 boolean values, true or false, which are 'valid' and 'format_valid'. You can use these in logical structures to determine what action to take, in our case we are simply setting the values of the related variables to 'Yes' or 'No'.
The response will also return the 'vat_number', 'country_code', 'company_name' and 'company_address' that the VAT number has been assigned to.
Getting the VAT Rate
To get the current VAT rate for a specific country we need to supply the standard 2 digit Country Code to the 'rate' endpoint.
$vatLayer->setEndPoint('rate');
$vatLayer->resetParams();
$vatLayer->setParam('country_code','DE');
$vatLayer->getResponse();
In the above code we have introduced a new class method 'resetParams' which we use to clear the parameters from a previous request so that we can start a new request clean.
The response will be contained in the $vatLayer->response property. The example script simply dumps all the data to the screen using...
var_dump($vatLayer->response);
The response will contain the boolean 'success', $vatLayer->response->success, value set to true or false depending on whether the request was successful, which you can use in a logical structure to determine what action to take.
The response will also return the 'country_code', 'country_name', 'standard_rate' and 'reduced_rates' for the given VAT number.
The standard rate is the VAT percentage for the given country and the reduced rates is a list of categories and the reduced VAT percentages for purchases within that category.
The service also provides an additional 2 ways, without using the country code, to get the VAT rate. These are...
by the customers IP Address
$vatLayer->setEndPoint('rate');
$vatLayer->resetParams();
$vatLayer->setParam('ip_address','134.245.44.17');
$vatLayer->getResponse();
or by the IP Address of the system making the request
$vatLayer->setEndPoint('rate');
$vatLayer->resetParams();
$vatLayer->setParam('use_client_ip','1');
$vatLayer->getResponse();
It is important to note that if the country code or IP Address resolves to a country not in the European Union, the response will be successful with a VAT rate of 0 (zero).
Getting VAT Rates for all EU Countries
If you need the VAT rates for every EU Country, we simply send a request to the rate_list endpoint.
$vatLayer->setEndPoint('rate_list');
$vatLayer->resetParams();
$vatLayer->getResponse();
var_dump($vatLayer->response);
The response will be significantly larger than requests for individual countries, since it will contain VAT rates for each country. You should only use this endpoint if you are updating the data in your own database.
Getting VAT Compliant Price Calculations
We can have the service perform the price calculations for us by providing the country and amount to the price endpoint.
$vatLayer->setEndPoint('price');
$vatLayer->resetParams();
$vatLayer->setParam('country_code','DE');
$vatLayer->setParam('amount',150);
$vatLayer->getResponse();
If you wanted the service to determine the country, just like with rates, you can replace the country_code parameter with:
$vatLayer->setParam('ip_address','134.245.44.17');
supplying the customers IP Address, or with
$vatLayer->setParam('use_client_ip','1');
to use the IP Address of the system making the request.
The response will contain the boolean 'success', $vatLayer->response->success, value set to true or false depending on whether the request was successful, which you can use in a logical structure to determine what action to take.
The response will also return the 'country_code', 'country_name', 'price_excl_vat', 'price_incl_vat' and the 'vat_rate'. The 'price_incl_vat' value is our amount with the VAT added.
If we want the calculation to use a rate from one of the reduced categories, we would also supply the category type...
$vatLayer->setParam('type','medical');
We can also request the service to do a reverse calculation if our amount already includes the VAT by setting the 'incl' parameter to 1 (one)...
$vatLayer->setParam('incl','1');
In this case, we would use the 'price_excl_vat' value for the amount without the VAT included.
The List of Reduced Categories
We can get a list of categories for reduced VAT rates by sending a request to the 'types' endpoint.
$vatLayer->setEndPoint('types');
$vatLayer->resetParams();
$vatLayer->getResponse();
The response will be a list of category types. In the example we loop through each type to display them...
foreach( $vatLayer->response->types as $type ) {
echo $type.'<br>';
}
Requests to this endpoint are not charged against any monthly limits of your selected plan.
Conclusion
As more and more sales where being made online across state and country lines, governments quickly realized that they where missing out on their share. The end result is that the largest to smallest businesses need to deal with the tax man.
Any resource that makes this task easier for our business or clients is a win in my book. I will gladly push the task of validating businesses and keeping rates up to date to a third party, freeing up time for making the all important sale.
If you liked this tutorial on using up to date VAT rates in your project sales, use the share buttons above to share it with other developers. If you have questions post a comment below.
You need to be a registered user or login to post a comment
1,411,262 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
1. How VAT works ... - Lester Caine (2016-09-28 07:51)
VAT is not so complicated!... - 0 replies
Read the whole comment and replies