Last active
December 14, 2023 11:00
-
-
Save wqweto/5553541 to your computer and use it in GitHub Desktop.
Check company VAT number using SOAP service at http://ec.europa.eu and return JSON encoded result
This file contains 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 | |
header('Content-type: application/json; charset=utf8'); | |
$vatno = str_replace(array(' ', '.', '-', ',', '"'), '', $_GET['vatno']); | |
echo serviceCheckVat($vatno, &$name, &$address, &$error); | |
/* | |
include "connect.php"; | |
mysql_query("SET NAMES utf8"); | |
$vatno = mysql_real_escape_string($_GET['vatno']); | |
$name = mysql_real_escape_string($name); | |
$address = mysql_real_escape_string(strlen($address) != 0 ? $address : $error); | |
@mysql_query("REPLACE INTO ppl_dreem_cg (TaxNo, Name, Address) SELECT '$vatno', '$name', '$address'"); | |
mysql_close(); | |
*/ | |
function serviceCheckVat($vatno, $name, $address, $error) { | |
if (strlen($vatno) <= 2) { | |
$error = "Incorrect VAT number"; | |
goto QH; | |
} | |
$client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl"); | |
if(!$client) { | |
$error = "web service at ec.europa.eu unavailable"; | |
goto QH; | |
} | |
try { | |
$response = $client->checkVat(array( | |
'countryCode' => substr($vatno, 0, 2), | |
'vatNumber' => substr($vatno, 2) | |
)); | |
} | |
catch (SoapFault $e) { | |
$faults = array ( | |
'INVALID_INPUT' => 'The provided CountryCode is invalid or the VAT number is empty', | |
'SERVICE_UNAVAILABLE' => 'The SOAP service is unavailable, try again later', | |
'MS_UNAVAILABLE' => 'The Member State service is unavailable, try again later or with another Member State', | |
'TIMEOUT' => 'The Member State service could not be reached in time, try again later or with another Member State', | |
'SERVER_BUSY' => 'The service cannot process your request. Try again later.' | |
); | |
$error = $faults[$e->faultstring]; | |
if (!is_set($error)) | |
$error = $e->faultstring; | |
goto QH; | |
} | |
if (!$response->valid) { | |
$error = "Not a valid VAT number"; | |
goto QH; | |
} | |
$retval = "{\n \"success\": 1"; | |
foreach ($response as $key => $prop) { | |
$retval .= ",\n \"" . $key . "\": \"" . str_replace('"', '\"', $prop) . "\""; | |
if ($key == 'name') | |
$name = $prop; | |
else if ($key == 'address') | |
$address = $prop; | |
} | |
$retval .= "\n}"; | |
return $retval; | |
QH: | |
return '{ "success": 0, "error": "' . $error . '" }'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment