custom/plugins/CioBudget/src/Subscriber/AutoChangePaymentMethodSubscriber.php line 90

Open in your IDE?
  1. <?php
  2. namespace CioBudget\Subscriber;
  3. use CioBudget\PaymentHandler\BudgetPayment;
  4. use CioBudget\Service\StoreLoaderService;
  5. use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
  6. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  8. use Shopware\Core\Checkout\Payment\Cart\PaymentHandler\DefaultPayment;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  14. use Shopware\Core\System\SalesChannel\Context\SalesChannelContextService;
  15. use Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute;
  16. use Shopware\Storefront\Event\StorefrontRenderEvent;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpFoundation\RedirectResponse;
  20. use Symfony\Component\Routing\RouterInterface;
  21. class AutoChangePaymentMethodSubscriber implements EventSubscriberInterface
  22. {
  23.     /** values coming from the database(payment_method) after payment method installation */
  24.     const BUDGET_PAYMENT_ID '420fb71d2ae84fbbb322e55329a3707a';
  25.     private ContainerInterface $container;
  26.     private StoreLoaderService $storeLoaderService;
  27.     private RouterInterface $router;
  28.     public function __construct(ContainerInterface $containerStoreLoaderService $storeLoaderServiceRouterInterface $router)
  29.     {
  30.         $this->container $container;
  31.         $this->storeLoaderService $storeLoaderService;
  32.         $this->router $router;
  33.     }
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return [
  37.             CustomerLoginEvent::class => 'onCustomerLoginEvent',
  38.             StorefrontRenderEvent::class => 'onStorefrontRender'
  39.         ];
  40.     }
  41.     public function onCustomerLoginEvent(CustomerLoginEvent $event)
  42.     {
  43.         /** @var EntityRepositoryInterface $customerRepository */
  44.         $customerRepository $this->container->get('customer.repository');
  45.         $customer $event->getCustomer();
  46.         /** @var CartService $cartService */
  47.         $cartService $this->container->get('Shopware\Core\Checkout\Cart\SalesChannel\CartService');
  48.         $cart $cartService->getCart($event->getContextToken(), $event->getSalesChannelContext());
  49.         /** @var ContextSwitchRoute $contextSwitchRoute */
  50.         $contextSwitchRoute $this->container->get('Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute');
  51.         $customerStores $this->storeLoaderService->getCustomerStores($customer);
  52.         if ($customer && count($customerStores)) {
  53.             $cart->setErrors(new ErrorCollection(array_filter($cart->getErrors()->getElements(), function ($key) {
  54.                 return $key !== 'payment-method-blocked-Budget' && $key !== 'payment-method-blocked-Kostenstelle' && $key !== 'payment-method-blocked-Punkte';
  55.             }, ARRAY_FILTER_USE_KEY)));
  56.             $data = new RequestDataBag([
  57.                 SalesChannelContextService::PAYMENT_METHOD_ID => self::BUDGET_PAYMENT_ID
  58.             ]);
  59.             $customerRepository->update([
  60.                 [
  61.                     'id' => $customer->getId(),
  62.                     'defaultPaymentMethodId' => self::BUDGET_PAYMENT_ID
  63.                 ]
  64.             ], $event->getContext());
  65.             $contextSwitchRoute->switchContext($data$event->getSalesChannelContext());
  66.         } else {
  67.             $cart->setErrors(new ErrorCollection(array_filter($cart->getErrors()->getElements(), function ($key) {
  68.                 return $key !== 'payment-method-blocked-Budget' && $key !== 'payment-method-blocked-Kostenstelle' && $key !== 'payment-method-blocked-Punkte';
  69.             }, ARRAY_FILTER_USE_KEY)));
  70.         }
  71.     }
  72.     public function onStorefrontRender(StorefrontRenderEvent $event)
  73.     {
  74.         $route $event->getRequest()->get('_route');
  75.         if (in_array($route, ['frontend.cart.offcanvas''frontend.checkout.confirm.page''frontend.checkout.cart.page'])) {
  76.             /** @var CartService $cartService */
  77.             $cartService $this->container->get('Shopware\Core\Checkout\Cart\SalesChannel\CartService');
  78.             $cart $cartService->getCart($event->getSalesChannelContext()->getToken(), $event->getSalesChannelContext(), CartService::SALES_CHANNELfalse);
  79.             if ($cart->getLineItems()->count() && count($cart->getErrors()->getElements())) {
  80.                 $data null;
  81.                 if (array_key_exists('payment-method-blocked-Punkte'$cart->getErrors()->getElements())) {
  82.                     $cart->setErrors(new ErrorCollection(array_filter($cart->getErrors()->getElements(), function ($key) {
  83.                         return $key !== 'payment-method-blocked-Punkte';
  84.                     }, ARRAY_FILTER_USE_KEY)));
  85.                     $data = new RequestDataBag([
  86.                         SalesChannelContextService::PAYMENT_METHOD_ID => $this->getPaymentMethodId(DefaultPayment::class, 'Keine Zahlungsdaten erforderlich')
  87.                     ]);
  88.                 }
  89.                 if (array_key_exists('payment-method-blocked-Keine Zahlungsdaten erforderlich'$cart->getErrors()->getElements())) {
  90.                     $cart->setErrors(new ErrorCollection(array_filter($cart->getErrors()->getElements(), function ($key) {
  91.                         return $key !== 'payment-method-blocked-Keine Zahlungsdaten erforderlich';
  92.                     }, ARRAY_FILTER_USE_KEY)));
  93.                     $data = new RequestDataBag([
  94.                         SalesChannelContextService::PAYMENT_METHOD_ID => $this->getPaymentMethodId(BudgetPayment::class)
  95.                     ]);
  96.                 }
  97.                 if (!is_null($data)) {
  98.                     /** @var ContextSwitchRoute $contextSwitchRoute */
  99.                     $contextSwitchRoute $this->container->get('Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute');
  100.                     $contextSwitchRoute->switchContext($data$event->getSalesChannelContext());
  101.                     $response = new RedirectResponse($this->router->generate($route));
  102.                     $response->send();
  103.                 }
  104.             } elseif (!$cart->getLineItems()->count()) {
  105.                 if (array_key_exists('payment-method-blocked-Punkte'$cart->getErrors()->getElements())) {
  106.                     $cart->setErrors(new ErrorCollection(array_filter($cart->getErrors()->getElements(), function ($key) {
  107.                         return $key !== 'payment-method-blocked-Punkte';
  108.                     }, ARRAY_FILTER_USE_KEY)));
  109.                     $data = new RequestDataBag([
  110.                         SalesChannelContextService::PAYMENT_METHOD_ID => $this->getPaymentMethodId(DefaultPayment::class, 'Keine Zahlungsdaten erforderlich')
  111.                     ]);
  112.                     /** @var ContextSwitchRoute $contextSwitchRoute */
  113.                     $contextSwitchRoute $this->container->get('Shopware\Core\System\SalesChannel\SalesChannel\ContextSwitchRoute');
  114.                     $contextSwitchRoute->switchContext($data$event->getSalesChannelContext());
  115.                     $response = new RedirectResponse($this->router->generate($route));
  116.                     $response->send();
  117.                 }
  118.             }
  119.         }
  120.     }
  121.     protected function getPaymentMethodId($handlerIdentifier$distinguishableName null)
  122.     {
  123.         /** @var EntityRepositoryInterface $paymentRepository */
  124.         $paymentRepository $this->container->get('payment_method.repository');
  125.         $paymentCriteria = (new Criteria())
  126.             ->addFilter(new EqualsFilter('handlerIdentifier'$handlerIdentifier));
  127.         if ($distinguishableName) {
  128.             $paymentCriteria
  129.                 ->addFilter(new EqualsFilter('name'$distinguishableName))
  130.                 ->addAssociation('payment_method_translation');
  131.         }
  132.         return $paymentRepository->searchIds($paymentCriteriaContext::createDefaultContext())->firstId();
  133.     }
  134. }