app/Plugin/PayPalCheckout42/Controller/OrderManagement/InitializeOrderController.php line 71

Open in your IDE?
  1. <?php
  2. namespace Plugin\PayPalCheckout42\Controller\OrderManagement;
  3. use Eccube\Controller\AbstractShoppingController as EccubeAbstractShoppingController;
  4. use Eccube\Entity\Cart;
  5. use Eccube\Entity\CartItem;
  6. use Eccube\Entity\Customer;
  7. use Eccube\Entity\CustomerAddress;
  8. use Eccube\Entity\Order;
  9. use Eccube\Entity\OrderItem;
  10. use Eccube\Entity\Shipping;
  11. use Eccube\Form\Type\Shopping\OrderType;
  12. use Eccube\Repository\OrderRepository;
  13. use Eccube\Service\CartService;
  14. use Eccube\Service\MailService;
  15. use Eccube\Service\OrderHelper;
  16. use Eccube\Service\PurchaseFlow\PurchaseContext;
  17. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  18. /**
  19.  * Class InitializeOrderController
  20.  * @package Plugin\PayPalCheckout42\Controller\ShippingOrder
  21.  */
  22. class InitializeOrderController extends EccubeAbstractShoppingController
  23. {
  24.     /**
  25.      * @var CartService
  26.      */
  27.     protected $cartService;
  28.     /**
  29.      * @var MailService
  30.      */
  31.     protected $mailService;
  32.     /**
  33.      * @var OrderHelper
  34.      */
  35.     protected $orderHelper;
  36.     /**
  37.      * @var OrderRepository
  38.      */
  39.     protected $orderRepository;
  40.     /**
  41.      * ShoppingController constructor.
  42.      * @param CartService $cartService
  43.      * @param MailService $mailService
  44.      * @param OrderRepository $orderRepository
  45.      * @param OrderHelper $orderHelper
  46.      */
  47.     public function __construct(
  48.         CartService $cartService,
  49.         MailService $mailService,
  50.         OrderRepository $orderRepository,
  51.         OrderHelper $orderHelper
  52.     ) {
  53.         $this->cartService $cartService;
  54.         $this->mailService $mailService;
  55.         $this->orderRepository $orderRepository;
  56.         $this->orderHelper $orderHelper;
  57.     }
  58.     /**
  59.      * @param PurchaseFlow $cartPurchaseFlow
  60.      * @param callable $setupShippingOrderAfterInitialization
  61.      * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
  62.      */
  63.     public function eccubeShoppingControllerIndex(PurchaseFlow $cartPurchaseFlow, callable $setupShippingOrderAfterInitialization)
  64.     {
  65.         // ログイン状態のチェック.
  66.         if ($this->orderHelper->isLoginRequired()) {
  67.             log_info('[注文手続] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  68.             return $this->redirectToRoute('shopping_login');
  69.         }
  70.         // カートチェック.
  71.         $Cart $this->cartService->getCart();
  72.         if (!($Cart && $this->orderHelper->verifyCart($Cart))) {
  73.             log_info('[注文手続] カートが購入フローへ遷移できない状態のため, カート画面に遷移します.');
  74.             return $this->redirectToRoute('cart');
  75.         }
  76.         // 受注の初期化.
  77.         log_info('[注文手続] 受注の初期化処理を開始します.');
  78.         $Customer $this->getUser() ? $this->getUser() : $this->orderHelper->getNonMember();
  79.         $Order $this->orderHelper->initializeOrder($Cart$Customer);
  80.         // 受注の初期化後の処理
  81.         call_user_func($setupShippingOrderAfterInitialization$Order);
  82.         // 集計処理.
  83.         log_info('[注文手続] 集計処理を開始します.', [$Order->getId()]);
  84.         $flowResult $this->executePurchaseFlow($Orderfalse);
  85.         $this->entityManager->flush();
  86.         if ($flowResult->hasError()) {
  87.             log_info('[注文手続] Errorが発生したため購入エラー画面へ遷移します.', [$flowResult->getErrors()]);
  88.             return $this->redirectToRoute('shopping_error');
  89.         }
  90.         if ($flowResult->hasWarning()) {
  91.             log_info('[注文手続] Warningが発生しました.', [$flowResult->getWarning()]);
  92.             // 受注明細と同期をとるため, CartPurchaseFlowを実行する
  93.             $cartPurchaseFlow->validate($Cart, new PurchaseContext());
  94.             $this->cartService->save();
  95.         }
  96.         // マイページで会員情報が更新されていれば, Orderの注文者情報も更新する.
  97.         if ($Customer->getId()) {
  98.             $this->orderHelper->updateCustomerInfo($Order$Customer);
  99.             $this->entityManager->flush();
  100.         }
  101.         $form $this->createForm(OrderType::class, $Order);
  102.         return [
  103.             'form' => $form->createView(),
  104.             'Order' => $Order,
  105.         ];
  106.     }
  107.     /**
  108.      * @param PurchaseFlow $cartPurchaseFlow
  109.      * @param Order $ReferenceOrder
  110.      * @param Customer $RefCustomer
  111.      * @param callable $setupShippingOrderAfterInitialization
  112.      * @return Order
  113.      */
  114.     protected function initializeSubscriptionOrder(PurchaseFlow $cartPurchaseFlowOrder $ReferenceOrderCustomer $RefCustomer, callable $setupShippingOrderAfterInitialization): Order
  115.     {
  116.         /** @var array $items */
  117.         $items array_map(function (OrderItem $item): CartItem {
  118.             $cartItem = new CartItem();
  119.             $cartItem->setQuantity($item->getQuantity());
  120.             $cartItem->setPrice($item->getProductClass()->getPrice02IncTax());
  121.             $cartItem->setProductClass($item->getProductClass());
  122.             return $cartItem;
  123.         }, $ReferenceOrder->getProductOrderItems());
  124.         /** @var Cart $cart */
  125.         $cart = new Cart();
  126.         $cart $cart->setCartItems($items);
  127.         /** @var Order $Order */
  128.         $Order $this->orderHelper->createPurchaseProcessingOrder($cart$RefCustomer);
  129.         call_user_func($setupShippingOrderAfterInitialization$Order);
  130.         $flowResult $this->executePurchaseFlow($Orderfalse);
  131.         $this->orderHelper->updateCustomerInfo($Order$RefCustomer);
  132.         $this->entityManager->flush();
  133.         return $Order;
  134.     }
  135.     /**
  136.      * @param PurchaseFlow $cartPurchaseFlow
  137.      * @param Customer $customer
  138.      * @return void
  139.      */
  140.     protected function updateGuestOrder(PurchaseFlow $cartPurchaseFlowCustomer $customer): void
  141.     {
  142.         $preOrderId $this->cartService->getPreOrderId();
  143.         $order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  144.         $this->orderHelper->updateCustomerInfo($order$customer);
  145.         /** @var Shipping $shipping */
  146.         $shipping $order->getShippings()->first();
  147.         /** @var CustomerAddress $customerAddress */
  148.         $customerAddress = new CustomerAddress();
  149.         $customerAddress->setFromCustomer($customer);
  150.         $shipping->setFromCustomerAddress($customerAddress);
  151.         $flowResult $this->executePurchaseFlow($orderfalse);
  152.         $this->entityManager->flush();
  153.     }
  154. }