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

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