<?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\Components\Button\Pay\Hydrator;
use Psr\Log\LoggerInterface;
use Shopware\Core\Framework\Context;
use Swag\AmazonPay\Components\Button\Pay\AddressRestriction\AddressRestrictionServiceInterface;
use Swag\AmazonPay\Components\Button\Pay\Struct\AmazonPayButtonPayloadStruct;
use Swag\AmazonPay\Components\Config\ConfigServiceInterface;
use Swag\AmazonPay\Components\Config\Validation\Exception\ConfigValidationException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
class AmazonPayButtonPayloadHydrator implements AmazonPayButtonPayloadHydratorInterface, AmazonPayButtonPayloadHydratorInterfaceV2
{
/**
* @var ConfigServiceInterface
*/
private $configService;
/**
* @var RouterInterface
*/
private $router;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var AddressRestrictionServiceInterface
*/
private $addressRestrictionService;
public function __construct(
ConfigServiceInterface $configService,
RouterInterface $router,
LoggerInterface $logger,
AddressRestrictionServiceInterface $addressRestrictionService
) {
$this->configService = $configService;
$this->router = $router;
$this->logger = $logger;
$this->addressRestrictionService = $addressRestrictionService;
}
public function hydrate(string $salesChannelId, bool $oneClickCheckout = true): string
{
$this->logger->warning('You are using the deprecated \Swag\AmazonPay\Components\Button\Pay\Hydrator\AmazonPayButtonPayloadHydrator::hydrate function use \Swag\AmazonPay\Components\Button\Pay\Hydrator\AmazonPayButtonPayloadHydrator::hydratePayload instead.');
$payload = $this->hydratePayload($salesChannelId, Context::createDefaultContext(), $oneClickCheckout);
return (string) \json_encode($payload, \JSON_UNESCAPED_SLASHES | \JSON_FORCE_OBJECT);
}
public function hydratePayload(string $salesChannelId, Context $context, bool $oneClickCheckout = true): ?AmazonPayButtonPayloadStruct
{
try {
$pluginConfig = $this->configService->getPluginConfig($salesChannelId);
} catch (ConfigValidationException $e) {
$this->logger->error('Could not generate Amazon login button payload', ['Exception' => $e->getMessage()]);
return null;
}
$payload = new AmazonPayButtonPayloadStruct();
$payload->setCheckoutReviewReturnUrl($this->router->generate('payment.swag_amazon_pay.review', ['oneClickCheckout' => $oneClickCheckout], UrlGeneratorInterface::ABSOLUTE_URL));
$payload->setStoreId($pluginConfig->getClientId());
$payload->setAddressRestrictions($this->addressRestrictionService->getAddressRestrictions($salesChannelId, $context));
return $payload;
}
}