VAT number validation with VIES
Posted by Kim | Posted in Coding, Drupal, PHP | Posted on 15-03-2010
17
For a couple of our clients we implemented a webshop with Drupal and Ubercart. One feature which was missing from the default Ubercart setup, was a VAT number field, and the accompanying validation.
After some googling I found out about the VAT number module . This module adds a VAT field to the checkout billing fieldset, depending on which country you have selected. Because only european corporate visitors should be able to fill in a VAT number, it’s useless for other countries and can be hidden.
Now there are 2 ways of validating a VAT number:
1. Through a fixed preg-match scheme, which validates the format of the VAT number depending on the country. This method ensures you have a valid formatted VAT number but it does not validate if this VAT number belongs to an actual company. Hence method 2.
2. You can also validate a VAT number through the European VAT validation service, in short “VIES”. They allow for a developer to do a Soap call to their service, providing the country code and VAT number. The Drupal module already provides this Soap call, but there has been an update to the VIES services, and the endpoint url of the Soap service has moved.
So to get your VAT checker up and running again, here’s The Fix:
Just replace the existing endpoint by the new one:
$client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/api/checkVatPort?wsdl");
By
$client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/services/checkVatService.wsdl");
For a full implementation on how to do a VAT validation through SOAP:
function hale_validate_vat($args = array()) { if ( '' != $args['vatnumber'] ) { $vat_number = str_replace(array(' ', '.', '-', ',', ', '), '', $args['vatnumber']); $countryCode = substr($vat_number, 0, 2); $vatNumber = substr($vat_number, 2); if ( strlen($countryCode) != 2 || is_numeric(substr($countryCode, 0, 1)) || is_numeric(substr($countryCode, 1, 2)) ) { $error = array('result' => false, 'message' => 'Your VAT Number syntax is not correct. You should have something like this: BE805670816B01'); return $error; } if ( $args['country'] != $countryCode ) { $error = array('result' => false, 'message' => 'Your VAT Number is not valid for the selected country.'); return $error; } $client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/services/checkVatService.wsdl"); $params = array('countryCode' => $countryCode, 'vatNumber' => $vatNumber); $result = $client->checkVat($params); if ( !$result->valid ) { $error = array('result' => false, 'message' => sprintf('Invalid VAT Number. Check the validity on the customer VAT Number via <a href="%s">Europa VAT Number validation webservice</a>', 'http://ec.europa.eu/taxation_customs/vies/lang.do?fromWhichPage=vieshome')); return $error; } else { return true; } } return false; }
Use it like this:
$result = hale_validate_vat(array(‘vatnumber’ => ‘BE0123456789′, ‘country’ => ‘BE’));
Where the result is either TRUE or contains the error message in $result['message']
Update: Like promised in the comments below I have created a txt file with the code of this post and some comments. Download it here.
Greets,
Kim



Great! Thanks for this function
Hello,
I’ve tried this function and I have this error message
Warning: Unexpected character in input: ”’ (ASCII=39) state=1 in /home/www/2c73df02618c862f044b14225b689028/web/check_vat.php on line 34
Could you help me
Thx
@Pierrot
Pierrot, have you copied the code above and replaced all single quotes and double quotes with their correct version (numbers 3 / 4) ?
It can be that by copy/pasting the above code, the quoting has been messed up.
Just do a text-replace on ` , ´ and ” and replace those with either a ‘ or a ” depending on the situation.
I hope that fixes the problem, otherwise I’ll attach the code in TXT format to the post for easier download.
Greets,
Kim
Even in comments the quoting gets messed up… but I hope you understand my point!
@Kim
Thank you for your answer.
I’ve replace single quote by double quote but it doesn’t work
Please can you upload a text file with correct syntax
Thank you in advance
Please review the post for the download link…
Greets!
Hello,
Thank you for this function.
I don’t see anywhere a require to the nosoap class.
How does that works?
I get following error:
Fatal error: Cannot instantiate non-existent class: soapclient
My nosoap class doesn’t have any checkVat method.
After I add the line
require_once(‘nusoap.php’);
I get following error:
Call to undefined method: soapclient_nusoap->checkvat()
It seems I´m in the dark…
Greets
Thanks for the info, really helped me out to do this quick. I wrote a validator for using it with Agavi PHP5 Framwork in case anybody is interested in the future: http://pastebin.com/Kh465Dwg
Thanks for your input Luis! We’ll surely use your validator class in the future!
The code can use more exception handling for errors like: Fatal error: Uncaught SoapFault exception: [soapenv:Server] { ‘MS_UNAVAILABLE’ }
Thanks for the info!!!
Thanks for this script, working perfectly.
Do you konw how to handle error messages like {MS_UNAVAILABLE} (‘Pro Backup’ talked about it) ?
Thanks in advance.
Thanks for this script but when I test it or more simple one with this webservice I have an error like :
Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn’t load from ‘http://ec.europa.eu/taxation_customs/vies/services/checkVatService.wsdl‘ in /var/www/regie/www/tva/index.php:18 Stack trace: #0 /var/www/regie/www/tva/index.php(18): SoapClient->SoapClient(‘http://ec.europ…‘) #1 /var/www/regie/www/tva/index.php(33): hale_validate_vat(Array) #2 {main} thrown in /var/www/regie/www/tva/index.php on line 18
Someone have an idea ?
Thanks,
Dear Kim (and other readers),
Although untill recently this worked without any problems, for about a week now I get a 404 not found message when using this functionality.
There is a similar WSDL file (which is also referenced in the FAQ (http://ec.europa.eu/taxation_customs/vies/lang.do?fromWhichPage=faqvies&selectedLanguage=EN#item16) however this does not work with this script or any generic SOAP Client tester (for example, try it on http://www.soapclient.com).
Does anybody have more information on this?
Thanks a lot!
Dennis
I have the same problem:
Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn’t load from ‘http://ec.europa.eu/taxation_customs/vies/services/checkVatService.wsdl’ in
Any solution?
Thank you so much.
OK, I have solved it by changing URL to new one:
http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl
It solved the problem to me.
Mass VAT number Validation
http://vatnumber.info/en/vies_mass_vat_numbers_validation.html
For many it can be very helpful.