custom/plugins/KlarnaPayment/src/Components/EventListener/PaymentMethodEventListener.php line 39

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace KlarnaPayment\Components\EventListener;
  4. use KlarnaPayment\Components\ConfigReader\ConfigReaderInterface;
  5. use KlarnaPayment\Installer\Modules\PaymentMethodInstaller;
  6. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  7. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityIdSearchResultLoadedEvent;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  12. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityIdSearchResultLoadedEvent;
  13. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntitySearchResultLoadedEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class PaymentMethodEventListener implements EventSubscriberInterface
  16. {
  17.     /** @var ConfigReaderInterface */
  18.     private $configReader;
  19.     public function __construct(ConfigReaderInterface $configReader)
  20.     {
  21.         $this->configReader $configReader;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             'sales_channel.payment_method.search.id.result.loaded' => ['onSalesChannelIdSearchResultLoaded', -1],
  27.             'payment_method.search.id.result.loaded'               => ['onIdSearchResultLoaded', -1],
  28.             'sales_channel.payment_method.search.result.loaded'    => ['onSalesChannelSearchResultLoaded', -1],
  29.             'payment_method.search.result.loaded'                  => ['onSearchResultLoaded', -1],
  30.         ];
  31.     }
  32.     public function onSalesChannelIdSearchResultLoaded(SalesChannelEntityIdSearchResultLoadedEvent $event): void
  33.     {
  34.         $source $event->getContext()->getSource();
  35.         if (!($source instanceof SalesChannelApiSource)) {
  36.             return;
  37.         }
  38.         $this->removeDeactivatedPaymentMethodsIds($event->getResult(), $source->getSalesChannelId());
  39.     }
  40.     public function onIdSearchResultLoaded(EntityIdSearchResultLoadedEvent $event): void
  41.     {
  42.         $source $event->getContext()->getSource();
  43.         if (!($source instanceof SalesChannelApiSource)) {
  44.             return;
  45.         }
  46.         $this->removeDeactivatedPaymentMethodsIds($event->getResult(), $source->getSalesChannelId());
  47.     }
  48.     public function onSalesChannelSearchResultLoaded(SalesChannelEntitySearchResultLoadedEvent $event): void
  49.     {
  50.         $source $event->getContext()->getSource();
  51.         if (!($source instanceof SalesChannelApiSource)) {
  52.             return;
  53.         }
  54.         $this->removeDeactivatedPaymentMethods($event->getResult(), $source->getSalesChannelId());
  55.     }
  56.     public function onSearchResultLoaded(EntitySearchResultLoadedEvent $event): void
  57.     {
  58.         $source $event->getContext()->getSource();
  59.         if (!($source instanceof SalesChannelApiSource)) {
  60.             return;
  61.         }
  62.         $this->removeDeactivatedPaymentMethods($event->getResult(), $source->getSalesChannelId());
  63.     }
  64.     private function removeDeactivatedPaymentMethods(EntitySearchResult $resultstring $salesChannelId null): void
  65.     {
  66.         $validPaymentMethods     $this->getValidPaymentMethods($salesChannelId);
  67.         $allKlarnaPaymentMethods $this->getAllKlarnaPaymentMethods();
  68.         $filter = static function (PaymentMethodEntity $entity) use ($validPaymentMethods$allKlarnaPaymentMethods) {
  69.             if (!in_array($entity->getId(), $allKlarnaPaymentMethodstrue)) {
  70.                 return true;
  71.             }
  72.             return in_array($entity->getId(), $validPaymentMethodstrue);
  73.         };
  74.         $filteredPaymentMethods $result->getEntities()->filter($filter);
  75.         $result->assign([
  76.             'total'    => count($filteredPaymentMethods),
  77.             'entities' => $filteredPaymentMethods,
  78.             'elements' => $filteredPaymentMethods->getElements(),
  79.         ]);
  80.     }
  81.     private function removeDeactivatedPaymentMethodsIds(IdSearchResult $resultstring $salesChannelId null): void
  82.     {
  83.         $validPaymentMethods     $this->getValidPaymentMethods($salesChannelId);
  84.         $allKlarnaPaymentMethods $this->getAllKlarnaPaymentMethods();
  85.         $filter = static function (string $paymentMethod) use ($validPaymentMethods$allKlarnaPaymentMethods) {
  86.             if (!in_array($paymentMethod$allKlarnaPaymentMethodstrue)) {
  87.                 return true;
  88.             }
  89.             return in_array($paymentMethod$validPaymentMethodstrue);
  90.         };
  91.         /** @var array<string> $ids */
  92.         $ids $result->getIds();
  93.         $filteredPaymentMethods array_filter($ids$filter);
  94.         $result->assign([
  95.             'total'    => count($filteredPaymentMethods),
  96.             'ids'      => $filteredPaymentMethods,
  97.             'entities' => $filteredPaymentMethods,
  98.             'elements' => $filteredPaymentMethods,
  99.         ]);
  100.     }
  101.     /**
  102.      * @return string[]
  103.      */
  104.     private function getValidPaymentMethods(string $salesChannelId null): array
  105.     {
  106.         $config $this->configReader->read($salesChannelId);
  107.         if ($config->get('klarnaType') === 'checkout') {
  108.             return array_keys(PaymentMethodInstaller::KLARNA_CHECKOUT_CODES);
  109.         }
  110.         if ($config->get('klarnaType') === 'payments') {
  111.             $validPaymentMethods array_keys(PaymentMethodInstaller::KLARNA_PAYMENTS_CODES);
  112.             $merchantValidKlarnaPaymentsMethods   $config->get('allowedKlarnaPaymentsCodes', []);
  113.             $merchantValidKlarnaPaymentsMethods[] = PaymentMethodInstaller::KLARNA_PAYMENTS_KLARNA_CODE;
  114.             if (in_array(PaymentMethodInstaller::KLARNA_PAYMENTS_PAY_NOW_CODE$merchantValidKlarnaPaymentsMethodstrue)) {
  115.                 $additionalValidCodes array_map(
  116.                     static function (string $paymentMethodId) {
  117.                         return PaymentMethodInstaller::KLARNA_PAYMENTS_CODES[$paymentMethodId];
  118.                     },
  119.                     PaymentMethodInstaller::KLARNA_PAYMENTS_CODES_PAY_NOW_STANDALONE
  120.                 );
  121.                 $merchantValidKlarnaPaymentsMethods array_unique(array_merge($merchantValidKlarnaPaymentsMethods$additionalValidCodes));
  122.             }
  123.             return array_filter(
  124.                 $validPaymentMethods,
  125.                 static function (string $paymentMethodId) use ($merchantValidKlarnaPaymentsMethods) {
  126.                     return in_array(PaymentMethodInstaller::KLARNA_PAYMENTS_CODES[$paymentMethodId], $merchantValidKlarnaPaymentsMethodstrue);
  127.                 }
  128.             );
  129.         }
  130.         return [];
  131.     }
  132.     /**
  133.      * @return string[]
  134.      */
  135.     private function getAllKlarnaPaymentMethods(): array
  136.     {
  137.         return array_merge(
  138.             array_keys(PaymentMethodInstaller::KLARNA_CHECKOUT_CODES),
  139.             array_keys(PaymentMethodInstaller::KLARNA_PAYMENTS_CODES)
  140.         );
  141.     }
  142. }