vendor/store.shopware.com/adyenpaymentshopware6/src/Subscriber/AdyenGivingSubscriber.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 AdyenGivingSubscriber implements EventSubscriberInterface
  35. {
  36.     const ADYEN_DATA_EXTENSION_ID 'adyenFrontendData';
  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.         $context $salesChannelContext->getContext();
  95.         $backgroundImageUrl $this->configurationService->getAdyenGivingBackgroundUrl($salesChannelId$context);
  96.         $charityLogoUrl $this->configurationService->getAdyenGivingCharityLogo($salesChannelId$context);
  97.         $currency $salesChannelContext->getCurrency()->getIsoCode();
  98.         $amounts $this->configurationService->getAdyenGivingDonationAmounts($salesChannelId);
  99.         $order $page->getOrder();
  100.         $orderTransaction $order->getTransactions()
  101.             ->filterByState(OrderTransactionStates::STATE_AUTHORIZED)->first();
  102.         if (is_null($orderTransaction)) {
  103.             return;
  104.         }
  105.         $customFields $orderTransaction->getCustomFields();
  106.         if (isset($customFields['donationToken']) &&
  107.             $this->configurationService->isAdyenGivingEnabled($salesChannelId)) {
  108.             $donationAmounts = [];
  109.             try {
  110.                 foreach (explode(','$amounts) as $donationAmount) {
  111.                     $donationAmounts[] = $this->currency->sanitize($donationAmount$currency);
  112.                 }
  113.                 $donationAmounts implode(','$donationAmounts);
  114.             } catch (\Exception $e) {
  115.                 $this->logger->error("Field 'donationAmounts' is not valid.");
  116.                 return;
  117.             }
  118.             $page->addExtension(
  119.                 self::ADYEN_DATA_EXTENSION_ID,
  120.                 new ArrayEntity(
  121.                     [
  122.                         'clientKey' => $this->configurationService->getClientKey($salesChannelId),
  123.                         'locale' => $this->salesChannelRepository->getSalesChannelAssocLocale($salesChannelContext)
  124.                             ->getLanguage()->getLocale()->getCode(),
  125.                         'environment' => $this->configurationService->getEnvironment($salesChannelId),
  126.                         'currency' => $currency,
  127.                         'values' => $donationAmounts,
  128.                         'backgroundUrl' => $backgroundImageUrl,
  129.                         'logoUrl' => $charityLogoUrl,
  130.                         'description' => $this->configurationService->getAdyenGivingCharityDescription($salesChannelId),
  131.                         'name' => $this->configurationService->getAdyenGivingCharityName($salesChannelId),
  132.                         'charityUrl' => $this->configurationService->getAdyenGivingCharityWebsite($salesChannelId),
  133.                         'orderId' => $order->getId(),
  134.                         'donationEndpointUrl' => $this->router->generate(
  135.                             'store-api.action.adyen.donate'
  136.                         ),
  137.                         'continueActionUrl' => $this->router->generate(
  138.                             'frontend.home.page'
  139.                         )
  140.                     ]
  141.                 )
  142.             );
  143.         }
  144.     }
  145. }