src/Eccube/Controller/ShoppingController.php line 1131

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\Customer;
  14. use Eccube\Entity\CustomerAddress;
  15. use Eccube\Entity\Order;
  16. use Eccube\Entity\Shipping;
  17. use Eccube\Event\EccubeEvents;
  18. use Eccube\Event\EventArgs;
  19. use Eccube\Exception\ShoppingException;
  20. use Eccube\Form\Type\Front\CustomerLoginType;
  21. use Eccube\Form\Type\Front\ShoppingShippingType;
  22. use Eccube\Form\Type\Shopping\CustomerAddressType;
  23. use Eccube\Form\Type\Shopping\OrderType;
  24. use Eccube\Repository\OrderRepository;
  25. use Eccube\Repository\TradeLawRepository;
  26. use Eccube\Service\CartService;
  27. use Eccube\Service\MailService;
  28. use Eccube\Service\OrderHelper;
  29. use Eccube\Service\Payment\PaymentDispatcher;
  30. use Eccube\Service\Payment\PaymentMethodInterface;
  31. use Eccube\Service\PurchaseFlow\PurchaseContext;
  32. use Eccube\Service\PurchaseFlow\PurchaseFlow;
  33. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  34. use Symfony\Component\DependencyInjection\ContainerInterface;
  35. use Symfony\Component\Form\FormInterface;
  36. use Symfony\Component\HttpFoundation\Request;
  37. use Symfony\Component\HttpFoundation\Response;
  38. use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
  39. use Symfony\Component\RateLimiter\RateLimiterFactory;
  40. use Symfony\Component\Routing\Annotation\Route;
  41. use Symfony\Component\Routing\RouterInterface;
  42. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  43. class ShoppingController extends AbstractShoppingController
  44. {
  45.     /**
  46.      * @var CartService
  47.      */
  48.     protected $cartService;
  49.     /**
  50.      * @var MailService
  51.      */
  52.     protected $mailService;
  53.     /**
  54.      * @var OrderHelper
  55.      */
  56.     protected $orderHelper;
  57.     /**
  58.      * @var OrderRepository
  59.      */
  60.     protected $orderRepository;
  61.     /**
  62.      * @var ContainerInterface
  63.      */
  64.     protected $serviceContainer;
  65.     /**
  66.      * @var TradeLawRepository
  67.      */
  68.     protected TradeLawRepository $tradeLawRepository;
  69.     protected RateLimiterFactory $shoppingConfirmIpLimiter;
  70.     protected RateLimiterFactory $shoppingConfirmCustomerLimiter;
  71.     protected RateLimiterFactory $shoppingCheckoutIpLimiter;
  72.     protected RateLimiterFactory $shoppingCheckoutCustomerLimiter;
  73.     public function __construct(
  74.         CartService $cartService,
  75.         MailService $mailService,
  76.         OrderRepository $orderRepository,
  77.         OrderHelper $orderHelper,
  78.         ContainerInterface $serviceContainer,
  79.         TradeLawRepository $tradeLawRepository,
  80.         RateLimiterFactory $shoppingConfirmIpLimiter,
  81.         RateLimiterFactory $shoppingConfirmCustomerLimiter,
  82.         RateLimiterFactory $shoppingCheckoutIpLimiter,
  83.         RateLimiterFactory $shoppingCheckoutCustomerLimiter
  84.     ) {
  85.         $this->cartService $cartService;
  86.         $this->mailService $mailService;
  87.         $this->orderRepository $orderRepository;
  88.         $this->orderHelper $orderHelper;
  89.         $this->serviceContainer $serviceContainer;
  90.         $this->tradeLawRepository $tradeLawRepository;
  91.         $this->shoppingConfirmIpLimiter $shoppingConfirmIpLimiter;
  92.         $this->shoppingConfirmCustomerLimiter $shoppingConfirmCustomerLimiter;
  93.         $this->shoppingCheckoutIpLimiter $shoppingCheckoutIpLimiter;
  94.         $this->shoppingCheckoutCustomerLimiter $shoppingCheckoutCustomerLimiter;
  95.     }
  96.     /**
  97.      * 注文手続き画面を表示する
  98.      *
  99.      * 未ログインまたはRememberMeログインの場合はログイン画面に遷移させる.
  100.      * ただし、非会員でお客様情報を入力済の場合は遷移させない.
  101.      *
  102.      * カート情報から受注データを生成し, `pre_order_id`でカートと受注の紐付けを行う.
  103.      * 既に受注が生成されている場合(pre_order_idで取得できる場合)は, 受注の生成を行わずに画面を表示する.
  104.      *
  105.      * purchaseFlowの集計処理実行後, warningがある場合はカートど同期をとるため, カートのPurchaseFlowを実行する.
  106.      *
  107.      * @Route("/shopping", name="shopping", methods={"GET"})
  108.      * @Template("Shopping/index.twig")
  109.      */
  110.     public function index(PurchaseFlow $cartPurchaseFlow)
  111.     {
  112.         // ログイン状態のチェック.
  113.         if ($this->orderHelper->isLoginRequired()) {
  114.             log_info('[注文手続] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  115.             return $this->redirectToRoute('shopping_login');
  116.         }
  117.         // カートチェック.
  118.         $Cart $this->cartService->getCart();
  119.         if (!($Cart && $this->orderHelper->verifyCart($Cart))) {
  120.             log_info('[注文手続] カートが購入フローへ遷移できない状態のため, カート画面に遷移します.');
  121.             return $this->redirectToRoute('cart');
  122.         }
  123.         // 受注の初期化.
  124.         log_info('[注文手続] 受注の初期化処理を開始します.');
  125.         $Customer $this->getUser() ? $this->getUser() : $this->orderHelper->getNonMember();
  126.         $Order $this->orderHelper->initializeOrder($Cart$Customer);
  127.         // 集計処理.
  128.         log_info('[注文手続] 集計処理を開始します.', [$Order->getId()]);
  129.         $Order->setBuyflg("0");
  130.         $flowResult $this->executePurchaseFlow($Orderfalse);
  131.         $this->entityManager->flush();
  132.         if ($flowResult->hasError()) {
  133.             log_info('[注文手続] Errorが発生したため購入エラー画面へ遷移します.', [$flowResult->getErrors()]);
  134.             return $this->redirectToRoute('shopping_error');
  135.         }
  136.         if ($flowResult->hasWarning()) {
  137.             log_info('[注文手続] Warningが発生しました.', [$flowResult->getWarning()]);
  138.             // 受注明細と同期をとるため, CartPurchaseFlowを実行する
  139.             $cartPurchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  140.             // 注文フローで取得されるカートの入れ替わりを防止する
  141.             // @see https://github.com/EC-CUBE/ec-cube/issues/4293
  142.             $this->cartService->setPrimary($Cart->getCartKey());
  143.         }
  144.         // マイページで会員情報が更新されていれば, Orderの注文者情報も更新する.
  145.         if ($Customer->getId()) {
  146.             $this->orderHelper->updateCustomerInfo($Order$Customer);
  147.             $this->entityManager->flush();
  148.         }
  149.         $activeTradeLaws $this->tradeLawRepository->findBy(['displayOrderScreen' => true], ['sortNo' => 'ASC']);
  150.         $form $this->createForm(OrderType::class, $Order);
  151.         return [
  152.             'form' => $form->createView(),
  153.             'Order' => $Order,
  154.             'activeTradeLaws' => $activeTradeLaws,
  155.         ];
  156.     }
  157.     /**
  158.      * 注文手続き画面を表示する(買取)
  159.      *
  160.      * 未ログインまたはRememberMeログインの場合はログイン画面に遷移させる.
  161.      * ただし、非会員でお客様情報を入力済の場合は遷移させない.
  162.      *
  163.      * カート情報から受注データを生成し, `pre_order_id`でカートと受注の紐付けを行う.
  164.      * 既に受注が生成されている場合(pre_order_idで取得できる場合)は, 受注の生成を行わずに画面を表示する.
  165.      *
  166.      * purchaseFlowの集計処理実行後, warningがある場合はカートど同期をとるため, カートのPurchaseFlowを実行する.
  167.      *
  168.      * @Route("/buyshopping", name="buyshopping", methods={"GET"})
  169.      * @Template("Shopping/index_buy.twig")
  170.      */
  171.     public function buyindex(PurchaseFlow $cartPurchaseFlow)
  172.     {
  173.         // ログイン状態のチェック.
  174.         if ($this->orderHelper->isLoginRequired()) {
  175.             log_info('[注文手続] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  176.             return $this->redirectToRoute('buyshopping_login');
  177.         }
  178.         // カートチェック.
  179.         $Cart $this->cartService->getCart(1);
  180.         if (!($Cart && $this->orderHelper->verifyCart($Cart))) {
  181.             log_info('[注文手続] カートが購入フローへ遷移できない状態のため, カート画面に遷移します.');
  182.             return $this->redirectToRoute('buycart');
  183.         }
  184.         // 受注の初期化.
  185.         log_info('[注文手続] 受注の初期化処理を開始します.');
  186.         $Customer $this->getUser() ? $this->getUser() : $this->orderHelper->getNonMember();
  187.         $Order $this->orderHelper->initializeOrder($Cart$Customer,1);
  188.         $Order->setBuyflg("1");
  189.         $Order $Order->setPaymentTotal($Order->getPaymentTotal() - $Order->getDeliveryFeeTotal());
  190.         $Order $Order->setTotal($Order->getTotal() - $Order->getDeliveryFeeTotal());
  191.         $Order $Order->setDeliveryFeeTotal("0.00");
  192.         // 集計処理.
  193.         log_info('[注文手続] 集計処理を開始します.', [$Order->getId()]);
  194.         
  195.         $flowResult $this->executePurchaseFlow($Orderfalse);
  196.         $this->entityManager->flush();
  197.         if ($flowResult->hasError()) {
  198.             log_info('[注文手続] Errorが発生したため購入エラー画面へ遷移します.', [$flowResult->getErrors()]);
  199.             return $this->redirectToRoute('shopping_error');
  200.         }
  201.         if ($flowResult->hasWarning()) {
  202.             log_info('[注文手続] Warningが発生しました.', [$flowResult->getWarning()]);
  203.             // 受注明細と同期をとるため, CartPurchaseFlowを実行する
  204.             $cartPurchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  205.             // 注文フローで取得されるカートの入れ替わりを防止する
  206.             // @see https://github.com/EC-CUBE/ec-cube/issues/4293
  207.             $this->cartService->setPrimary($Cart->getCartKey());
  208.         }
  209.         // マイページで会員情報が更新されていれば, Orderの注文者情報も更新する.
  210.         if ($Customer->getId()) {
  211.             $this->orderHelper->updateCustomerInfo($Order$Customer);
  212.             $this->entityManager->flush();
  213.         }
  214.         $activeTradeLaws $this->tradeLawRepository->findBy(['displayOrderScreen' => true], ['sortNo' => 'ASC']);
  215.         $form $this->createForm(OrderType::class, $Order);
  216.         
  217.         // $Order = $Order->setPaymentTotal($Order->getPaymentTotal() - $Order->getDeliveryFeeTotal());
  218.         // $Order = $Order->setTotal($Order->getTotal() - $Order->getDeliveryFeeTotal());
  219.         // $Order = $Order->setDeliveryFeeTotal("0.00");
  220.         //dd($Order);
  221.         $Order_items $Order->getOrderItems();
  222.         foreach($Order_items as $item){
  223.             if($item->getProductName() == "手数料" || $item->getProductName() == "送料"){
  224.                 $Order->removeOrderItem($item);
  225.                 continue;
  226.             }
  227.         }
  228.         return [
  229.             'form' => $form->createView(),
  230.             'Order' => $Order,
  231.             'activeTradeLaws' => $activeTradeLaws,
  232.         ];
  233.     }
  234.     /**
  235.      * 他画面への遷移を行う.
  236.      *
  237.      * お届け先編集画面など, 他画面へ遷移する際に, フォームの値をDBに保存してからリダイレクトさせる.
  238.      * フォームの`redirect_to`パラメータの値にリダイレクトを行う.
  239.      * `redirect_to`パラメータはpath('遷移先のルーティング')が渡される必要がある.
  240.      *
  241.      * 外部のURLやPathを渡された場合($router->matchで展開出来ない場合)は, 購入エラーとする.
  242.      *
  243.      * プラグインやカスタマイズでこの機能を使う場合は, twig側で以下のように記述してください.
  244.      *
  245.      * <button data-trigger="click" data-path="path('ルーティング')">更新する</button>
  246.      *
  247.      * data-triggerは, click/change/blur等のイベント名を指定してください。
  248.      * data-pathは任意のパラメータです. 指定しない場合, 注文手続き画面へリダイレクトします.
  249.      *
  250.      * @Route("/shopping/redirect_to", name="shopping_redirect_to", methods={"POST"})
  251.      * @Template("Shopping/index.twig")
  252.      */
  253.     public function redirectTo(Request $requestRouterInterface $router)
  254.     {
  255.         // ログイン状態のチェック.
  256.         if ($this->orderHelper->isLoginRequired()) {
  257.             log_info('[リダイレクト] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  258.             return $this->redirectToRoute('shopping_login');
  259.         }
  260.         // 受注の存在チェック.
  261.         $preOrderId $this->cartService->getPreOrderId();
  262.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  263.         if (!$Order) {
  264.             log_info('[リダイレクト] 購入処理中の受注が存在しません.');
  265.             return $this->redirectToRoute('shopping_error');
  266.         }
  267.         $form $this->createForm(OrderType::class, $Order);
  268.         $form->handleRequest($request);
  269.         if ($form->isSubmitted() && $form->isValid()) {
  270.             log_info('[リダイレクト] 集計処理を開始します.', [$Order->getId()]);
  271.             $response $this->executePurchaseFlow($Order);
  272.             $this->entityManager->flush();
  273.             if ($response) {
  274.                 return $response;
  275.             }
  276.             $redirectTo $form['redirect_to']->getData();
  277.             if (empty($redirectTo)) {
  278.                 log_info('[リダイレクト] リダイレクト先未指定のため注文手続き画面へ遷移します.');
  279.                 return $this->redirectToRoute('shopping');
  280.             }
  281.             try {
  282.                 // リダイレクト先のチェック.
  283.                 $pattern '/^'.preg_quote($request->getBasePath(), '/').'/';
  284.                 $redirectTo preg_replace($pattern''$redirectTo);
  285.                 $result $router->match($redirectTo);
  286.                 // パラメータのみ抽出
  287.                 $params array_filter($result, function ($key) {
  288.                     return !== \strpos($key'_');
  289.                 }, ARRAY_FILTER_USE_KEY);
  290.                 log_info('[リダイレクト] リダイレクトを実行します.', [$result['_route'], $params]);
  291.                 // pathからurlを再構築してリダイレクト.
  292.                 return $this->redirectToRoute($result['_route'], $params);
  293.             } catch (\Exception $e) {
  294.                 log_info('[リダイレクト] URLの形式が不正です', [$redirectTo$e->getMessage()]);
  295.                 return $this->redirectToRoute('shopping_error');
  296.             }
  297.         }
  298.         $activeTradeLaws $this->tradeLawRepository->findBy(['displayOrderScreen' => true], ['sortNo' => 'ASC']);
  299.         log_info('[リダイレクト] フォームエラーのため, 注文手続き画面を表示します.', [$Order->getId()]);
  300.         return [
  301.             'form' => $form->createView(),
  302.             'Order' => $Order,
  303.             'activeTradeLaws' => $activeTradeLaws,
  304.         ];
  305.     }
  306.     /**
  307.      * 注文確認画面を表示する.
  308.      *
  309.      * ここではPaymentMethod::verifyがコールされます.
  310.      * PaymentMethod::verifyではクレジットカードの有効性チェック等, 注文手続きを進められるかどうかのチェック処理を行う事を想定しています.
  311.      * PaymentMethod::verifyでエラーが発生した場合は, 注文手続き画面へリダイレクトします.
  312.      *
  313.      * @Route("/shopping/confirm", name="shopping_confirm", methods={"POST"})
  314.      * @Template("Shopping/confirm.twig")
  315.      */
  316.     public function confirm(Request $request)
  317.     {
  318.         // ログイン状態のチェック.
  319.         if ($this->orderHelper->isLoginRequired()) {
  320.             log_info('[注文確認] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  321.             return $this->redirectToRoute('shopping_login');
  322.         }
  323.         // 受注の存在チェック
  324.         $preOrderId $this->cartService->getPreOrderId();
  325.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  326.         if (!$Order) {
  327.             log_info('[注文確認] 購入処理中の受注が存在しません.', [$preOrderId]);
  328.             return $this->redirectToRoute('shopping_error');
  329.         }
  330.         $activeTradeLaws $this->tradeLawRepository->findBy(['displayOrderScreen' => true], ['sortNo' => 'ASC']);
  331.         $form $this->createForm(OrderType::class, $Order);
  332.         $form->handleRequest($request);
  333.         if ($form->isSubmitted() && $form->isValid()) {
  334.             log_info('[注文確認] 集計処理を開始します.', [$Order->getId()]);
  335.             $response $this->executePurchaseFlow($Order);
  336.             $this->entityManager->flush();
  337.             if ($response) {
  338.                 return $response;
  339.             }
  340.             log_info('[注文確認] IPベースのスロットリングを実行します.');
  341.             $ipLimiter $this->shoppingConfirmIpLimiter->create($request->getClientIp());
  342.             if (!$ipLimiter->consume()->isAccepted()) {
  343.                 log_info('[注文確認] 試行回数制限を超過しました(IPベース)');
  344.                 throw new TooManyRequestsHttpException();
  345.             }
  346.             $Customer $this->getUser();
  347.             if ($Customer instanceof Customer) {
  348.                 log_info('[注文確認] 会員ベースのスロットリングを実行します.');
  349.                 $customerLimiter $this->shoppingConfirmCustomerLimiter->create($Customer->getId());
  350.                 if (!$customerLimiter->consume()->isAccepted()) {
  351.                     log_info('[注文確認] 試行回数制限を超過しました(会員ベース)');
  352.                     throw new TooManyRequestsHttpException();
  353.                 }
  354.             }
  355.             log_info('[注文確認] PaymentMethod::verifyを実行します.', [$Order->getPayment()->getMethodClass()]);
  356.             $paymentMethod $this->createPaymentMethod($Order$form);
  357.             $PaymentResult $paymentMethod->verify();
  358.             if ($PaymentResult) {
  359.                 if (!$PaymentResult->isSuccess()) {
  360.                     $this->entityManager->rollback();
  361.                     foreach ($PaymentResult->getErrors() as $error) {
  362.                         $this->addError($error);
  363.                     }
  364.                     log_info('[注文確認] PaymentMethod::verifyのエラーのため, 注文手続き画面へ遷移します.', [$PaymentResult->getErrors()]);
  365.                     return $this->redirectToRoute('shopping');
  366.                 }
  367.                 $response $PaymentResult->getResponse();
  368.                 if ($response instanceof Response && ($response->isRedirection() || $response->isSuccessful())) {
  369.                     $this->entityManager->flush();
  370.                     log_info('[注文確認] PaymentMethod::verifyが指定したレスポンスを表示します.');
  371.                     return $response;
  372.                 }
  373.             }
  374.             $this->entityManager->flush();
  375.             log_info('[注文確認] 注文確認画面を表示します.');
  376.             return [
  377.                 'form' => $form->createView(),
  378.                 'Order' => $Order,
  379.                 'activeTradeLaws' => $activeTradeLaws,
  380.             ];
  381.         }
  382.         log_info('[注文確認] フォームエラーのため, 注文手続画面を表示します.', [$Order->getId()]);
  383.         $template = new Template([
  384.             'owner' => [$this'confirm'],
  385.             'template' => 'Shopping/index.twig',
  386.         ]);
  387.         $request->attributes->set('_template'$template);
  388.         return [
  389.             'form' => $form->createView(),
  390.             'Order' => $Order,
  391.             'activeTradeLaws' => $activeTradeLaws,
  392.         ];
  393.     }
  394.     /**
  395.      * 注文確認画面を表示する.
  396.      *
  397.      * ここではPaymentMethod::verifyがコールされます.
  398.      * PaymentMethod::verifyではクレジットカードの有効性チェック等, 注文手続きを進められるかどうかのチェック処理を行う事を想定しています.
  399.      * PaymentMethod::verifyでエラーが発生した場合は, 注文手続き画面へリダイレクトします.
  400.      *
  401.      * @Route("/buyshopping/confirm", name="buyshopping_confirm", methods={"POST"})
  402.      * @Template("Shopping/confirm_buy.twig")
  403.      */
  404.     public function buyconfirm(Request $request)
  405.     {
  406.         // ログイン状態のチェック.
  407.         if ($this->orderHelper->isLoginRequired()) {
  408.             log_info('[注文確認] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  409.             return $this->redirectToRoute('buyshopping_login');
  410.         }
  411.         // 受注の存在チェック
  412.         $preOrderId $this->cartService->getPreOrderId(1);
  413.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  414.         if (!$Order) {
  415.             log_info('[注文確認] 購入処理中の受注が存在しません.', [$preOrderId]);
  416.             return $this->redirectToRoute('shopping_error');
  417.         }
  418.         $activeTradeLaws $this->tradeLawRepository->findBy(['displayOrderScreen' => true], ['sortNo' => 'ASC']);
  419.         //$form = $this->createForm(OrderType::class, $Order);
  420.         //allow_extra_fieldsをtrueにすることで、フォームに存在しないフィールドを受け付ける
  421.         $form $this->createForm(OrderType::class, $Order, ['allow_extra_fields' => true]);
  422.         $form->handleRequest($request);
  423.         if ($form->isSubmitted() && $form->isValid()) {
  424.             log_info('[注文確認] 集計処理を開始します.', [$Order->getId()]);
  425.             $response $this->executePurchaseFlow($Order);
  426.             $Order->setBuyflg("1");
  427.             $this->entityManager->flush();
  428.             if ($response) {
  429.                 return $response;
  430.             }
  431.             log_info('[注文確認] IPベースのスロットリングを実行します.');
  432.             $ipLimiter $this->shoppingConfirmIpLimiter->create($request->getClientIp());
  433.             if (!$ipLimiter->consume()->isAccepted()) {
  434.                 log_info('[注文確認] 試行回数制限を超過しました(IPベース)');
  435.                 throw new TooManyRequestsHttpException();
  436.             }
  437.             $Customer $this->getUser();
  438.             if ($Customer instanceof Customer) {
  439.                 log_info('[注文確認] 会員ベースのスロットリングを実行します.');
  440.                 $customerLimiter $this->shoppingConfirmCustomerLimiter->create($Customer->getId());
  441.                 if (!$customerLimiter->consume()->isAccepted()) {
  442.                     log_info('[注文確認] 試行回数制限を超過しました(会員ベース)');
  443.                     throw new TooManyRequestsHttpException();
  444.                 }
  445.             }
  446.             log_info('[注文確認] PaymentMethod::verifyを実行します.', [$Order->getPayment()->getMethodClass()]);
  447.             $paymentMethod $this->createPaymentMethod($Order$form);
  448.             $PaymentResult $paymentMethod->verify();
  449.             if ($PaymentResult) {
  450.                 if (!$PaymentResult->isSuccess()) {
  451.                     $this->entityManager->rollback();
  452.                     foreach ($PaymentResult->getErrors() as $error) {
  453.                         $this->addError($error);
  454.                     }
  455.                     log_info('[注文確認] PaymentMethod::verifyのエラーのため, 注文手続き画面へ遷移します.', [$PaymentResult->getErrors()]);
  456.                     return $this->redirectToRoute('buyshopping');
  457.                 }
  458.                 $response $PaymentResult->getResponse();
  459.                 if ($response instanceof Response && ($response->isRedirection() || $response->isSuccessful())) {
  460.                     $this->entityManager->flush();
  461.                     log_info('[注文確認] PaymentMethod::verifyが指定したレスポンスを表示します.');
  462.                     return $response;
  463.                 }
  464.             }
  465.             $Order $Order->setPaymentTotal($Order->getPaymentTotal() - $Order->getDeliveryFeeTotal());
  466.             $Order $Order->setTotal($Order->getTotal() - $Order->getDeliveryFeeTotal());
  467.             $Order $Order->setDeliveryFeeTotal("0.00");
  468.             $Order_items $Order->getOrderItems();
  469.             foreach($Order_items as $item){
  470.                 if($item->getProductName() == "手数料" || $item->getProductName() == "送料"){
  471.                     $Order->removeOrderItem($item);
  472.                     continue;
  473.                 }
  474.             }
  475.             $this->entityManager->flush();
  476.             log_info('[注文確認] 注文確認画面を表示します.');
  477.             return [
  478.                 'form' => $form->createView(),
  479.                 'Order' => $Order,
  480.                 'activeTradeLaws' => $activeTradeLaws,
  481.             ];
  482.         }
  483.         log_info('[注文確認] フォームエラーのため, 注文手続画面を表示します.', [$Order->getId()]);
  484.         $template = new Template([
  485.             'owner' => [$this'confirm'],
  486.             'template' => 'Shopping/index_buy.twig',
  487.         ]);
  488.         $request->attributes->set('_template'$template);
  489.         return [
  490.             'form' => $form->createView(),
  491.             'Order' => $Order,
  492.             'activeTradeLaws' => $activeTradeLaws,
  493.         ];
  494.     }
  495.     /**
  496.      * 注文処理を行う.
  497.      *
  498.      * 決済プラグインによる決済処理および注文の確定処理を行います.
  499.      *
  500.      * @Route("/shopping/checkout", name="shopping_checkout", methods={"POST"})
  501.      * @Template("Shopping/confirm.twig")
  502.      */
  503.     public function checkout(Request $request)
  504.     {
  505.         // ログイン状態のチェック.
  506.         if ($this->orderHelper->isLoginRequired()) {
  507.             log_info('[注文処理] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  508.             return $this->redirectToRoute('shopping_login');
  509.         }
  510.         // 受注の存在チェック
  511.         $preOrderId $this->cartService->getPreOrderId();
  512.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  513.         if (!$Order) {
  514.             log_info('[注文処理] 購入処理中の受注が存在しません.', [$preOrderId]);
  515.             return $this->redirectToRoute('shopping_error');
  516.         }
  517.         // フォームの生成.
  518.         $form $this->createForm(OrderType::class, $Order, [
  519.             // 確認画面から注文処理へ遷移する場合は, Orderエンティティで値を引き回すためフォーム項目の定義をスキップする.
  520.             'skip_add_form' => true,
  521.         ]);
  522.         $form->handleRequest($request);
  523.         if ($form->isSubmitted() && $form->isValid()) {
  524.             log_info('[注文処理] 注文処理を開始します.', [$Order->getId()]);
  525.             try {
  526.                 /*
  527.                  * 集計処理
  528.                  */
  529.                 log_info('[注文処理] 集計処理を開始します.', [$Order->getId()]);
  530.                 $response $this->executePurchaseFlow($Order);
  531.                 $this->entityManager->flush();
  532.                 if ($response) {
  533.                     return $response;
  534.                 }
  535.                 log_info('[注文完了] IPベースのスロットリングを実行します.');
  536.                 $ipLimiter $this->shoppingCheckoutIpLimiter->create($request->getClientIp());
  537.                 if (!$ipLimiter->consume()->isAccepted()) {
  538.                     log_info('[注文完了] 試行回数制限を超過しました(IPベース)');
  539.                     throw new TooManyRequestsHttpException();
  540.                 }
  541.                 $Customer $this->getUser();
  542.                 if ($Customer instanceof Customer) {
  543.                     log_info('[注文完了] 会員ベースのスロットリングを実行します.');
  544.                     $customerLimiter $this->shoppingCheckoutCustomerLimiter->create($Customer->getId());
  545.                     if (!$customerLimiter->consume()->isAccepted()) {
  546.                         log_info('[注文完了] 試行回数制限を超過しました(会員ベース)');
  547.                         throw new TooManyRequestsHttpException();
  548.                     }
  549.                 }
  550.                 log_info('[注文処理] PaymentMethodを取得します.', [$Order->getPayment()->getMethodClass()]);
  551.                 $paymentMethod $this->createPaymentMethod($Order$form);
  552.                 /*
  553.                  * 決済実行(前処理)
  554.                  */
  555.                 log_info('[注文処理] PaymentMethod::applyを実行します.');
  556.                 if ($response $this->executeApply($paymentMethod)) {
  557.                     return $response;
  558.                 }
  559.                 /*
  560.                  * 決済実行
  561.                  *
  562.                  * PaymentMethod::checkoutでは決済処理が行われ, 正常に処理出来た場合はPurchaseFlow::commitがコールされます.
  563.                  */
  564.                 log_info('[注文処理] PaymentMethod::checkoutを実行します.');
  565.                 if ($response $this->executeCheckout($paymentMethod)) {
  566.                     return $response;
  567.                 }
  568.                 $this->entityManager->flush();
  569.                 log_info('[注文処理] 注文処理が完了しました.', [$Order->getId()]);
  570.             } catch (ShoppingException $e) {
  571.                 log_error('[注文処理] 購入エラーが発生しました.', [$e->getMessage()]);
  572.                 $this->entityManager->rollback();
  573.                 $this->addError($e->getMessage());
  574.                 return $this->redirectToRoute('shopping_error');
  575.             } catch (\Exception $e) {
  576.                 log_error('[注文処理] 予期しないエラーが発生しました.', [$e->getMessage()]);
  577.                 // $this->entityManager->rollback(); FIXME ユニットテストで There is no active transaction エラーになってしまう
  578.                 $this->addError('front.shopping.system_error');
  579.                 return $this->redirectToRoute('shopping_error');
  580.             }
  581.             // カート削除
  582.             log_info('[注文処理] カートをクリアします.', [$Order->getId()]);
  583.             $this->cartService->clear();
  584.             // 受注IDをセッションにセット
  585.             $this->session->set(OrderHelper::SESSION_ORDER_ID$Order->getId());
  586.             // メール送信
  587.             log_info('[注文処理] 注文メールの送信を行います.', [$Order->getId()]);
  588.             $this->mailService->sendOrderMail($Order);
  589.             $this->entityManager->flush();
  590.             log_info('[注文処理] 注文処理が完了しました. 購入完了画面へ遷移します.', [$Order->getId()]);
  591.             return $this->redirectToRoute('shopping_complete');
  592.         }
  593.         log_info('[注文処理] フォームエラーのため, 購入エラー画面へ遷移します.', [$Order->getId()]);
  594.         return $this->redirectToRoute('shopping_error');
  595.     }
  596.     /**
  597.      * 注文処理を行う.
  598.      *
  599.      * 決済プラグインによる決済処理および注文の確定処理を行います.
  600.      *
  601.      * @Route("/buyshopping/checkout", name="buyshopping_checkout", methods={"POST"})
  602.      * @Template("Shopping/confirm_buy.twig")
  603.      */
  604.     public function buycheckout(Request $request)
  605.     {
  606.         // ログイン状態のチェック.
  607.         if ($this->orderHelper->isLoginRequired()) {
  608.             log_info('[注文処理] 未ログインもしくはRememberMeログインのため, ログイン画面に遷移します.');
  609.             return $this->redirectToRoute('buyshopping_login');
  610.         }
  611.         // 受注の存在チェック
  612.         $preOrderId $this->cartService->getPreOrderId(1);
  613.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  614.         if (!$Order) {
  615.             log_info('[注文処理] 購入処理中の受注が存在しません.', [$preOrderId]);
  616.             return $this->redirectToRoute('shopping_error');
  617.         }
  618.         // フォームの生成.
  619.         $form $this->createForm(OrderType::class, $Order, [
  620.             // 確認画面から注文処理へ遷移する場合は, Orderエンティティで値を引き回すためフォーム項目の定義をスキップする.
  621.             'skip_add_form' => true,
  622.             //allow_extra_fieldsをtrueにすることで、フォームに存在しないフィールドを受け付ける
  623.             'allow_extra_fields' => true,
  624.         ]);
  625.         $form->handleRequest($request);
  626.         if ($form->isSubmitted() && $form->isValid()) {
  627.             log_info('[注文処理] 注文処理を開始します.', [$Order->getId()]);
  628.             try {
  629.                 /*
  630.                  * 集計処理
  631.                  */
  632.                 log_info('[注文処理] 集計処理を開始します.', [$Order->getId()]);
  633.                 $Order->setBuyflg("1");
  634.                 $response $this->executePurchaseFlow($Order);
  635.                 $this->entityManager->flush();
  636.                 // if ($response) {
  637.                 //     return $response;
  638.                 // }
  639.                 log_info('[注文完了] IPベースのスロットリングを実行します.');
  640.                 $ipLimiter $this->shoppingCheckoutIpLimiter->create($request->getClientIp());
  641.                 if (!$ipLimiter->consume()->isAccepted()) {
  642.                     log_info('[注文完了] 試行回数制限を超過しました(IPベース)');
  643.                     throw new TooManyRequestsHttpException();
  644.                 }
  645.                 $Customer $this->getUser();
  646.                 if ($Customer instanceof Customer) {
  647.                     log_info('[注文完了] 会員ベースのスロットリングを実行します.');
  648.                     $customerLimiter $this->shoppingCheckoutCustomerLimiter->create($Customer->getId());
  649.                     if (!$customerLimiter->consume()->isAccepted()) {
  650.                         log_info('[注文完了] 試行回数制限を超過しました(会員ベース)');
  651.                         throw new TooManyRequestsHttpException();
  652.                     }
  653.                 }
  654.                 log_info('[注文処理] PaymentMethodを取得します.', [$Order->getPayment()->getMethodClass()]);
  655.                 $paymentMethod $this->createPaymentMethod($Order$form);
  656.                 /*
  657.                  * 決済実行(前処理)
  658.                  */
  659.                 log_info('[注文処理] PaymentMethod::applyを実行します.');
  660.                 if ($response $this->executeApply($paymentMethod)) {
  661.                     return $response;
  662.                 }
  663.                 /*
  664.                  * 決済実行
  665.                  *
  666.                  * PaymentMethod::checkoutでは決済処理が行われ, 正常に処理出来た場合はPurchaseFlow::commitがコールされます.
  667.                  */
  668.                 log_info('[注文処理] PaymentMethod::checkoutを実行します.');
  669.                 if ($response $this->executeCheckout($paymentMethod)) {
  670.                     return $response;
  671.                 }
  672.                 $this->entityManager->flush();
  673.                 log_info('[注文処理] 注文処理が完了しました.', [$Order->getId()]);
  674.             } catch (ShoppingException $e) {
  675.                 log_error('[注文処理] 購入エラーが発生しました.', [$e->getMessage()]);
  676.                 $this->entityManager->rollback();
  677.                 $this->addError($e->getMessage());
  678.                 return $this->redirectToRoute('shopping_error');
  679.             } catch (\Exception $e) {
  680.                 log_error('[注文処理] 予期しないエラーが発生しました.', [$e->getMessage()]);
  681.                 // $this->entityManager->rollback(); FIXME ユニットテストで There is no active transaction エラーになってしまう
  682.                 $this->addError('front.shopping.system_error');
  683.                 return $this->redirectToRoute('shopping_error');
  684.             }
  685.             // カート削除
  686.             log_info('[注文処理] カートをクリアします.', [$Order->getId()]);
  687.             $this->cartService->clear();
  688.             // 受注IDをセッションにセット
  689.             $this->session->set(OrderHelper::SESSION_ORDER_ID$Order->getId());
  690.             // メール送信
  691.             log_info('[注文処理] 注文メールの送信を行います.', [$Order->getId()]);
  692.             $this->mailService->sendOrderMail($Order 1);
  693.             $this->entityManager->flush();
  694.             log_info('[注文処理] 注文処理が完了しました. 購入完了画面へ遷移します.', [$Order->getId()]);
  695.             return $this->redirectToRoute('buyshopping_complete');
  696.         }
  697.         log_info('[注文処理] フォームエラーのため, 購入エラー画面へ遷移します.', [$Order->getId()]);
  698.         return $this->redirectToRoute('shopping_error');
  699.     }
  700.     /**
  701.      * 購入完了画面を表示する.
  702.      *
  703.      * @Route("/shopping/complete", name="shopping_complete", methods={"GET"})
  704.      * @Template("Shopping/complete.twig")
  705.      */
  706.     public function complete(Request $request)
  707.     {
  708.         log_info('[注文完了] 注文完了画面を表示します.');
  709.         // 受注IDを取得
  710.         $orderId $this->session->get(OrderHelper::SESSION_ORDER_ID);
  711.         if (empty($orderId)) {
  712.             log_info('[注文完了] 受注IDを取得できないため, トップページへ遷移します.');
  713.             return $this->redirectToRoute('homepage');
  714.         }
  715.         $Order $this->orderRepository->find($orderId);
  716.         $event = new EventArgs(
  717.             [
  718.                 'Order' => $Order,
  719.             ],
  720.             $request
  721.         );
  722.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_COMPLETE_INITIALIZE);
  723.         if ($event->getResponse() !== null) {
  724.             return $event->getResponse();
  725.         }
  726.         log_info('[注文完了] 購入フローのセッションをクリアします. ');
  727.         $this->orderHelper->removeSession();
  728.         $hasNextCart = !empty($this->cartService->getCarts());
  729.         log_info('[注文完了] 注文完了画面を表示しました. ', [$hasNextCart]);
  730.         return [
  731.             'Order' => $Order,
  732.             'hasNextCart' => $hasNextCart,
  733.         ];
  734.     }
  735.     /**
  736.      * 購入完了画面を表示する.
  737.      *
  738.      * @Route("/buyshopping/complete", name="buyshopping_complete", methods={"GET"})
  739.      * @Template("Shopping/complete_buy.twig")
  740.      */
  741.     public function buycomplete(Request $request)
  742.     {
  743.         log_info('[注文完了] 注文完了画面を表示します.');
  744.         // 受注IDを取得
  745.         $orderId $this->session->get(OrderHelper::SESSION_ORDER_ID);
  746.         if (empty($orderId)) {
  747.             log_info('[注文完了] 受注IDを取得できないため, トップページへ遷移します.');
  748.             return $this->redirectToRoute('homepage');
  749.         }
  750.         $Order $this->orderRepository->find($orderId);
  751.         $event = new EventArgs(
  752.             [
  753.                 'Order' => $Order,
  754.             ],
  755.             $request
  756.         );
  757.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_COMPLETE_INITIALIZE);
  758.         if ($event->getResponse() !== null) {
  759.             return $event->getResponse();
  760.         }
  761.         log_info('[注文完了] 購入フローのセッションをクリアします. ');
  762.         $this->orderHelper->removeSession();
  763.         $hasNextCart = !empty($this->cartService->getCarts(false 1));
  764.         log_info('[注文完了] 注文完了画面を表示しました. ', [$hasNextCart]);
  765.         return [
  766.             'Order' => $Order,
  767.             'hasNextCart' => $hasNextCart,
  768.         ];
  769.     }
  770.     /**
  771.      * お届け先選択画面.
  772.      *
  773.      * 会員ログイン時, お届け先を選択する画面を表示する
  774.      * 非会員の場合はこの画面は使用しない。
  775.      *
  776.      * @Route("/shopping/shipping/{id}", name="shopping_shipping", requirements={"id" = "\d+"}, methods={"GET", "POST"})
  777.      * @Template("Shopping/shipping.twig")
  778.      */
  779.     public function shipping(Request $requestShipping $Shipping)
  780.     {
  781.         // ログイン状態のチェック.
  782.         if ($this->orderHelper->isLoginRequired()) {
  783.             return $this->redirectToRoute('shopping_login');
  784.         }
  785.         // 受注の存在チェック
  786.         $preOrderId $this->cartService->getPreOrderId();
  787.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  788.         if (!$Order) {
  789.             return $this->redirectToRoute('shopping_error');
  790.         }
  791.         // 受注に紐づくShippingかどうかのチェック.
  792.         if (!$Order->findShipping($Shipping->getId())) {
  793.             return $this->redirectToRoute('shopping_error');
  794.         }
  795.         $builder $this->formFactory->createBuilder(CustomerAddressType::class, null, [
  796.             'customer' => $this->getUser(),
  797.             'shipping' => $Shipping,
  798.         ]);
  799.         $form $builder->getForm();
  800.         $form->handleRequest($request);
  801.         if ($form->isSubmitted() && $form->isValid()) {
  802.             log_info('お届先情報更新開始', [$Shipping->getId()]);
  803.             /** @var CustomerAddress $CustomerAddress */
  804.             $CustomerAddress $form['addresses']->getData();
  805.             // お届け先情報を更新
  806.             $Shipping->setFromCustomerAddress($CustomerAddress);
  807.             // 合計金額の再計算
  808.             $response $this->executePurchaseFlow($Order);
  809.             $this->entityManager->flush();
  810.             if ($response) {
  811.                 return $response;
  812.             }
  813.             $event = new EventArgs(
  814.                 [
  815.                     'Order' => $Order,
  816.                     'Shipping' => $Shipping,
  817.                 ],
  818.                 $request
  819.             );
  820.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_SHIPPING_COMPLETE);
  821.             log_info('お届先情報更新完了', [$Shipping->getId()]);
  822.             return $this->redirectToRoute('shopping');
  823.         }
  824.         return [
  825.             'form' => $form->createView(),
  826.             'Customer' => $this->getUser(),
  827.             'shippingId' => $Shipping->getId(),
  828.         ];
  829.     }
  830.     /**
  831.      * お届け先の新規作成または編集画面.
  832.      *
  833.      * 会員時は新しいお届け先を作成し, 作成したお届け先を選択状態にして注文手続き画面へ遷移する.
  834.      * 非会員時は選択されたお届け先の編集を行う.
  835.      *
  836.      * @Route("/shopping/shipping_edit/{id}", name="shopping_shipping_edit", requirements={"id" = "\d+"}, methods={"GET", "POST"})
  837.      * @Template("Shopping/shipping_edit.twig")
  838.      */
  839.     public function shippingEdit(Request $requestShipping $Shipping)
  840.     {
  841.         // ログイン状態のチェック.
  842.         if ($this->orderHelper->isLoginRequired()) {
  843.             return $this->redirectToRoute('shopping_login');
  844.         }
  845.         // 受注の存在チェック
  846.         $preOrderId $this->cartService->getPreOrderId();
  847.         $Order $this->orderHelper->getPurchaseProcessingOrder($preOrderId);
  848.         if (!$Order) {
  849.             return $this->redirectToRoute('shopping_error');
  850.         }
  851.         // 受注に紐づくShippingかどうかのチェック.
  852.         if (!$Order->findShipping($Shipping->getId())) {
  853.             return $this->redirectToRoute('shopping_error');
  854.         }
  855.         $CustomerAddress = new CustomerAddress();
  856.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  857.             // ログイン時は会員と紐付け
  858.             $CustomerAddress->setCustomer($this->getUser());
  859.         } else {
  860.             // 非会員時はお届け先をセット
  861.             $CustomerAddress->setFromShipping($Shipping);
  862.         }
  863.         $builder $this->formFactory->createBuilder(ShoppingShippingType::class, $CustomerAddress);
  864.         $event = new EventArgs(
  865.             [
  866.                 'builder' => $builder,
  867.                 'Order' => $Order,
  868.                 'Shipping' => $Shipping,
  869.                 'CustomerAddress' => $CustomerAddress,
  870.             ],
  871.             $request
  872.         );
  873.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_INITIALIZE);
  874.         $form $builder->getForm();
  875.         $form->handleRequest($request);
  876.         if ($form->isSubmitted() && $form->isValid()) {
  877.             log_info('お届け先追加処理開始', ['order_id' => $Order->getId(), 'shipping_id' => $Shipping->getId()]);
  878.             $Shipping->setFromCustomerAddress($CustomerAddress);
  879.             if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  880.                 $this->entityManager->persist($CustomerAddress);
  881.             }
  882.             // 合計金額の再計算
  883.             $response $this->executePurchaseFlow($Order);
  884.             $this->entityManager->flush();
  885.             if ($response) {
  886.                 return $response;
  887.             }
  888.             $event = new EventArgs(
  889.                 [
  890.                     'form' => $form,
  891.                     'Shipping' => $Shipping,
  892.                     'CustomerAddress' => $CustomerAddress,
  893.                 ],
  894.                 $request
  895.             );
  896.             $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_SHIPPING_EDIT_COMPLETE);
  897.             log_info('お届け先追加処理完了', ['order_id' => $Order->getId(), 'shipping_id' => $Shipping->getId()]);
  898.             return $this->redirectToRoute('shopping');
  899.         }
  900.         return [
  901.             'form' => $form->createView(),
  902.             'shippingId' => $Shipping->getId(),
  903.         ];
  904.     }
  905.     /**
  906.      * ログイン画面.
  907.      *
  908.      * @Route("/shopping/login", name="shopping_login", methods={"GET"})
  909.      * @Template("Shopping/login.twig")
  910.      */
  911.     public function login(Request $requestAuthenticationUtils $authenticationUtils)
  912.     {
  913.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  914.             return $this->redirectToRoute('shopping');
  915.         }
  916.         /* @var $form \Symfony\Component\Form\FormInterface */
  917.         $builder $this->formFactory->createNamedBuilder(''CustomerLoginType::class);
  918.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  919.             $Customer $this->getUser();
  920.             if ($Customer) {
  921.                 $builder->get('login_email')->setData($Customer->getEmail());
  922.             }
  923.         }
  924.         $event = new EventArgs(
  925.             [
  926.                 'builder' => $builder,
  927.             ],
  928.             $request
  929.         );
  930.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_LOGIN_INITIALIZE);
  931.         $form $builder->getForm();
  932.         return [
  933.             'error' => $authenticationUtils->getLastAuthenticationError(),
  934.             'form' => $form->createView(),
  935.         ];
  936.     }
  937.     /**
  938.      * ログイン画面(買取).
  939.      *
  940.      * @Route("/buyshopping/login", name="buyshopping_login", methods={"GET"})
  941.      * @Template("Shopping/login_buy.twig")
  942.      */
  943.     public function buylogin(Request $requestAuthenticationUtils $authenticationUtils)
  944.     {
  945.         if ($this->isGranted('IS_AUTHENTICATED_FULLY')) {
  946.             return $this->redirectToRoute('buyshopping');
  947.         }
  948.         /* @var $form \Symfony\Component\Form\FormInterface */
  949.         $builder $this->formFactory->createNamedBuilder(''CustomerLoginType::class);
  950.         if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  951.             $Customer $this->getUser();
  952.             if ($Customer) {
  953.                 $builder->get('login_email')->setData($Customer->getEmail());
  954.             }
  955.         }
  956.         $event = new EventArgs(
  957.             [
  958.                 'builder' => $builder,
  959.             ],
  960.             $request
  961.         );
  962.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_LOGIN_INITIALIZE);
  963.         $form $builder->getForm();
  964.         return [
  965.             'error' => $authenticationUtils->getLastAuthenticationError(),
  966.             'form' => $form->createView(),
  967.         ];
  968.     }
  969.     /**
  970.      * 購入エラー画面.
  971.      *
  972.      * @Route("/shopping/error", name="shopping_error", methods={"GET"})
  973.      * @Template("Shopping/shopping_error.twig")
  974.      */
  975.     public function error(Request $requestPurchaseFlow $cartPurchaseFlow)
  976.     {
  977.         // 受注とカートのずれを合わせるため, カートのPurchaseFlowをコールする.
  978.         $Cart $this->cartService->getCart();
  979.         if (null !== $Cart) {
  980.             $cartPurchaseFlow->validate($Cart, new PurchaseContext($Cart$this->getUser()));
  981.             $this->cartService->setPreOrderId(null);
  982.             $this->cartService->save();
  983.         }
  984.         $event = new EventArgs(
  985.             [],
  986.             $request
  987.         );
  988.         $this->eventDispatcher->dispatch($eventEccubeEvents::FRONT_SHOPPING_SHIPPING_ERROR_COMPLETE);
  989.         if ($event->getResponse() !== null) {
  990.             return $event->getResponse();
  991.         }
  992.         return [];
  993.     }
  994.     /**
  995.      * PaymentMethodをコンテナから取得する.
  996.      *
  997.      * @param Order $Order
  998.      * @param FormInterface $form
  999.      *
  1000.      * @return PaymentMethodInterface
  1001.      */
  1002.     private function createPaymentMethod(Order $OrderFormInterface $form)
  1003.     {
  1004.         $PaymentMethod $this->serviceContainer->get($Order->getPayment()->getMethodClass());
  1005.         $PaymentMethod->setOrder($Order);
  1006.         $PaymentMethod->setFormType($form);
  1007.         return $PaymentMethod;
  1008.     }
  1009.     /**
  1010.      * PaymentMethod::applyを実行する.
  1011.      *
  1012.      * @param PaymentMethodInterface $paymentMethod
  1013.      *
  1014.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
  1015.      */
  1016.     protected function executeApply(PaymentMethodInterface $paymentMethod)
  1017.     {
  1018.         $dispatcher $paymentMethod->apply(); // 決済処理中.
  1019.         // リンク式決済のように他のサイトへ遷移する場合などは, dispatcherに処理を移譲する.
  1020.         if ($dispatcher instanceof PaymentDispatcher) {
  1021.             $response $dispatcher->getResponse();
  1022.             $this->entityManager->flush();
  1023.             // dispatcherがresponseを保持している場合はresponseを返す
  1024.             if ($response instanceof Response && ($response->isRedirection() || $response->isSuccessful())) {
  1025.                 log_info('[注文処理] PaymentMethod::applyが指定したレスポンスを表示します.');
  1026.                 return $response;
  1027.             }
  1028.             // forwardすることも可能.
  1029.             if ($dispatcher->isForward()) {
  1030.                 log_info('[注文処理] PaymentMethod::applyによりForwardします.',
  1031.                     [$dispatcher->getRoute(), $dispatcher->getPathParameters(), $dispatcher->getQueryParameters()]);
  1032.                 return $this->forwardToRoute($dispatcher->getRoute(), $dispatcher->getPathParameters(),
  1033.                     $dispatcher->getQueryParameters());
  1034.             } else {
  1035.                 log_info('[注文処理] PaymentMethod::applyによりリダイレクトします.',
  1036.                     [$dispatcher->getRoute(), $dispatcher->getPathParameters(), $dispatcher->getQueryParameters()]);
  1037.                 return $this->redirectToRoute($dispatcher->getRoute(),
  1038.                     array_merge($dispatcher->getPathParameters(), $dispatcher->getQueryParameters()));
  1039.             }
  1040.         }
  1041.     }
  1042.     /**
  1043.      * PaymentMethod::checkoutを実行する.
  1044.      *
  1045.      * @param PaymentMethodInterface $paymentMethod
  1046.      *
  1047.      * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response|null
  1048.      */
  1049.     protected function executeCheckout(PaymentMethodInterface $paymentMethod)
  1050.     {
  1051.         $PaymentResult $paymentMethod->checkout();
  1052.         $response $PaymentResult->getResponse();
  1053.         // PaymentResultがresponseを保持している場合はresponseを返す
  1054.         if ($response instanceof Response && ($response->isRedirection() || $response->isSuccessful())) {
  1055.             $this->entityManager->flush();
  1056.             log_info('[注文処理] PaymentMethod::checkoutが指定したレスポンスを表示します.');
  1057.             return $response;
  1058.         }
  1059.         // エラー時はロールバックして購入エラーとする.
  1060.         if (!$PaymentResult->isSuccess()) {
  1061.             $this->entityManager->rollback();
  1062.             foreach ($PaymentResult->getErrors() as $error) {
  1063.                 $this->addError($error);
  1064.             }
  1065.             log_info('[注文処理] PaymentMethod::checkoutのエラーのため, 購入エラー画面へ遷移します.', [$PaymentResult->getErrors()]);
  1066.             return $this->redirectToRoute('shopping_error');
  1067.         }
  1068.         return null;
  1069.     }
  1070. }