kfoobar / laravel-euro-vat
A Laravel package for Euro VAT validation
Requires
- php: >=8.1
- illuminate/config: >=10.0
- illuminate/support: >=10.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
A Laravel package for validating EU VAT numbers, including local format validation and online validation against the EU VIES API.
Installation
Install the package via Composer:
composer require kfoobar/laravel-euro-vat
Publish the configuration file:
php artisan vendor:publish --provider="KFoobar\EuroVAT\EuroVATServiceProvider"
Configuration
The configuration file config/euro-vat.php allows you to set:
urls.number: Endpoint used when validating a VAT number online.urls.status: Endpoint used for the VIES status check.ttl.number: Cache lifetime in minutes for VAT validation results.ttl.status: Cache lifetime in minutes for VIES status results.
Usage
The main service exposes:
validate(string $number, ?string $country = null): ?stringvalidateOffline(string $number, ?string $country = null): ?stringvalidateOnline(string $number, string $country): ?stringstatus(?string $country = null): bool
Using the Facade
Validate a VAT number:
use KFoobar\EuroVAT\Facades\EuroVAT; EuroVAT::validate('SE123456789012'); EuroVAT::validateOffline('SE123456789012'); EuroVAT::validateOnline('123456789012', 'SE'); EuroVAT::validate('123456789012', 'SE'); EuroVAT::status('SE');
validate() is the default entrypoint and performs online validation.
validateOffline() only performs the local VAT format check. No external HTTP request is made.
validateOnline() sends the provided VAT number and country code directly to the EU VIES API and caches the response. It does not normalize or pre-validate the VAT number locally.
Using the Validation Rule
use KFoobar\EuroVAT\Rules\VatId; $request->validate([ 'vat_number' => ['required', new VatId('SE')], ]);
Using the vat_id Validator Extension
$request->validate([ 'vat_number' => 'required|vat_id:SE', ]);
Contributing
Contributions are welcome!
License
The MIT License (MIT). Please see License File for more information.