src/Storefront/Controller/CheckoutController.php line 68

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