From 0d63b2efb38d12009637ad7a4a54fb70b54fc74a Mon Sep 17 00:00:00 2001 From: Jules Dohmen Date: Wed, 19 Jan 2022 13:38:23 +0100 Subject: [PATCH] Resolve issue #143: process excluded countries correctly for all implementations --- src/Vies/Vies.php | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/Vies/Vies.php b/src/Vies/Vies.php index e445c04..3a4b7b9 100644 --- a/src/Vies/Vies.php +++ b/src/Vies/Vies.php @@ -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 = [ @@ -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)); } @@ -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); @@ -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']; @@ -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; @@ -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); @@ -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; + } }