VAT validation not working for Greek customers - MagentosteemCreated with Sketch.

in #magento8 years ago (edited)

The problem

Last week I noted that Greek customers that entered a valid VAT number on checkout in my Magento store (1.9.x) where not automatically added to the Valid Vat-number customer group and still had to pay tax.

The cause

Magento uses VIES (http://ec.europa.eu/taxation_customs/vies/) to validate VAT number for you. In the case of the failure in validing Greek VAT numbers I found out there were actually a number of problems.

Problem 1: country code

Magento uses the country code GR for Greece, but VIES uses EL. Therefore all VAT numbers with country code GR are automatically invalid. Changing the country code to EL temporarily will fix this.

Problem 2: EU country validation

Magento checks if a country is in the EU (European Union) to see if a VAT number should be validated. After changing the country code from GR to EL it is no longer recognized as a EU country and Magento does not even attempt to validate the VAT number on VIES.

Problem 3: API instability or rate limit

Magento validates the VAT number on every step of the checkout process. After adding some extra log lines I found out that when I fixed the previous two problems the first attempt at validation succeeded but subsequent ones failes. This problem did not occur for other countries and was reliably reproducible. This has lead me to conclude that there is some sort of rate limiting implemented in validating a Greek VAT number. I was not able to find any information on the internet about this though, so if anybody knows more please let me know.

The solution

Problem 1

Create your own extension. If you don't know how to do this please leave a comment and I'll explain. In the config.xml of your extension add the following tags to override the Mage_Customer_Helper_Data model

<global>
    <helpers>
        <customer>
            <rewrite>
                <data>Company_Extension_Helper_Customer_Data</data>
            </rewrite>
        </customer>
    </helpers>
</global>

Then add a helper model in app/code/local/Company/Extension/Helper/Customer/Data.php. The file looks like this

<?php

class Company_Extension_Helper_Customer_Data extends Mage_Customer_Helper_Data
{

    public function checkVatNumber($countryCode, $vatNumber, $requesterCountryCode = '', $requesterVatNumber = '')
    {

        if ($countryCode == 'GR') {
            $countryCode = 'EL';
        }

        return parent::checkVatNumber($countryCode, $vatNumber, $requesterCountryCode, $requesterVatNumber);

    }

}

As you can see it simply checks if the country code is GR and then changes it to EL before running the default Magento validation code.

Problem 2

After changing the country code to EL Magento does no longer recognize Greece as a EU country and will not attempt to validate the VAT number. This check is performed in app/code/Core/Mage/Core/Helper/Data.php so the update it we need to also extend the core helper. To do this add the following line to the helpers section of the config.xml of your custom extension

<core>
    <rewrite>
        <data>Company_Extension_Helper_Core_Data</data>
    </rewrite>
</core>

The total global section will now look like this

<global>
    <helpers>
        <customer>
            <rewrite>
                <data>Company_Extension_Helper_Customer_Data</data>
            </rewrite>
        </customer>
        <core>
            <rewrite>
                <data>Company_Extension_Helper_Core_Data</data>
            </rewrite>
        </core>
    </helpers>
</global>

Next we need to add the model to extend the core helper. To do this create a file pp/code/local/Company/Extension/Helper/Core/Data.php. The content of the file is

<?php

class Company_Extension_Helper_Core_Data extends Mage_Core_Helper_Data
{

    public function isCountryInEU($countryCode, $storeId = null)
    {
        if ($countryCode == 'EL') {
            $countryCode = 'GR';
        }
        return parent::isCountryInEU($countryCode, $storeId);
    }

}

Basically you're undoing the previous change so Greece is recognized as a EU country again.

Problem 3

I have not implemented a problem for this yet, but I've been thinking about changing the code so the VAT number will only be checked if it's changed. If anybody is interested please let me know and I'll write up the solution.

Coin Marketplace

STEEM 0.13
TRX 0.33
JST 0.034
BTC 111107.86
ETH 4299.29
SBD 0.83