custom/plugins/PayonePayment/src/EventListener/CheckoutConfirmRatepayEventListener.php line 106

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace PayonePayment\EventListener;
  4. use PayonePayment\Components\Ratepay\Installment\InstallmentServiceInterface;
  5. use PayonePayment\Components\Ratepay\Profile\ProfileServiceInterface;
  6. use PayonePayment\PaymentHandler\PaymentHandlerGroups;
  7. use PayonePayment\PaymentHandler\PayoneRatepayInstallmentPaymentHandler;
  8. use PayonePayment\PaymentMethod\PayoneRatepayInstallment;
  9. use PayonePayment\Storefront\Struct\RatepayInstallmentCalculatorData;
  10. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  11. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  15. use Shopware\Storefront\Page\Account\PaymentMethod\AccountPaymentMethodPageLoadedEvent;
  16. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  17. use Shopware\Storefront\Page\Page;
  18. use Shopware\Storefront\Page\PageLoadedEvent;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class CheckoutConfirmRatepayEventListener implements EventSubscriberInterface
  21. {
  22.     protected SystemConfigService $systemConfigService;
  23.     protected InstallmentServiceInterface $installmentService;
  24.     protected ProfileServiceInterface $profileService;
  25.     public function __construct(
  26.         SystemConfigService $systemConfigService,
  27.         InstallmentServiceInterface $installmentService,
  28.         ProfileServiceInterface $profileService
  29.     ) {
  30.         $this->systemConfigService $systemConfigService;
  31.         $this->installmentService $installmentService;
  32.         $this->profileService $profileService;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             CheckoutConfirmPageLoadedEvent::class => [
  38.                 ['hidePaymentMethodsForCompanies'],
  39.                 ['hidePaymentMethodsByProfiles'],
  40.                 ['addPayonePageData'],
  41.             ],
  42.             AccountEditOrderPageLoadedEvent::class => [
  43.                 ['hidePaymentMethodsForCompanies'],
  44.                 ['hidePaymentMethodsByProfiles'],
  45.                 ['addPayonePageData'],
  46.             ],
  47.             AccountPaymentMethodPageLoadedEvent::class => 'hidePaymentMethodsForCompanies',
  48.         ];
  49.     }
  50.     public function hidePaymentMethodsForCompanies(PageLoadedEvent $event): void
  51.     {
  52.         $page $event->getPage();
  53.         if (
  54.             !method_exists($page'getPaymentMethods')
  55.             || !method_exists($page'setPaymentMethods')
  56.         ) {
  57.             return;
  58.         }
  59.         if (!$this->customerHasCompanyAddress($event->getSalesChannelContext())) {
  60.             return;
  61.         }
  62.         $paymentMethods $this->removePaymentMethods($page->getPaymentMethods(), PaymentHandlerGroups::RATEPAY);
  63.         $page->setPaymentMethods($paymentMethods);
  64.     }
  65.     public function hidePaymentMethodsByProfiles(PageLoadedEvent $event): void
  66.     {
  67.         $page $event->getPage();
  68.         if (
  69.             !method_exists($page'getPaymentMethods')
  70.             || !method_exists($page'setPaymentMethods')
  71.         ) {
  72.             return;
  73.         }
  74.         $paymentMethods $page->getPaymentMethods();
  75.         foreach (PaymentHandlerGroups::RATEPAY as $ratepayPaymentHandler) {
  76.             $profile $this->profileService->getProfileBySalesChannelContext(
  77.                 $event->getSalesChannelContext(),
  78.                 $ratepayPaymentHandler
  79.             );
  80.             if (!$profile) {
  81.                 $paymentMethods $this->removePaymentMethods($paymentMethods, [$ratepayPaymentHandler]);
  82.             }
  83.         }
  84.         $page->setPaymentMethods($paymentMethods);
  85.     }
  86.     public function addPayonePageData(PageLoadedEvent $event): void
  87.     {
  88.         /** @var Page $page */
  89.         $page $event->getPage();
  90.         $context $event->getSalesChannelContext();
  91.         if ($context->getPaymentMethod()->getId() === PayoneRatepayInstallment::UUID) {
  92.             $this->addInstallmentCalculatorData($page$context);
  93.         }
  94.     }
  95.     protected function removePaymentMethods(PaymentMethodCollection $paymentMethods, array $paymentHandler): PaymentMethodCollection
  96.     {
  97.         return $paymentMethods->filter(
  98.             static function (PaymentMethodEntity $paymentMethod) use ($paymentHandler) {
  99.                 return !\in_array($paymentMethod->getHandlerIdentifier(), $paymentHandlertrue);
  100.             }
  101.         );
  102.     }
  103.     protected function customerHasCompanyAddress(SalesChannelContext $context): bool
  104.     {
  105.         $customer $context->getCustomer();
  106.         if ($customer === null) {
  107.             return false;
  108.         }
  109.         $billingAddress $customer->getActiveBillingAddress();
  110.         if ($billingAddress === null) {
  111.             return false;
  112.         }
  113.         return !empty($billingAddress->getCompany());
  114.     }
  115.     protected function addInstallmentCalculatorData(Page $pageSalesChannelContext $context): void
  116.     {
  117.         if (
  118.             !method_exists($page'getPaymentMethods')
  119.             || !method_exists($page'setPaymentMethods')
  120.         ) {
  121.             return;
  122.         }
  123.         $installmentCalculator $this->installmentService->getInstallmentCalculatorData($context);
  124.         if ($installmentCalculator === null) {
  125.             $paymentMethods $this->removePaymentMethods($page->getPaymentMethods(), [
  126.                 PayoneRatepayInstallmentPaymentHandler::class,
  127.             ]);
  128.             $page->setPaymentMethods($paymentMethods);
  129.             return;
  130.         }
  131.         $page->addExtension(RatepayInstallmentCalculatorData::EXTENSION_NAME$installmentCalculator);
  132.     }
  133. }