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

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.         $cartErrors->clear();
  138.         return $this->renderStorefront('@Storefront/storefront/page/checkout/confirm/index.html.twig', ['page' => $page]);
  139.     }
  140.     /**
  141.      * @Since("6.0.0.0")
  142.      * @Route("/checkout/finish", name="frontend.checkout.finish.page", options={"seo"="false"}, methods={"GET"})
  143.      * @NoStore
  144.      */
  145.     public function finishPage(Request $requestSalesChannelContext $contextRequestDataBag $dataBag): Response
  146.     {
  147.         if ($context->getCustomer() === null) {
  148.             return $this->redirectToRoute('frontend.checkout.register.page');
  149.         }
  150.         $page $this->finishPageLoader->load($request$context);
  151.         $this->hook(new CheckoutFinishPageLoadedHook($page$context));
  152.         if ($page->isPaymentFailed() === true) {
  153.             return $this->redirectToRoute(
  154.                 'frontend.account.edit-order.page',
  155.                 [
  156.                     'orderId' => $request->get('orderId'),
  157.                     'error-code' => 'CHECKOUT__UNKNOWN_ERROR',
  158.                 ]
  159.             );
  160.         }
  161.         if ($context->getCustomer()->getGuest() && $this->config->get('core.cart.logoutGuestAfterCheckout'$context->getSalesChannelId())) {
  162.             $this->logoutRoute->logout($context$dataBag);
  163.         }
  164.         return $this->renderStorefront('@Storefront/storefront/page/checkout/finish/index.html.twig', ['page' => $page]);
  165.     }
  166.     /**
  167.      * @Since("6.0.0.0")
  168.      * @Route("/checkout/order", name="frontend.checkout.finish.order", options={"seo"="false"}, methods={"POST"})
  169.      */
  170.     public function order(RequestDataBag $dataSalesChannelContext $contextRequest $request): Response
  171.     {
  172.         if (!$context->getCustomer()) {
  173.             return $this->redirectToRoute('frontend.checkout.register.page');
  174.         }
  175.         try {
  176.             $this->addAffiliateTracking($data$request->getSession());
  177.             $orderId Profiler::trace('checkout-order', function () use ($data$context) {
  178.                 return $this->orderService->createOrder($data$context);
  179.             });
  180.         } catch (ConstraintViolationException $formViolations) {
  181.             return $this->forwardToRoute('frontend.checkout.confirm.page', ['formViolations' => $formViolations]);
  182.         } catch (InvalidCartException Error EmptyCartException $error) {
  183.             $this->addCartErrors(
  184.                 $this->cartService->getCart($context->getToken(), $context)
  185.             );
  186.             return $this->forwardToRoute('frontend.checkout.confirm.page');
  187.         }
  188.         try {
  189.             $finishUrl $this->generateUrl('frontend.checkout.finish.page', ['orderId' => $orderId]);
  190.             $errorUrl $this->generateUrl('frontend.account.edit-order.page', ['orderId' => $orderId]);
  191.             $response Profiler::trace('handle-payment', function () use ($orderId$data$context$finishUrl$errorUrl): ?RedirectResponse {
  192.                 return $this->paymentService->handlePaymentByOrder($orderId$data$context$finishUrl$errorUrl);
  193.             });
  194.             return $response ?? new RedirectResponse($finishUrl);
  195.         } catch (PaymentProcessException InvalidOrderException UnknownPaymentMethodException $e) {
  196.             return $this->forwardToRoute('frontend.checkout.finish.page', ['orderId' => $orderId'changedPayment' => false'paymentFailed' => true]);
  197.         }
  198.     }
  199.     /**
  200.      * @Since("6.0.0.0")
  201.      * @Route("/widgets/checkout/info", name="frontend.checkout.info", methods={"GET"}, defaults={"XmlHttpRequest"=true})
  202.      */
  203.     public function info(Request $requestSalesChannelContext $context): Response
  204.     {
  205.         $cart $this->cartService->getCart($context->getToken(), $context);
  206.         if ($cart->getLineItems()->count() <= 0
  207.             && (Feature::isActive('v6.5.0.0') || Feature::isActive('PERFORMANCE_TWEAKS'))
  208.         ) {
  209.             return new Response(nullResponse::HTTP_NO_CONTENT);
  210.         }
  211.         $page $this->offcanvasCartPageLoader->load($request$context);
  212.         $this->hook(new CheckoutInfoWidgetLoadedHook($page$context));
  213.         $response $this->renderStorefront('@Storefront/storefront/layout/header/actions/cart-widget.html.twig', ['page' => $page]);
  214.         $response->headers->set('x-robots-tag''noindex');
  215.         return $response;
  216.     }
  217.     /**
  218.      * @Since("6.0.0.0")
  219.      * @Route("/checkout/offcanvas", name="frontend.cart.offcanvas", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  220.      */
  221.     public function offcanvas(Request $requestSalesChannelContext $context): Response
  222.     {
  223.         $page $this->offcanvasCartPageLoader->load($request$context);
  224.         $this->hook(new CheckoutOffcanvasWidgetLoadedHook($page$context));
  225.         $cart $page->getCart();
  226.         $this->addCartErrors($cart);
  227.         $cartErrors $cart->getErrors();
  228.         if (!$request->query->getBoolean(self::REDIRECTED_FROM_SAME_ROUTE) && $this->routeNeedsReload($cartErrors)) {
  229.             $cartErrors->clear();
  230.             // To prevent redirect loops add the identifier that the request already got redirected from the same origin
  231.             return $this->redirectToRoute(
  232.                 'frontend.cart.offcanvas',
  233.                 \array_merge(
  234.                     $request->query->all(),
  235.                     [self::REDIRECTED_FROM_SAME_ROUTE => true]
  236.                 ),
  237.             );
  238.         }
  239.         $cartErrors->clear();
  240.         return $this->renderStorefront('@Storefront/storefront/component/checkout/offcanvas-cart.html.twig', ['page' => $page]);
  241.     }
  242.     private function addAffiliateTracking(RequestDataBag $dataBagSessionInterface $session): void
  243.     {
  244.         $affiliateCode $session->get(AffiliateTrackingListener::AFFILIATE_CODE_KEY);
  245.         $campaignCode $session->get(AffiliateTrackingListener::CAMPAIGN_CODE_KEY);
  246.         if ($affiliateCode) {
  247.             $dataBag->set(AffiliateTrackingListener::AFFILIATE_CODE_KEY$affiliateCode);
  248.         }
  249.         if ($campaignCode) {
  250.             $dataBag->set(AffiliateTrackingListener::CAMPAIGN_CODE_KEY$campaignCode);
  251.         }
  252.     }
  253.     private function routeNeedsReload(ErrorCollection $cartErrors): bool
  254.     {
  255.         foreach ($cartErrors as $error) {
  256.             if ($error instanceof ShippingMethodChangedError || $error instanceof PaymentMethodChangedError) {
  257.                 return true;
  258.             }
  259.         }
  260.         return false;
  261.     }
  262. }