/** * Validate VAT number * * @param \Magento\Sales\Model\Quote\Address $quoteAddress * @param \Magento\Store\Model\Store|int $store * @return \Magento\Framework\Object */ public function validate(\Magento\Sales\Model\Quote\Address $quoteAddress, $store) { $customerCountryCode = $quoteAddress->getCountryId(); $customerVatNumber = $quoteAddress->getVatId(); $merchantCountryCode = $this->customerData->getMerchantCountryCode(); $merchantVatNumber = $this->customerData->getMerchantVatNumber(); $validationResult = null; if ($this->customerAddress->hasValidateOnEachTransaction($store) || $customerCountryCode != $quoteAddress->getValidatedCountryCode() || $customerVatNumber != $quoteAddress->getValidatedVatNumber()) { // Send request to gateway $validationResult = $this->customerData->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(array('is_valid' => (int) $quoteAddress->getVatIsValid(), 'request_identifier' => (string) $quoteAddress->getVatRequestId(), 'request_date' => (string) $quoteAddress->getVatRequestDate(), 'request_success' => (bool) $quoteAddress->getVatRequestSuccess())); } return $validationResult; }