config = $config; $this->countryGuard = $countryGuard; $this->taxInvoice = $taxInvoice; $this->messageManager = $messageManager; $this->invoiceSentRegistry = $invoiceSentRegistry; $this->configValidator = $configValidator; $this->invoiceRequestBuilder = $invoiceRequestBuilder; $this->extensionLoader = $extensionLoader; $this->attributeManager = $attributeManager; $this->showSuccessMessage = $showSuccessMessage; $this->vertexExtensionLoader = $vertexExtensionLoader; } /** * Commit an invoice to the Vertex Tax Log * * Only when Request by Invoice Creation is turned on * * @param Observer $observer * @return void */ public function execute(Observer $observer) { /** @var Invoice $invoice */ $invoice = $observer->getEvent()->getInvoice(); $storeId = $invoice->getStoreId(); if (!$this->config->isVertexActive($storeId) || !$this->config->isTaxCalculationEnabled($storeId)) { return; } $this->extensionLoader->loadOnInvoice($invoice); /** @var \Magento\Sales\Model\Order $order */ $order = $invoice->getOrder(); /** @var boolean $isInvoiceSent */ $isInvoiceSent = $this->invoiceSentRegistry->hasInvoiceBeenSentToVertex($invoice); /** @var boolean $requestByInvoice */ $requestByInvoice = $this->config->requestByInvoiceCreation($invoice->getStore()); /** @var boolean $canService */ $canService = $this->countryGuard->isOrderServiceableByVertex($order); /** @var boolean $configValid */ $configValid = $this->configValidator->execute(ScopeInterface::SCOPE_STORE, $invoice->getStoreId(), true) ->isValid(); if (!$isInvoiceSent && $requestByInvoice && $canService && $configValid) { // During checkout for authorize & capture, the invoice will not have the address IDs $invoice = $this->vertexExtensionLoader->loadOnInvoice($invoice); $request = $this->invoiceRequestBuilder->buildFromInvoice($invoice); $response = $this->taxInvoice->sendInvoiceRequest($request, $invoice->getOrder()); $this->processResponse($response, $invoice); } } /** * Process response * * @param null|ResponseInterface $response * @param Invoice $invoice * @return void */ private function processResponse($response, $invoice) { if ($response) { $this->attributeManager->saveAllVertexAttributes($response->getLineItems()); $this->invoiceSentRegistry->setInvoiceHasBeenSentToVertex($invoice); $this->addSuccessMessage(); } } /** * Notify administrator that the order has been committed to the tax log * * @return void */ private function addSuccessMessage() { if ($this->showSuccessMessage) { $this->messageManager->addSuccessMessage(__('The Vertex invoice has been sent.')->render()); } } }