<?php
declare(strict_types=1);
/*
* Copyright (c) Ratepay GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ratepay\RpayPayments\Components\CheckoutRedirectFix\Subscriber;
use Ratepay\RpayPayments\Components\Checkout\Service\ExtensionService;
use Ratepay\RpayPayments\Components\CheckoutRedirectFix\Helper\AddressHelper;
use Ratepay\RpayPayments\Util\MethodHelper;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CheckoutSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CheckoutConfirmPageLoadedEvent::class => ['addRatepayTemplateData', 310],
];
}
public function addRatepayTemplateData(CheckoutConfirmPageLoadedEvent $event): void
{
$paymentMethod = $event->getSalesChannelContext()->getPaymentMethod();
if (MethodHelper::isRatepayMethod($paymentMethod->getHandlerIdentifier()) &&
$event->getPage()->getPaymentMethods()->has($paymentMethod->getId())
) {
$customer = $event->getSalesChannelContext()->getCustomer();
if ($customer instanceof CustomerEntity) {
$extension = $event->getPage()->getExtension(ExtensionService::PAYMENT_PAGE_EXTENSION_NAME) ?? new ArrayStruct();
$extension->assign([
'validation' => [
'billing_address_md5' => AddressHelper::createMd5Hash($customer->getActiveBillingAddress()),
'shipping_address_md5' => AddressHelper::createMd5Hash($customer->getActiveShippingAddress()),
],
]);
$event->getPage()->addExtension(ExtensionService::PAYMENT_PAGE_EXTENSION_NAME, $extension);
}
}
}
}