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

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