custom/plugins/PevrPayeverIntegration/src/EventListener/CheckoutConfirmEventListener.php line 86

Open in your IDE?
  1. <?php
  2. /**
  3.  * payever GmbH
  4.  *
  5.  * NOTICE OF LICENSE
  6.  *
  7.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8.  * of this software and associated documentation files (the "Software"), to deal
  9.  * in the Software without restriction, including without limitation the rights
  10.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11.  * copies of the Software, and to permit persons to whom the Software is
  12.  * furnished to do so, subject to the following conditions:
  13.  *
  14.  * The above copyright notice and this permission notice shall be included in all
  15.  * copies or substantial portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23.  * SOFTWARE.
  24.  *
  25.  * DISCLAIMER
  26.  *
  27.  * Do not edit or add to this file if you wish to upgrade payever Shopware package
  28.  * to newer versions in the future.
  29.  *
  30.  * @category    Payever
  31.  * @author      payever GmbH <service@payever.de>
  32.  * @copyright   Copyright (c) 2021 payever GmbH (http://www.payever.de)
  33.  * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  34.  */
  35. declare(strict_types=1);
  36. namespace Payever\PayeverPayments\EventListener;
  37. use Payever\PayeverPayments\PevrPayeverIntegration;
  38. use Payever\PayeverPayments\Service\Payment\HiddenMethodService;
  39. use Payever\PayeverPayments\Service\Setting\SettingsServiceInterface;
  40. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  41. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  42. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  43. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  44. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  45. class CheckoutConfirmEventListener implements EventSubscriberInterface
  46. {
  47.     /** @var HiddenMethodService */
  48.     private $hiddenMethodService;
  49.     /** @var SettingsServiceInterface */
  50.     private $settingsService;
  51.     /**
  52.      * CheckoutConfirmEventListener constructor.
  53.      * @param HiddenMethodService $hiddenMethodService
  54.      * @param SettingsServiceInterface $settingsService
  55.      */
  56.     public function __construct(HiddenMethodService $hiddenMethodServiceSettingsServiceInterface $settingsService)
  57.     {
  58.         $this->hiddenMethodService $hiddenMethodService;
  59.         $this->settingsService $settingsService;
  60.     }
  61.     /**
  62.      * @inheritDoc
  63.      */
  64.     public static function getSubscribedEvents(): array
  65.     {
  66.         return [
  67.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirm',
  68.             AccountEditOrderPageLoadedEvent::class => 'onAccountEditOrder',
  69.         ];
  70.     }
  71.     /**
  72.      * @param AccountEditOrderPageLoadedEvent $event
  73.      *
  74.      * @throws \Payever\PayeverPayments\Service\Setting\Exception\PayeverSettingsInvalidException
  75.      * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  76.      */
  77.     public function onAccountEditOrder(AccountEditOrderPageLoadedEvent $event): void
  78.     {
  79.         $hiddenMethodsRow $this->hiddenMethodService->getCurrentHiddenMethods($event->getPage()->getOrder()->getId());
  80.         $hiddenMethods $hiddenMethodsRow json_decode($hiddenMethodsRow->getHiddenMethods(), true) : [];
  81.         $hiddenMethodsFromSession $this->hiddenMethodService->getCurrentHiddenMethodsFromSession();
  82.         foreach (array_keys($hiddenMethodsFromSession) as $methodCode) {
  83.             $hiddenMethods[$methodCode] = time();
  84.         }
  85.         $this->hiddenMethodService->setHiddenMethodsToSession($hiddenMethods);
  86.         $paymentMethodCollection $event->getPage()->getPaymentMethods();
  87.         $salesChannelContext $event->getSalesChannelContext();
  88.         $this->processPaymentMethods($paymentMethodCollection$salesChannelContext$hiddenMethods);
  89.     }
  90.     /**
  91.      * @param CheckoutConfirmPageLoadedEvent $event
  92.      *
  93.      * @throws \Payever\PayeverPayments\Service\Setting\Exception\PayeverSettingsInvalidException
  94.      * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  95.      */
  96.     public function onCheckoutConfirm(CheckoutConfirmPageLoadedEvent $event): void
  97.     {
  98.         $paymentMethodCollection $event->getPage()->getPaymentMethods();
  99.         $salesChannelContext $event->getSalesChannelContext();
  100.         $hiddenMethods $this->hiddenMethodService->getCurrentHiddenMethodsFromSession();
  101.         $this->processPaymentMethods($paymentMethodCollection$salesChannelContext$hiddenMethods);
  102.     }
  103.     /**
  104.      * @param PaymentMethodCollection $paymentMethodCollection
  105.      * @param SalesChannelContext $salesChannelContext
  106.      * @param array $hiddenList
  107.      */
  108.     private function processPaymentMethods(
  109.         PaymentMethodCollection $paymentMethodCollection,
  110.         SalesChannelContext $salesChannelContext,
  111.         array $hiddenList
  112.     ): void {
  113.         foreach ($paymentMethodCollection->getElements() as $paymentMethod) {
  114.             if ($this->hiddenMethodService->isMethodHidden($paymentMethod$hiddenList)) {
  115.                 $paymentMethodCollection->remove($paymentMethod->getId());
  116.             }
  117.         }
  118.         $activeMethods $this->settingsService->getSettings($salesChannelContext->getSalesChannel()->getId())
  119.             ->getActivePayeverMethods();
  120.         foreach ($paymentMethodCollection->getElements() as $paymentMethod) {
  121.             $methodCode $paymentMethod->getCustomFields()[PevrPayeverIntegration::CUSTOM_FIELD_METHOD_CODE] ?? null;
  122.             if ($methodCode && $activeMethods && !in_array($methodCode$activeMethods)) {
  123.                 $paymentMethodCollection->remove($paymentMethod->getId());
  124.             }
  125.         }
  126.         foreach ($paymentMethodCollection->getElements() as $paymentMethod) {
  127.             $customFields $paymentMethod->getCustomFields() ?? [];
  128.             if (
  129.                 isset($customFields[PevrPayeverIntegration::CUSTOM_FIELD_ACCEPT_FEE])
  130.                 && !$customFields[PevrPayeverIntegration::CUSTOM_FIELD_ACCEPT_FEE]
  131.             ) {
  132.                 $currency $salesChannelContext->getCurrency()->getSymbol();
  133.                 $customName $this->getCustomName($customFields$currency);
  134.                 $paymentMethod->setName($paymentMethod->getName() . $customName);
  135.                 $translated $paymentMethod->getTranslated();
  136.                 $translated['name'] = $translated['name'] . $customName;
  137.                 $paymentMethod->setTranslated($translated);
  138.             }
  139.         }
  140.     }
  141.     /**
  142.      * @param array $customFields
  143.      * @param string $currency
  144.      * @return string
  145.      */
  146.     private function getCustomName(array $customFieldsstring $currency): string
  147.     {
  148.         $customName '';
  149.         $fixedFee $customFields[PevrPayeverIntegration::CUSTOM_FIELD_FIXED_FEE];
  150.         $variableFee $customFields[PevrPayeverIntegration::CUSTOM_FIELD_VARIABLE_FEE];
  151.         if ($fixedFee && $variableFee 0) {
  152.             $customName sprintf(
  153.                 ' (%s%s + %s%%)',
  154.                 $fixedFee,
  155.                 $currency,
  156.                 $variableFee
  157.             );
  158.         } elseif ($fixedFee == && $variableFee 0) {
  159.             $customName sprintf(
  160.                 ' (+ %s%%)',
  161.                 $variableFee
  162.             );
  163.         } elseif ($fixedFee && == $variableFee) {
  164.             $customName sprintf(
  165.                 ' (+ %s%s)',
  166.                 $fixedFee,
  167.                 $currency
  168.             );
  169.         }
  170.         return $customName;
  171.     }
  172. }