src/Storefront/Controller/AccountOrderController.php line 57

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