Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate function in vatvalidation calls checkVatNumber a lot #17658

Closed
sanderjongsma opened this issue Aug 17, 2018 · 3 comments
Closed

validate function in vatvalidation calls checkVatNumber a lot #17658

sanderjongsma opened this issue Aug 17, 2018 · 3 comments
Assignees
Labels
Component: Quote Fixed in 2.3.x The issue has been fixed in 2.3 release line help wanted Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release

Comments

@sanderjongsma
Copy link
Contributor

I discovered this issue because I use the following module: Dutchento_Vatfallback. In here I configured Vatlayer so vatnumbers within Magento are validated through their API. No I saw the calls to their API where extreme high, so I went debugging first in the module and after that in Magento itself. After a while I came across the validate function in the file magento/module-quote/Observer/Frontend/Quote/Address/VatValidator.php.

public function validate(\Magento\Quote\Model\Quote\Address $quoteAddress, $store)
    {
        $customerCountryCode = $quoteAddress->getCountryId();
        $customerVatNumber = $quoteAddress->getVatId();

        $merchantCountryCode = $this->customerVat->getMerchantCountryCode();
        $merchantVatNumber = $this->customerVat->getMerchantVatNumber();

        $validationResult = null;
        if ($this->customerAddress->hasValidateOnEachTransaction(
            $store
        ) ||
            $customerCountryCode != $quoteAddress->getValidatedCountryCode() ||
            $customerVatNumber != $quoteAddress->getValidatedVatNumber()
        ) {
            // Send request to gateway
            $validationResult = $this->customerVat->checkVatNumber(
                $customerCountryCode,
                $customerVatNumber,
                $merchantVatNumber !== '' ? $merchantCountryCode : '',
                $merchantVatNumber
            );

            // Store validation results in corresponding quote address
            $quoteAddress->setVatIsValid((int)$validationResult->getIsValid());
            $quoteAddress->setVatRequestId($validationResult->getRequestIdentifier());
            $quoteAddress->setVatRequestDate($validationResult->getRequestDate());
            $quoteAddress->setVatRequestSuccess($validationResult->getRequestSuccess());
            $quoteAddress->setValidatedVatNumber($customerVatNumber);
            $quoteAddress->setValidatedCountryCode($customerCountryCode);
            $quoteAddress->save();
        } else {
            // Restore validation results from corresponding quote address
            $validationResult = new \Magento\Framework\DataObject(
                [
                    'is_valid' => (int)$quoteAddress->getVatIsValid(),
                    'request_identifier' => (string)$quoteAddress->getVatRequestId(),
                    'request_date' => (string)$quoteAddress->getVatRequestDate(),
                    'request_success' => (bool)$quoteAddress->getVatRequestSuccess(),
                ]
            );
        }

        return $validationResult;
    }

Before calling $this->customerVat->checkVatNumber this function checks:

$customerCountryCode != $quoteAddress->getValidatedCountryCode() ||
$customerVatNumber != $quoteAddress->getValidatedVatNumber()

I expected validated addresses would be stored in de db because of this check, but after digging around, $quoteAddress->getValidatedCountryCode() and $quoteAddress->getValidatedVatNumber() are doing nothing at all, so it is expected they are quote_address attributes in the db.

But in the file magento/module-quote/Setup/InstallData.php only the following attributes are installed:

$attributes = [
            'vat_id' => ['type' => Table::TYPE_TEXT],
            'vat_is_valid' => ['type' => Table::TYPE_SMALLINT],
            'vat_request_id' => ['type' => Table::TYPE_TEXT],
            'vat_request_date' => ['type' => Table::TYPE_TEXT],
            'vat_request_success' => ['type' => Table::TYPE_SMALLINT],
        ];

but validated_country_code and validated_vat_number are missing and nowhere to be found.

When I installed the missing attributes with a custom InstallData script everything works as expected.

Preconditions

  1. Magento 2.2.4
  2. Php 7.1
  3. Mysql 5.7
  4. Docker

Steps to reproduce

To reproduce this I recommend setting up vat validation with help from the docs and enable breakpoints with xdebug within the validate function and check how often $this->customerVat->checkVatNumber is called. validate is triggered by an Observer: magento/module-quote/Observer/Frontend/Quote/Address/CollectTotalsObserver.php so it is called a lot. Try adding a product to the cart for example.

Expected result

The expected result is the check inside the validate function:

$customerCountryCode != $quoteAddress->getValidatedCountryCode() ||
$customerVatNumber != $quoteAddress->getValidatedVatNumber()

will actually do something.

Actual result

The checks are now doing nothing because the attributes in quote_address don't exist. When I install the missing attributes: validated_country_code and validated_vat_number with a custom InstallData script everything works as expected.

@sanderjongsma sanderjongsma changed the title validate function in vatvalidation is calls checkVatNumber a lot validate function in vatvalidation calls checkVatNumber a lot Aug 17, 2018
@magento-engcom-team
Copy link
Contributor

magento-engcom-team commented Aug 17, 2018

Hi @sanderjongsma. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento-engcom-team give me {$VERSION} instance

where {$VERSION} is version tags (starting from 2.2.0+) or develop branches (2.2-develop +).
For more details, please, review the Magento Contributor Assistant documentation.

@sanderjongsma do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • yes
  • no

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Aug 17, 2018
@engcom-backlog-nickolas engcom-backlog-nickolas added Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release Component: Quote labels Aug 17, 2018
@engcom-backlog-nickolas engcom-backlog-nickolas removed their assignment Aug 17, 2018
@engcom-backlog-nickolas

Hello @sanderjongsma, thank you for your report.
We've acknowledged the issue and added to our backlog.

@eduard13 eduard13 self-assigned this Oct 3, 2018
@eduard13 eduard13 removed their assignment Dec 3, 2018
@ghost ghost assigned ErikPel Mar 28, 2019
@magento-engcom-team
Copy link
Contributor

Hi @sanderjongsma. Thank you for your report.
The issue has been fixed in #19265 by @ErikPel in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.2 release.

@magento-engcom-team magento-engcom-team added the Fixed in 2.3.x The issue has been fixed in 2.3 release line label Mar 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Quote Fixed in 2.3.x The issue has been fixed in 2.3 release line help wanted Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Reproduced on 2.2.x The issue has been reproduced on latest 2.2 release Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release
Projects
None yet
Development

No branches or pull requests

5 participants