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

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.         $filteredPaymentMethods array_filter($result->getIds(), $filter);
  92.         $result->assign([
  93.             'total'    => count($filteredPaymentMethods),
  94.             'ids'      => $filteredPaymentMethods,
  95.             'entities' => $filteredPaymentMethods,
  96.             'elements' => $filteredPaymentMethods,
  97.         ]);
  98.     }
  99.     private function getValidPaymentMethods(string $salesChannelId null): array
  100.     {
  101.         $config $this->configReader->read($salesChannelId);
  102.         if ($config->get('klarnaType') === 'checkout') {
  103.             $validPaymentMethods array_keys(PaymentMethodInstaller::KLARNA_CHECKOUT_CODES);
  104.         } elseif ($config->get('klarnaType') === 'payments') {
  105.             $validPaymentMethods array_keys(PaymentMethodInstaller::KLARNA_PAYMENTS_CODES);
  106.             $merchantValidKlarnaPaymentsMethods $config->get('allowedKlarnaPaymentsCodes', []);
  107.             if (in_array(PaymentMethodInstaller::KLARNA_PAYMENTS_PAY_NOW_CODE$merchantValidKlarnaPaymentsMethodstrue)) {
  108.                 $additionalValidCodes array_map(
  109.                     static function (string $paymentMethodId) {
  110.                         return PaymentMethodInstaller::KLARNA_PAYMENTS_CODES[$paymentMethodId];
  111.                     },
  112.                     PaymentMethodInstaller::KLARNA_PAYMENTS_CODES_PAY_NOW_STANDALONE
  113.                 );
  114.                 $merchantValidKlarnaPaymentsMethods array_unique(array_merge($merchantValidKlarnaPaymentsMethods$additionalValidCodes));
  115.             }
  116.             $validPaymentMethods array_filter(
  117.                 $validPaymentMethods,
  118.                 static function (string $paymentMethodId) use ($merchantValidKlarnaPaymentsMethods) {
  119.                     return in_array(PaymentMethodInstaller::KLARNA_PAYMENTS_CODES[$paymentMethodId], $merchantValidKlarnaPaymentsMethodstrue);
  120.                 }
  121.             );
  122.         } else {
  123.             $validPaymentMethods = [];
  124.         }
  125.         return $validPaymentMethods;
  126.     }
  127.     private function getAllKlarnaPaymentMethods(): array
  128.     {
  129.         return array_merge(
  130.             array_keys(PaymentMethodInstaller::KLARNA_CHECKOUT_CODES),
  131.             array_keys(PaymentMethodInstaller::KLARNA_PAYMENTS_CODES)
  132.         );
  133.     }
  134. }