VatValidator.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Observer\Frontend\Quote\Address;
  7. class VatValidator
  8. {
  9. /**
  10. * Customer address
  11. *
  12. * @var \Magento\Customer\Helper\Address
  13. */
  14. protected $customerAddress;
  15. /**
  16. * Customer VAT
  17. *
  18. * @var \Magento\Customer\Model\Vat
  19. */
  20. protected $customerVat;
  21. /**
  22. * @param \Magento\Customer\Helper\Address $customerAddress
  23. * @param \Magento\Customer\Model\Vat $customerVat
  24. */
  25. public function __construct(
  26. \Magento\Customer\Helper\Address $customerAddress,
  27. \Magento\Customer\Model\Vat $customerVat
  28. ) {
  29. $this->customerVat = $customerVat;
  30. $this->customerAddress = $customerAddress;
  31. }
  32. /**
  33. * Validate VAT number
  34. *
  35. * @param \Magento\Quote\Model\Quote\Address $quoteAddress
  36. * @param \Magento\Store\Model\Store|int $store
  37. * @return \Magento\Framework\DataObject
  38. */
  39. public function validate(\Magento\Quote\Model\Quote\Address $quoteAddress, $store)
  40. {
  41. $customerCountryCode = $quoteAddress->getCountryId();
  42. $customerVatNumber = $quoteAddress->getVatId();
  43. $merchantCountryCode = $this->customerVat->getMerchantCountryCode();
  44. $merchantVatNumber = $this->customerVat->getMerchantVatNumber();
  45. $validationResult = null;
  46. if ($this->customerAddress->hasValidateOnEachTransaction(
  47. $store
  48. ) ||
  49. $customerCountryCode != $quoteAddress->getValidatedCountryCode() ||
  50. $customerVatNumber != $quoteAddress->getValidatedVatNumber()
  51. ) {
  52. // Send request to gateway
  53. $validationResult = $this->customerVat->checkVatNumber(
  54. $customerCountryCode,
  55. $customerVatNumber,
  56. $merchantVatNumber !== '' ? $merchantCountryCode : '',
  57. $merchantVatNumber
  58. );
  59. // Store validation results in corresponding quote address
  60. $quoteAddress->setVatIsValid((int)$validationResult->getIsValid());
  61. $quoteAddress->setVatRequestId($validationResult->getRequestIdentifier());
  62. $quoteAddress->setVatRequestDate($validationResult->getRequestDate());
  63. $quoteAddress->setVatRequestSuccess($validationResult->getRequestSuccess());
  64. $quoteAddress->setValidatedVatNumber($customerVatNumber);
  65. $quoteAddress->setValidatedCountryCode($customerCountryCode);
  66. $quoteAddress->save();
  67. } else {
  68. // Restore validation results from corresponding quote address
  69. $validationResult = new \Magento\Framework\DataObject(
  70. [
  71. 'is_valid' => (int)$quoteAddress->getVatIsValid(),
  72. 'request_identifier' => (string)$quoteAddress->getVatRequestId(),
  73. 'request_date' => (string)$quoteAddress->getVatRequestDate(),
  74. 'request_success' => (bool)$quoteAddress->getVatRequestSuccess(),
  75. ]
  76. );
  77. }
  78. return $validationResult;
  79. }
  80. /**
  81. * Check whether VAT ID validation is enabled
  82. *
  83. * @param \Magento\Quote\Model\Quote\Address $quoteAddress
  84. * @param \Magento\Store\Model\Store|int $store
  85. * @return bool
  86. */
  87. public function isEnabled(\Magento\Quote\Model\Quote\Address $quoteAddress, $store)
  88. {
  89. $configAddressType = $this->customerAddress->getTaxCalculationAddressType($store);
  90. // When VAT is based on billing address then Magento have to handle only billing addresses
  91. $additionalBillingAddressCondition = $configAddressType ==
  92. \Magento\Customer\Model\Address\AbstractAddress::TYPE_BILLING ? $configAddressType !=
  93. $quoteAddress->getAddressType() : false;
  94. // Handle only addresses that corresponds to VAT configuration
  95. if (!$this->customerAddress->isVatValidationEnabled($store) || $additionalBillingAddressCondition) {
  96. return false;
  97. }
  98. return true;
  99. }
  100. }