├── .gitignore ├── LICENSE ├── README.md ├── example.php └── vatValidation.class.php /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | .settings 3 | .buildpath -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2011, Edwin Hermans 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vat Validation # 2 | 3 | ## About ## 4 | Vat-Validation is a PHP class allowing you to 5 | 6 | - Validate a VAT number 7 | - Retrieve information like the name or the address of the company 8 | 9 | The data is extracted from a European Commission webservice 10 | 11 | __It actually only works for European countries__ 12 | 13 | ## Usage ## 14 | 15 | require_once('vatValidation.class.php'); 16 | $vatValidation = new vatValidation( array('debug' => false)); 17 | $vatValidation->check($countryCode, $vatNumber)); 18 | 19 | Your instance can now access the following methods: 20 | 21 | $vatValidation->isValid() 22 | $vatValidation->getName() 23 | $vatValidation->getDenomination() 24 | $vatValidation->getAddress() 25 | 26 | You'll find an example in the example.php file 27 | 28 | ## Requirements ## 29 | 30 | PHP with Soap enabled 31 | 32 | ## Disclaimer ## 33 | 34 | Take a look at http://ec.europa.eu/taxation_customs/vies/viesdisc.do to know when/how you're allowed to use this service and his information -------------------------------------------------------------------------------- /example.php: -------------------------------------------------------------------------------- 1 | false)); 4 | 5 | 6 | if($vatValidation->check('BE', '0828639227')) { 7 | echo '

valid one!

'; 8 | echo 'denomination: ' . $vatValidation->getDenomination(). '
'; 9 | echo 'name: ' . $vatValidation->getName(). '
'; 10 | echo 'address: ' . $vatValidation->getAddress(). '
'; 11 | } else { 12 | echo '

Invalid VAT

'; 13 | } 14 | -------------------------------------------------------------------------------- /vatValidation.class.php: -------------------------------------------------------------------------------- 1 | false, 12 | ); 13 | 14 | private $_valid = false; 15 | private $_data = array(); 16 | 17 | public function __construct($options = array()) { 18 | 19 | foreach($options as $option => $value) { 20 | $this->_options[$option] = $value; 21 | } 22 | 23 | if(!class_exists('SoapClient')) { 24 | throw new Exception('The Soap library has to be installed and enabled'); 25 | } 26 | 27 | try { 28 | $this->_client = new SoapClient(self::WSDL, array('trace' => true) ); 29 | } catch(Exception $e) { 30 | $this->trace('Vat Translation Error', $e->getMessage()); 31 | } 32 | } 33 | 34 | public function check($countryCode, $vatNumber) { 35 | 36 | $rs = $this->_client->checkVat( array('countryCode' => $countryCode, 'vatNumber' => $vatNumber) ); 37 | 38 | if($this->isDebug()) { 39 | $this->trace('Web Service result', $this->_client->__getLastResponse()); 40 | } 41 | 42 | if($rs->valid) { 43 | $this->_valid = true; 44 | $name_arr = explode(" ", $rs->name, 2); 45 | if (count($name_arr) > 1) { 46 | list($denomination,$name) = $name_arr; 47 | } else { 48 | $denomination = $name_arr[0]; 49 | $name = ""; 50 | } 51 | $this->_data = array( 52 | 'denomination' => $denomination, 53 | 'name' => $this->cleanUpString($name), 54 | 'address' => $this->cleanUpString($rs->address), 55 | ); 56 | return true; 57 | } else { 58 | $this->_valid = false; 59 | $this->_data = array(); 60 | return false; 61 | } 62 | } 63 | 64 | public function isValid() { 65 | return $this->_valid; 66 | } 67 | 68 | public function getDenomination() { 69 | return $this->_data['denomination']; 70 | } 71 | 72 | public function getName() { 73 | return $this->_data['name']; 74 | } 75 | 76 | public function getAddress() { 77 | return $this->_data['address']; 78 | } 79 | 80 | public function isDebug() { 81 | return ($this->_options['debug'] === true); 82 | } 83 | private function trace($title,$body) { 84 | echo '

TRACE: '.$title.'

'. htmlentities($body).'
'; 85 | } 86 | private function cleanUpString($string) { 87 | for($i=0;$i<100;$i++) 88 | { 89 | $newString = str_replace(" "," ",$string); 90 | if($newString === $string) { 91 | break; 92 | } else { 93 | $string = $newString; 94 | } 95 | } 96 | 97 | $newString = ""; 98 | $words = explode(" ",$string); 99 | foreach($words as $k=>$w) 100 | { 101 | $newString .= ucfirst(strtolower($w))." "; 102 | } 103 | return $newString; 104 | } 105 | } 106 | 107 | ?> 108 | --------------------------------------------------------------------------------