custom/plugins/AdyenPaymentShopware6/src/Subscriber/PostPaymentSubscriber.php line 100

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  *                       ######
  4.  *                       ######
  5.  * ############    ####( ######  #####. ######  ############   ############
  6.  * #############  #####( ######  #####. ######  #############  #############
  7.  *        ######  #####( ######  #####. ######  #####  ######  #####  ######
  8.  * ###### ######  #####( ######  #####. ######  #####  #####   #####  ######
  9.  * ###### ######  #####( ######  #####. ######  #####          #####  ######
  10.  * #############  #############  #############  #############  #####  ######
  11.  *  ############   ############  #############   ############  #####  ######
  12.  *                                      ######
  13.  *                               #############
  14.  *                               ############
  15.  *
  16.  * Adyen Payment Module
  17.  *
  18.  * Copyright (c) 2022 Adyen N.V.
  19.  * This file is open source and available under the MIT license.
  20.  * See the LICENSE file for more info.
  21.  *
  22.  * Author: Adyen <shopware@adyen.com>
  23.  */
  24. namespace Adyen\Shopware\Subscriber;
  25. use Adyen\Shopware\Service\ConfigurationService;
  26. use Adyen\Shopware\Service\Repository\SalesChannelRepository;
  27. use Adyen\Util\Currency;
  28. use Psr\Log\LoggerInterface;
  29. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionStates;
  30. use Shopware\Core\Framework\Struct\ArrayEntity;
  31. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  32. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  33. use Symfony\Component\Routing\RouterInterface;
  34. class PostPaymentSubscriber extends StorefrontSubscriber implements EventSubscriberInterface
  35. {
  36.     const ACTION_TYPE_VOUCHER = 'voucher';
  37.     /**
  38.      * @var ConfigurationService
  39.      */
  40.     private $configurationService;
  41.     /**
  42.      * @var SalesChannelRepository
  43.      */
  44.     private $salesChannelRepository;
  45.     /**
  46.      * @var Currency
  47.      */
  48.     private $currency;
  49.     /**
  50.      * @var RouterInterface
  51.      */
  52.     private $router;
  53.     /**
  54.      * @var LoggerInterface
  55.      */
  56.     private $logger;
  57.     /**
  58.      * @param SalesChannelRepository $salesChannelRepository
  59.      * @param ConfigurationService $configurationService
  60.      * @param Currency $currency
  61.      * @param RouterInterface $router
  62.      * @param LoggerInterface $logger
  63.      */
  64.     public function __construct(
  65.         SalesChannelRepository $salesChannelRepository,
  66.         ConfigurationService $configurationService,
  67.         Currency $currency,
  68.         RouterInterface $router,
  69.         LoggerInterface $logger
  70.     ) {
  71.         $this->configurationService = $configurationService;
  72.         $this->salesChannelRepository = $salesChannelRepository;
  73.         $this->currency = $currency;
  74.         $this->router = $router;
  75.         $this->logger = $logger;
  76.     }
  77.     /**
  78.      * @return string[]
  79.      */
  80.     public static function getSubscribedEvents() : array
  81.     {
  82.         return [
  83.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishPageLoaded'
  84.         ];
  85.     }
  86.     /**
  87.      * @param CheckoutFinishPageLoadedEvent $event
  88.      */
  89.     public function onCheckoutFinishPageLoaded(CheckoutFinishPageLoadedEvent $event)
  90.     {
  91.         $page = $event->getPage();
  92.         $salesChannelContext = $event->getSalesChannelContext();
  93.         $salesChannelId = $salesChannelContext->getSalesChannel()->getId();
  94.         $order = $page->getOrder();
  95.         $frontendData = [
  96.             'clientKey' => $this->configurationService->getClientKey($salesChannelId),
  97.             'locale' => $this->salesChannelRepository
  98.                 ->getSalesChannelAssoc($salesChannelContext, ['language.locale'])
  99.                 ->getLanguage()->getLocale()->getCode(),
  100.             'environment' => $this->configurationService->getEnvironment($salesChannelId),
  101.             'orderId' => $order->getId(),
  102.         ];
  103.         if ($this->configurationService->isAdyenGivingEnabled($salesChannelId)) {
  104.             $frontendData = $this->buildAdyenGivingData($frontendData, $order, $salesChannelContext);
  105.         }
  106.         $frontendData = $this->buildVoucherActionData($frontendData, $order);
  107.         $page->addExtension(
  108.             self::ADYEN_DATA_EXTENSION_ID,
  109.             new ArrayEntity($frontendData)
  110.         );
  111.     }
  112.     private function buildAdyenGivingData($frontendData, $order, $salesChannelContext)
  113.     {
  114.         $orderTransaction = $order->getTransactions()
  115.             ->filterByState(OrderTransactionStates::STATE_AUTHORIZED)->first();
  116.         if (is_null($orderTransaction)) {
  117.             return $frontendData;
  118.         }
  119.         $customFields = $orderTransaction->getCustomFields();
  120.         if (!isset($customFields['donationToken'])) {
  121.             return $frontendData;
  122.         }
  123.         $salesChannelId = $salesChannelContext->getSalesChannel()->getId();
  124.         $backgroundImageUrl = $this->configurationService->getAdyenGivingBackgroundUrl(
  125.             $salesChannelId,
  126.             $salesChannelContext->getContext()
  127.         );
  128.         $charityLogoUrl = $this->configurationService->getAdyenGivingCharityLogo(
  129.             $salesChannelId,
  130.             $salesChannelContext->getContext()
  131.         );
  132.         $currency = $salesChannelContext->getCurrency()->getIsoCode();
  133.         $amounts = $this->configurationService->getAdyenGivingDonationAmounts($salesChannelId);
  134.         $donationAmounts = [];
  135.         try {
  136.             foreach (explode(',', $amounts) as $donationAmount) {
  137.                 $donationAmounts[] = $this->currency->sanitize($donationAmount, $currency);
  138.             }
  139.             $donationAmounts = implode(',', $donationAmounts);
  140.         } catch (\Exception $e) {
  141.             $this->logger->error("Field 'donationAmounts' is not valid.");
  142.             return $frontendData;
  143.         }
  144.         $adyenGivingData = [
  145.             'givingEnabled' => true,
  146.             'currency' => $currency,
  147.             'values' => $donationAmounts,
  148.             'backgroundUrl' => $backgroundImageUrl,
  149.             'logoUrl' => $charityLogoUrl,
  150.             'description' => $this->configurationService->getAdyenGivingCharityDescription($salesChannelId),
  151.             'name' => $this->configurationService->getAdyenGivingCharityName($salesChannelId),
  152.             'charityUrl' => $this->configurationService->getAdyenGivingCharityWebsite($salesChannelId),
  153.             'donationEndpointUrl' => $this->router->generate(
  154.                 'payment.adyen.proxy-donate'
  155.             ),
  156.             'continueActionUrl' => $this->router->generate(
  157.                 'frontend.home.page'
  158.             )
  159.         ];
  160.         return array_merge($frontendData, $adyenGivingData);
  161.     }
  162.     private function buildVoucherActionData($frontendData, $order)
  163.     {
  164.         $orderTransaction = $order->getTransactions()
  165.             ->filterByState(OrderTransactionStates::STATE_IN_PROGRESS)->first();
  166.         if (is_null($orderTransaction)) {
  167.             return $frontendData;
  168.         }
  169.         $customFields = $orderTransaction->getCustomFields();
  170.         if (isset($customFields['action']) && $customFields['action']['type'] == self::ACTION_TYPE_VOUCHER) {
  171.             $voucherData = [
  172.                 'action' => json_encode($customFields['action'])
  173.             ];
  174.             return array_merge($frontendData, $voucherData);
  175.         }
  176.         return $frontendData;
  177.     }
  178. }