src/Eccube/Controller/ShoppingController.php line 644

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Controller;
  13. use Eccube\Entity\CustomerAddress;
  14. use Eccube\Entity\Order;
  15. use Eccube\Entity\Shipping;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Exception\ShoppingException;
  19. use Eccube\Form\Type\Front\CustomerLoginType;
  20. use Eccube\Form\Type\Front\ShoppingShippingType;
  21. use Eccube\Form\Type\Shopping\CustomerAddressType;
  22. use Eccube\Form\Type\Shopping\OrderType;
  23. use Eccube\Repository\OrderRepository;
  24. use Eccube\Service\CartService;
  25. use Eccube\Service\MailService;
  26. use Eccube\Service\OrderHelper;
  27. use Eccube\Service\Payment\PaymentDispatcher;
  28. use Eccube\Service\Payment\PaymentMethodInterface;
  29. use Eccube\Service\PurchaseFlow\PurchaseContext;
  30. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  31. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  32. use Symfony\Component\Form\FormInterface;
  33. use Symfony\Component\HttpFoundation\Request;
  34. use Symfony\Component\Routing\Annotation\Route;
  35. use Symfony\Component\Routing\RouterInterface;
  36. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  37. class ShoppingController extends AbstractShoppingController
  38. {
  39.     /**
  40.      * @var CartService
  41.      */
  42.     protected $cartService;
  43.     /**
  44.      * @var MailService
  45.      */
  46.     protected $mailService;
  47.     /**
  48.      * @var OrderHelper
  49.      */
  50.     protected $orderHelper;
  51.     /**
  52.      * @var OrderRepository
  53.      */
  54.     protected $orderRepository;
  55.     public function __construct(
  56.         CartService $cartService,
  57.         MailService $mailService,
  58.         OrderRepository $orderRepository,
  59.         OrderHelper $orderHelper
  60.     ) {
  61.         $this->cartService $cartService;
  62.         $this->mailService $mailService;
  63.         $this->orderRepository $orderRepository;
  64.         $this->orderHelper $orderHelper;
  65.     }
  66.     /**
  67.      * 注文手続き画面を表示する
  68.      *
  69.      * 未ログインまたはRememberMeログインの場合はログイン画面に遷移させる.
  70.      * ただし、非会員でお客様情報を入力済の場合は遷移させない.
  71.      *
  72.      * カート情報から受注データを生成し, `pre_order_id`でカートと受注の紐付けを行う.
  73.      * 既に受注が生成されている場合(pre_order_idで取得できる場合)は, 受注の生成を行わずに画面を表示する.
  74.      *
  75.      * purchaseFlowの集計処理実行後, warningがある場合はカートど同期をとるため, カートのPurchaseFlowを実行する.
  76.      *
  77.      * @Route("/shopping", name="shopping")
  78.      * @Template("Shopping/index.twig")
  79.      */
  80.     public function index(PurchaseFlow $cartPurchaseFlow)
  81.     {
  82.         // ログイン状態のチェック.
  83.         if ($this->orderHelper->isLoginRequired()) {
  84.             log_info('[注文手続] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  85.             return $this->redirectToRoute('shopping_login');
  86.         }
  87.         // カートチェック.
  88.         $Cart $this->cartService->getCart();
  89.         if (!($Cart && $this->orderHelper->verifyCart($Cart))) {
  90.             log_info('[注文手続] カートが購入フローへ遷移できない状態のため, カート画面に遷移します.');
  91.             return $this->redirectToRoute('cart');
  92.         }
  93.         // 受注の初期化.
  94.         log_info('[注文手続] 受注の初期化処理を開始します.');
  95.         $Customer $this->getUser() ? $this->getUser() : $this->orderHelper->getNonMember();
  96.         $Order $this->orderHelper->initializeOrder($Cart$Customer);
  97.         // 集計処理.
  98.         log_info('[注文手続] 集計処理を開始します.', [$Order->getId()]);
  99.         $flowResult $this->executePurchaseFlow($Orderfalse);
  100.         $this->entityManager->flush();
  101.         if ($flowResult->hasError()) {
  102.             log_info('[注文手続] Errorが発生したため購入エラー画面へ遷移します.', [$flowResult->getErrors()]);
  103.             return $this->redirectToRoute('shopping_error');
  104.         }
  105.         if ($flowResult->hasWarning()) {
  106.             log_info('[注文手続] Warningが発生しました.', [$flowResult->getWarning()]);
  107.             // 受注明細と同期をとるため, CartPurchaseFlowを実行する
  108.             $cartPurchaseFlow->validate($Cart, new PurchaseContext());
  109.             $this->cartService->save();
  110.         }
  111.         // マイページで会員情報が更新されていれば, Orderの注文者情報も更新する.
  112.         if ($Customer->getId()) {
  113.             $this->orderHelper->updateCustomerInfo($Order$Customer);
  114.             $this->entityManager->flush();
  115.         }
  116.         $form $this->createForm(OrderType::class, $Order);
  117.         return [
  118.             'form' => $form->createView(),
  119.             'Order' => $Order,
  120.         ];
  121.     }
  122.     /**
  123.      * 他画面への遷移を行う.
  124.      *
  125.      * お届け先編集画面など, 他画面へ遷移する際に, フォームの値をDBに保存してからリダイレクトさせる.
  126.      * フォームの`redirect_to`パラメータの値にリダイレクトを行う.
  127.      * `redirect_to`パラメータはpath('遷移先のルーティング')が渡される必要がある.
  128.      *
  129.      * 外部のURLやPathを渡された場合($router->matchで展開出来ない場合)は, 購入エラーとする.
  130.      *
  131.      * プラグインやカスタマイズでこの機能を使う場合は, twig側で以下のように記述してください.
  132.      *
  133.      * <button data-trigger="click" data-path="path('ルーティング')">更新する</button>
  134.      *
  135.      * data-triggerは, click/change/blur等のイベント名を指定してください。
  136.      * data-pathは任意のパラメータです. 指定しない場合, 注文手続き画面へリダイレクトします.
  137.      *
  138.      * @Route("/shopping/redirect_to", name="shopping_redirect_to", methods={"POST"})
  139.      * @Template("Shopping/index.twig")
  140.      */
  141.     public function redirectTo(Request $requestRouterInterface $router)
  142.     {
  143.         // ログイン状態のチェック.
  144.         if ($this->orderHelper->isLoginRequired()) {
  145.             log_info('[リダイレクト] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  146.             return $this->redirectToRoute('shopping_login');
  147.         }
  148.         // 受注の存在チェック.
  149.         $preOrderId $this->cartService->getPreOrderId();
  150.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  151.         if (!$Order) {
  152.             log_info('[リダイレクト] 購入処理中の受注が存在しません.');
  153.             return $this->redirectToRoute('shopping_error');
  154.         }
  155.         $form $this->createForm(OrderType::class, $Order);
  156.         $form->handleRequest($request);
  157.         if ($form->isSubmitted() && $form->isValid()) {
  158.             log_info('[リダイレクト] 集計処理を開始します.', [$Order->getId()]);
  159.             $response $this->executePurchaseFlow($Order);
  160.             $this->entityManager->flush();
  161.             if ($response) {
  162.                 return $response;
  163.             }
  164.             $redirectTo $form['redirect_to']->getData();
  165.             if (empty($redirectTo)) {
  166.                 log_info('[リダイレクト] リダイレクト先未指定のため注文手続き画面へ遷移します.');
  167.                 return $this->redirectToRoute('shopping');
  168.             }
  169.             try {
  170.                 // リダイレクト先のチェック.
  171.                 $pattern '/^'.preg_quote($request->getBasePath(), '/').'/';
  172.                 $redirectTo preg_replace($pattern''$redirectTo);
  173.                 $result $router->match($redirectTo);
  174.                 // パラメータのみ抽出
  175.                 $params array_filter($result, function ($key) {
  176.                     return !== \strpos($key'_');
  177.                 }, ARRAY_FILTER_USE_KEY);
  178.                 log_info('[リダイレクト] リダイレクトを実行します.', [$result['_route'], $params]);
  179.                 // pathからurlを再構築してリダイレクト.
  180.                 return $this->redirectToRoute($result['_route'], $params);
  181.             } catch (\Exception $e) {
  182.                 log_info('[リダイレクト] URLの形式が不正です', [$redirectTo$e->getMessage()]);
  183.                 return $this->redirectToRoute('shopping_error');
  184.             }
  185.         }
  186.         log_info('[リダイレクト] フォームエラーのため, 注文手続き画面を表示します.', [$Order->getId()]);
  187.         return [
  188.             'form' => $form->createView(),
  189.             'Order' => $Order,
  190.         ];
  191.     }
  192.     /**
  193.      * 注文確認画面を表示する.
  194.      *
  195.      * ここではPaymentMethod::verifyがコールされます.
  196.      * PaymentMethod::verifyではクレジットカードの有効性チェック等, 注文手続きを進められるかどうかのチェック処理を行う事を想定しています.
  197.      * PaymentMethod::verifyでエラーが発生した場合は, 注文手続き画面へリダイレクトします.
  198.      *
  199.      * @Route("/shopping/confirm", name="shopping_confirm", methods={"POST"})
  200.      * @Template("Shopping/confirm.twig")
  201.      */
  202.     public function confirm(Request $request)
  203.     {
  204.         // ログイン状態のチェック.
  205.         if ($this->orderHelper->isLoginRequired()) {
  206.             log_info('[注文確認] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  207.             return $this->redirectToRoute('shopping_login');
  208.         }
  209.         // 受注の存在チェック
  210.         $preOrderId $this->cartService->getPreOrderId();
  211.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  212.         if (!$Order) {
  213.             log_info('[注文確認] 購入処理中の受注が存在しません.', [$preOrderId]);
  214.             return $this->redirectToRoute('shopping_error');
  215.         }
  216.         $form $this->createForm(OrderType::class, $Order);
  217.         $form->handleRequest($request);
  218.         if ($form->isSubmitted() && $form->isValid()) {
  219.             log_info('[注文確認] 集計処理を開始します.', [$Order->getId()]);
  220.             $response $this->executePurchaseFlow($Order);
  221.             $this->entityManager->flush();
  222.             if ($response) {
  223.                 return $response;
  224.             }
  225.             log_info('[注文確認] PaymentMethod::verifyを実行します.', [$Order->getPayment()->getMethodClass()]);
  226.             $paymentMethod $this->createPaymentMethod($Order$form);
  227.             $PaymentResult $paymentMethod->verify();
  228.             if ($PaymentResult) {
  229.                 if (!$PaymentResult->isSuccess()) {
  230.                     $this->entityManager->rollback();
  231.                     foreach ($PaymentResult->getErrors() as $error) {
  232.                         $this->addError($error);
  233.                     }
  234.                     log_info('[注文確認] PaymentMethod::verifyのエラーのため, 注文手続き画面へ遷移します.', [$PaymentResult->getErrors()]);
  235.                     return $this->redirectToRoute('shopping');
  236.                 }
  237.                 $response $PaymentResult->getResponse();
  238.                 if ($response && ($response->isRedirection() || $response->getContent())) {
  239.                     $this->entityManager->flush();
  240.                     log_info('[注文確認] PaymentMethod::verifyが指定したレスポンスを表示します.');
  241.                     return $response;
  242.                 }
  243.             }
  244.             $this->entityManager->flush();
  245.             log_info('[注文確認] 注文確認画面を表示します.');
  246.             return [
  247.                 'form' => $form->createView(),
  248.                 'Order' => $Order,
  249.             ];
  250.         }
  251.         log_info('[注文確認] フォームエラーのため, 注文手続画面を表示します.', [$Order->getId()]);
  252.         // FIXME @Templateの差し替え.
  253.         $request->attributes->set('_template', new Template(['template' => 'Shopping/index.twig']));
  254.         return [
  255.             'form' => $form->createView(),
  256.             'Order' => $Order,
  257.         ];
  258.     }
  259.     /**
  260.      * 注文処理を行う.
  261.      *
  262.      * 決済プラグインによる決済処理および注文の確定処理を行います.
  263.      *
  264.      * @Route("/shopping/checkout", name="shopping_checkout", methods={"POST"})
  265.      * @Template("Shopping/confirm.twig")
  266.      */
  267.     public function checkout(Request $request)
  268.     {
  269.         // ログイン状態のチェック.
  270.         if ($this->orderHelper->isLoginRequired()) {
  271.             log_info('[注文処理] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  272.             return $this->redirectToRoute('shopping_login');
  273.         }
  274.         // 受注の存在チェック
  275.         $preOrderId $this->cartService->getPreOrderId();
  276.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  277.         if (!$Order) {
  278.             log_info('[注文処理] 購入処理中の受注が存在しません.', [$preOrderId]);
  279.             return $this->redirectToRoute('shopping_error');
  280.         }
  281.         // フォームの生成.
  282.         $form $this->createForm(OrderType::class, $Order, [
  283.             // 確認画面から注文処理へ遷移する場合は, Orderエンティティで値を引き回すためフォーム項目の定義をスキップする.
  284.             'skip_add_form' => true,
  285.         ]);
  286.         $form->handleRequest($request);
  287.         if ($form->isSubmitted() && $form->isValid()) {
  288.             log_info('[注文処理] 注文処理を開始します.', [$Order->getId()]);
  289.             try {
  290.                 /*
  291.                  * 集計処理
  292.                  */
  293.                 log_info('[注文処理] 集計処理を開始します.', [$Order->getId()]);
  294.                 $response $this->executePurchaseFlow($Order);
  295.                 $this->entityManager->flush();
  296.                 if ($response) {
  297.                     return $response;
  298.                 }
  299.                 log_info('[注文処理] PaymentMethodを取得します.', [$Order->getPayment()->getMethodClass()]);
  300.                 $paymentMethod $this->createPaymentMethod($Order$form);
  301.                 /*
  302.                  * 決済実行(前処理)
  303.                  */
  304.                 log_info('[注文処理] PaymentMethod::applyを実行します.');
  305.                 if ($response $this->executeApply($paymentMethod)) {
  306.                     return $response;
  307.                 }
  308.                 /*
  309.                  * 決済実行
  310.                  *
  311.                  * PaymentMethod::checkoutでは決済処理が行われ, 正常に処理出来た場合はPurchaseFlow::commitがコールされます.
  312.                  */
  313.                 log_info('[注文処理] PaymentMethod::checkoutを実行します.');
  314.                 if ($response $this->executeCheckout($paymentMethod)) {
  315.                     return $response;
  316.                 }
  317.                 $this->entityManager->flush();
  318.                 log_info('[注文処理] 注文処理が完了しました.', [$Order->getId()]);
  319.             } catch (ShoppingException $e) {
  320.                 log_error('[注文処理] 購入エラーが発生しました.', [$e->getMessage()]);
  321.                 $this->entityManager->rollback();
  322.                 $this->addError($e->getMessage());
  323.                 return $this->redirectToRoute('shopping_error');
  324.             } catch (\Exception $e) {
  325.                 log_error('[注文処理] 予期しないエラーが発生しました.', [$e->getMessage()]);
  326.                 $this->entityManager->rollback();
  327.                 $this->addError('front.shopping.system_error');
  328.                 return $this->redirectToRoute('shopping_error');
  329.             }
  330.             // カート削除
  331.             log_info('[注文処理] カートをクリアします.', [$Order->getId()]);
  332.             $this->cartService->clear();
  333.             // 受注IDをセッションにセット
  334.             $this->session->set(OrderHelper::SESSION_ORDER_ID$Order->getId());
  335.             // メール送信
  336.             log_info('[注文処理] 注文メールの送信を行います.', [$Order->getId()]);
  337.             $this->mailService->sendOrderMail($Order);
  338.             $this->entityManager->flush();
  339.             log_info('[注文処理] 注文処理が完了しました. 購入完了画面へ遷移します.', [$Order->getId()]);
  340.             return $this->redirectToRoute('shopping_complete');
  341.         }
  342.         log_info('[注文処理] フォームエラーのため, 購入エラー画面へ遷移します.', [$Order->getId()]);
  343.         return $this->redirectToRoute('shopping_error');
  344.     }
  345.     /**
  346.      * 購入完了画面を表示する.
  347.      *
  348.      * @Route("/shopping/complete", name="shopping_complete")
  349.      * @Template("Shopping/complete.twig")
  350.      */
  351.     public function complete(Request $request)
  352.     {
  353.         log_info('[注文完了] 注文完了画面を表示します.');
  354.         // 受注IDを取得
  355.         $orderId $this->session->get(OrderHelper::SESSION_ORDER_ID);
  356.         if (empty($orderId)) {
  357.             log_info('[注文完了] 受注IDを取得できないため, トップページへ遷移します.');
  358.             return $this->redirectToRoute('homepage');
  359.         }
  360.         $Order $this->orderRepository->find($orderId);
  361.         $event = new EventArgs(
  362.             [
  363.                 'Order' => $Order,
  364.             ],
  365.             $request
  366.         );
  367.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_SHOPPING_COMPLETE_INITIALIZE$event);
  368.         if ($event->getResponse() !== null) {
  369.             return $event->getResponse();
  370.         }
  371.         log_info('[注文完了] 購入フローのセッションをクリアします. ');
  372.         $this->orderHelper->removeSession();
  373.         $hasNextCart = !empty($this->cartService->getCarts());
  374.         log_info('[注文完了] 注文完了画面を表示しました. ', [$hasNextCart]);
  375.         return [
  376.             'Order' => $Order,
  377.             'hasNextCart' => $hasNextCart,
  378.         ];
  379.     }
  380.     /**
  381.      * お届け先選択画面.
  382.      *
  383.      * 会員ログイン時, お届け先を選択する画面を表示する
  384.      * 非会員の場合はこの画面は使用しない。
  385.      *
  386.      * @Route("/shopping/shipping/{id}", name="shopping_shipping", requirements={"id" = "\d+"})
  387.      * @Template("Shopping/shipping.twig")
  388.      */
  389.     public function shipping(Request $requestShipping $Shipping)
  390.     {
  391.         // ログイン状態のチェック.
  392.         if ($this->orderHelper->isLoginRequired()) {
  393.             return $this->redirectToRoute('shopping_login');
  394.         }
  395.         // 受注の存在チェック
  396.         $preOrderId $this->cartService->getPreOrderId();
  397.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  398.         if (!$Order) {
  399.             return $this->redirectToRoute('shopping_error');
  400.         }
  401.         // 受注に紐づくShippingかどうかのチェック.
  402.         if (!$Order->findShipping($Shipping->getId())) {
  403.             return $this->redirectToRoute('shopping_error');
  404.         }
  405.         $builder $this->formFactory->createBuilder(CustomerAddressType::class, null, [
  406.             'customer' => $this->getUser(),
  407.             'shipping' => $Shipping,
  408.         ]);
  409.         $form $builder->getForm();
  410.         $form->handleRequest($request);
  411.         if ($form->isSubmitted() && $form->isValid()) {
  412.             log_info('お届先情報更新開始', [$Shipping->getId()]);
  413.             /** @var CustomerAddress $CustomerAddress */
  414.             $CustomerAddress $form['addresses']->getData();
  415.             // お届け先情報を更新
  416.             $Shipping->setFromCustomerAddress($CustomerAddress);
  417.             // 合計金額の再計算
  418.             $response $this->executePurchaseFlow($Order);
  419.             $this->entityManager->flush();
  420.             if ($response) {
  421.                 return $response;
  422.             }
  423.             $event = new EventArgs(
  424.                 [
  425.                     'Order' => $Order,
  426.                     'Shipping' => $Shipping,
  427.                 ],
  428.                 $request
  429.             );
  430.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_COMPLETE$event);
  431.             log_info('お届先情報更新完了', [$Shipping->getId()]);
  432.             return $this->redirectToRoute('shopping');
  433.         }
  434.         return [
  435.             'form' => $form->createView(),
  436.             'Customer' => $this->getUser(),
  437.             'shippingId' => $Shipping->getId(),
  438.         ];
  439.     }
  440.     /**
  441.      * お届け先の新規作成または編集画面.
  442.      *
  443.      * 会員時は新しいお届け先を作成し, 作成したお届け先を選択状態にして注文手続き画面へ遷移する.
  444.      * 非会員時は選択されたお届け先の編集を行う.
  445.      *
  446.      * @Route("/shopping/shipping_edit/{id}", name="shopping_shipping_edit", requirements={"id" = "\d+"})
  447.      * @Template("Shopping/shipping_edit.twig")
  448.      */
  449.     public function shippingEdit(Request $requestShipping $Shipping)
  450.     {
  451.         // ログイン状態のチェック.
  452.         if ($this->orderHelper->isLoginRequired()) {
  453.             return $this->redirectToRoute('shopping_login');
  454.         }
  455.         // 受注の存在チェック
  456.         $preOrderId $this->cartService->getPreOrderId();
  457.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  458.         if (!$Order) {
  459.             return $this->redirectToRoute('shopping_error');
  460.         }
  461.         // 受注に紐づくShippingかどうかのチェック.
  462.         if (!$Order->findShipping($Shipping->getId())) {
  463.             return $this->redirectToRoute('shopping_error');
  464.         }
  465.         $CustomerAddress = new CustomerAddress();
  466.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  467.             // ログイン時は会員と紐付け
  468.             $CustomerAddress->setCustomer($this->getUser());
  469.         } else {
  470.             // 非会員時はお届け先をセット
  471.             $CustomerAddress->setFromShipping($Shipping);
  472.         }
  473.         $builder $this->formFactory->createBuilder(ShoppingShippingType::class, $CustomerAddress);
  474.         $event = new EventArgs(
  475.             [
  476.                 'builder' => $builder,
  477.                 'Order' => $Order,
  478.                 'Shipping' => $Shipping,
  479.                 'CustomerAddress' => $CustomerAddress,
  480.             ],
  481.             $request
  482.         );
  483.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_INITIALIZE$event);
  484.         $form $builder->getForm();
  485.         $form->handleRequest($request);
  486.         if ($form->isSubmitted() && $form->isValid()) {
  487.             log_info('お届け先追加処理開始', ['order_id' => $Order->getId(), 'shipping_id' => $Shipping->getId()]);
  488.             $Shipping->setFromCustomerAddress($CustomerAddress);
  489.             if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  490.                 $this->entityManager->persist($CustomerAddress);
  491.             }
  492.             // 合計金額の再計算
  493.             $response $this->executePurchaseFlow($Order);
  494.             $this->entityManager->flush();
  495.             if ($response) {
  496.                 return $response;
  497.             }
  498.             $event = new EventArgs(
  499.                 [
  500.                     'form' => $form,
  501.                     'Shipping' => $Shipping,
  502.                     'CustomerAddress' => $CustomerAddress,
  503.                 ],
  504.                 $request
  505.             );
  506.             $this->eventDispatcher->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_COMPLETE$event);
  507.             log_info('お届け先追加処理完了', ['order_id' => $Order->getId(), 'shipping_id' => $Shipping->getId()]);
  508.             return $this->redirectToRoute('shopping');
  509.         }
  510.         return [
  511.             'form' => $form->createView(),
  512.             'shippingId' => $Shipping->getId(),
  513.         ];
  514.     }
  515.     /**
  516.      * ログイン画面.
  517.      *
  518.      * @Route("/shopping/login", name="shopping_login")
  519.      * @Template("Shopping/login.twig")
  520.      */
  521.     public function login(Request $requestAuthenticationUtils $authenticationUtils)
  522.     {
  523.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  524.             return $this->redirectToRoute('shopping');
  525.         }
  526.         /* @var $form \Symfony\Component\Form\FormInterface */
  527.         $builder $this->formFactory->createNamedBuilder(''CustomerLoginType::class);
  528.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  529.             $Customer $this->getUser();
  530.             if ($Customer) {
  531.                 $builder->get('login_email')->setData($Customer->getEmail());
  532.             }
  533.         }
  534.         $event = new EventArgs(
  535.             [
  536.                 'builder' => $builder,
  537.             ],
  538.             $request
  539.         );
  540.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_SHOPPING_LOGIN_INITIALIZE$event);
  541.         $form $builder->getForm();
  542.         return [
  543.             'error' => $authenticationUtils->getLastAuthenticationError(),
  544.             'form' => $form->createView(),
  545.         ];
  546.     }
  547.     /**
  548.      * 購入エラー画面.
  549.      *
  550.      * @Route("/shopping/error", name="shopping_error")
  551.      * @Template("Shopping/shopping_error.twig")
  552.      */
  553.     public function error(Request $requestPurchaseFlow $cartPurchaseFlow)
  554.     {
  555.         // 受注とカートのずれを合わせるため, カートのPurchaseFlowをコールする.
  556.         $Cart $this->cartService->getCart();
  557.         if (null !== $Cart) {
  558.             $cartPurchaseFlow->validate($Cart, new PurchaseContext());
  559.             $this->cartService->setPreOrderId(null);
  560.             $this->cartService->save();
  561.         }
  562.         $event = new EventArgs(
  563.             [],
  564.             $request
  565.         );
  566.         $this->eventDispatcher->dispatch(EccubeEvents::FRONT_SHOPPING_SHIPPING_ERROR_COMPLETE$event);
  567.         if ($event->getResponse() !== null) {
  568.             return $event->getResponse();
  569.         }
  570.         return [];
  571.     }
  572.     /**
  573.      * PaymentMethodをコンテナから取得する.
  574.      *
  575.      * @param Order $Order
  576.      * @param FormInterface $form
  577.      *
  578.      * @return PaymentMethodInterface
  579.      */
  580.     private function createPaymentMethod(Order $OrderFormInterface $form)
  581.     {
  582.         $PaymentMethod $this->container->get($Order->getPayment()->getMethodClass());
  583.         $PaymentMethod->setOrder($Order);
  584.         $PaymentMethod->setFormType($form);
  585.         return $PaymentMethod;
  586.     }
  587.     /**
  588.      * PaymentMethod::applyを実行する.
  589.      *
  590.      * @param PaymentMethodInterface $paymentMethod
  591.      *
  592.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  593.      */
  594.     protected function executeApply(PaymentMethodInterface $paymentMethod)
  595.     {
  596.         $dispatcher $paymentMethod->apply(); // 決済処理中.
  597.         // リンク式決済のように他のサイトへ遷移する場合などは, dispatcherに処理を移譲する.
  598.         if ($dispatcher instanceof PaymentDispatcher) {
  599.             $response $dispatcher->getResponse();
  600.             $this->entityManager->flush();
  601.             // dispatcherがresponseを保持している場合はresponseを返す
  602.             if ($response && ($response->isRedirection() || $response->getContent())) {
  603.                 log_info('[注文処理] PaymentMethod::applyが指定したレスポンスを表示します.');
  604.                 return $response;
  605.             }
  606.             // forwardすることも可能.
  607.             if ($dispatcher->isForward()) {
  608.                 log_info('[注文処理] PaymentMethod::applyによりForwardします.',
  609.                     [$dispatcher->getRoute(), $dispatcher->getPathParameters(), $dispatcher->getQueryParameters()]);
  610.                 return $this->forwardToRoute($dispatcher->getRoute(), $dispatcher->getPathParameters(),
  611.                     $dispatcher->getQueryParameters());
  612.             } else {
  613.                 log_info('[注文処理] PaymentMethod::applyによりリダイレクトします.',
  614.                     [$dispatcher->getRoute(), $dispatcher->getPathParameters(), $dispatcher->getQueryParameters()]);
  615.                 return $this->redirectToRoute($dispatcher->getRoute(),
  616.                     array_merge($dispatcher->getPathParameters(), $dispatcher->getQueryParameters()));
  617.             }
  618.         }
  619.     }
  620.     /**
  621.      * PaymentMethod::checkoutを実行する.
  622.      *
  623.      * @param PaymentMethodInterface $paymentMethod
  624.      *
  625.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  626.      */
  627.     protected function executeCheckout(PaymentMethodInterface $paymentMethod)
  628.     {
  629.         $PaymentResult $paymentMethod->checkout();
  630.         $response $PaymentResult->getResponse();
  631.         // PaymentResultがresponseを保持している場合はresponseを返す
  632.         if ($response && ($response->isRedirection() || $response->getContent())) {
  633.             $this->entityManager->flush();
  634.             log_info('[注文処理] PaymentMethod::checkoutが指定したレスポンスを表示します.');
  635.             return $response;
  636.         }
  637.         // エラー時はロールバックして購入エラーとする.
  638.         if (!$PaymentResult->isSuccess()) {
  639.             $this->entityManager->rollback();
  640.             foreach ($PaymentResult->getErrors() as $error) {
  641.                 $this->addError($error);
  642.             }
  643.             log_info('[注文処理] PaymentMethod::checkoutのエラーのため, 購入エラー画面へ遷移します.', [$PaymentResult->getErrors()]);
  644.             return $this->redirectToRoute('shopping_error');
  645.         }
  646.     }
  647. }