|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +use Magento\Framework\DB\Transaction; |
| 8 | +use Magento\Paypal\Model\Config; |
| 9 | +use Magento\Sales\Api\InvoiceManagementInterface; |
| 10 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 11 | +use Magento\Sales\Model\Order; |
| 12 | +use Magento\Sales\Model\Order\Item; |
| 13 | +use Magento\Sales\Model\Order\Address; |
| 14 | +use Magento\Sales\Model\Order\Payment; |
| 15 | +use Magento\Sales\Model\Service\InvoiceService; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | + |
| 18 | +require __DIR__ . '/../../../Magento/Catalog/_files/product_simple.php'; |
| 19 | +$product->setPrice(121); |
| 20 | + |
| 21 | +$objectManager = Bootstrap::getObjectManager(); |
| 22 | + |
| 23 | +$addressData = include __DIR__ . '/address_data.php'; |
| 24 | +$billingAddress = $objectManager->create( |
| 25 | + Address::class, |
| 26 | + ['data' => $addressData] |
| 27 | +); |
| 28 | +$billingAddress->setAddressType('billing'); |
| 29 | +$shippingAddress = clone $billingAddress; |
| 30 | +$shippingAddress->setId(null)->setAddressType('shipping'); |
| 31 | + |
| 32 | +$payment = $objectManager->create(Payment::class); |
| 33 | +$payment->setMethod(Config::METHOD_WPS_EXPRESS); |
| 34 | + |
| 35 | +$taxRate = .0825; |
| 36 | +$taxAmount = $product->getPrice() * $taxRate; |
| 37 | + |
| 38 | +/** @var Item $orderItem */ |
| 39 | +$orderItem = $objectManager->create(Item::class); |
| 40 | +$orderItem->setProductId($product->getId())->setQtyOrdered(1); |
| 41 | +$orderItem->setBasePrice($product->getPrice()); |
| 42 | +$orderItem->setPrice($product->getPrice()); |
| 43 | +$orderItem->setRowTotal($product->getPrice()); |
| 44 | +$orderItem->setRowTotalInclTax($product->getPrice() + $taxAmount); |
| 45 | +$orderItem->setBaseRowTotal($product->getPrice()); |
| 46 | +$orderItem->setBaseRowTotalInclTax($product->getPrice() + $taxAmount); |
| 47 | +$orderItem->setBaseRowInvoiced($product->getPrice() + $taxAmount); |
| 48 | +$orderItem->setProductType('simple'); |
| 49 | + |
| 50 | +$itemsAmount = $product->getPrice(); |
| 51 | +$shippingAmount = 15; |
| 52 | +$itemsAmountInclTax = $itemsAmount + $taxAmount; |
| 53 | +$totalAmount = $itemsAmountInclTax + $shippingAmount; |
| 54 | + |
| 55 | +/** @var Order $order */ |
| 56 | +$order = $objectManager->create(Order::class); |
| 57 | +$order-> setCustomerEmail( '[email protected]') |
| 58 | + ->setIncrementId('100000001') |
| 59 | + ->addItem($orderItem) |
| 60 | + ->setSubtotal($itemsAmount) |
| 61 | + ->setBaseSubtotal($itemsAmount) |
| 62 | + ->setBaseGrandTotal($totalAmount) |
| 63 | + ->setGrandTotal($totalAmount) |
| 64 | + ->setTaxAmount($taxAmount) |
| 65 | + ->setBaseCurrencyCode('USD') |
| 66 | + ->setCustomerIsGuest(true) |
| 67 | + ->setStoreId(1) |
| 68 | + ->setEmailSent(true) |
| 69 | + ->setState(Order::STATE_PROCESSING) |
| 70 | + ->setBillingAddress($billingAddress) |
| 71 | + ->setShippingAddress($shippingAddress) |
| 72 | + ->setBaseTotalPaid($totalAmount) |
| 73 | + ->setTotalPaid($totalAmount) |
| 74 | + ->setData('base_to_global_rate', 1) |
| 75 | + ->setData('base_to_order_rate', 1) |
| 76 | + ->setData('shipping_amount', $shippingAmount) |
| 77 | + ->setData('base_shipping_amount', $shippingAmount) |
| 78 | + ->setPayment($payment); |
| 79 | + |
| 80 | +/** @var OrderRepositoryInterface $orderRepository */ |
| 81 | +$orderRepository = $objectManager->get(OrderRepositoryInterface::class); |
| 82 | +$orderRepository->save($order); |
| 83 | + |
| 84 | +/** @var InvoiceService $invoiceService */ |
| 85 | +$invoiceService = $objectManager->create(InvoiceManagementInterface::class); |
| 86 | + |
| 87 | +/** @var Transaction $transaction */ |
| 88 | +$transaction = $objectManager->create(Transaction::class); |
| 89 | + |
| 90 | +$invoice = $invoiceService->prepareInvoice($order, [$orderItem->getId() => 1]); |
| 91 | +$invoice->register(); |
| 92 | + |
| 93 | +$transaction->addObject($invoice)->addObject($order)->save(); |
0 commit comments