PHP library for dealing with European VAT

Overview

ibericode/vat

Build Status Latest Stable Version PHP from Packagist Total Downloads License

This is a simple PHP library to help you deal with Europe's VAT rules.

  • Fetch VAT rates for any EU member state using ibericode/vat-rates.
  • Validate VAT numbers (by format and/or existence)
  • Validate ISO-3316 alpha-2 country codes
  • Determine whether a country is part of the EU
  • Geo-locate IP addresses

Installation

PHP version 7.1 or higher with the CURL and JSON extension is required.

For VAT number existence checking, the PHP SOAP extension is required as well.

To get the latest stable version, install the package using Composer:

composer require ibericode/vat

Usage

This library exposes 4 main classes to interact with: Rates, Countries, Validator and Geolocator.

Retrieving VAT rates.

$rates = new Ibericode\Vat\Rates('/path-for-storing-cache-file.txt');
$rates->getRateForCountry('NL'); // 21
$rates->getRateForCountry('NL', 'standard'); // 21
$rates->getRateForCountry('NL', 'reduced'); // 9
$rates->getRateForCountryOnDate('NL', new \Datetime('2010-01-01'), 'standard'); // 19

This fetches rate from ibericode/vat-rates and stores a local copy that is periodically refreshed (once every 12 hours by default).

Validation

Validating a VAT number:

$validator = new Ibericode\Vat\Validator();
$validator->validateVatNumberFormat('NL203458239B01'); // true (checks format)
$validator->validateVatNumber('NL203458239B01'); // false (checks format + existence)

Validating an IP address:

$validator = new Ibericode\Vat\Validator();
$validator->validateIpAddress('256.256.256.256'); // false
$validator->validateIpAddress('8.8.8.8'); // true

Validating an ISO-3166-1-alpha2 country code:

$validator = new Ibericode\Vat\Validator();
$validator->validateCountryCode('DE'); // true
$validator->validateCountryCode('ZZ'); // false

Dealing with ISO-3166-1-alpha2 country codes

$countries = new Ibericode\Vat\Countries();

// access country name using array access
echo $countries['NL']; // Netherlands

// loop over countries
foreach ($countries as $code => $name) {
    // ...
}

// check if country is in EU
$countries->isCountryCodeInEU('NL'); // true
$countries->isCountryCodeInEU('US'); // false

Geo-location

This library includes a simple geo-location service using ip2c.org or ip2country.info.

$geolocator = new Ibericode\Vat\Geolocator();
$geolocator->locateIpAddress('8.8.8.8'); // US

To use ip2country.info explicitly.

$geolocator = new Ibericode\Vat\Geolocator('ip2country.info');
$geolocator->locateIpAddress('8.8.8.8'); // US

Or, to use ip2c.org explicitly.

$geolocator = new Ibericode\Vat\Geolocator('ip2c.org');
$geolocator->locateIpAddress('8.8.8.8'); // US

Symfony support

If you need to use this package in a Symfony environment, check out ibericode/vat-bundle.

License

ibericode/vat is licensed under the MIT License.

