Skip to content

Instantly share code, notes, and snippets.

@brunogaspar brunogaspar/README.md
Last active Sep 16, 2016

Embed
What would you like to do?
PHP Vat Validator
$vat = new Vat('PT', 123456789);

var_dump($vat->isValid());
var_dump($vat->getName());
var_dump($vat->getAddress());
<?php
class Vat
{
/**
* The WSDL url to perform the validation.
*
* @var string
*/
protected $wsdl = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
/**
* List of valid european countries.
*
* @var array
*/
protected $validCountries = [
'AT', 'BE', 'BG', 'CY', 'CZ', 'DE', 'DK',
'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HU',
'IE', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL',
'PL', 'PT', 'RO', 'SE', 'SI', 'SK'
];
/**
* Constructor.
*
* @return void
*/
public function __construct(string $countryCode, int $vatNumber)
{
if(! class_exists('SoapClient')) {
throw new \Exception('The Soap library has to be installed and enabled');
}
if (! in_array($countryCode, $this->validCountries)) {
throw new \RuntimeException('Invalid country provided!');
}
$this->response = (new \SoapClient($this->wsdl))->checkVat(compact('countryCode', 'vatNumber'));
}
/**
* Determines if the vat number is valid.
*
* @return bool
*/
public function isValid()
{
return (bool) $this->response->valid;
}
/**
* Returns the name associated with the vat number.
*
* @return string
*/
public function getName()
{
return $this->response->name;
}
/**
* Returns the address associated with the vat number.
*
* @return string
*/
public function getAddress()
{
return $this->response->address;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.