This Laravel package enables the formatting and validation of (EU) VAT numbers.
For validation it uses the endpoints of the European Commission's VAT Information Exchange Service (VIES)
Via Composer
$ composer require eventix/eu-vat
When the package is included in a Laravel project, composer autoload functionality has automatically discovered a Service Provider. This will extend the Laravel Validator with a validation rule (vat_number).
Also an alias for a Facade is registered. This facade (EuVat) will enable manual formatting and validation for vat numbers.
EuVat::codes(): array; // List of supported country codes
EuVat::supports(string $countryCode): bool; // Determines if a given country code is supported
EuVat::name(string $countryCode): ?string; // Returns the (english) name associated with a country code if it is supported)
EuVat::inferCountry(string $vatNumber): ?string; // (Try to) guess the country of a vat number.
EuVat::format(string $vatNumber, ?string $countryCode = null): ?string // (Try to) format a vat number by the formatting rules of a given country, or a guessed country
EuVat::validate(string $vatNumber, ?string $countryCode = null): ?string // (Try to) validate a vat number by the formatting rules of a given country, or a guessed country
Validates the vat number by inferring its country
$data = [
'vat_nr' => 'NL123456789B01',
];
$validator = Validator::make($data, [
'vat_nr' => 'required|vat_number',
]);
Validates the vat number for a given country
$data = [
'vat_nr' => 'NL123456789B01',
];
$validator = Validator::make($data, [
'vat_nr' => 'required|vat_number:NL',
]);
Validates the vat number for a country determined by another field
$data = [
'country' => 'NL',
'vat_nr' => 'NL123456789B01',
];
$validator = Validator::make($data, [
'vat_nr' => 'required|vat_number:country',
]);
Validation ONLY when the vat number changes
If the value does not change... it should already be valid. This will reduce the calls to VIES. Note: The validation rule needs the original value for this to work.
Validates a changed vat number for an inferred country
$data = [
'vat_nr' => 'NL123456789B01',
];
$validator = Validator::make($data, [
'vat_nr' => 'required|vat_number:NULL,NL123456789B01',
]);
Validates a changed vat number for a country determined by another field
$data = [
'country' => 'NL',
'vat_nr' => 'NL123456789B01',
];
$validator = Validator::make($data, [
'vat_nr' => 'required|vat_number:country,NL123456789B01',
]);
Validates a changed vat number for a given country
$data = [
'vat_nr' => 'NL123456789B01',
];
$validator = Validator::make($data, [
'vat_nr' => 'required|vat_number:NL,NL123456789B01',
]);
Note, for testing the project needs to be cloned and all dependencies installed first.
$ cd /packages/directory
$ git clone [email protected]:Eventix/eu-vat.git
$ composer install
$ composer test
Please see License File.
No liability, implementor is responsible for reviewing code!
Please open an issue.
Please contribute using Github Flow. Fork the project, create a branch, add commits, and open a pull request.
- Test coverage is not a 100% yet.
Potentials
- Opportunities for localized country names
- Opportunities for 3 char country codes
- Opportunities for country/ies outside the EU