custom/plugins/PayonePayment/src/EventListener/CheckoutConfirmSecuredInstallmentEventListener.php line 37

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\Components\SecuredInstallment\InstallmentServiceInterface;
  5. use PayonePayment\PaymentHandler\PayoneSecuredInstallmentPaymentHandler;
  6. use PayonePayment\PaymentMethod\PayoneSecuredInstallment;
  7. use PayonePayment\Storefront\Struct\SecuredInstallmentOptionsData;
  8. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  12. use Shopware\Storefront\Page\Page;
  13. use Shopware\Storefront\Page\PageLoadedEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class CheckoutConfirmSecuredInstallmentEventListener implements EventSubscriberInterface
  16. {
  17.     protected InstallmentServiceInterface $installmentService;
  18.     public function __construct(InstallmentServiceInterface $installmentService)
  19.     {
  20.         $this->installmentService $installmentService;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             CheckoutConfirmPageLoadedEvent::class => [
  26.                 ['addPayonePageData'],
  27.             ],
  28.         ];
  29.     }
  30.     public function addPayonePageData(PageLoadedEvent $event): void
  31.     {
  32.         /** @var Page $page */
  33.         $page $event->getPage();
  34.         $context $event->getSalesChannelContext();
  35.         if ($context->getPaymentMethod()->getId() === PayoneSecuredInstallment::UUID) {
  36.             $this->addInstallmentOptionsData($page$context);
  37.         }
  38.     }
  39.     protected function addInstallmentOptionsData(Page $pageSalesChannelContext $context): void
  40.     {
  41.         $installmentOptions $this->installmentService->getInstallmentOptions($context);
  42.         if (\count($installmentOptions->getOptions()) > 0) {
  43.             $page->addExtension(SecuredInstallmentOptionsData::EXTENSION_NAME$installmentOptions);
  44.         } elseif (method_exists($page'getPaymentMethods') && method_exists($page'setPaymentMethods')) {
  45.             $page->setPaymentMethods(
  46.                 $this->removePaymentMethods(
  47.                     $page->getPaymentMethods(),
  48.                     [PayoneSecuredInstallmentPaymentHandler::class]
  49.                 )
  50.             );
  51.         }
  52.     }
  53.     protected function removePaymentMethods(PaymentMethodCollection $paymentMethods, array $paymentHandler): PaymentMethodCollection
  54.     {
  55.         return $paymentMethods->filter(
  56.             static function (PaymentMethodEntity $paymentMethod) use ($paymentHandler) {
  57.                 return !\in_array($paymentMethod->getHandlerIdentifier(), $paymentHandlertrue);
  58.             }
  59.         );
  60.     }
  61. }