custom/plugins/LyranetworkLyra/src/Subscriber/CheckoutConfirmSubscriber.php line 52

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright © Lyra Network.
  4.  * This file is part of Lyra Collect plugin for Shopware. See COPYING.md for license details.
  5.  *
  6.  * @author    Lyra Network <https://www.lyra.com>
  7.  * @copyright Lyra Network
  8.  * @license   https://opensource.org/licenses/mit-license.html The MIT License (MIT)
  9.  */
  10. declare(strict_types=1);
  11. namespace Lyranetwork\Lyra\Subscriber;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  14. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPage;
  15. use Shopware\Core\Checkout\Cart\Cart;
  16. use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
  17. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  18. use Shopware\Core\System\SalesChannel\SalesChannelEntity;
  19. use Shopware\Core\System\Currency\CurrencyEntity;
  20. use Lyranetwork\Lyra\Service\ConfigService;
  21. use Lyranetwork\Lyra\Service\PaymentMethodService;
  22. use Lyranetwork\Lyra\Sdk\Form\Api as LyraApi;
  23. class CheckoutConfirmSubscriber implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var ConfigService
  27.      */
  28.     private $configService;
  29.     /**
  30.      * @var PaymentMethodService
  31.      */
  32.     private $paymentMethodService;
  33.     public function __construct(ConfigService $configServicePaymentMethodService $paymentMethodService)
  34.     {
  35.         $this->configService $configService;
  36.         $this->paymentMethodService $paymentMethodService;
  37.     }
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             CheckoutConfirmPageLoadedEvent::class => ['onConfirmPageLoaded'1]
  42.         ];
  43.     }
  44.     public function onConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  45.     {
  46.         $currency $event->getSalesChannelContext()->getSalesChannel()->getCurrency()->getIsoCode(); // Current shop currency.
  47.         $amount $event->getPage()->getCart()->getPrice()->getTotalPrice(); // Current basket amount.
  48.         $min $this->configService->get('min_amount'); // Minimum amount to activate this module.
  49.         $max $this->configService->get('max_amount'); // Maximum amount to activate this module.
  50.         if (($min && $amount $min) || ($max && $amount $max) || ! LyraApi::findCurrencyByAlphaCode($currency)) {
  51.             $this->removePaymentMethodFromConfirmPage($event);
  52.         }
  53.     }
  54.     private function removePaymentMethodFromConfirmPage(CheckoutConfirmPageLoadedEvent $event): void
  55.     {
  56.         $paymentMethodCollection $event->getPage()->getPaymentMethods();
  57.         $paymentMethodCollection->remove($this->paymentMethodService->getPaymentMethodId($event->getContext()));
  58.     }
  59. }