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

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