custom/plugins/PevrPayeverIntegration/src/EventListener/OrderStateChangeEventListener.php line 93

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\Management\OrderTotalsManager;
  39. use Payever\PayeverPayments\Service\Payment\PaymentActionService;
  40. use Shopware\Core\Checkout\Order\OrderEntity;
  41. use Shopware\Core\Checkout\Order\OrderStates;
  42. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  43. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  44. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  45. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  46. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  47. class OrderStateChangeEventListener implements EventSubscriberInterface
  48. {
  49.     /** @var PaymentActionService */
  50.     private $payeverTriggersHandler;
  51.     /** @var EntityRepository */
  52.     private $orderRepository;
  53.     /** @var OrderTotalsManager */
  54.     private $totalsManager;
  55.     /**
  56.      * @param PaymentActionService $payeverTriggersHandler
  57.      * @param EntityRepository $orderRepository
  58.      * @param OrderTotalsManager $totalsManager
  59.      */
  60.     public function __construct(
  61.         PaymentActionService $payeverTriggersHandler,
  62.         EntityRepository $orderRepository,
  63.         OrderTotalsManager $totalsManager
  64.     ) {
  65.         $this->payeverTriggersHandler $payeverTriggersHandler;
  66.         $this->orderRepository $orderRepository;
  67.         $this->totalsManager $totalsManager;
  68.     }
  69.     /**
  70.      * {@inheritDoc}
  71.      */
  72.     public static function getSubscribedEvents(): array
  73.     {
  74.         return [
  75.             'state_machine.order.state_changed' => 'onOrderStateChanged',
  76.         ];
  77.     }
  78.     /**
  79.      * @param StateMachineStateChangeEvent $event
  80.      * @throws \Exception
  81.      * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  82.      */
  83.     public function onOrderStateChanged(StateMachineStateChangeEvent $event): void
  84.     {
  85.         if ($event->getTransitionSide() !== StateMachineStateChangeEvent::STATE_MACHINE_TRANSITION_SIDE_ENTER) {
  86.             return;
  87.         }
  88.         // Prevent action when another action was performed from order detail window
  89.         if ($this->payeverTriggersHandler->orderEventLock) {
  90.             return;
  91.         }
  92.         $order $this->getOrder($event);
  93.         if (!$this->isPayeverPaymentMethod($order)) {
  94.             return;
  95.         }
  96.         $orderTransaction $order->getTransactions()->first();
  97.         $orderTransaction->setOrder($order);
  98.         switch ($event->getStateName()) {
  99.             case OrderStates::STATE_CANCELLED:
  100.                 if ($this->totalsManager->getAvailableForCancelling($order) > 0.01) {
  101.                     try {
  102.                         $this->payeverTriggersHandler->cancelTransaction($orderTransaction);
  103.                     } catch (\Exception $exception) {
  104.                         // Silence is golden
  105.                     }
  106.                 }
  107.                 if ($this->totalsManager->getAvailableForRefunding($order) > 0.01) {
  108.                     try {
  109.                         $this->payeverTriggersHandler->refundTransaction(
  110.                             $orderTransaction,
  111.                             $this->totalsManager->getAvailableForRefunding($order)
  112.                         );
  113.                     } catch (\Exception $exception) {
  114.                         // Silence is golden
  115.                     }
  116.                 }
  117.                 break;
  118.             case OrderStates::STATE_COMPLETED:
  119.                 if ($this->totalsManager->getAvailableForCapturing($order) > 0.01) {
  120.                     try {
  121.                         $this->payeverTriggersHandler->shippingTransaction(
  122.                             $orderTransaction,
  123.                             $this->totalsManager->getAvailableForCapturing($order)
  124.                         );
  125.                     } catch (\Exception $exception) {
  126.                         // Silence is golden
  127.                     }
  128.                 }
  129.                 break;
  130.         }
  131.     }
  132.     /**
  133.      * Check if this event is triggered using a payever Payment Method
  134.      *
  135.      * @param OrderEntity $order
  136.      * @return bool
  137.      */
  138.     private function isPayeverPaymentMethod(OrderEntity $order): bool
  139.     {
  140.         $transaction $order->getTransactions()->first();
  141.         if (!$transaction || !$transaction->getPaymentMethod() || !$transaction->getPaymentMethod()->getPlugin()) {
  142.             return false;
  143.         }
  144.         $plugin $transaction->getPaymentMethod()->getPlugin();
  145.         return $plugin->getBaseClass() === PevrPayeverIntegration::class;
  146.     }
  147.     /**
  148.      * Get the data we need from the order
  149.      *
  150.      * @param string $orderId
  151.      * @return Criteria
  152.      * @throws InconsistentCriteriaIdsException
  153.      */
  154.     private function getOrderCriteria(string $orderId): Criteria
  155.     {
  156.         $orderCriteria = new Criteria([$orderId]);
  157.         $orderCriteria->addAssociation('orderCustomer.salutation');
  158.         $orderCriteria->addAssociation('stateMachineState');
  159.         $orderCriteria->addAssociation('transactions');
  160.         $orderCriteria->addAssociation('transactions.paymentMethod');
  161.         $orderCriteria->addAssociation('transactions.paymentMethod.plugin');
  162.         $orderCriteria->addAssociation('salesChannel');
  163.         return $orderCriteria;
  164.     }
  165.     /**
  166.      * @param StateMachineStateChangeEvent $event
  167.      * @return OrderEntity
  168.      * @throws InconsistentCriteriaIdsException
  169.      */
  170.     private function getOrder(StateMachineStateChangeEvent $event): OrderEntity
  171.     {
  172.         $orderCriteria $this->getOrderCriteria($event->getTransition()->getEntityId());
  173.         /** @var OrderEntity $order */
  174.         $order $this->orderRepository->search($orderCriteria$event->getContext())->first();
  175.         return $order;
  176.     }
  177. }