/** * Submit quote * * @param Quote $quote * @param array $orderData * @return \Magento\Framework\Model\AbstractExtensibleModel|\Magento\Sales\Api\Data\OrderInterface|object * @throws \Exception * @throws \Magento\Framework\Exception\LocalizedException */ protected function submitQuote(QuoteEntity $quote, $orderData = []) { $order = $this->orderFactory->create(); $this->quoteValidator->validateBeforeSubmit($quote); if (!$quote->getCustomerIsGuest()) { if ($quote->getCustomerId()) { $this->_prepareCustomerQuote($quote); } $this->customerManagement->populateCustomerInfo($quote); } $addresses = []; $quote->reserveOrderId(); if ($quote->isVirtual()) { $this->dataObjectHelper->mergeDataObjects('\\Magento\\Sales\\Api\\Data\\OrderInterface', $order, $this->quoteAddressToOrder->convert($quote->getBillingAddress(), $orderData)); } else { $this->dataObjectHelper->mergeDataObjects('\\Magento\\Sales\\Api\\Data\\OrderInterface', $order, $this->quoteAddressToOrder->convert($quote->getShippingAddress(), $orderData)); $shippingAddress = $this->quoteAddressToOrderAddress->convert($quote->getShippingAddress(), ['address_type' => 'shipping', 'email' => $quote->getCustomerEmail()]); $addresses[] = $shippingAddress; $order->setShippingAddress($shippingAddress); $order->setShippingMethod($quote->getShippingAddress()->getShippingMethod()); } $billingAddress = $this->quoteAddressToOrderAddress->convert($quote->getBillingAddress(), ['address_type' => 'billing', 'email' => $quote->getCustomerEmail()]); $addresses[] = $billingAddress; $order->setBillingAddress($billingAddress); $order->setAddresses($addresses); $order->setPayment($this->quotePaymentToOrderPayment->convert($quote->getPayment())); $order->setItems($this->resolveItems($quote)); if ($quote->getCustomer()) { $order->setCustomerId($quote->getCustomer()->getId()); } $order->setQuoteId($quote->getId()); //To Set Vat Exempt Quote values to Order $order->setVatpername($quote->getVatpername()); $order->setVatcomment($quote->getVatcomment()); $order->setVatdeclare($quote->getVatdeclare()); $order->setCustomerEmail($quote->getCustomerEmail()); $order->setCustomerFirstname($quote->getCustomerFirstname()); $order->setCustomerMiddlename($quote->getCustomerMiddlename()); $order->setCustomerLastname($quote->getCustomerLastname()); $this->eventManager->dispatch('sales_model_service_quote_submit_before', ['order' => $order, 'quote' => $quote]); try { $order = $this->orderManagement->place($order); $quote->setIsActive(false); $this->eventManager->dispatch('sales_model_service_quote_submit_success', ['order' => $order, 'quote' => $quote]); $this->quoteRepository->save($quote); } catch (\Exception $e) { $this->eventManager->dispatch('sales_model_service_quote_submit_failure', ['order' => $order, 'quote' => $quote, 'exception' => $e]); throw $e; } return $order; }
/** * Tests Convert method in payment to order payment converter */ public function testConvert() { $methodInterface = $this->getMock('Magento\\Payment\\Model\\MethodInterface', [], [], '', false); $orderPayment = $this->getMockForAbstractClass('Magento\\Sales\\Api\\Data\\OrderPaymentInterface', [], '', false, true, true, ['setCcNumber', 'setCcCid']); $paymentData = ['test' => 'test2']; $data = ['some_id' => 1]; $paymentMethodTitle = 'TestTitle'; $additionalInfo = ['token' => 'TOKEN-123']; $this->paymentMock->expects($this->once())->method('getMethodInstance')->willReturn($methodInterface); $methodInterface->expects($this->once())->method('getTitle')->willReturn($paymentMethodTitle); $this->objectCopyMock->expects($this->once())->method('getDataFromFieldset')->with('quote_convert_payment', 'to_order_payment', $this->paymentMock)->willReturn($paymentData); $this->orderPaymentBuilderMock->expects($this->once())->method('populateWithArray')->with(array_merge($paymentData, $data))->willReturnSelf(); $this->paymentMock->expects($this->once())->method('getAdditionalInformation')->willReturn($additionalInfo); $ccNumber = 123456798; $ccCid = 1234; $this->paymentMock->expects($this->once())->method('getCcNumber')->willReturn($ccNumber); $this->paymentMock->expects($this->once())->method('getCcCid')->willReturn($ccCid); $this->orderPaymentBuilderMock->expects($this->once())->method('setAdditionalInformation')->with(serialize(array_merge($additionalInfo, [Substitution::INFO_KEY_TITLE => $paymentMethodTitle])))->willReturnSelf(); $this->orderPaymentBuilderMock->expects($this->once())->method('create')->willReturn($orderPayment); $orderPayment->expects($this->once())->method('setCcNumber')->with($ccNumber)->willReturnSelf(); $orderPayment->expects($this->once())->method('setCcCid')->with($ccCid)->willReturnSelf(); $this->assertSame($orderPayment, $this->converter->convert($this->paymentMock, $data)); }
/** * Prepare order based on quote address * * @param \Magento\Quote\Model\Quote\Address $address * @return \Magento\Sales\Model\Order * @throws \Magento\Checkout\Exception */ protected function _prepareOrder(\Magento\Quote\Model\Quote\Address $address) { $quote = $this->getQuote(); $quote->unsReservedOrderId(); $quote->reserveOrderId(); $quote->collectTotals(); $order = $this->quoteAddressToOrder->convert($address); $order->setQuote($quote); $order->setBillingAddress($this->quoteAddressToOrderAddress->convert($quote->getBillingAddress())); if ($address->getAddressType() == 'billing') { $order->setIsVirtual(1); } else { $order->setShippingAddress($this->quoteAddressToOrderAddress->convert($address)); } $order->setPayment($this->quotePaymentToOrderPayment->convert($quote->getPayment())); if ($this->priceCurrency->round($address->getGrandTotal()) == 0) { $order->getPayment()->setMethod('free'); } foreach ($address->getAllItems() as $item) { $_quoteItem = $item->getQuoteItem(); if (!$_quoteItem) { throw new \Magento\Checkout\Exception(__('Item not found or already ordered')); } $item->setProductType($_quoteItem->getProductType())->setProductOptions($_quoteItem->getProduct()->getTypeInstance()->getOrderOptions($_quoteItem->getProduct())); $orderItem = $this->quoteItemToOrderItem->convert($item); if ($item->getParentItem()) { $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId())); } $order->addItem($orderItem); } return $order; }