custom/plugins/WizmoGmbhIvyPayment/src/Subscriber/ButtonSubscriber.php line 56

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) WIZMO GmbH <plugins@shopentwickler.berlin>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace WizmoGmbh\IvyPayment\Subscriber;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  14. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  15. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  16. use Shopware\Storefront\Page\PageLoadedEvent;
  17. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use WizmoGmbh\IvyPayment\Components\Config\ConfigHandler;
  20. use WizmoGmbh\IvyPayment\PaymentHandler\IvyPaymentHandler;
  21. class ButtonSubscriber implements EventSubscriberInterface
  22. {
  23.     private ConfigHandler $configHandler;
  24.     private EntityRepositoryInterface $salesChannelRepository;
  25.     public function __construct(
  26.         ConfigHandler $configHandler,
  27.         EntityRepositoryInterface $salesChannelRepository
  28.     ) {
  29.         $this->configHandler $configHandler;
  30.         $this->salesChannelRepository $salesChannelRepository;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             ProductPageLoadedEvent::class => 'onIvyButtonLoaded',
  36.             CheckoutCartPageLoadedEvent::class => 'onIvyButtonLoaded',
  37.             CheckoutConfirmPageLoadedEvent::class => 'onIvyButtonLoaded',
  38.             OffcanvasCartPageLoadedEvent::class => 'onIvyButtonLoaded',
  39.             CheckoutRegisterPageLoadedEvent::class => 'onIvyButtonLoaded',
  40.         ];
  41.     }
  42.     /**
  43.      * @param PageLoadedEvent $event
  44.      * @return void
  45.      * @throws \Doctrine\DBAL\Exception
  46.      */
  47.     public function onIvyButtonLoaded(PageLoadedEvent $event): void
  48.     {
  49.         $paymentMethods $event->getPage()->getSalesChannelPaymentMethods();
  50.         if ($paymentMethods === null) {
  51.             $criteria = new Criteria([$event->getSalesChannelContext()->getSalesChannel()->getId()]);
  52.             $criteria->addAssociation('paymentMethods');
  53.             $loadedSalesChannel $this->salesChannelRepository->search($criteria$event->getContext())->first();
  54.             if ($loadedSalesChannel !== null) {
  55.                 $paymentMethods $loadedSalesChannel->getPaymentMethods();
  56.             }
  57.         }
  58.         if ($paymentMethods !== null) {
  59.             /** @var PaymentMethodEntity $paymentMethod */
  60.             foreach ($paymentMethods as $paymentMethod) {
  61.                 if ($paymentMethod->getActive() && $paymentMethod->getHandlerIdentifier() === IvyPaymentHandler::class) {
  62.                     $config $this->configHandler->getFullConfig($event->getSalesChannelContext());
  63.                     $event->getPage()->assign($config);
  64.                     break;
  65.                 }
  66.             }
  67.         }
  68.         $config $this->configHandler->getFullConfig($event->getSalesChannelContext());
  69.         $event->getPage()->assign($config);
  70.     }
  71. }