custom/plugins/KlarnaPayment/src/Components/EventListener/CheckoutEventListener.php line 56

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace KlarnaPayment\Components\EventListener;
  4. use KlarnaPayment\Components\Extension\TemplateData\CheckoutDataExtension;
  5. use KlarnaPayment\Components\Helper\PaymentHelper\PaymentHelperInterface;
  6. use KlarnaPayment\Core\Checkout\Cart\Error\MissingStateError;
  7. use KlarnaPayment\Installer\Modules\PaymentMethodInstaller;
  8. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  10. use Shopware\Storefront\Page\PageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class CheckoutEventListener implements EventSubscriberInterface
  13. {
  14.     /** @var PaymentHelperInterface */
  15.     private $paymentHelper;
  16.     public function __construct(PaymentHelperInterface $paymentHelper)
  17.     {
  18.         $this->paymentHelper $paymentHelper;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             CheckoutConfirmPageLoadedEvent::class  => [['addKlarnaTemplateData'], ['validateCountryState']],
  24.             AccountEditOrderPageLoadedEvent::class => [['addKlarnaTemplateData']],
  25.         ];
  26.     }
  27.     public function addKlarnaTemplateData(PageLoadedEvent $event): void
  28.     {
  29.         if (!($event instanceof CheckoutConfirmPageLoadedEvent) && !($event instanceof AccountEditOrderPageLoadedEvent)) {
  30.             return;
  31.         }
  32.         if ($this->paymentHelper->isKlarnaPaymentsEnabled($event->getSalesChannelContext())) {
  33.             $type CheckoutDataExtension::TYPE_PAYMENTS;
  34.         } elseif ($this->paymentHelper->isKlarnaCheckoutEnabled($event->getSalesChannelContext())) {
  35.             $type CheckoutDataExtension::TYPE_CHECKOUT;
  36.         } else {
  37.             return;
  38.         }
  39.         $templateData = new CheckoutDataExtension();
  40.         $templateData->assign([
  41.             'klarnaType' => $type,
  42.         ]);
  43.         $event->getPage()->addExtension(CheckoutDataExtension::EXTENSION_NAME$templateData);
  44.     }
  45.     public function validateCountryState(CheckoutConfirmPageLoadedEvent $event): void
  46.     {
  47.         if (!($this->paymentHelper->isKlarnaPaymentsEnabled($event->getSalesChannelContext()) || $this->paymentHelper->isKlarnaCheckoutEnabled($event->getSalesChannelContext()))) {
  48.             return;
  49.         }
  50.         if (!$this->paymentHelper->isKlarnaPaymentsSelected($event->getSalesChannelContext())) {
  51.             return;
  52.         }
  53.         $customer $event->getSalesChannelContext()->getCustomer();
  54.         if ($customer === null) {
  55.             return;
  56.         }
  57.         if (is_null($customer->getActiveBillingAddress())) {
  58.             return;
  59.         }
  60.         $activeBillingAddress $customer->getActiveBillingAddress();
  61.         if ($activeBillingAddress->getCountry() === null) {
  62.             return;
  63.         }
  64.         if ($activeBillingAddress->getCountry()->getIso() === PaymentMethodInstaller::KLARNA_API_REGION_US && $activeBillingAddress->getCountryStateId() === null) {
  65.             $errors $event->getPage()->getCart()->getErrors();
  66.             $errors->add(new MissingStateError());
  67.             $event->getPage()->getCart()->setErrors($errors);
  68.         }
  69.     }
  70. }