<?php
declare(strict_types=1);
namespace KlarnaPayment\Components\EventListener;
use KlarnaPayment\Components\Extension\TemplateData\CheckoutDataExtension;
use KlarnaPayment\Components\Helper\PaymentHelper\PaymentHelperInterface;
use KlarnaPayment\Core\Checkout\Cart\Error\MissingStateError;
use KlarnaPayment\Installer\Modules\PaymentMethodInstaller;
use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckoutEventListener implements EventSubscriberInterface
{
/** @var PaymentHelperInterface */
private $paymentHelper;
public function __construct(PaymentHelperInterface $paymentHelper)
{
$this->paymentHelper = $paymentHelper;
}
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => [['addKlarnaTemplateData'], ['validateCountryState']],
AccountEditOrderPageLoadedEvent::class => [['addKlarnaTemplateData']],
];
}
public function addKlarnaTemplateData(PageLoadedEvent $event): void
{
if (!($event instanceof CheckoutConfirmPageLoadedEvent) && !($event instanceof AccountEditOrderPageLoadedEvent)) {
return;
}
if ($this->paymentHelper->isKlarnaPaymentsEnabled($event->getSalesChannelContext())) {
$type = CheckoutDataExtension::TYPE_PAYMENTS;
} elseif ($this->paymentHelper->isKlarnaCheckoutEnabled($event->getSalesChannelContext())) {
$type = CheckoutDataExtension::TYPE_CHECKOUT;
} else {
return;
}
$templateData = new CheckoutDataExtension();
$templateData->assign([
'klarnaType' => $type,
]);
$event->getPage()->addExtension(CheckoutDataExtension::EXTENSION_NAME, $templateData);
}
public function validateCountryState(CheckoutConfirmPageLoadedEvent $event): void
{
if (!($this->paymentHelper->isKlarnaPaymentsEnabled($event->getSalesChannelContext()) || $this->paymentHelper->isKlarnaCheckoutEnabled($event->getSalesChannelContext()))) {
return;
}
if (!$this->paymentHelper->isKlarnaPaymentsSelected($event->getSalesChannelContext())) {
return;
}
$customer = $event->getSalesChannelContext()->getCustomer();
if ($customer === null) {
return;
}
if (is_null($customer->getActiveBillingAddress())) {
return;
}
$activeBillingAddress = $customer->getActiveBillingAddress();
if ($activeBillingAddress->getCountry() === null) {
return;
}
if ($activeBillingAddress->getCountry()->getIso() === PaymentMethodInstaller::KLARNA_API_REGION_US && $activeBillingAddress->getCountryStateId() === null) {
$errors = $event->getPage()->getCart()->getErrors();
$errors->add(new MissingStateError());
$event->getPage()->getCart()->setErrors($errors);
}
}
}