Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
function is_valid_regex($vat,$country){
// current regex function
return true;
}
function validate_vat($vat,$country){
if(is_valid_regex($vat,$country)) {
$iso_code_2_data = array(
'AT' => 'AT', //Austria
'BE' => 'BE', //Belgium
'BG' => 'BG', //Bulgaria
'DK' => 'DK', //Denmark
'FI' => 'FI', //Finland
'FR' => 'FR', //France
'FX' => 'FR', //France métropolitaine
'DE' => 'DE', //Germany
'GR' => 'EL', //Greece
'IE' => 'IE', //Irland
'IT' => 'IT', //Italy
'LU' => 'LU', //Luxembourg
'NL' => 'NL', //Netherlands
'PT' => 'PT', //Portugal
'ES' => 'ES', //Spain
'SE' => 'SE', //Sweden
'GB' => 'GB', //United Kingdom
'CY' => 'CY', //Cyprus
'EE' => 'EE', //Estonia
'HU' => 'HU', //Hungary
'LV' => 'LV', //Latvia
'LT' => 'LT', //Lithuania
'MT' => 'MT', //Malta
'PL' => 'PL', //Poland
'RO' => 'RO', //Romania
'SK' => 'SK', //Slovakia
'CZ' => 'CZ', //Czech Republic
'SI' => 'SI' //Slovania
);
if (array_search(substr($vat, 0, 2), $iso_code_2_data)) {
$vat = str_replace(' ','', substr($vat, 2));
}
if(class_exists('SoapClient')) {
$remoteCheckVatServiceWSDL = "http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl";
$localCheckVatServiceWSDL = false;
if(function_exists('wp_upload_dir')){
$localCheckVatServiceWSDL = wp_upload_dir() . '/taxation_customs/vies/checkVatService.wsdl';
if(!is_file($localCheckVatServiceWSDL)){
$current = file_get_contents($remoteCheckVatServiceWSDL);
file_put_contents($localCheckVatServiceWSDL, $current);
}
}
if(!is_file($localCheckVatServiceWSDL)) {
$localCheckVatServiceWSDL = $remoteCheckVatServiceWSDL;
}
$client = new \SoapClient( $localCheckVatServiceWSDL );
$result = $client->checkVat( [
'countryCode' => $country ,
'vatNumber' => $vat,
] );
return $result->valid;
} else {
if (array_key_exists($country, $iso_code_2_data)) {
$response = file_get_contents('http://ec.europa.eu/taxation_customs/vies/viesquer.do?ms=' . $iso_code_2_data[$country] . '&iso=' . $iso_code_2_data[$country] . '&vat=' . $vat);
if (preg_match('/\bvalid VAT number\b/i', $response)) {
return true;
}
if (preg_match('/\binvalid VAT number\b/i', $response)) {
return false;
}
}
}
}
return false;
}
$vat = 'GB864438791';
$country = 'GB';
var_dump( validate_vat($vat,$country));die;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment