/** * Handle customer VAT number if needed on collect_totals_before event of quote address * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function dispatch(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Sales\Model\Quote\Address $quoteAddress */ $quoteAddress = $observer->getQuoteAddress(); /** @var \Magento\Sales\Model\Quote $quote */ $quote = $quoteAddress->getQuote(); $customerData = $quote->getCustomerData(); $storeId = $customerData->getStoreId(); if ($customerData->getCustomAttribute('disable_auto_group_change') && $customerData->getCustomAttribute('disable_auto_group_change')->getValue() || false == $this->vatValidator->isEnabled($quoteAddress, $storeId)) { return; } $customerCountryCode = $quoteAddress->getCountryId(); $customerVatNumber = $quoteAddress->getVatId(); $groupId = null; if (empty($customerVatNumber) || false == $this->customerHelper->isCountryInEU($customerCountryCode)) { $groupId = $customerData->getId() ? $this->customerHelper->getDefaultCustomerGroupId($storeId) : \Magento\Customer\Service\V1\CustomerGroupServiceInterface::NOT_LOGGED_IN_ID; } else { // Magento always has to emulate group even if customer uses default billing/shipping address $groupId = $this->customerHelper->getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $this->vatValidator->validate($quoteAddress, $storeId), $storeId); } if ($groupId) { $quoteAddress->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId()); $quote->setCustomerGroupId($groupId); $customerData = $this->customerBuilder->mergeDataObjectWithArray($customerData, array('group_id' => $groupId)); $quote->setCustomerData($customerData); } }
/** * Handle customer VAT number if needed on collect_totals_before event of quote address * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function dispatch(\Magento\Framework\Event\Observer $observer) { /** @var \Magento\Quote\Model\Quote\Address $quoteAddress */ $quoteAddress = $observer->getQuoteAddress(); /** @var \Magento\Quote\Model\Quote $quote */ $quote = $quoteAddress->getQuote(); $customer = $quote->getCustomer(); $storeId = $customer->getStoreId(); if ($customer->getCustomAttribute('disable_auto_group_change') && $customer->getCustomAttribute('disable_auto_group_change')->getValue() || false == $this->vatValidator->isEnabled($quoteAddress, $storeId)) { return; } $customerCountryCode = $quoteAddress->getCountryId(); $customerVatNumber = $quoteAddress->getVatId(); $groupId = null; if (empty($customerVatNumber) || false == $this->customerVat->isCountryInEU($customerCountryCode)) { $groupId = $customer->getId() ? $this->groupManagement->getDefaultGroup($storeId)->getId() : $this->groupManagement->getNotLoggedInGroup()->getId(); } else { // Magento always has to emulate group even if customer uses default billing/shipping address $groupId = $this->customerVat->getCustomerGroupIdBasedOnVatNumber($customerCountryCode, $this->vatValidator->validate($quoteAddress, $storeId), $storeId); } if ($groupId) { $quoteAddress->setPrevQuoteCustomerGroupId($quote->getCustomerGroupId()); $quote->setCustomerGroupId($groupId); $customer->setGroupId($groupId); $quote->setCustomer($customer); } }
$vat = $this->prepareVat($country, $number); if ($vat) { $this->response = array('is_valid' => $this->soap->checkVat($vat)->valid); } return json_encode($this->response); } /** * Checks that there are all needed params ( Code Country and number ); */ protected function prepareVat($country, $number) { try { if (empty($country) || empty($number)) { throw new Exception("Both 'country' and 'number' params are mandatory"); } if (!in_array($country, self::$european_countries)) { throw new Exception("Invalid country"); } $vat = array('vatNumber' => $number, 'countryCode' => $country); return $vat; } catch (Exception $e) { $this->response = array('error_message' => $e->getMessage()); return false; } } } // API Call $vies = new VatValidator(); $vies->checkVat(strtoupper($_GET['country']), $_GET['number']); $response = json_encode($vies->response); echo $response;