1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196:
<?php
namespace MvcCore\Ext\Forms\Validators;
class CompanyVatIdEu extends \MvcCore\Ext\Forms\Validator {
const SOAP_URL = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
const GET_URL = 'http://ec.europa.eu/taxation_customs/vies/vatResponse.html?&action=check&check=Verify&memberStateCode={0}&number={1}';
const ERROR_VAT_ID = 0;
const ERROR_TAX_ID = 1;
protected static $errorMessages = [
self::ERROR_VAT_ID => "Field '{0}' requires a valid VAT ID.",
self::ERROR_TAX_ID => "Field '{0}' requires a valid TAX ID.",
];
public function Validate ($rawSubmittedValue) {
$formLocale = $this->form->GetLocale();
if (!$formLocale) {
return $this->throwNewInvalidArgumentException(
'Unable to validate ZIP code without configured '
.'form `locale` property. Use `$form->SetLocale(\'[A-Z]{2}\');` '
.'to internally create proper ZIP code validator.'
);
} else {
$notCheckedValue = preg_replace('#^[^a-zA-Z0-9\-\. ]$#', '', mb_strtoupper(trim((string) $rawSubmittedValue)));
if (!$notCheckedValue) return NULL;
$result = $this->checkEuVatNumber($formLocale, (string) $notCheckedValue);
if (!$result)
$this->field->AddValidationError(
static::GetErrorMessage(self::ERROR_VAT_ID)
);
return $result;
}
}
protected function checkEuVatNumber ($localeCode, $notCheckedValue) {
if (class_exists('\\SoapClient')) {
$companyInfo = $this->checkEuVatNumberBySoap($localeCode, $notCheckedValue);
} else {
$companyInfo = $this->checkEuVatNumberByGet($localeCode, $notCheckedValue);
}
if (isset($companyInfo->vatNumber) && $companyInfo->vatNumber)
return $companyInfo->vatNumber;
return NULL;
}
protected function checkEuVatNumberBySoap ($localeCode, $notCheckedValue) {
try {
$client = new \SoapClient(static::SOAP_URL, ['trace' => TRUE]);
} catch(Exception $e) {
$this->field->AddValidationError('VAT number validation SOAP error.');
return NULL;
}
$response = $client->checkVat(['countryCode' => $localeCode, 'vatNumber' => $notCheckedValue]);
if ($response->valid) {
return (object) [
'countryCode' => $response->countryCode,
'vatNumber' => $response->vatNumber,
'name' => trim($response->name),
'address' => trim($response->address),
];
} else {
return NULL;
}
}
protected function checkEuVatNumberByGet ($localeCode, $notCheckedValue) {
$result = NULL;
$url = str_replace(['{0}','{1}'], [$localeCode,$notCheckedValue], static::GET_URL);
$valid = 'Yes, valid VAT number';
$response = @file_get_contents($url);
if (!$response) {
$this->field->AddValidationError('VAT number validation error response.');
return $result;
}
if (mb_strpos($response, $valid) !== FALSE) {
$tableBegin = '<table id="vatResponseFormTable">';
$tableEnd = '</table>';
$parsingError = 'VAT number validation parsing error.';
$tableBeginPos = mb_strpos($response, $tableBegin);
if ($tableBeginPos === FALSE) {
$this->field->AddValidationError($parsingError);
return $result;
}
$tableEndPos = mb_strpos($response, $tableEnd, $tableBeginPos + mb_strlen($tableBegin));
if ($tableEndPos === FALSE) {
$this->field->AddValidationError($parsingError);
return $result;
}
$table = mb_substr($response, $tableBeginPos, $tableEndPos + mb_strlen($tableEnd) - $tableBeginPos);
$tableXml = @simplexml_load_string($table);
if (!$tableXml) return $result;
$result = [];
$processing = [
'Member State' => 'countryCode',
'VAT Number' => 'vatNumber',
'Name' => 'name',
'Address' => 'address'
];
foreach ($tableXml->tr as $tr) {
$tds = $tr->td;
if (count($tds) > 1) {
$cols = array();
foreach ($tds as $td) $cols[] = trim((string) $td);
$key = $cols[0];
if (isset($processing[$cols[0]])) {
$key = $processing[$cols[0]];
} else {
continue;
}
$val = $cols[1];
$result[$key] = $key == 'vatNumber'
? str_replace(' ', '', $val)
: $val ;
}
}
if (count($processing) === count($result)) {
$result = (object) $result;
} else {
$this->field->AddValidationError($parsingError);
return NULL;
}
}
return $result;
}
}