custom/plugins/UnzerPayment6/src/EventListeners/PaymentMethod/PaymentMethodLoadedEventListener.php line 42

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace UnzerPayment6\EventListeners\PaymentMethod;
  4. use Shopware\Core\Checkout\Payment\Cart\Error\PaymentMethodBlockedError;
  5. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  8. use Shopware\Core\System\Currency\CurrencyEntity;
  9. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityIdSearchResultLoadedEvent;
  10. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntitySearchResultLoadedEvent;
  11. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use UnzerPayment6\Components\ConfigReader\ConfigReader;
  15. use UnzerPayment6\Components\ConfigReader\ConfigReaderInterface;
  16. use UnzerPayment6\Installer\PaymentInstaller;
  17. use UnzerPayment6\UnzerPayment6;
  18. class PaymentMethodLoadedEventListener implements EventSubscriberInterface
  19. {
  20.     /** @var ConfigReaderInterface */
  21.     private $configReader;
  22.     public function __construct(ConfigReaderInterface $configReader)
  23.     {
  24.         $this->configReader $configReader;
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             'sales_channel.payment_method.search.id.result.loaded' => ['onSalesChannelIdSearchResultLoaded', -1],
  30.             'sales_channel.payment_method.search.result.loaded'    => ['onSalesChannelSearchResultLoaded', -1],
  31.             AccountEditOrderPageLoadedEvent::class                 => 'onAccountEditOrderPageLoaded',
  32.             CheckoutConfirmPageLoadedEvent::class                  => 'onCheckoutConfirmPageLoaded',
  33.         ];
  34.     }
  35.     public function onSalesChannelIdSearchResultLoaded(SalesChannelEntityIdSearchResultLoadedEvent $event): void
  36.     {
  37.         $result              $event->getResult();
  38.         $salesChannelContext $event->getSalesChannelContext();
  39.         if (!$this->isConfigurationValid($salesChannelContext->getSalesChannel()->getId())) {
  40.             $this->removePaymentMethodsFromIdResult($result);
  41.         }
  42.     }
  43.     public function onSalesChannelSearchResultLoaded(SalesChannelEntitySearchResultLoadedEvent $event): void
  44.     {
  45.         $result              $event->getResult();
  46.         $salesChannelContext $event->getSalesChannelContext();
  47.         if (!$this->isConfigurationValid($salesChannelContext->getSalesChannel()->getId())) {
  48.             $this->removePaymentMethodsFromResult($result);
  49.         }
  50.     }
  51.     public function onAccountEditOrderPageLoaded(AccountEditOrderPageLoadedEvent $pageLoadedEvent): void
  52.     {
  53.         $page        $pageLoadedEvent->getPage();
  54.         $order       $page->getOrder();
  55.         $totalAmount $order->getAmountTotal();
  56.         if ($this->isZeroAmount($totalAmount$pageLoadedEvent->getSalesChannelContext()->getCurrency())) {
  57.             $page->setPaymentMethods($page->getPaymentMethods()->filter(static function (PaymentMethodEntity $paymentMethod) {
  58.                 return !in_array($paymentMethod->getId(), PaymentInstaller::PAYMENT_METHOD_IDStrue);
  59.             })
  60.             );
  61.             $pageLoadedEvent->getSalesChannelContext()->assign(['paymentMethods' => $page->getPaymentMethods()]);
  62.         }
  63.     }
  64.     public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $pageLoadedEvent): void
  65.     {
  66.         $salesChannelContext $pageLoadedEvent->getSalesChannelContext();
  67.         $page                $pageLoadedEvent->getPage();
  68.         $cart                $page->getCart();
  69.         $totalAmount         $cart->getPrice()->getTotalPrice();
  70.         if ($this->isZeroAmount($totalAmount$salesChannelContext->getCurrency())) {
  71.             $page->setPaymentMethods($page->getPaymentMethods()->filter(static function (PaymentMethodEntity $paymentMethod) {
  72.                 return !in_array($paymentMethod->getId(), PaymentInstaller::PAYMENT_METHOD_IDStrue);
  73.             })
  74.             );
  75.             $salesChannelContext->assign(['paymentMethods' => $page->getPaymentMethods()]);
  76.         }
  77.         if (in_array($salesChannelContext->getPaymentMethod()->getId(), PaymentInstaller::PAYMENT_METHOD_IDStrue)
  78.             && !array_key_exists($salesChannelContext->getPaymentMethod()->getId(), $page->getPaymentMethods()->getElements())) {
  79.             $page->getCart()->addErrors(new PaymentMethodBlockedError($salesChannelContext->getPaymentMethod()->getName()));
  80.         }
  81.     }
  82.     protected function removePaymentMethodsFromIdResult(IdSearchResult $result): void
  83.     {
  84.         $filteredPaymentMethods array_filter($result->getIds(), static function (string $paymentMethod) {
  85.             return !in_array($paymentMethodPaymentInstaller::PAYMENT_METHOD_IDStrue);
  86.         });
  87.         $result->assign([
  88.             'total'    => count($filteredPaymentMethods),
  89.             'ids'      => $filteredPaymentMethods,
  90.             'entities' => $filteredPaymentMethods,
  91.             'elements' => $filteredPaymentMethods,
  92.         ]);
  93.     }
  94.     protected function removePaymentMethodsFromResult(EntitySearchResult $result): void
  95.     {
  96.         $filteredResult $result->getEntities()->filter(static function (PaymentMethodEntity $entity) {
  97.             return !in_array($entity->getId(), PaymentInstaller::PAYMENT_METHOD_IDStrue);
  98.         });
  99.         $result->assign([
  100.             'total'    => count($filteredResult),
  101.             'entities' => $filteredResult,
  102.             'elements' => $filteredResult->getElements(),
  103.         ]);
  104.     }
  105.     protected function isConfigurationValid(string $salesChannelId): bool
  106.     {
  107.         $configData $this->configReader->read($salesChannelId);
  108.         return !(empty($configData->get(ConfigReader::CONFIG_KEY_PUBLIC_KEY'')) || empty($configData->get(ConfigReader::CONFIG_KEY_PRIVATE_KEY'')));
  109.     }
  110.     protected function isZeroAmount(float $totalAmountCurrencyEntity $currency): bool
  111.     {
  112.         $currencyPrecision  min($currency->getDecimalPrecision(), UnzerPayment6::MAX_DECIMAL_PRECISION);
  113.         $roundedAmountTotal = (int) round($totalAmount * (10 ** $currencyPrecision));
  114.         return $roundedAmountTotal <= 0;
  115.     }
  116. }