DragonBe/vies

View on GitHub
bin/vatcheck

Summary

Maintainability
Test Coverage
#!/usr/bin/env php
<?php
use DragonBe\Vies\Vies;
use DragonBe\Vies\ViesException;
use DragonBe\Vies\ViesServiceException;

require_once __DIR__ . '/../vendor/autoload.php';

if (2 !== $argc) {
    echo 'Usage: ' . $argv[0] . ' <VATID>' . PHP_EOL;
    exit(1);
}

$vies = new Vies();
if (false === $vies->getHeartBeat()->isAlive()) {
    echo 'EU VIES Service not available at this moment, please try again later' . PHP_EOL;
    exit(2);
}

$vatId = $vies->splitVatId($argv[1]);

$result = null;
try {
    $result = $vies->validateVat($vatId['country'], $vatId['id']);
} catch (ViesServiceException $vse) {
    echo $vse->getMessage() . PHP_EOL;
    exit(3);
} catch (ViesException $ve) {
    echo $ve->getMessage() . PHP_EOL;
    exit(4);
}

echo sprintf(
        'VAT ID %s %s is %s',
        $result->getCountryCode(),
        $result->getVatNumber(),
        $result->isValid() ? 'VALID' : 'NOT VALID'
    ) . PHP_EOL;