Created
July 26, 2017 12:14
-
-
Save tarranjones/1013ac4679f89a5f2a03ffda7246ef2e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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