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

  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.  * Do not use direct or indirect repository calls in a controller. Always use a store-api route to get or put data
  41.  */
  42. #[Route(defaults: ['_routeScope' => ['storefront']])]
  43. #[Package('storefront')]
  44. class AccountOrderController extends StorefrontController
  45. {
  46.     /**
  47.      * @internal
  48.      */
  49.     public function __construct(
  50.         private readonly AccountOrderPageLoader $orderPageLoader,
  51.         private readonly AccountEditOrderPageLoader $accountEditOrderPageLoader,
  52.         private readonly AbstractContextSwitchRoute $contextSwitchRoute,
  53.         private readonly AbstractCancelOrderRoute $cancelOrderRoute,
  54.         private readonly AbstractSetPaymentOrderRoute $setPaymentOrderRoute,
  55.         private readonly AbstractHandlePaymentMethodRoute $handlePaymentMethodRoute,
  56.         private readonly EventDispatcherInterface $eventDispatcher,
  57.         private readonly AccountOrderDetailPageLoader $orderDetailPageLoader,
  58.         private readonly AbstractOrderRoute $orderRoute,
  59.         private readonly SalesChannelContextServiceInterface $contextService,
  60.         private readonly SystemConfigService $systemConfigService,
  61.         private readonly OrderService $orderService
  62.     ) {
  63.     }
  64.     #[Route(path'/account/order'name'frontend.account.order.page'options: ['seo' => false], defaults: ['XmlHttpRequest' => true'_loginRequired' => true'_loginRequiredAllowGuest' => true'_noStore' => true], methods: ['GET''POST'])]
  65.     #[Route(path'/account/order'name'frontend.account.order.page'options: ['seo' => false], defaults: ['XmlHttpRequest' => true'_noStore' => true], methods: ['GET''POST'])]
  66.     public function orderOverview(Request $requestSalesChannelContext $context): Response
  67.     {
  68.         $page $this->orderPageLoader->load($request$context);
  69.         $this->hook(new AccountOrderPageLoadedHook($page$context));
  70.         return $this->renderStorefront('@Storefront/storefront/page/account/order-history/index.html.twig', ['page' => $page]);
  71.     }
  72.     #[Route(path'/account/order/cancel'name'frontend.account.order.cancel'methods: ['POST'])]
  73.     public function cancelOrder(Request $requestSalesChannelContext $context): Response
  74.     {
  75.         $cancelOrderRequest = new Request();
  76.         $cancelOrderRequest->request->set('orderId'$request->get('orderId'));
  77.         $cancelOrderRequest->request->set('transition''cancel');
  78.         $event = new CancelOrderRouteRequestEvent($request$cancelOrderRequest$context);
  79.         $this->eventDispatcher->dispatch($event);
  80.         $this->cancelOrderRoute->cancel($event->getStoreApiRequest(), $context);
  81.         if ($context->getCustomer() && $context->getCustomer()->getGuest() === true) {
  82.             return $this->redirectToRoute(
  83.                 'frontend.account.order.single.page',
  84.                 [
  85.                     'deepLinkCode' => $request->get('deepLinkCode'),
  86.                 ]
  87.             );
  88.         }
  89.         return $this->redirectToRoute('frontend.account.order.page');
  90.     }
  91.     #[Route(path'/account/order/{deepLinkCode}'name'frontend.account.order.single.page'options: ['seo' => false], defaults: ['_noStore' => true], methods: ['GET''POST'])]
  92.     public function orderSingleOverview(Request $requestSalesChannelContext $context): Response
  93.     {
  94.         try {
  95.             $page $this->orderPageLoader->load($request$context);
  96.             $this->hook(new AccountOrderPageLoadedHook($page$context));
  97.         } catch (GuestNotAuthenticatedException WrongGuestCredentialsException CustomerAuthThrottledException $exception) {
  98.             return $this->redirectToRoute(
  99.                 'frontend.account.guest.login.page',
  100.                 [
  101.                     'redirectTo' => 'frontend.account.order.single.page',
  102.                     'redirectParameters' => ['deepLinkCode' => $request->get('deepLinkCode')],
  103.                     'loginError' => ($exception instanceof WrongGuestCredentialsException),
  104.                     'waitTime' => ($exception instanceof CustomerAuthThrottledException) ? $exception->getWaitTime() : '',
  105.                 ]
  106.             );
  107.         }
  108.         return $this->renderStorefront('@Storefront/storefront/page/account/order-history/index.html.twig', ['page' => $page]);
  109.     }
  110.     #[Route(path'/widgets/account/order/detail/{id}'name'widgets.account.order.detail'options: ['seo' => false], defaults: ['XmlHttpRequest' => true'_loginRequired' => true], methods: ['GET'])]
  111.     public function ajaxOrderDetail(Request $requestSalesChannelContext $context): Response
  112.     {
  113.         $page $this->orderDetailPageLoader->load($request$context);
  114.         $this->hook(new AccountOrderDetailPageLoadedHook($page$context));
  115.         $response $this->renderStorefront('@Storefront/storefront/page/account/order-history/order-detail-list.html.twig', [
  116.             'orderDetails' => $page->getLineItems(),
  117.             'orderId' => $page->getOrder()->getId(),
  118.             'page' => $page,
  119.         ]);
  120.         $response->headers->set('x-robots-tag''noindex');
  121.         return $response;
  122.     }
  123.     #[Route(path'/account/order/edit/{orderId}'name'frontend.account.edit-order.page'defaults: ['_loginRequired' => true'_loginRequiredAllowGuest' => true'_noStore' => true], methods: ['GET'])]
  124.     #[Route(path'/account/order/edit/{orderId}'name'frontend.account.edit-order.page'defaults: ['_noStore' => true], methods: ['GET'])]
  125.     public function editOrder(string $orderIdRequest $requestSalesChannelContext $context): Response
  126.     {
  127.         $criteria = new Criteria([$orderId]);
  128.         $deliveriesCriteria $criteria->getAssociation('deliveries');
  129.         $deliveriesCriteria->addSorting(new FieldSorting('createdAt'FieldSorting::ASCENDING));
  130.         $order $this->orderRoute->load($request$context$criteria)->getOrders()->first();
  131.         if ($order === null) {
  132.             throw OrderException::orderNotFound($orderId);
  133.         }
  134.         if ($context->getCurrency()->getId() !== $order->getCurrencyId()) {
  135.             $this->contextSwitchRoute->switchContext(
  136.                 new RequestDataBag([SalesChannelContextService::CURRENCY_ID => $order->getCurrencyId()]),
  137.                 $context
  138.             );
  139.             return $this->redirectToRoute('frontend.account.edit-order.page', ['orderId' => $orderId]);
  140.         }
  141.         /** @var OrderDeliveryEntity|null $mostCurrentDelivery */
  142.         $mostCurrentDelivery $order->getDeliveries()->last();
  143.         if ($mostCurrentDelivery !== null && $context->getShippingMethod()->getId() !== $mostCurrentDelivery->getShippingMethodId()) {
  144.             $this->contextSwitchRoute->switchContext(
  145.                 new RequestDataBag([SalesChannelContextService::SHIPPING_METHOD_ID => $mostCurrentDelivery->getShippingMethodId()]),
  146.                 $context
  147.             );
  148.             return $this->redirectToRoute('frontend.account.edit-order.page', ['orderId' => $orderId]);
  149.         }
  150.         $page $this->accountEditOrderPageLoader->load($request$context);
  151.         $this->hook(new AccountEditOrderPageLoadedHook($page$context));
  152.         if ($page->isPaymentChangeable() === false) {
  153.             $refundsEnabled $this->systemConfigService->get('core.cart.enableOrderRefunds');
  154.             if ($refundsEnabled) {
  155.                 $this->addFlash(self::DANGER$this->trans('account.editOrderPaymentNotChangeableWithRefunds'));
  156.             } else {
  157.                 $this->addFlash(self::DANGER$this->trans('account.editOrderPaymentNotChangeable'));
  158.             }
  159.         }
  160.         $page->setErrorCode($request->get('error-code'));
  161.         return $this->renderStorefront('@Storefront/storefront/page/account/order/index.html.twig', ['page' => $page]);
  162.     }
  163.     #[Route(path'/account/order/payment/{orderId}'name'frontend.account.edit-order.change-payment-method'methods: ['POST'])]
  164.     public function orderChangePayment(string $orderIdRequest $requestSalesChannelContext $context): Response
  165.     {
  166.         $this->contextSwitchRoute->switchContext(
  167.             new RequestDataBag(
  168.                 [
  169.                     SalesChannelContextService::PAYMENT_METHOD_ID => $request->get('paymentMethodId'),
  170.                 ]
  171.             ),
  172.             $context
  173.         );
  174.         return $this->redirectToRoute('frontend.account.edit-order.page', ['orderId' => $orderId]);
  175.     }
  176.     #[Route(path'/account/order/update/{orderId}'name'frontend.account.edit-order.update-order'methods: ['POST'])]
  177.     public function updateOrder(string $orderIdRequest $requestSalesChannelContext $context): Response
  178.     {
  179.         $finishUrl $this->generateUrl('frontend.checkout.finish.page', [
  180.             'orderId' => $orderId,
  181.             'changedPayment' => true,
  182.         ]);
  183.         /** @var OrderEntity|null $order */
  184.         $order $this->orderRoute->load($request$context, new Criteria([$orderId]))->getOrders()->first();
  185.         if ($order === null) {
  186.             throw OrderException::orderNotFound($orderId);
  187.         }
  188.         if (!$this->orderService->isPaymentChangeableByTransactionState($order)) {
  189.             throw OrderException::paymentMethodNotChangeable();
  190.         }
  191.         if ($context->getCurrency()->getId() !== $order->getCurrencyId()) {
  192.             $this->contextSwitchRoute->switchContext(
  193.                 new RequestDataBag([SalesChannelContextService::CURRENCY_ID => $order->getCurrencyId()]),
  194.                 $context
  195.             );
  196.             $context $this->contextService->get(
  197.                 new SalesChannelContextServiceParameters(
  198.                     $context->getSalesChannelId(),
  199.                     $context->getToken(),
  200.                     $context->getContext()->getLanguageId()
  201.                 )
  202.             );
  203.         }
  204.         $errorUrl $this->generateUrl('frontend.account.edit-order.page', ['orderId' => $orderId]);
  205.         $setPaymentRequest = new Request();
  206.         $setPaymentRequest->request->set('orderId'$orderId);
  207.         $setPaymentRequest->request->set('paymentMethodId'$request->get('paymentMethodId'));
  208.         $setPaymentOrderRouteRequestEvent = new SetPaymentOrderRouteRequestEvent($request$setPaymentRequest$context);
  209.         $this->eventDispatcher->dispatch($setPaymentOrderRouteRequestEvent);
  210.         $this->setPaymentOrderRoute->setPayment($setPaymentOrderRouteRequestEvent->getStoreApiRequest(), $context);
  211.         $handlePaymentRequest = new Request();
  212.         $handlePaymentRequest->request->set('orderId'$orderId);
  213.         $handlePaymentRequest->request->set('finishUrl'$finishUrl);
  214.         $handlePaymentRequest->request->set('errorUrl'$errorUrl);
  215.         $handlePaymentMethodRouteRequestEvent = new HandlePaymentMethodRouteRequestEvent($request$handlePaymentRequest$context);
  216.         $this->eventDispatcher->dispatch($handlePaymentMethodRouteRequestEvent);
  217.         try {
  218.             $routeResponse $this->handlePaymentMethodRoute->load(
  219.                 $handlePaymentMethodRouteRequestEvent->getStoreApiRequest(),
  220.                 $context
  221.             );
  222.             $response $routeResponse->getRedirectResponse();
  223.         } catch (PaymentProcessException) {
  224.             return $this->forwardToRoute(
  225.                 'frontend.checkout.finish.page',
  226.                 ['orderId' => $orderId'changedPayment' => true'paymentFailed' => true]
  227.             );
  228.         }
  229.         return $response ?? $this->redirectToRoute(
  230.             'frontend.checkout.finish.page',
  231.             ['orderId' => $orderId'changedPayment' => true]
  232.         );
  233.     }
  234. }