<?php
declare(strict_types=1);
/*
* (c) shopware AG <info@shopware.com>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swag\AmazonPay\Storefront\EventListeners;
use Psr\Log\LoggerInterface;
use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntitySearchResultLoadedEvent;
use Swag\AmazonPay\Components\Config\ConfigServiceInterface;
use Swag\AmazonPay\Components\Config\Validation\Exception\ConfigValidationException;
use Swag\AmazonPay\Installer\PaymentMethodInstaller;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class AmazonPayAvailabilityListener implements EventSubscriberInterface
{
/**
* @var ConfigServiceInterface
*/
private $configService;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var RequestStack
*/
private $requestStack;
public function __construct(
ConfigServiceInterface $configService,
LoggerInterface $logger,
RequestStack $requestStack
) {
$this->configService = $configService;
$this->logger = $logger;
$this->requestStack = $requestStack;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
'sales_channel.payment_method.search.result.loaded' => ['onSalesChannelSearchResultLoaded', -1],
];
}
public function onSalesChannelSearchResultLoaded(SalesChannelEntitySearchResultLoadedEvent $event): void
{
try {
$config = $this->configService->getPluginConfig($event->getSalesChannelContext()->getSalesChannel()->getId());
} catch (ConfigValidationException $e) {
$this->logger->error('Invalid plugin configuration', ['field' => $e->getField()]);
$this->removeSwagAmazonPayPaymentMethod($event);
return;
}
if ($event->getSalesChannelContext()->getSalesChannel()->getPaymentMethodId() === PaymentMethodInstaller::AMAZON_PAYMENT_ID) {
return;
}
if ($config->hideOneClickCheckoutButtons() === true) {
$this->removeSwagAmazonPayPaymentMethod($event);
}
}
private function removeSwagAmazonPayPaymentMethod(SalesChannelEntitySearchResultLoadedEvent $event): void
{
$filter = static function (PaymentMethodEntity $entity): bool {
return $entity->getId() !== PaymentMethodInstaller::AMAZON_PAYMENT_ID;
};
$filteredPaymentMethods = $event->getResult()->getEntities()->filter($filter);
$event->getResult()->assign([
'total' => \count($filteredPaymentMethods),
'entities' => $filteredPaymentMethods,
'elements' => $filteredPaymentMethods->getElements(),
]);
}
}