Skip to content

Commit 0d63b2e

Browse files
jlsdohmenmoonen-packaging
authored andcommitted
Resolve issue DragonBe#143: process excluded countries correctly for all implementations
1 parent 73baac6 commit 0d63b2e

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

src/Vies/Vies.php

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Vies
5151
const VIES_PORT = 443;
5252
const VIES_WSDL = '/taxation_customs/vies/checkVatService.wsdl';
5353
const VIES_TEST_WSDL = '/taxation_customs/vies/checkVatTestService.wsdl';
54-
const VIES_EU_COUNTRY_TOTAL = 29;
54+
const VIES_EU_COUNTRY_TOTAL = 28;
5555
const VIES_TEST_VAT_NRS = [100, 200, 201, 202, 300, 301, 302, 400, 401, 500, 501, 600, 601];
5656

5757
protected const VIES_EU_COUNTRY_LIST = [
@@ -294,7 +294,7 @@ public function validateVat(
294294
string $traderCity = ''
295295
): CheckVatResponse {
296296

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

@@ -336,7 +336,7 @@ public function validateVat(
336336
$this->addOptionalArguments($requestParams, 'traderCity', $traderCity);
337337

338338
if ($requesterCountryCode && $requesterVatNumber) {
339-
if (! isset(self::VIES_EU_COUNTRY_LIST[$requesterCountryCode])) {
339+
if ($this->validateCountryCode($requesterCountryCode) === false) {
340340
throw new ViesException(sprintf('Invalid requestor country code "%s" provided', $requesterCountryCode));
341341
}
342342
$requesterVatNumber = self::filterVat($requesterVatNumber);
@@ -378,7 +378,7 @@ public function validateVat(
378378
*/
379379
public function validateVatSum(string $countryCode, string $vatNumber): bool
380380
{
381-
if (! isset(self::VIES_EU_COUNTRY_LIST[$countryCode])) {
381+
if ($this->validateCountryCode($countryCode, true) === false) {
382382
throw new ViesException(sprintf('Invalid country code "%s" provided', $countryCode));
383383
}
384384
$className = self::VIES_EU_COUNTRY_LIST[$countryCode]['validator'];
@@ -427,6 +427,9 @@ public static function listEuropeanCountries(): array
427427
array_column(self::VIES_EU_COUNTRY_LIST, 'name')
428428
);
429429
unset($list['EU']);
430+
foreach (array_keys(self::VIES_EXCLUDED_COUNTRY_CODES) as $excludedCountryCode) {
431+
unset($list[$excludedCountryCode]);
432+
}
430433
}
431434

432435
return $list;
@@ -484,7 +487,14 @@ private function validateArgument(string $argumentValue): bool
484487
return true;
485488
}
486489

487-
private function validateTestVat($countryCode, $testVatNumber): CheckVatResponse
490+
/**
491+
* @param string $countryCode
492+
* @param string $testVatNumber
493+
*
494+
* @return CheckVatResponse
495+
* @throws ViesServiceException
496+
*/
497+
private function validateTestVat(string $countryCode, string $testVatNumber): CheckVatResponse
488498
{
489499
$wsdlUri = sprintf('%s://%s%s', self::VIES_PROTO, self::VIES_DOMAIN, self::VIES_TEST_WSDL);
490500
$this->setWsdl($wsdlUri);
@@ -509,4 +519,22 @@ private function validateTestVat($countryCode, $testVatNumber): CheckVatResponse
509519
throw new ViesServiceException($message, 0, $e);
510520
}
511521
}
522+
523+
/**
524+
* @param string $countryCode
525+
* @param bool $useExcludedCountries
526+
*
527+
* @return bool
528+
*/
529+
private function validateCountryCode(string $countryCode, bool $useExcludedCountries = false): bool
530+
{
531+
if (! isset(self::VIES_EU_COUNTRY_LIST[$countryCode])) {
532+
return false;
533+
}
534+
if ($useExcludedCountries === false && isset(self::VIES_EXCLUDED_COUNTRY_CODES[$countryCode])) {
535+
return false;
536+
}
537+
538+
return true;
539+
}
512540
}

0 commit comments

Comments
 (0)