vendor/shopware/storefront/Controller/AccountOrderController.php line 113

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Checkout\Cart\Exception\OrderNotFoundException;
  4. use Shopware\Core\Checkout\Cart\Exception\OrderPaymentMethodNotChangeable;
  5. use Shopware\Core\Checkout\Customer\Exception\CustomerAuthThrottledException;
  6. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
  7. use Shopware\Core\Checkout\Order\Exception\GuestNotAuthenticatedException;
  8. use Shopware\Core\Checkout\Order\Exception\WrongGuestCredentialsException;
  9. use Shopware\Core\Checkout\Order\OrderEntity;
  10. use Shopware\Core\Checkout\Order\SalesChannel\AbstractCancelOrderRoute;
  11. use Shopware\Core\Checkout\Order\SalesChannel\AbstractOrderRoute;
  12. use Shopware\Core\Checkout\Order\SalesChannel\AbstractSetPaymentOrderRoute;
  13. use Shopware\Core\Checkout\Order\SalesChannel\OrderService;
  14. use Shopware\Core\Checkout\Payment\Exception\PaymentProcessException;
  15. use Shopware\Core\Checkout\Payment\SalesChannel\AbstractHandlePaymentMethodRoute;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  18. use Shopware\Core\Framework\Routing\Annotation\LoginRequired;
  19. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  20. use Shopware\Core\Framework\Routing\Annotation\Since;
  21. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  22. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  23. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceInterface;
  24. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextServiceParameters;
  25. use Shopware\Core\System\SalesChannel\SalesChannel\AbstractContextSwitchRoute;
  26. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  27. use Shopware\Core\System\SystemConfig\SystemConfigService;
  28. use Shopware\Storefront\Event\RouteRequest\CancelOrderRouteRequestEvent;
  29. use Shopware\Storefront\Event\RouteRequest\HandlePaymentMethodRouteRequestEvent;
  30. use Shopware\Storefront\Event\RouteRequest\SetPaymentOrderRouteRequestEvent;
  31. use Shopware\Storefront\Framework\Routing\Annotation\NoStore;
  32. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedHook;
  33. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoader;
  34. use Shopware\Storefront\Page\Account\Order\AccountOrderDetailPageLoadedHook;
  35. use Shopware\Storefront\Page\Account\Order\AccountOrderDetailPageLoader;
  36. use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoadedHook;
  37. use Shopware\Storefront\Page\Account\Order\AccountOrderPageLoader;
  38. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  39. use Symfony\Component\HttpFoundation\Request;
  40. use Symfony\Component\HttpFoundation\Response;
  41. use Symfony\Component\Routing\Annotation\Route;
  42. /**
  43.  * @Route(defaults={"_routeScope"={"storefront"}})
  44.  *
  45.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  46.  */
  47. class AccountOrderController extends StorefrontController
  48. {
  49.     private AccountOrderPageLoader $orderPageLoader;
  50.     private AbstractContextSwitchRoute $contextSwitchRoute;
  51.     private AccountEditOrderPageLoader $accountEditOrderPageLoader;
  52.     private AbstractCancelOrderRoute $cancelOrderRoute;
  53.     private AbstractSetPaymentOrderRoute $setPaymentOrderRoute;
  54.     private AbstractHandlePaymentMethodRoute $handlePaymentMethodRoute;
  55.     private EventDispatcherInterface $eventDispatcher;
  56.     private AccountOrderDetailPageLoader $orderDetailPageLoader;
  57.     private AbstractOrderRoute $orderRoute;
  58.     private SalesChannelContextServiceInterface $contextService;
  59.     private SystemConfigService $systemConfigService;
  60.     private OrderService $orderService;
  61.     /**
  62.      * @internal
  63.      */
  64.     public function __construct(
  65.         AccountOrderPageLoader $orderPageLoader,
  66.         AccountEditOrderPageLoader $accountEditOrderPageLoader,
  67.         AbstractContextSwitchRoute $contextSwitchRoute,
  68.         AbstractCancelOrderRoute $cancelOrderRoute,
  69.         AbstractSetPaymentOrderRoute $setPaymentOrderRoute,
  70.         AbstractHandlePaymentMethodRoute $handlePaymentMethodRoute,
  71.         EventDispatcherInterface $eventDispatcher,
  72.         AccountOrderDetailPageLoader $orderDetailPageLoader,
  73.         AbstractOrderRoute $orderRoute,
  74.         SalesChannelContextServiceInterface $contextService,
  75.         SystemConfigService $systemConfigService,
  76.         OrderService $orderService
  77.     ) {
  78.         $this->orderPageLoader $orderPageLoader;
  79.         $this->contextSwitchRoute $contextSwitchRoute;
  80.         $this->accountEditOrderPageLoader $accountEditOrderPageLoader;
  81.         $this->cancelOrderRoute $cancelOrderRoute;
  82.         $this->setPaymentOrderRoute $setPaymentOrderRoute;
  83.         $this->handlePaymentMethodRoute $handlePaymentMethodRoute;
  84.         $this->eventDispatcher $eventDispatcher;
  85.         $this->orderDetailPageLoader $orderDetailPageLoader;
  86.         $this->orderRoute $orderRoute;
  87.         $this->contextService $contextService;
  88.         $this->systemConfigService $systemConfigService;
  89.         $this->orderService $orderService;
  90.     }
  91.     /**
  92.      * @Since("6.0.0.0")
  93.      * @Route("/account/order", name="frontend.account.order.page", options={"seo"="false"}, methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true, "_loginRequired"=true, "_loginRequiredAllowGuest"=true})
  94.      * @Route("/account/order", name="frontend.account.order.page", options={"seo"="false"}, methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true})
  95.      * @NoStore
  96.      */
  97.     public function orderOverview(Request $requestSalesChannelContext $context): Response
  98.     {
  99.         $page $this->orderPageLoader->load($request$context);
  100.         $this->hook(new AccountOrderPageLoadedHook($page$context));
  101.         return $this->renderStorefront('@Storefront/storefront/page/account/order-history/index.html.twig', ['page' => $page]);
  102.     }
  103.     /**
  104.      * @Since("6.2.0.0")
  105.      * @Route("/account/order/cancel", name="frontend.account.order.cancel", methods={"POST"})
  106.      */
  107.     public function cancelOrder(Request $requestSalesChannelContext $context): Response
  108.     {
  109.         $cancelOrderRequest = new Request();
  110.         $cancelOrderRequest->request->set('orderId'$request->get('orderId'));
  111.         $cancelOrderRequest->request->set('transition''cancel');
  112.         $event = new CancelOrderRouteRequestEvent($request$cancelOrderRequest$context);
  113.         $this->eventDispatcher->dispatch($event);
  114.         $this->cancelOrderRoute->cancel($event->getStoreApiRequest(), $context);
  115.         if ($context->getCustomer() && $context->getCustomer()->getGuest() === true) {
  116.             return $this->redirectToRoute(
  117.                 'frontend.account.order.single.page',
  118.                 [
  119.                     'deepLinkCode' => $request->get('deepLinkCode'),
  120.                 ]
  121.             );
  122.         }
  123.         return $this->redirectToRoute('frontend.account.order.page');
  124.     }
  125.     /**
  126.      * @Since("6.2.0.0")
  127.      * @Route("/account/order/{deepLinkCode}", name="frontend.account.order.single.page", options={"seo"="false"}, methods={"GET", "POST"})
  128.      * @NoStore
  129.      */
  130.     public function orderSingleOverview(Request $requestSalesChannelContext $context): Response
  131.     {
  132.         try {
  133.             $page $this->orderPageLoader->load($request$context);
  134.             $this->hook(new AccountOrderPageLoadedHook($page$context));
  135.         } catch (GuestNotAuthenticatedException WrongGuestCredentialsException CustomerAuthThrottledException $exception) {
  136.             return $this->redirectToRoute(
  137.                 'frontend.account.guest.login.page',
  138.                 [
  139.                     'redirectTo' => 'frontend.account.order.single.page',
  140.                     'redirectParameters' => ['deepLinkCode' => $request->get('deepLinkCode')],
  141.                     'loginError' => ($exception instanceof WrongGuestCredentialsException),
  142.                     'waitTime' => ($exception instanceof CustomerAuthThrottledException) ? $exception->getWaitTime() : '',
  143.                 ]
  144.             );
  145.         }
  146.         return $this->renderStorefront('@Storefront/storefront/page/account/order-history/index.html.twig', ['page' => $page]);
  147.     }
  148.     /**
  149.      * @Since("6.0.0.0")
  150.      * @Route("/widgets/account/order/detail/{id}", name="widgets.account.order.detail", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true, "_loginRequired"=true})
  151.      */
  152.     public function ajaxOrderDetail(Request $requestSalesChannelContext $context): Response
  153.     {
  154.         $page $this->orderDetailPageLoader->load($request$context);
  155.         $this->hook(new AccountOrderDetailPageLoadedHook($page$context));
  156.         $response $this->renderStorefront('@Storefront/storefront/page/account/order-history/order-detail-list.html.twig', [
  157.             'orderDetails' => $page->getLineItems(),
  158.             'orderId' => $page->getOrder()->getId(),
  159.             'page' => $page,
  160.         ]);
  161.         $response->headers->set('x-robots-tag''noindex');
  162.         return $response;
  163.     }
  164.     /**
  165.      * @Since("6.2.0.0")
  166.      * @Route("/account/order/edit/{orderId}", name="frontend.account.edit-order.page", methods={"GET"}, defaults={"_loginRequired"=true, "_loginRequiredAllowGuest"=true})
  167.      * @Route("/account/order/edit/{orderId}", name="frontend.account.edit-order.page", methods={"GET"})
  168.      * @NoStore
  169.      */
  170.     public function editOrder(string $orderIdRequest $requestSalesChannelContext $context): Response
  171.     {
  172.         $criteria = new Criteria([$orderId]);
  173.         $deliveriesCriteria $criteria->getAssociation('deliveries');
  174.         $deliveriesCriteria->addSorting(new FieldSorting('createdAt'FieldSorting::ASCENDING));
  175.         $order $this->orderRoute->load($request$context$criteria)->getOrders()->first();
  176.         if ($order === null) {
  177.             throw new OrderNotFoundException($orderId);
  178.         }
  179.         if ($context->getCurrency()->getId() !== $order->getCurrencyId()) {
  180.             $this->contextSwitchRoute->switchContext(
  181.                 new RequestDataBag([SalesChannelContextService::CURRENCY_ID => $order->getCurrencyId()]),
  182.                 $context
  183.             );
  184.             return $this->redirectToRoute('frontend.account.edit-order.page', ['orderId' => $orderId]);
  185.         }
  186.         /** @var OrderDeliveryEntity|null $mostCurrentDelivery */
  187.         $mostCurrentDelivery $order->getDeliveries()->last();
  188.         if ($mostCurrentDelivery !== null && $context->getShippingMethod()->getId() !== $mostCurrentDelivery->getShippingMethodId()) {
  189.             $this->contextSwitchRoute->switchContext(
  190.                 new RequestDataBag([SalesChannelContextService::SHIPPING_METHOD_ID => $mostCurrentDelivery->getShippingMethodId()]),
  191.                 $context
  192.             );
  193.             return $this->redirectToRoute('frontend.account.edit-order.page', ['orderId' => $orderId]);
  194.         }
  195.         $page $this->accountEditOrderPageLoader->load($request$context);
  196.         $this->hook(new AccountEditOrderPageLoadedHook($page$context));
  197.         if ($page->isPaymentChangeable() === false) {
  198.             $refundsEnabled $this->systemConfigService->get('core.cart.enableOrderRefunds');
  199.             if ($refundsEnabled) {
  200.                 $this->addFlash(self::DANGER$this->trans('account.editOrderPaymentNotChangeableWithRefunds'));
  201.             } else {
  202.                 $this->addFlash(self::DANGER$this->trans('account.editOrderPaymentNotChangeable'));
  203.             }
  204.         }
  205.         $page->setErrorCode($request->get('error-code'));
  206.         return $this->renderStorefront('@Storefront/storefront/page/account/order/index.html.twig', ['page' => $page]);
  207.     }
  208.     /**
  209.      * @Since("6.2.0.0")
  210.      * @Route("/account/order/payment/{orderId}", name="frontend.account.edit-order.change-payment-method", methods={"POST"})
  211.      */
  212.     public function orderChangePayment(string $orderIdRequest $requestSalesChannelContext $context): Response
  213.     {
  214.         $this->contextSwitchRoute->switchContext(
  215.             new RequestDataBag(
  216.                 [
  217.                     SalesChannelContextService::PAYMENT_METHOD_ID => $request->get('paymentMethodId'),
  218.                 ]
  219.             ),
  220.             $context
  221.         );
  222.         return $this->redirectToRoute('frontend.account.edit-order.page', ['orderId' => $orderId]);
  223.     }
  224.     /**
  225.      * @Since("6.2.0.0")
  226.      * @Route("/account/order/update/{orderId}", name="frontend.account.edit-order.update-order", methods={"POST"})
  227.      */
  228.     public function updateOrder(string $orderIdRequest $requestSalesChannelContext $context): Response
  229.     {
  230.         $finishUrl $this->generateUrl('frontend.checkout.finish.page', [
  231.             'orderId' => $orderId,
  232.             'changedPayment' => true,
  233.         ]);
  234.         /** @var OrderEntity|null $order */
  235.         $order $this->orderRoute->load($request$context, new Criteria([$orderId]))->getOrders()->first();
  236.         if ($order === null) {
  237.             throw new OrderNotFoundException($orderId);
  238.         }
  239.         if (!$this->orderService->isPaymentChangeableByTransactionState($order)) {
  240.             throw new OrderPaymentMethodNotChangeable();
  241.         }
  242.         if ($context->getCurrency()->getId() !== $order->getCurrencyId()) {
  243.             $this->contextSwitchRoute->switchContext(
  244.                 new RequestDataBag([SalesChannelContextService::CURRENCY_ID => $order->getCurrencyId()]),
  245.                 $context
  246.             );
  247.             $context $this->contextService->get(
  248.                 new SalesChannelContextServiceParameters(
  249.                     $context->getSalesChannelId(),
  250.                     $context->getToken(),
  251.                     $context->getContext()->getLanguageId()
  252.                 )
  253.             );
  254.         }
  255.         $errorUrl $this->generateUrl('frontend.account.edit-order.page', ['orderId' => $orderId]);
  256.         $setPaymentRequest = new Request();
  257.         $setPaymentRequest->request->set('orderId'$orderId);
  258.         $setPaymentRequest->request->set('paymentMethodId'$request->get('paymentMethodId'));
  259.         $setPaymentOrderRouteRequestEvent = new SetPaymentOrderRouteRequestEvent($request$setPaymentRequest$context);
  260.         $this->eventDispatcher->dispatch($setPaymentOrderRouteRequestEvent);
  261.         $this->setPaymentOrderRoute->setPayment($setPaymentOrderRouteRequestEvent->getStoreApiRequest(), $context);
  262.         $handlePaymentRequest = new Request();
  263.         $handlePaymentRequest->request->set('orderId'$orderId);
  264.         $handlePaymentRequest->request->set('finishUrl'$finishUrl);
  265.         $handlePaymentRequest->request->set('errorUrl'$errorUrl);
  266.         $handlePaymentMethodRouteRequestEvent = new HandlePaymentMethodRouteRequestEvent($request$handlePaymentRequest$context);
  267.         $this->eventDispatcher->dispatch($handlePaymentMethodRouteRequestEvent);
  268.         try {
  269.             $routeResponse $this->handlePaymentMethodRoute->load(
  270.                 $handlePaymentMethodRouteRequestEvent->getStoreApiRequest(),
  271.                 $context
  272.             );
  273.             $response $routeResponse->getRedirectResponse();
  274.         } catch (PaymentProcessException $paymentProcessException) {
  275.             return $this->forwardToRoute(
  276.                 'frontend.checkout.finish.page',
  277.                 ['orderId' => $orderId'changedPayment' => true'paymentFailed' => true]
  278.             );
  279.         }
  280.         return $response ?? $this->redirectToRoute(
  281.             'frontend.checkout.finish.page',
  282.             ['orderId' => $orderId'changedPayment' => true]
  283.         );
  284.     }
  285. }