Skip to content

Commit

Permalink
Removed EU Online Validation. VIES not available.
Browse files Browse the repository at this point in the history
Updated Interface between main module and country modules.
  • Loading branch information
gwharton committed Jan 18, 2025
1 parent 21534c2 commit 927b25e
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 198 deletions.
99 changes: 4 additions & 95 deletions Model/TaxScheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ public function getOrderValue(Quote $quote): float
/**
* Get customer group based on Validation Result and Country of customer
* @param string $customerCountryCode
* @param string|null $customerPostCode
* @param bool $taxIdValidated
* @param float $orderValue
* @param string|null $customerPostCode
* @param int|null $storeId
* @return int|null
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
Expand All @@ -128,9 +128,9 @@ public function getOrderValue(Quote $quote): float
*/
public function getCustomerGroup(
string $customerCountryCode,
?string $customerPostCode,
bool $taxIdValidated,
float $orderValue,
?string $customerPostCode,
?int $storeId
): ?int {
$merchantCountry = $this->scopeConfig->getValue(
Expand Down Expand Up @@ -250,12 +250,12 @@ public function getCustomerGroup(
* Peform validation of the VAT Number, returning a gatewayResponse object
*
* @param string $countryCode
* @param string|null $taxId
* @param string $taxId
* @return TaxIdCheckResponseInterface
*/
public function checkTaxId(
string $countryCode,
?string $taxId
string $taxId
): TaxIdCheckResponseInterface {
$taxIdCheckResponse = $this->ticrFactory->create();

Expand All @@ -276,15 +276,7 @@ public function checkTaxId(

$taxIdCheckResponse = $this->validateFormat($taxIdCheckResponse, $taxId, $countryCode);

if ($taxIdCheckResponse->getIsValid() && $this->scopeConfig->isSetFlag(
"autocustomergroup/" . self::CODE . "/validate_online",
ScopeInterface::SCOPE_STORE
)) {
$taxIdCheckResponse = $this->validateOnline($taxIdCheckResponse, $taxId, $countryCode);
}

return $taxIdCheckResponse;

}

/**
Expand Down Expand Up @@ -407,89 +399,6 @@ private function validateFormat($taxIdCheckResponse, $taxId, $countryCode): TaxI
return $taxIdCheckResponse;
}

/**
* Perform online validation of the Tax Identifier
*
* @param $taxIdCheckResponse
* @param $taxId
* @return TaxIdCheckResponseInterface
*/
private function validateOnline($taxIdCheckResponse, $taxId, $countryCode): TaxIdCheckResponseInterface
{
try {
$body = [];
$body['countryCode'] = $countryCode;
$body['vatNumber'] = $taxId;

$requesterCountryCode = $this->scopeConfig->getValue(
"autocustomergroup/" . self::CODE . "/viesregistrationcountry",
ScopeInterface::SCOPE_STORE
);
$requesterVatNumber = $this->scopeConfig->getValue(
"autocustomergroup/" . self::CODE . "/viesregistrationnumber",
ScopeInterface::SCOPE_STORE
);

if (!empty($requesterCountryCode) && !empty($requesterVatNumber)) {
$requesterVatNumber = str_replace(
[' ', '-', $this->getCountryCodeForVatNumber($requesterCountryCode)],
['', '', ''],
$requesterVatNumber
);
$body['requesterMemberStateCode'] = $requesterCountryCode;
$body['requesterNumber'] = $requesterVatNumber;
}

$client = $this->clientFactory->create();
$response = $client->send(
new Request(
"POST",
"https://ec.europa.eu/taxation_customs/vies/rest-api/check-vat-number",
[
'Content-Type' => "application/json",
'Accept' => "application/json"
],
$this->serializer->serialize($body)
)
);
$responseBody = $response->getBody();
$vatRegistration = $this->serializer->unserialize($responseBody->getContents());
if (isset($vatRegistration['actionSucceeded']) && $vatRegistration['actionSucceeded'] == false) {
$taxIdCheckResponse->setIsValid(false);
$taxIdCheckResponse->setRequestSuccess(false);
$taxIdCheckResponse->setRequestMessage(__('There was an error checking the VAT number.'));
} else {
$taxIdCheckResponse->setIsValid($vatRegistration['valid']);
$taxIdCheckResponse->setRequestSuccess(true);
$taxIdCheckResponse->setRequestDate($vatRegistration['requestDate']);
$taxIdCheckResponse->setRequestIdentifier($vatRegistration['requestIdentifier']);
if ($taxIdCheckResponse->getIsValid()) {
$taxIdCheckResponse->setRequestMessage(__('VAT Number validated with VIES.'));
} else {
$taxIdCheckResponse->setRequestMessage(__('Please enter a valid VAT number including country code.'));
}
}
} catch (BadResponseException $e) {
switch ($e->getCode()) {
case 404:
$taxIdCheckResponse->setIsValid(false);
$taxIdCheckResponse->setRequestSuccess(true);
$taxIdCheckResponse->setRequestMessage(__('Please enter a valid VAT number.'));
break;
default:
$taxIdCheckResponse->setIsValid(false);
$taxIdCheckResponse->setRequestSuccess(false);
$taxIdCheckResponse->setRequestMessage(__('There was an error checking the VAT number.'));
$this->logger->error(
"Gw/AutoCustomerGroup/Model/TaxSchemes/EuVat::checkTaxId() : EuVat Error received from " .
"VIES. " . $e->getCode()
);
break;
}
}
return $taxIdCheckResponse;
}

/**
* Returns the country code to use in the VAT number which is not always the same as the normal country code
*
Expand Down
20 changes: 2 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,12 @@
</ul>

<h2>VAT Number Verification</h2>
<ul>
<li><b>Offline Validation</b> - A simple format validation is performed.</li>
<li><b>Online Validation</b> - In addition to the offline checks above, an online validation check is performed with the EU VIES service.</li>
</ul>
<p>When the module submits requests to the VIES service for VAT number validation, it can do so in two ways, depending on whether VIES Registration Country and VIES Registration number is set or left blank.</p>
<ul>
<li>The first method is anonymous, and a basic response to the validation request is given. If you leave these two fields blank, the module will use this method.</li>
<li>The second method provides more details and includes a "proof of validation" identifier which the module will store with the VAT validation details in the Magento database after verification. You can use this as proof that you checked the status. The only issue is that you have to provide a VAT registration country and VAT registration number of a business registered for VAT in the EU. Providing your IOSS details will not work. It is up to you, if you want to enable this feature, to locate suitable details to enter here. The EU will log the request with those company details, and no doubt, the IP address of your server.</li>
</ul>
<p>EU VAT Numbers are verified by a simple format test. No online lookups are performed.</p>

<h2>Configuration Options</h2>
<ul>
<li><b>Enabled</b> - Enable/Disable this Tax Scheme.</li>
<li><b>Tax Identifier Field - Customer Prompt</b> - Displayed under the Tax Identifier field at checkout when a shipping country supported by this module is selected. Use this to include information to the user about why to include their Tax Identifier.</li>
<li><b>Validate Online</b> - Whether to validate VAT numbers with the EU VIES Service, or just perform simple format validation.</li>
<li><b>VIES Registration Country</b> - Optional. Must be valid EU country, if completed, it will be passed to the VIES with the validation request and a unique verification code will be returned and stored with the order as proof of validation.</li>
<li><b>VIES Registration Number</b> - Optional. Must be valid EU VAT number, if completed, it will be passed to the VIES with the validation request and a unique verification code will be returned and stored with the order as proof of validation.</li>
<li><b>VAT Registration Number</b> - The Scheme Registration Number for the Merchant. Supplementary functions in AutoCustomerGroup may use this, for example displaying on invoices etc.</li>
<li><b>Import VAT Threshold</b> - If the order value is above the VAT Threshold, no VAT should be charged.</li>
<li><b>Use Magento Exchange Rate</b> - To convert from EUR Threshold to Store Currency Threshold, should we use the Magento Exchange Rate, or our own.</li>
Expand All @@ -72,9 +61,4 @@
</ul>

<h2>Integration Tests</h2>
<p>To run the integration tests, it is optional, but to support all functions, you should add the VIES Registration Country and VIES Registration Number. Please add them to config-global.php.</p>
<p>Please note that the EU VIES Service does not have a sandbox for testing, so live details should be used.</p>
<ul>
<li>autocustomergroup/euvat/viesregistrationcountry</li>
<li>autocustomergroup/euvat/viesregistrationnumber</li>
</ul>
<p>No specific setup is required to run the integration tests.</p>
52 changes: 4 additions & 48 deletions Test/Integration/TaxSchemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ public function testGetCustomerGroup(
);
$result = $this->taxScheme->getCustomerGroup(
$customerCountryCode,
$customerPostCode,
$taxIdValidated,
$orderValue,
$customerPostCode,
$storeId
);
$this->assertEquals($expectedGroup, $result);
Expand Down Expand Up @@ -242,53 +242,9 @@ public function getCustomerGroupDataProvider(): array
}

/**
* @magentoConfigFixture current_store autocustomergroup/euvat/registrationnumber IE8256796U
* @magentoConfigFixture current_store autocustomergroup/euvat/environment sandbox
* @magentoConfigFixture current_store autocustomergroup/euvat/validate_online 1
* @dataProvider checkTaxIdDataProviderOnline
*/
public function testCheckTaxIdOnline(
$countryCode,
$taxId,
$isValid
): void {
$result = $this->taxScheme->checkTaxId(
$countryCode,
$taxId
);
$this->assertEquals($isValid, $result->getIsValid());
}

/**
* @return array
*/
public function checkTaxIdDataProviderOnline(): array
{
//Country code
//Tax Id
//IsValid
return [
['DE', '', false],
['PO', null, false],
['NL', '810433941B01', true], // Valid VAT
['IE', 'IE8256796U', true], // Valid VAT
['IE', 'IE3206488LH', true], // Valid VAT
['BE', 'reghewrhwh', false],
['NO', '43643634', false],
['NO', '3y534673333y', false],
['NO', 'AB6564764586587', false],
['US', 'IE8256796U', false], // Unsupported Country, despite valid VAT Number
['PO', 'th', false],
['NO', '786176152', false], // Unsupported Country
];
}

/**
* @magentoConfigFixture current_store autocustomergroup/euvat/registrationnumber IE8256796U
* @magentoConfigFixture current_store autocustomergroup/euvat/environment sandbox
* @dataProvider checkTaxIdDataProviderOffline
* @dataProvider checkTaxIdDataProvider
*/
public function testCheckTaxIdOffline(
public function testCheckTaxId(
$countryCode,
$taxId,
$isValid
Expand All @@ -303,7 +259,7 @@ public function testCheckTaxIdOffline(
/**
* @return array
*/
public function checkTaxIdDataProviderOffline(): array
public function checkTaxIdDataProvider(): array
{
//Country code
//Tax Id
Expand Down
13 changes: 1 addition & 12 deletions Test/Unit/TaxSchemeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Gw\AutoCustomerGroupEu\Test\Unit;

use GuzzleHttp\ClientFactory;
use Gw\AutoCustomerGroup\Model\TaxSchemeHelper;
use Gw\AutoCustomerGroupEu\Model\TaxScheme;
use Gw\AutoCustomerGroup\Api\Data\TaxIdCheckResponseInterfaceFactory;
use Magento\Directory\Model\CurrencyFactory;
Expand Down Expand Up @@ -56,11 +55,6 @@ class TaxSchemeTest extends TestCase
*/
private $jsonMock;

/**
* @var TaxSchemeHelper|MockObject
*/
private $helperMock;

protected function setUp(): void
{
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
Expand Down Expand Up @@ -91,19 +85,14 @@ protected function setUp(): void
->disableOriginalConstructor()
->getMock();

$this->helperMock = $this->getMockBuilder(TaxSchemeHelper::class)
->disableOriginalConstructor()
->getMock();

$this->model = new TaxScheme(
$this->scopeConfigMock,
$this->loggerMock,
$this->storeManagerMock,
$this->currencyFactoryMock,
$this->taxIdCheckResponseInterfaceFactoryMock,
$this->clientFactoryMock,
$this->jsonMock,
$this->helperMock
$this->jsonMock
);
}

Expand Down
22 changes: 0 additions & 22 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,6 @@
<field id="enabled">1</field>
</depends>
</field>
<field id="validate_online" translate="label" type="select" sortOrder="25" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Validate Online</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<depends>
<field id="enabled">1</field>
</depends>
</field>
<field id="viesregistrationcountry" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>VIES Registration Country</label>
<source_model>Magento\Directory\Model\Config\Source\Country</source_model>
<depends>
<field id="validate_online">1</field>
<field id="enabled">1</field>
</depends>
</field>
<field id="viesregistrationnumber" translate="label" type="text" sortOrder="40" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>VIES Registration Number</label>
<depends>
<field id="validate_online">1</field>
<field id="enabled">1</field>
</depends>
</field>
<field id="registrationnumber" translate="label" type="text" sortOrder="50" showInDefault="1" showInWebsite="1" showInStore="1">
<label>VAT/Scheme Registration Number</label>
<depends>
Expand Down
3 changes: 0 additions & 3 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
<euvat>
<enabled>0</enabled>
<frontendprompt>EU VAT Registered Businesses, please enter your EU VAT Number (including Country Code), so we can apply the correct rate of VAT to your order.</frontendprompt>
<validate_online>0</validate_online>
<viesregistrationcountry></viesregistrationcountry>
<viesregistrationnumber></viesregistrationnumber>
<registrationnumber></registrationnumber>
<importthreshold>150</importthreshold>
<usemagentoexchangerate>1</usemagentoexchangerate>
Expand Down

0 comments on commit 927b25e

Please sign in to comment.