Comments
  • SSL certificate error

    SSL certificate error

    The library is crashing because https://jsonvat.com has an expired certificate. Please change https://jsonvat.com/ to http://jsonvat.com/ by removing the https or add a rule to curl to accept invalid certificate.

    opened by paul321 8
  • New feature: fetch rate from an older date

    New feature: fetch rate from an older date

    Hi,

    For reasons, I had to find which was the VAT rate for a country on a specific date. jsonvat.com provides this information, but this is currently squeezed by the vat.php library, which only fetches the currently applicable rate.

    This PR changes the way rates are fetched (which may introduce a BC break for advanced users, but nothing really new otherwise), so if merged, this may need a bound to a new minor version.

    The case of a "future" VAT rate (a future applicable rate already defined) is also handled (cf tests).

    opened by bpolaszek 4
  • CURL Refresh Fails

    CURL Refresh Fails

    This could just be my setup but I was getting failure when

    IbericodeVatRatesClient fetch() was being called. Returning an empty body and no status code. Throwing an exception preventing all other code doing-the-do.

    Had to add in curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE); to get it to work, not ideal as a temp mod to the main library... but might be an cert issue on the HTTPS for https://raw.githubusercontent.com/ibericode/vat-rates/master/vat-rates.json that needs resolving? Or could be something else but this fixed it for me.

    opened by smuglet 2
  • UK/GB Vat number support

    UK/GB Vat number support

    As of 01/01/2021 (Brexit) the Vies validator no longer supports GB VAT numbers.

    Currently this library still supports validateVatNumberFormat('GB000000000') as true, but running validate on any GB code throws a ViesException of invalid input.

    There is a (new) API from the UK here - Would you be open to a PR that added support for GB numbers using this or do you want to stay EU/Vies specific?

    opened by paulhennell 2
  • [bug] Fix spanish vat regex, which previously allowed invalid vat ending overflow

    [bug] Fix spanish vat regex, which previously allowed invalid vat ending overflow

    Since the Spanish VAT pattern allows multiple combinaisons (with the regex pipe "|"), it looks like the regex accept any string if only the beginning of the string is matching any of the possible patterns.

    This means that the validator will consider those examples as valid:

    • ESB1234567C
    • ES12345678D
    • ESE12345678

    but also those ones:

    • ESB1234567C_insert_here_anything
    • ES12345678D_the_end_will_never_be_checked
    • ESE12345678_wink_wink

    Here's a patch to avoid this problem.

    opened by Swop 2
  • Allow letter in validation key of french VAT number

    Allow letter in validation key of french VAT number

    The french VAT number format is : 'FR'+ 2 digits or letter (as validation key ) + 9 digits (as SIREN) Before this PR the validation key was only 2 digits or 2 letters, but the format allows to have 1 digit and 1 letter (in first or second position)

    Source : https://en.wikipedia.org/wiki/VAT_identification_number

    opened by malengrin 2
  • Improvements - GetInfo

    Improvements - GetInfo

    Hi,

    I wanted to get more infos from the SoapAPI. So I have add a function to get the full response. I also refactored some code.

    Thanks for your help and to read my contributions,

    Aurélien

    opened by aurel282 2
  • Store vat rates in local file and add failover client

    Store vat rates in local file and add failover client

    As jsonvat.com had another certificate issue today I decided to add a client that loads the vat rates from a local file (data from jsonvat.com).

    As the file needs to be updated from time to time, I implemented a failover client that tries to fetch data from one client (jsonvat.com) and falls back to another one (the local client) if the first fails. So one can get the current rates, but does not need to fear outages when jsonvat.com is down (even though in that case it's possible to work with outdated data)

    fixes #5 and #2

    Tests are failing because I added a test case for jsonvat.com which is currently "down" (SSL certificate error) :wink:

    opened by digilist 2
  • Embedding your lib into a WP plugin

    Embedding your lib into a WP plugin

    Hi, i embedded your lib in a plugin and it works perfectly, but one of my users gets the following error, which seem to be linked to your code:

    [08-Apr-2018 13:05:09 UTC] PHP Fatal error:  Uncaught Error: Class 'SoapClient' not found in /var/www/html/illustration.tools/public_html/wp-content/plugins/rcp-vat/libs/vendor/dannyvankooten/vat.php/src/Vies/Client.php:63
    Stack trace:
    #0 /var/www/html/illustration.tools/public_html/wp-content/plugins/rcp-vat/libs/vendor/dannyvankooten/vat.php/src/Vies/Client.php(44): DvK\Vat\Vies\Client->getClient()
    #1 /var/www/html/illustration.tools/public_html/wp-content/plugins/rcp-vat/libs/vendor/dannyvankooten/vat.php/src/Validator.php(93): DvK\Vat\Vies\Client->checkVat('39', '281928M')
    #2 /var/www/html/illustration.tools/public_html/wp-content/plugins/rcp-vat/admin/class-rcp-vat-admin.php(666): DvK\Vat\Validator->validateExistence('39281928M')
    #3 /var/www/html/illustration.tools/public_html/wp-content/plugins/rcp-vat/admin/class-rcp-vat-admin.php(724): Rcp_Vat_Admin->validateUserFields(Array, 'register')
    #4 /var/www/html/illustration.tools/public_html/wp-includes/class-wp-hook.php(286): Rcp_Vat_Admin->rcp_vat_rcp_validate_user_fields_on_register(Array)
    #5 /var/w in /var/www/html/illustration.tools/public_html/wp-content/plugins/rcp-vat/libs/vendor/dannyvankooten/vat.php/src/Vies/Client.php on line 63```
    
    Any idea ?
    Thanks
    opened by crouti 2
  • Always Invalid country code when trying to get rates

    Always Invalid country code when trying to get rates

    I'm trying to grab VAT rates for specific countries but i always get the error:

    Invalid country code "country"

    What i'm doing:

         $rates = new Rates($_SERVER["DOCUMENT_ROOT"].'assets/vat.txt');
         $rate = $rates->getRateForCountry('NL'); // 21
         return $rate;
    
    opened by Stelikas 1
  • vat rates package abandoned?

    vat rates package abandoned?

    Hi, I see your package relies on this (https://github.com/adamcooke/vat-rates) which is no more available. So your package will still be able to get updated VAT rates by countries?

    opened by Echecivuole 1
  • Get linked company info if VAT number exists

    Get linked company info if VAT number exists

    opened by Eazis-Music-Systems 1
  • add hasPattern to Validator

    add hasPattern to Validator

    being able to check if here is a pattern for a country would it possible to add fx VAT number format check for Norway

    so I could first do a check for "hasPattern" and then call the validateVatNumberFormat if hasPattern returned true

    Today all numbers from Norway return false due to this check:

            if (! isset($this->patterns[$country])) {
                return false;
            }
    
    
    enhancement 
    opened by gemal 2
  • GB brexit and XI Northern Ireland continue to stay on EU

    GB brexit and XI Northern Ireland continue to stay on EU

    https://tulli.fi/en/-/brexit-and-country-codes-from-1-january-2021-#:~:text=The%20country%20code%20for%20Northern,be%20used%20in%20customs%20declarations.

    opened by trippo 1
Releases(2.0.6)
  • 2.0.6(Nov 15, 2021)

  • 2.0.4(Dec 17, 2020)

    Brexit transition period ends on Dec 31, so

    Countries::isCountryCodeInEU(string $country_code)
    

    will return false starting 2021-01-01 00:00:00

    Source code(tar.gz)
    Source code(zip)
  • 2.0.3(Apr 17, 2020)

    Ditch client for jsonvat.com as it is discontinued. This package already was already using the client for ibericode/vat-rates by default.

    If you were manually using Ibericode\Vat\Clients\JsonVatClient in your code then change it to use Ibericode\Vat\Clients\IbericodeVatRatesClient instead.

    $client = new Ibericode\Vat\Clients\IbericodeVatRatesClient();
    $rates = new Ibericode\Rates\Rates('/path-for-storing-cache-file.txt', 12 * 3600, $client);
    
    Source code(tar.gz)
    Source code(zip)
  • 2.0.2(Jan 16, 2020)

    • Fixes an issue with invalid ES, IE and GB VAT numbers being accepted (when given a valid VAT number with a suffix or prefix). Thanks to @Swop for catching it.
    Source code(tar.gz)
    Source code(zip)
  • 2.0(Feb 11, 2019)

    Version 2.0 release is a complete rewrite, resulting in a much simpler and more usable public API. Check out the README for updated usage examples.

    The package has moved to a new Composer package name as well. It's now called ibericode/vat.

    Source code(tar.gz)
    Source code(zip)
  • 1.2.1(Feb 7, 2019)

    Additions

    • validateCountryCode method on Countries class.
    • validateIpAddress method on Countries class.

    Improvements

    • Use HTTPS protocol for ip2c.org geo-location service.
    • Bump package requirement to PHP 7.1 or later
    • Add argument and return type declarations
    • Don't issue HTTP request for IP geo-location if IP isn't valid or is in local range
    Source code(tar.gz)
    Source code(zip)
  • 1.1.2(Dec 4, 2017)

  • 1.1.1(Jul 6, 2017)

  • 1.1(Feb 24, 2017)

    This release allows you to fetch the VAT rates for any future or past date thanks to a pull request by @bpolaszek.

    *Example: *

    This fetches the VAT rate in The Netherlands (NL) on 2010-01-01.

    $rates = new DvK\Vat\Rates\Rates();
    $rates->country('NL', 'standard', new \Datetime('2010-01-01')); // 19
    

    Because this change requires VAT periods to be stored in the Rates class, the Rates::all() method now returns a slightly different array format. See the inline docs on the Client interface for details on the format.

    Source code(tar.gz)
    Source code(zip)
  • 1.0.2(Feb 15, 2017)

  • 1.0.1(Jan 21, 2017)

Owner
ibericode
Our code. Your site. Beautiful.
ibericode
A simple library for dealing with docx word processed documents

WordCat Limited manipulation of docx word processed documents A simple php library for manipulation of docx word processed document; in particular the

Stephen J Sullivan 0 Oct 22, 2021
This car rental project system project in PHP focuses mainly on dealing with customers regarding their car rental hours and certain transactions.

Car-Rental Online Car Rental Management System This car rental project system project in PHP focuses mainly on dealing with customers regarding their

Adarsh Kumar Singh 2 Sep 29, 2022
Dobren Dragojević 6 Jun 11, 2023
Easy to use utility functions for everyday PHP projects. This is a port of the Lodash JS library to PHP

Lodash-PHP Lodash-PHP is a port of the Lodash JS library to PHP. It is a set of easy to use utility functions for everyday PHP projects. Lodash-PHP tr

Lodash PHP 474 Dec 31, 2022
PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP language

php-text-analysis PHP Text Analysis is a library for performing Information Retrieval (IR) and Natural Language Processing (NLP) tasks using the PHP l

null 464 Dec 28, 2022
php-echarts is a php library for the echarts 5.0.

php-echarts 一款支持Apache EChart5.0+图表的php开发库 优先ThinkPHP5/6的开发及测试。 Apache EChart5.0已经最新发布,在视觉效果、动画效果和大数据展示方面已经远超之前的版本; 故不考虑EChart5.0之前版本的兼容问题;建议直接尝试5.0+

youyiio 5 Aug 15, 2022
Minimalist PHP frame for Core-Library, for Developing PHP application that gives you the full control of your application.

LazyPHP lightweight Pre-Made Frame for Core-library Install Run the below command in your terminal $ composer create-project ryzen/lazyphp my-first-pr

Ry-Zen 7 Aug 21, 2022
Gettext is a PHP (^7.2) library to import/export/edit gettext from PO, MO, PHP, JS files, etc.

Gettext Note: this is the documentation of the new 5.x version. Go to 4.x branch if you're looking for the old 4.x version Created by Oscar Otero http

Gettext 651 Dec 29, 2022
Columnar analytics for PHP - a pure PHP library to read and write simple columnar files in a performant way.

Columnar Analytics (in pure PHP) On GitHub: https://github.com/envoymediagroup/columna About the project What does it do? This library allows you to w

Envoy Media Group 2 Sep 26, 2022
:date: The VObject library for PHP allows you to easily parse and manipulate iCalendar and vCard objects

sabre/vobject The VObject library allows you to easily parse and manipulate iCalendar and vCard objects using PHP. The goal of the VObject library is

sabre.io 532 Dec 25, 2022
Small convention based CQRS library for PHP

LiteCQRS for PHP Small naming-convention based CQRS library for PHP (loosely based on LiteCQRS for C#) that relies on the MessageBus, Command, EventSo

Benjamin Eberlei 560 Nov 20, 2022
Experimental library for forking PHP

Spork: PHP on a Fork <?php $manager = new Spork\ProcessManager(); $manager->fork(function() { // do something in another process! return 'Hel

Kris Wallsmith 588 Nov 20, 2022
Collection pipeline library for PHP

Knapsack Collection pipeline library for PHP Knapsack is a collection library for PHP >= 5.6 that implements most of the sequence operations proposed

Dušan Kasan 540 Dec 17, 2022
A PHP library to play with the Raspberry PI's GPIO pins

php-gpio php-gpio is a simple PHP library to play with the Raspberry PI's GPIO pins. It provides simple tools such as reading & writing to pins. [UPDA

Ronan Guilloux 266 Oct 17, 2022
iOS passbook library for PHP 5.4+

PHP PASSBOOK LIBRARY What is Passbook? Passbook is an application in iOS that allows users to store coupons, boarding passes, event tickets, store car

Eymen Gunay 256 Nov 17, 2022
A framework agnostic PHP library to build chat bots

BotMan If you want to learn how to create reusable PHP packages yourself, take a look at my upcoming PHP Package Development video course. About BotMa

BotMan 5.8k Jan 1, 2023
Lock library to provide serialized execution of PHP code.

Requirements | Installation | Usage | License and authors | Donations php-lock/lock This library helps executing critical code in concurrent situation

null 875 Jan 7, 2023
PHP Machine Learning library

PHP-ML - Machine Learning library for PHP Fresh approach to Machine Learning in PHP. Algorithms, Cross Validation, Neural Network, Preprocessing, Feat

Jorge Casas 204 Dec 27, 2022
A framework agnostic, multi-gateway payment processing library for PHP 5.6+

Omnipay An easy to use, consistent payment processing library for PHP Omnipay is a payment processing library for PHP. It has been designed based on i

The League of Extraordinary Packages 5.7k Dec 30, 2022