requestFactory = $requestFactory; $this->dateTimeFactory = $dateTimeFactory; $this->sellerBuilder = $sellerBuilder; $this->customerBuilder = $customerBuilder; $this->deliveryTerm = $deliveryTerm; $this->config = $config; $this->processorPool = $processorPool; } /** * Create a Vertex Invoice Request from a Magento Order * * @param OrderInterface $order * @return RequestInterface */ public function process(OrderInterface $order) { $address = $order->getIsVirtual() ? $order->getBillingAddress() : $this->getShippingFromOrder($order); $scopeCode = $order->getStoreId(); $seller = $this->sellerBuilder ->setScopeType(ScopeInterface::SCOPE_STORE) ->setScopeCode($scopeCode) ->build(); $customer = $this->customerBuilder->buildFromOrderAddress( $address, $address !== null ? $address->getCustomerId() : null, $order->getCustomerGroupId(), $scopeCode ); /** @var RequestInterface $request */ $request = $this->requestFactory->create(); $request->setDocumentNumber($order->getIncrementId()); $request->setDocumentDate($this->dateTimeFactory->create()); $request->setTransactionType(RequestInterface::TRANSACTION_TYPE_SALE); $request->setSeller($seller); $request->setCustomer($customer); $request->setCurrencyCode($order->getBaseCurrencyCode()); $this->deliveryTerm->addIfApplicable($request); if ($this->config->getLocationCode($scopeCode)) { $request->setLocationCode($this->config->getLocationCode($scopeCode)); } $request = $this->processorPool->process($request, $order); return $request; } /** * Retrieve the shipping address from an Order * * @param OrderInterface $order * @return OrderAddressInterface|null */ private function getShippingFromOrder(OrderInterface $order) { if ($order instanceof Order && $order->getShippingAddress()) { return $order->getShippingAddress(); } return $order->getExtensionAttributes() !== null && $order->getExtensionAttributes()->getShippingAssignments() && $order->getExtensionAttributes()->getShippingAssignments()[0] && $order->getExtensionAttributes()->getShippingAssignments()[0]->getShipping() ? $order->getExtensionAttributes()->getShippingAssignments()[0]->getShipping()->getAddress() : null; } }