vendor/shopware/storefront/Controller/CheckoutController.php line 75

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Checkout\Cart\Error\Error;
  4. use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
  5. use Shopware\Core\Checkout\Cart\Exception\InvalidCartException;
  6. use Shopware\Core\Checkout\Cart\SalesChannel\AbstractCartLoadRoute;
  7. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  8. use Shopware\Core\Checkout\Customer\SalesChannel\AbstractLogoutRoute;
  9. use Shopware\Core\Checkout\Order\Exception\EmptyCartException;
  10. use Shopware\Core\Checkout\Order\SalesChannel\OrderService;
  11. use Shopware\Core\Checkout\Payment\Exception\InvalidOrderException;
  12. use Shopware\Core\Checkout\Payment\Exception\PaymentProcessException;
  13. use Shopware\Core\Checkout\Payment\Exception\UnknownPaymentMethodException;
  14. use Shopware\Core\Checkout\Payment\PaymentService;
  15. use Shopware\Core\Framework\Log\Package;
  16. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  17. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  18. use Shopware\Core\Profiling\Profiler;
  19. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  20. use Shopware\Core\System\SystemConfig\SystemConfigService;
  21. use Shopware\Storefront\Checkout\Cart\Error\PaymentMethodChangedError;
  22. use Shopware\Storefront\Checkout\Cart\Error\ShippingMethodChangedError;
  23. use Shopware\Storefront\Framework\AffiliateTracking\AffiliateTrackingListener;
  24. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedHook;
  25. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoader;
  26. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedHook;
  27. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoader;
  28. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedHook;
  29. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoader;
  30. use Shopware\Storefront\Page\Checkout\Offcanvas\CheckoutInfoWidgetLoadedHook;
  31. use Shopware\Storefront\Page\Checkout\Offcanvas\CheckoutOffcanvasWidgetLoadedHook;
  32. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoader;
  33. use Symfony\Component\HttpFoundation\RedirectResponse;
  34. use Symfony\Component\HttpFoundation\Request;
  35. use Symfony\Component\HttpFoundation\Response;
  36. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  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 datas
  41.  */
  42. #[Route(defaults: ['_routeScope' => ['storefront']])]
  43. #[Package('storefront')]
  44. class CheckoutController extends StorefrontController
  45. {
  46.     private const REDIRECTED_FROM_SAME_ROUTE 'redirected';
  47.     /**
  48.      * @internal
  49.      */
  50.     public function __construct(
  51.         private readonly CartService $cartService,
  52.         private readonly CheckoutCartPageLoader $cartPageLoader,
  53.         private readonly CheckoutConfirmPageLoader $confirmPageLoader,
  54.         private readonly CheckoutFinishPageLoader $finishPageLoader,
  55.         private readonly OrderService $orderService,
  56.         private readonly PaymentService $paymentService,
  57.         private readonly OffcanvasCartPageLoader $offcanvasCartPageLoader,
  58.         private readonly SystemConfigService $config,
  59.         private readonly AbstractLogoutRoute $logoutRoute,
  60.         private readonly AbstractCartLoadRoute $cartLoadRoute
  61.     ) {
  62.     }
  63.     #[Route(path'/checkout/cart'name'frontend.checkout.cart.page'options: ['seo' => false], defaults: ['_noStore' => true], methods: ['GET'])]
  64.     public function cartPage(Request $requestSalesChannelContext $context): Response
  65.     {
  66.         $page $this->cartPageLoader->load($request$context);
  67.         $cart $page->getCart();
  68.         $cartErrors $cart->getErrors();
  69.         $this->hook(new CheckoutCartPageLoadedHook($page$context));
  70.         $this->addCartErrors($cart);
  71.         if (!$request->query->getBoolean(self::REDIRECTED_FROM_SAME_ROUTE) && $this->routeNeedsReload($cartErrors)) {
  72.             $cartErrors->clear();
  73.             // To prevent redirect loops add the identifier that the request already got redirected from the same origin
  74.             return $this->redirectToRoute(
  75.                 'frontend.checkout.cart.page',
  76.                 [...$request->query->all(), ...[self::REDIRECTED_FROM_SAME_ROUTE => true]],
  77.             );
  78.         }
  79.         $cartErrors->clear();
  80.         return $this->renderStorefront('@Storefront/storefront/page/checkout/cart/index.html.twig', ['page' => $page]);
  81.     }
  82.     #[Route(path'/checkout/cart.json'name'frontend.checkout.cart.json'methods: ['GET'], options: ['seo' => false], defaults: ['XmlHttpRequest' => true])]
  83.     public function cartJson(Request $requestSalesChannelContext $context): Response
  84.     {
  85.         return $this->cartLoadRoute->load($request$context);
  86.     }
  87.     #[Route(path'/checkout/confirm'name'frontend.checkout.confirm.page'options: ['seo' => false], defaults: ['XmlHttpRequest' => true'_noStore' => true], methods: ['GET'])]
  88.     public function confirmPage(Request $requestSalesChannelContext $context): Response
  89.     {
  90.         if (!$context->getCustomer()) {
  91.             return $this->redirectToRoute('frontend.checkout.register.page');
  92.         }
  93.         if ($this->cartService->getCart($context->getToken(), $context)->getLineItems()->count() === 0) {
  94.             return $this->redirectToRoute('frontend.checkout.cart.page');
  95.         }
  96.         $page $this->confirmPageLoader->load($request$context);
  97.         $cart $page->getCart();
  98.         $cartErrors $cart->getErrors();
  99.         $this->hook(new CheckoutConfirmPageLoadedHook($page$context));
  100.         $this->addCartErrors($cart);
  101.         if (!$request->query->getBoolean(self::REDIRECTED_FROM_SAME_ROUTE) && $this->routeNeedsReload($cartErrors)) {
  102.             $cartErrors->clear();
  103.             // To prevent redirect loops add the identifier that the request already got redirected from the same origin
  104.             return $this->redirectToRoute(
  105.                 'frontend.checkout.confirm.page',
  106.                 [...$request->query->all(), ...[self::REDIRECTED_FROM_SAME_ROUTE => true]],
  107.             );
  108.         }
  109.         return $this->renderStorefront('@Storefront/storefront/page/checkout/confirm/index.html.twig', ['page' => $page]);
  110.     }
  111.     #[Route(path'/checkout/finish'name'frontend.checkout.finish.page'options: ['seo' => false], defaults: ['_noStore' => true], methods: ['GET'])]
  112.     public function finishPage(Request $requestSalesChannelContext $contextRequestDataBag $dataBag): Response
  113.     {
  114.         if ($context->getCustomer() === null) {
  115.             return $this->redirectToRoute('frontend.checkout.register.page');
  116.         }
  117.         $page $this->finishPageLoader->load($request$context);
  118.         $this->hook(new CheckoutFinishPageLoadedHook($page$context));
  119.         if ($page->isPaymentFailed() === true) {
  120.             return $this->redirectToRoute(
  121.                 'frontend.account.edit-order.page',
  122.                 [
  123.                     'orderId' => $request->get('orderId'),
  124.                     'error-code' => 'CHECKOUT__UNKNOWN_ERROR',
  125.                 ]
  126.             );
  127.         }
  128.         if ($context->getCustomer()->getGuest() && $this->config->get('core.cart.logoutGuestAfterCheckout'$context->getSalesChannelId())) {
  129.             $this->logoutRoute->logout($context$dataBag);
  130.         }
  131.         return $this->renderStorefront('@Storefront/storefront/page/checkout/finish/index.html.twig', ['page' => $page]);
  132.     }
  133.     #[Route(path'/checkout/order'name'frontend.checkout.finish.order'options: ['seo' => false], methods: ['POST'])]
  134.     public function order(RequestDataBag $dataSalesChannelContext $contextRequest $request): Response
  135.     {
  136.         if (!$context->getCustomer()) {
  137.             return $this->redirectToRoute('frontend.checkout.register.page');
  138.         }
  139.         try {
  140.             $this->addAffiliateTracking($data$request->getSession());
  141.             $orderId Profiler::trace('checkout-order', fn () => $this->orderService->createOrder($data$context));
  142.         } catch (ConstraintViolationException $formViolations) {
  143.             return $this->forwardToRoute('frontend.checkout.confirm.page', ['formViolations' => $formViolations]);
  144.         } catch (InvalidCartException Error EmptyCartException) {
  145.             $this->addCartErrors(
  146.                 $this->cartService->getCart($context->getToken(), $context)
  147.             );
  148.             return $this->forwardToRoute('frontend.checkout.confirm.page');
  149.         }
  150.         try {
  151.             $finishUrl $this->generateUrl('frontend.checkout.finish.page', ['orderId' => $orderId]);
  152.             $errorUrl $this->generateUrl('frontend.account.edit-order.page', ['orderId' => $orderId]);
  153.             $response Profiler::trace('handle-payment', fn (): ?RedirectResponse => $this->paymentService->handlePaymentByOrder($orderId$data$context$finishUrl$errorUrl));
  154.             return $response ?? new RedirectResponse($finishUrl);
  155.         } catch (PaymentProcessException InvalidOrderException UnknownPaymentMethodException) {
  156.             return $this->forwardToRoute('frontend.checkout.finish.page', ['orderId' => $orderId'changedPayment' => false'paymentFailed' => true]);
  157.         }
  158.     }
  159.     #[Route(path'/widgets/checkout/info'name'frontend.checkout.info'defaults: ['XmlHttpRequest' => true], methods: ['GET'])]
  160.     public function info(Request $requestSalesChannelContext $context): Response
  161.     {
  162.         $cart $this->cartService->getCart($context->getToken(), $context);
  163.         if ($cart->getLineItems()->count() <= 0) {
  164.             return new Response(nullResponse::HTTP_NO_CONTENT);
  165.         }
  166.         $page $this->offcanvasCartPageLoader->load($request$context);
  167.         $this->hook(new CheckoutInfoWidgetLoadedHook($page$context));
  168.         $response $this->renderStorefront('@Storefront/storefront/layout/header/actions/cart-widget.html.twig', ['page' => $page]);
  169.         $response->headers->set('x-robots-tag''noindex');
  170.         return $response;
  171.     }
  172.     #[Route(path'/checkout/offcanvas'name'frontend.cart.offcanvas'options: ['seo' => false], defaults: ['XmlHttpRequest' => true], methods: ['GET'])]
  173.     public function offcanvas(Request $requestSalesChannelContext $context): Response
  174.     {
  175.         $page $this->offcanvasCartPageLoader->load($request$context);
  176.         $this->hook(new CheckoutOffcanvasWidgetLoadedHook($page$context));
  177.         $cart $page->getCart();
  178.         $this->addCartErrors($cart);
  179.         $cartErrors $cart->getErrors();
  180.         if (!$request->query->getBoolean(self::REDIRECTED_FROM_SAME_ROUTE) && $this->routeNeedsReload($cartErrors)) {
  181.             $cartErrors->clear();
  182.             // To prevent redirect loops add the identifier that the request already got redirected from the same origin
  183.             return $this->redirectToRoute(
  184.                 'frontend.cart.offcanvas',
  185.                 [...$request->query->all(), ...[self::REDIRECTED_FROM_SAME_ROUTE => true]],
  186.             );
  187.         }
  188.         $cartErrors->clear();
  189.         return $this->renderStorefront('@Storefront/storefront/component/checkout/offcanvas-cart.html.twig', ['page' => $page]);
  190.     }
  191.     private function addAffiliateTracking(RequestDataBag $dataBagSessionInterface $session): void
  192.     {
  193.         $affiliateCode $session->get(AffiliateTrackingListener::AFFILIATE_CODE_KEY);
  194.         $campaignCode $session->get(AffiliateTrackingListener::CAMPAIGN_CODE_KEY);
  195.         if ($affiliateCode) {
  196.             $dataBag->set(AffiliateTrackingListener::AFFILIATE_CODE_KEY$affiliateCode);
  197.         }
  198.         if ($campaignCode) {
  199.             $dataBag->set(AffiliateTrackingListener::CAMPAIGN_CODE_KEY$campaignCode);
  200.         }
  201.     }
  202.     private function routeNeedsReload(ErrorCollection $cartErrors): bool
  203.     {
  204.         foreach ($cartErrors as $error) {
  205.             if ($error instanceof ShippingMethodChangedError || $error instanceof PaymentMethodChangedError) {
  206.                 return true;
  207.             }
  208.         }
  209.         return false;
  210.     }
  211. }