custom/plugins/CioBudget/src/Subscriber/CheckBudgetForCartSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. namespace CioBudget\Subscriber;
  3. use CioBudget\Core\Error\CustomerHasNoActiveBudgetError;
  4. use CioBudget\PaymentHandler\BudgetPayment;
  5. use CioBudget\Service\BudgetInfoService;
  6. use CioBudget\Service\SessionService;
  7. use CioBudget\Service\StoreLoaderService;
  8. use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
  9. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  10. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  11. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DefaultPayment;
  12. use Shopware\Core\Framework\Context;
  13. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  18. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  19. use Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute;
  20. use Shopware\Storefront\Event\StorefrontRenderEvent;
  21. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  22. use Symfony\Component\DependencyInjection\ContainerInterface;
  23. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\Routing\RouterInterface;
  26. class CheckBudgetForCartSubscriber implements EventSubscriberInterface
  27. {
  28.     /** values coming from the database(payment_method) after payment method installation */
  29.     const BUDGET_PAYMENT_ID '420fb71d2ae84fbbb322e55329a3707a';
  30.     private ContainerInterface $container;
  31.     private StoreLoaderService $storeLoaderService;
  32.     private RouterInterface $router;
  33.     private SessionService $sessionService;
  34.     private BudgetInfoService $budgetInfoService;
  35.     public function __construct(ContainerInterface $containerStoreLoaderService $storeLoaderServiceRouterInterface $routerSessionService $sessionServiceBudgetInfoService $budgetInfoService)
  36.     {
  37.         $this->container $container;
  38.         $this->storeLoaderService $storeLoaderService;
  39.         $this->router $router;
  40.         $this->sessionService $sessionService;
  41.         $this->budgetInfoService $budgetInfoService;
  42.     }
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return [
  46.             StorefrontRenderEvent::class => 'onStorefrontRender'
  47.         ];
  48.     }
  49.     public function onStorefrontRender(StorefrontRenderEvent $event)
  50.     {
  51.         if (key_exists('page'$event->getParameters()) && $event->getParameters()['page'] instanceof CheckoutConfirmPage ) {
  52.             /** @var CheckoutConfirmPage $checkoutConfirmPage */
  53.             $checkoutConfirmPage $event->getParameters()['page'];
  54.             try {
  55.                 $cartPrice $checkoutConfirmPage->getCart()->getPrice()->getTotalPrice();
  56.                 $currentBudget $this->sessionService->getBudgetIdFromSession();
  57.                 if (is_string($currentBudget) && Uuid::isValid($currentBudget)) {
  58.                     $availableBudget $this->budgetInfoService->getAvailableBudget($currentBudget$event->getSalesChannelContext()->getContext(), $event->getSalesChannelContext()->getCustomer());
  59.                     /** @var EntityRepositoryInterface $paymentRepository */
  60.                     $paymentRepository $this->container->get('payment_method.repository');
  61.                     $criteria = (new Criteria())
  62.                         ->addFilter(new EqualsFilter('handlerIdentifier'BudgetPayment::class));
  63.                     $budgetPaymentMethod $paymentRepository->search($criteria$event->getContext())->first();
  64.                     if ($budgetPaymentMethod->getId() === $checkoutConfirmPage->getCart()->getTransactions()->first()->getPaymentMethodId() && $availableBudget $cartPrice) {
  65.                         $checkoutConfirmPage->getCart()->addErrors(new CustomerHasNoActiveBudgetError(''''));
  66.                         $this->container->get('session')->getFlashBag()->add('danger''Sie haben nicht ausreichend Punkte für diese Bestellung.');
  67.                     }
  68.                 }
  69.             } catch (\Throwable $exception) {}
  70.         }
  71.     }
  72. }