custom/plugins/KlarnaPayment/src/Components/Controller/Storefront/KlarnaCheckoutController.php line 95

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace KlarnaPayment\Components\Controller\Storefront;
  4. use KlarnaPayment\Components\DataAbstractionLayer\Entity\Order\OrderExtension;
  5. use KlarnaPayment\Components\Event\OrderCreatedThroughAuthorizationCallback;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  10. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Storefront\Controller\CheckoutController;
  13. use Shopware\Storefront\Controller\StorefrontController;
  14. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. /**
  20.  * @RouteScope(scopes={"storefront"})
  21.  * @Route(defaults={"_routeScope": {"storefront"}})
  22.  */
  23. class KlarnaCheckoutController extends CheckoutController
  24. {
  25.     /**
  26.      * For compatibility to other plugins, we set StorefrontController as the type hint for the argument.
  27.      *
  28.      * @var CheckoutController|StorefrontController
  29.      */
  30.     private $parent;
  31.     /** @var EntityRepository */
  32.     private $orderRepository;
  33.     /** @var EventDispatcherInterface */
  34.     private $eventDispatcher;
  35.     /** @var EntityRepository */
  36.     private $cartDataRepository;
  37.     public function __construct(StorefrontController $parentEntityRepository $orderRepositoryEventDispatcherInterface $eventDispatcherEntityRepository $cartDataRepository)
  38.     {
  39.         $this->parent             $parent;
  40.         $this->orderRepository    $orderRepository;
  41.         $this->eventDispatcher    $eventDispatcher;
  42.         $this->cartDataRepository $cartDataRepository;
  43.     }
  44.     public function order(RequestDataBag $dataSalesChannelContext $contextRequest $request): Response
  45.     {
  46.         if (!$data->has('klarnaAuthorizationToken')) {
  47.             return $this->parent->order($data$context$request);
  48.         }
  49.         // If the order has been created through the authorization callback, forward the user to the finish page
  50.         $criteria = new Criteria();
  51.         $criteria
  52.             ->addAssociation(OrderExtension::EXTENSION_NAME)
  53.             ->addFilter(new EqualsFilter(OrderExtension::EXTENSION_NAME '.authorizationToken'$data->get('klarnaAuthorizationToken')));
  54.         $orderId $this->orderRepository->searchIds($criteria$context->getContext())->getIds()[0] ?? null;
  55.         if (empty($orderId)) {
  56.             return $this->parent->order($data$context$request);
  57.         }
  58.         $finishPage $this->generateUrl('frontend.checkout.finish.page', ['orderId' => $orderId]);
  59.         $this->eventDispatcher->dispatch(new OrderCreatedThroughAuthorizationCallback());
  60.         return new RedirectResponse($finishPage);
  61.     }
  62.     /**
  63.      * @Route("/klarna/checkout/saveFormData", defaults={"csrf_protected": false, "XmlHttpRequest": true}, name="widgets.klarna.checkout.save", methods={"POST"})
  64.      */
  65.     public function saveFormData(RequestDataBag $dataBagSalesChannelContext $context): Response
  66.     {
  67.         // Save inputs excluding tos and inputs containing klarna
  68.         $params array_filter($dataBag->all(), static function ($param) {
  69.             return !(strpos((string) $param'klarna') !== false) && $param !== 'tos';
  70.         }, ARRAY_FILTER_USE_KEY);
  71.         $this->cartDataRepository->upsert([['cartToken' => $context->getToken(), 'payload' => $params]], $context->getContext());
  72.         return new Response();
  73.     }
  74.     public function cartPage(Request $requestSalesChannelContext $context): Response
  75.     {
  76.         return $this->parent->cartPage($request$context);
  77.     }
  78.     public function cartJson(Request $requestSalesChannelContext $context): Response
  79.     {
  80.         if (!method_exists($this->parent'cartJson')) {
  81.             return new Response();
  82.         }
  83.         return $this->parent->cartJson($request$context);
  84.     }
  85.     public function confirmPage(Request $requestSalesChannelContext $context): Response
  86.     {
  87.         return $this->parent->confirmPage($request$context);
  88.     }
  89.     public function finishPage(Request $requestSalesChannelContext $context, ?RequestDataBag $dataBag null): Response
  90.     {
  91.         /** @phpstan-ignore-next-line */
  92.         return $this->parent->finishPage($request$context$dataBag);
  93.     }
  94.     public function info(Request $requestSalesChannelContext $context): Response
  95.     {
  96.         return $this->parent->info($request$context);
  97.     }
  98.     public function offcanvas(Request $requestSalesChannelContext $context): Response
  99.     {
  100.         return $this->parent->offcanvas($request$context);
  101.     }
  102. }