Skip to content

Commit

Permalink
Merge branch 'julesdohmen-issue-143-use-of-vies-excluded-country-codes'
Browse files Browse the repository at this point in the history
  • Loading branch information
DragonBe committed Apr 13, 2022
2 parents 73baac6 + 0d63b2e commit c7a1c44
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/Vies/Vies.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Vies
const VIES_PORT = 443;
const VIES_WSDL = '/taxation_customs/vies/checkVatService.wsdl';
const VIES_TEST_WSDL = '/taxation_customs/vies/checkVatTestService.wsdl';
const VIES_EU_COUNTRY_TOTAL = 29;
const VIES_EU_COUNTRY_TOTAL = 28;
const VIES_TEST_VAT_NRS = [100, 200, 201, 202, 300, 301, 302, 400, 401, 500, 501, 600, 601];

protected const VIES_EU_COUNTRY_LIST = [
Expand Down Expand Up @@ -294,7 +294,7 @@ public function validateVat(
string $traderCity = ''
): CheckVatResponse {

if (! isset(self::VIES_EU_COUNTRY_LIST[$countryCode])) {
if ($this->validateCountryCode($countryCode, true) === false) {
throw new ViesException(sprintf('Invalid country code "%s" provided', $countryCode));
}

Expand Down Expand Up @@ -336,7 +336,7 @@ public function validateVat(
$this->addOptionalArguments($requestParams, 'traderCity', $traderCity);

if ($requesterCountryCode && $requesterVatNumber) {
if (! isset(self::VIES_EU_COUNTRY_LIST[$requesterCountryCode])) {
if ($this->validateCountryCode($requesterCountryCode) === false) {
throw new ViesException(sprintf('Invalid requestor country code "%s" provided', $requesterCountryCode));
}
$requesterVatNumber = self::filterVat($requesterVatNumber);
Expand Down Expand Up @@ -378,7 +378,7 @@ public function validateVat(
*/
public function validateVatSum(string $countryCode, string $vatNumber): bool
{
if (! isset(self::VIES_EU_COUNTRY_LIST[$countryCode])) {
if ($this->validateCountryCode($countryCode, true) === false) {
throw new ViesException(sprintf('Invalid country code "%s" provided', $countryCode));
}
$className = self::VIES_EU_COUNTRY_LIST[$countryCode]['validator'];
Expand Down Expand Up @@ -427,6 +427,9 @@ public static function listEuropeanCountries(): array
array_column(self::VIES_EU_COUNTRY_LIST, 'name')
);
unset($list['EU']);
foreach (array_keys(self::VIES_EXCLUDED_COUNTRY_CODES) as $excludedCountryCode) {
unset($list[$excludedCountryCode]);
}
}

return $list;
Expand Down Expand Up @@ -484,7 +487,14 @@ private function validateArgument(string $argumentValue): bool
return true;
}

private function validateTestVat($countryCode, $testVatNumber): CheckVatResponse
/**
* @param string $countryCode
* @param string $testVatNumber
*
* @return CheckVatResponse
* @throws ViesServiceException
*/
private function validateTestVat(string $countryCode, string $testVatNumber): CheckVatResponse
{
$wsdlUri = sprintf('%s://%s%s', self::VIES_PROTO, self::VIES_DOMAIN, self::VIES_TEST_WSDL);
$this->setWsdl($wsdlUri);
Expand All @@ -509,4 +519,22 @@ private function validateTestVat($countryCode, $testVatNumber): CheckVatResponse
throw new ViesServiceException($message, 0, $e);
}
}

/**
* @param string $countryCode
* @param bool $useExcludedCountries
*
* @return bool
*/
private function validateCountryCode(string $countryCode, bool $useExcludedCountries = false): bool
{
if (! isset(self::VIES_EU_COUNTRY_LIST[$countryCode])) {
return false;
}
if ($useExcludedCountries === false && isset(self::VIES_EXCLUDED_COUNTRY_CODES[$countryCode])) {
return false;
}

return true;
}
}

0 comments on commit c7a1c44

Please sign in to comment.