custom/plugins/SwagAmazonPay/src/Storefront/EventListeners/AmazonPayAvailabilityListener.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * (c) shopware AG <info@shopware.com>
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Swag\AmazonPay\Storefront\EventListeners;
  9. use Psr\Log\LoggerInterface;
  10. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  11. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntitySearchResultLoadedEvent;
  12. use Swag\AmazonPay\Components\Config\ConfigServiceInterface;
  13. use Swag\AmazonPay\Components\Config\Validation\Exception\ConfigValidationException;
  14. use Swag\AmazonPay\Installer\PaymentMethodInstaller;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class AmazonPayAvailabilityListener implements EventSubscriberInterface
  17. {
  18.     private ConfigServiceInterface $configService;
  19.     private LoggerInterface $logger;
  20.     public function __construct(
  21.         ConfigServiceInterface $configService,
  22.         LoggerInterface $logger
  23.     ) {
  24.         $this->configService $configService;
  25.         $this->logger $logger;
  26.     }
  27.     /**
  28.      * {@inheritdoc}
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             'sales_channel.payment_method.search.result.loaded' => ['onSalesChannelSearchResultLoaded', -1],
  34.         ];
  35.     }
  36.     public function onSalesChannelSearchResultLoaded(SalesChannelEntitySearchResultLoadedEvent $event): void
  37.     {
  38.         try {
  39.             $config $this->configService->getPluginConfig($event->getSalesChannelContext()->getSalesChannel()->getId());
  40.         } catch (ConfigValidationException $e) {
  41.             $this->logger->error('Invalid plugin configuration', ['field' => $e->getField()]);
  42.             $this->removeSwagAmazonPayPaymentMethod($event);
  43.             return;
  44.         }
  45.         if ($event->getSalesChannelContext()->getSalesChannel()->getPaymentMethodId() === PaymentMethodInstaller::AMAZON_PAYMENT_ID) {
  46.             return;
  47.         }
  48.         if ($config->hideOneClickCheckoutButtons() === true) {
  49.             $this->removeSwagAmazonPayPaymentMethod($event);
  50.         }
  51.     }
  52.     private function removeSwagAmazonPayPaymentMethod(SalesChannelEntitySearchResultLoadedEvent $event): void
  53.     {
  54.         $filter = static function (PaymentMethodEntity $entity): bool {
  55.             return $entity->getId() !== PaymentMethodInstaller::AMAZON_PAYMENT_ID;
  56.         };
  57.         $filteredPaymentMethods $event->getResult()->getEntities()->filter($filter);
  58.         $event->getResult()->assign([
  59.             'total' => \count($filteredPaymentMethods),
  60.             'entities' => $filteredPaymentMethods,
  61.             'elements' => $filteredPaymentMethods->getElements(),
  62.         ]);
  63.     }
  64. }