/** * Address after save event handler * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function afterAddressSave($observer) { /** @var $customerAddress Address */ $customerAddress = $observer->getCustomerAddress(); $customer = $customerAddress->getCustomer(); if (!$this->_customerAddress->isVatValidationEnabled($customer->getStore()) || $this->_coreRegistry->registry(self::VIV_PROCESSED_FLAG) || !$this->_canProcessAddress($customerAddress)) { return; } try { $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, true); if ($customerAddress->getVatId() == '' || !$this->_customerVat->isCountryInEU($customerAddress->getCountry())) { $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStore())->getId(); if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) { $customer->setGroupId($defaultGroupId); $customer->save(); } } else { $result = $this->_customerVat->checkVatNumber($customerAddress->getCountryId(), $customerAddress->getVatId()); $newGroupId = $this->_customerVat->getCustomerGroupIdBasedOnVatNumber($customerAddress->getCountryId(), $result, $customer->getStore()); if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $newGroupId) { $customer->setGroupId($newGroupId); $customer->save(); } $customerAddress->setVatValidationResult($result); } } catch (\Exception $e) { $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, false, true); } }
/** * Validate VAT number * * @param \Magento\Quote\Model\Quote\Address $quoteAddress * @param \Magento\Store\Model\Store|int $store * @return \Magento\Framework\Object */ 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\Object(['is_valid' => (int) $quoteAddress->getVatIsValid(), 'request_identifier' => (string) $quoteAddress->getVatRequestId(), 'request_date' => (string) $quoteAddress->getVatRequestDate(), 'request_success' => (bool) $quoteAddress->getVatRequestSuccess()]); } return $validationResult; }
/** * Address after save event handler * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function execute(\Magento\Framework\Event\Observer $observer) { /** @var $customerAddress Address */ $customerAddress = $observer->getCustomerAddress(); $customer = $customerAddress->getCustomer(); if (!$this->_customerAddress->isVatValidationEnabled($customer->getStore()) || $this->_coreRegistry->registry(self::VIV_PROCESSED_FLAG) || !$this->_canProcessAddress($customerAddress)) { return; } try { $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, true); if ($customerAddress->getVatId() == '' || !$this->_customerVat->isCountryInEU($customerAddress->getCountry())) { $defaultGroupId = $this->_groupManagement->getDefaultGroup($customer->getStore())->getId(); if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $defaultGroupId) { $customer->setGroupId($defaultGroupId); $customer->save(); $this->customerSession->setCustomerGroupId($defaultGroupId); } } else { $result = $this->_customerVat->checkVatNumber($customerAddress->getCountryId(), $customerAddress->getVatId()); $newGroupId = $this->_customerVat->getCustomerGroupIdBasedOnVatNumber($customerAddress->getCountryId(), $result, $customer->getStore()); if (!$customer->getDisableAutoGroupChange() && $customer->getGroupId() != $newGroupId) { $customer->setGroupId($newGroupId); $customer->save(); $this->customerSession->setCustomerGroupId($newGroupId); } $customerAddress->setVatValidationResult($result); if ($this->appState->getAreaCode() == Area::AREA_FRONTEND) { if ($result->getIsValid()) { $this->addValidMessage($customerAddress, $result); } elseif ($result->getRequestSuccess()) { $this->addInvalidMessage($customerAddress); } else { $this->addErrorMessage($customerAddress); } } } } catch (\Exception $e) { $this->_coreRegistry->register(self::VIV_PROCESSED_FLAG, false, true); } }