<?php
namespace CioBudget\Subscriber;
use CioBudget\Core\Error\CustomerHasNoActiveBudgetError;
use CioBudget\PaymentHandler\BudgetPayment;
use CioBudget\Service\BudgetInfoService;
use CioBudget\Service\SessionService;
use CioBudget\Service\StoreLoaderService;
use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DefaultPayment;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
use Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
class CheckBudgetForCartSubscriber implements EventSubscriberInterface
{
/** values coming from the database(payment_method) after payment method installation */
const BUDGET_PAYMENT_ID = '420fb71d2ae84fbbb322e55329a3707a';
private ContainerInterface $container;
private StoreLoaderService $storeLoaderService;
private RouterInterface $router;
private SessionService $sessionService;
private BudgetInfoService $budgetInfoService;
public function __construct(ContainerInterface $container, StoreLoaderService $storeLoaderService, RouterInterface $router, SessionService $sessionService, BudgetInfoService $budgetInfoService)
{
$this->container = $container;
$this->storeLoaderService = $storeLoaderService;
$this->router = $router;
$this->sessionService = $sessionService;
$this->budgetInfoService = $budgetInfoService;
}
public static function getSubscribedEvents()
{
return [
StorefrontRenderEvent::class => 'onStorefrontRender'
];
}
public function onStorefrontRender(StorefrontRenderEvent $event)
{
if (key_exists('page', $event->getParameters()) && $event->getParameters()['page'] instanceof CheckoutConfirmPage ) {
/** @var CheckoutConfirmPage $checkoutConfirmPage */
$checkoutConfirmPage = $event->getParameters()['page'];
try {
$cartPrice = $checkoutConfirmPage->getCart()->getPrice()->getTotalPrice();
$currentBudget = $this->sessionService->getBudgetIdFromSession();
if (is_string($currentBudget) && Uuid::isValid($currentBudget)) {
$availableBudget = $this->budgetInfoService->getAvailableBudget($currentBudget, $event->getSalesChannelContext()->getContext(), $event->getSalesChannelContext()->getCustomer());
/** @var EntityRepositoryInterface $paymentRepository */
$paymentRepository = $this->container->get('payment_method.repository');
$criteria = (new Criteria())
->addFilter(new EqualsFilter('handlerIdentifier', BudgetPayment::class));
$budgetPaymentMethod = $paymentRepository->search($criteria, $event->getContext())->first();
if ($budgetPaymentMethod->getId() === $checkoutConfirmPage->getCart()->getTransactions()->first()->getPaymentMethodId() && $availableBudget < $cartPrice) {
$checkoutConfirmPage->getCart()->addErrors(new CustomerHasNoActiveBudgetError('', ''));
$this->container->get('session')->getFlashBag()->add('danger', 'Sie haben nicht ausreichend Punkte für diese Bestellung.');
}
}
} catch (\Throwable $exception) {}
}
}
}