Skip to content

Instantly share code, notes, and snippets.

@wqweto
Last active December 14, 2023 11:00

Revisions

  1. wqweto revised this gist May 10, 2013. 1 changed file with 43 additions and 21 deletions.
    64 changes: 43 additions & 21 deletions check_vat.php
    Original file line number Diff line number Diff line change
    @@ -1,41 +1,63 @@
    <?php

    header('Content-type: application/json; charset=utf8');
    $vatno = str_replace(array(' ', '.', '-', ',', ', ', '"'), '', $_GET['vatno']);
    echo serviceCheckVat($vatno);
    $vatno = str_replace(array(' ', '.', '-', ',', '"'), '', $_GET['vatno']);
    echo serviceCheckVat($vatno, &$name, &$address, &$error);
    /*
    include "connect.php";
    mysql_query("SET NAMES utf8");
    function serviceCheckVat($vatno) {
    if (strlen($vatno) <= 2)
    return '{ "success": 0, "error": "invalid argument: vatno="' . $vatno . '" }';

    $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)
    return '{ "success": 0, "error": "web service at ec.europa.eu unavailable" }';

    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.'
    );
    return '{ "success": 0, "error": "' . $faults[$e->faultstring] . '" }';
    $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;
    }
    if (!$response->valid)
    return '{ "success": 0, "error": "vat number " . $vatno . " is invalid" }';

    $retval = "{\n \"success\": 1";
    foreach ($response as $key => $prop) {
    $retval .= ",\n \"" . $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 . '" }';
    }
    ?>
  2. wqweto revised this gist May 10, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion check_vat.php
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@

    function serviceCheckVat($vatno) {
    if (strlen($vatno) <= 2)
    return '{ "success": 0, "error": "invalid argument: vatno=" . $vatno }';
    return '{ "success": 0, "error": "invalid argument: vatno="' . $vatno . '" }';

    $client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");
    if(!$client)
  3. wqweto created this gist May 10, 2013.
    41 changes: 41 additions & 0 deletions check_vat.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    <?php

    header('Content-type: application/json; charset=utf8');
    $vatno = str_replace(array(' ', '.', '-', ',', ', ', '"'), '', $_GET['vatno']);
    echo serviceCheckVat($vatno);

    function serviceCheckVat($vatno) {
    if (strlen($vatno) <= 2)
    return '{ "success": 0, "error": "invalid argument: vatno=" . $vatno }';

    $client = new SoapClient("http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");
    if(!$client)
    return '{ "success": 0, "error": "web service at ec.europa.eu unavailable" }';

    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.'
    );
    return '{ "success": 0, "error": "' . $faults[$e->faultstring] . '" }';
    }
    if (!$response->valid)
    return '{ "success": 0, "error": "vat number " . $vatno . " is invalid" }';

    $retval = "{\n \"success\": 1";
    foreach ($response as $key => $prop) {
    $retval .= ",\n \"" . $key . "\": \"" . $prop . "\"";
    }
    $retval .= "\n}";
    return $retval;
    }
    ?>