├── src ├── VatBundle.php ├── Validator │ └── Constraints │ │ ├── VatNumber.php │ │ └── VatNumberValidator.php ├── DependencyInjection │ └── VatExtension.php └── Resources │ └── config │ └── services.xml ├── composer.json ├── LICENSE └── README.md /src/VatBundle.php: -------------------------------------------------------------------------------- 1 | load('services.xml'); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ibericode/vat-bundle", 3 | "description": "Bundle for using ibericode/vat in a Symfony environment", 4 | "keywords": ["vat"], 5 | "type": "symfony-bundle", 6 | "license": "MIT", 7 | "require": { 8 | "php": ">=8.1", 9 | "ibericode/vat": "^2.1", 10 | "symfony/framework-bundle": "^5.4|^6.0|^7.0", 11 | "symfony/validator": "^5.4|^6.0|^7.0" 12 | }, 13 | "require-dev": { 14 | "ext-intl": "*", 15 | "ext-soap": "*", 16 | "phpunit/phpunit": "^10.0" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "Ibericode\\Vat\\Bundle\\": "src/" 21 | } 22 | }, 23 | "autoload-dev": { 24 | "psr-4": { 25 | "Ibericode\\Vat\\Bundle\\Tests\\": "tests/" 26 | } 27 | }, 28 | "authors": [ 29 | { 30 | "name": "Danny van Kooten", 31 | "email": "danny@ibericode.com" 32 | } 33 | ], 34 | "scripts": { 35 | "check-syntax": "find . -name '*.php' -not -path './vendor/*' -print0 | xargs -0 -n1 php --define error_reporting=-1 -l" 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Danny van Kooten 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/Resources/config/services.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | %kernel.project_dir%/var/vat-rates 8 | 86400 9 | ip2c.org 10 | 11 | 12 | 13 | 14 | 15 | 16 | %ibericode_vat.rates.storage_path% 17 | %ibericode_vat.rates.ttl% 18 | 19 | 20 | %ibericode_vat.geolocator.service% 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/Validator/Constraints/VatNumberValidator.php: -------------------------------------------------------------------------------- 1 | checkExistence) { 36 | try { 37 | $valid = $this->validator->validateVatNumber($value); 38 | } catch (ViesException $e) { 39 | if ($constraint->violateOnException) { 40 | $this->context->buildViolation($constraint->exceptionMessage) 41 | ->setCode(VatNumber::VIES_EXCEPTION_ERROR_CODE) 42 | ->addViolation(); 43 | 44 | return; 45 | } 46 | 47 | // ignore VIES VAT exceptions (when the service is down) 48 | // this could mean that an unexisting VAT number passes validation, 49 | // but it's (probably) better than a hard-error 50 | $valid = true; 51 | } 52 | } else { 53 | $valid = $this->validator->validateVatNumberFormat($value); 54 | } 55 | 56 | if (false === $valid) { 57 | $this->context->buildViolation($constraint->message) 58 | ->setParameter('{{ string }}', $value) 59 | ->setCode(VatNumber::INVALID_ERROR_CODE) 60 | ->addViolation(); 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | VAT Bundle 2 | ========== 3 | 4 | [![Build Status](https://github.com/ibericode/vat-bundle/actions/workflows/php.yml/badge.svg)](https://github.com/ibericode/vat-bundle/actions/runs/5474441948) 5 | [![Latest Stable Version](https://img.shields.io/packagist/v/ibericode/vat-bundle.svg)](https://packagist.org/packages/ibericode/vat-bundle) 6 | ![PHP from Packagist](https://img.shields.io/packagist/php-v/ibericode/vat-bundle.svg) 7 | ![Total Downloads](https://img.shields.io/packagist/dt/ibericode/vat-bundle.svg) 8 | ![License](https://img.shields.io/github/license/ibericode/vat-bundle.svg) 9 | 10 | This bundle allows you to use [ibericode/vat](https://github.com/ibericode/vat) in your [Symfony](https://symfony.com/) projects. 11 | 12 | - Fetch VAT rates for any European member state from [ibericode/vat-rates](https://github.com/ibericode/vat-rates) 13 | - Validate VAT numbers (by format and existence) 14 | - Validate ISO-3316 alpha-2 country codes 15 | - Determine whether a country is part of the EU 16 | - Geo-locate IP addresses 17 | 18 | The official [VIES VAT number validation](http://ec.europa.eu/taxation_customs/vies/) SOAP API is used for validating VAT numbers. 19 | 20 | ## Installation 21 | 22 | First, install the bundle using Composer. 23 | 24 | ``` 25 | composer require ibericode/vat-bundle 26 | ``` 27 | 28 | Then, load the bundle by adding it to your `config/bundles.php` file. 29 | 30 | ```php 31 | Ibericode\Vat\Bundle\VatBundle::class => ['all' => true] 32 | ``` 33 | 34 | ## Usage 35 | 36 | Check out [ibericode/vat](https://github.com/ibericode/vat) for direct usage examples. This bundle adds service configuration & a validation constraint for VAT numbers. 37 | 38 | ### Dependency injection 39 | 40 | With this bundle enabled, you can use dependency injection to retrieve a class instance for the `Countries`, `Validator`, `Rates` or `Geolocator` classes. 41 | 42 | ```php 43 | use Ibericode\Vat\Countries; 44 | use Ibericode\Vat\Validator; 45 | use Ibericode\Vat\Rates; 46 | use Ibericode\Vat\Geolocator; 47 | 48 | class MyController 49 | { 50 | /** 51 | * Type-hint the class on your service constructors to retrieve a class instance 52 | */ 53 | public function __construct( 54 | Rates $rates, 55 | Validator $validator, 56 | Countries $countries, 57 | Geolocator $geolocator 58 | ) 59 | { 60 | $rates->getRateForCountry('NL'); // 21.00 61 | $validator->validateVatNumber('NL123456789B01'); // false 62 | $countries->isCountryCodeInEU('US') // false 63 | $geolocator->locateIpAddress('8.8.8.8'); // US 64 | } 65 | } 66 | ``` 67 | 68 | ### Validation 69 | 70 | To validate a VAT number using Symfony's [Validation](https://symfony.com/doc/current/validation.html) component, you can use the `VatNumber` constraint. 71 | 72 | ```php 73 | use Ibericode\Vat\Bundle\Validator\Constraints\VatNumber; 74 | 75 | class Customer 76 | { 77 | /** 78 | * @VatNumber() 79 | */ 80 | public $vatNumber; 81 | } 82 | ``` 83 | 84 | ## License 85 | 86 | MIT licensed. See the [LICENSE](LICENSE) file for details. 87 | --------------------------------------------------------------------------------