custom/plugins/RpayPayments/src/Components/StateMachine/Subscriber/TransitionSubscriber.php line 72

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright (c) Ratepay GmbH
  4.  *
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  */
  8. namespace Ratepay\RpayPayments\Components\StateMachine\Subscriber;
  9. use Exception;
  10. use Monolog\Logger;
  11. use Ratepay\RpayPayments\Components\Checkout\Model\Extension\OrderExtension;
  12. use Ratepay\RpayPayments\Components\Checkout\Model\RatepayOrderDataEntity;
  13. use Ratepay\RpayPayments\Components\PluginConfig\Service\ConfigService;
  14. use Ratepay\RpayPayments\Components\RatepayApi\Dto\OrderOperationData;
  15. use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\PaymentCancelService;
  16. use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\PaymentDeliverService;
  17. use Ratepay\RpayPayments\Components\RatepayApi\Service\Request\PaymentReturnService;
  18. use Ratepay\RpayPayments\Util\CriteriaHelper;
  19. use Ratepay\RpayPayments\Util\MethodHelper;
  20. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
  21. use Shopware\Core\Checkout\Order\OrderEntity;
  22. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  24. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. class TransitionSubscriber implements EventSubscriberInterface
  27. {
  28.     private ConfigService $configService;
  29.     private PaymentDeliverService $paymentDeliverService;
  30.     private PaymentCancelService $paymentCancelService;
  31.     private PaymentReturnService $paymentReturnService;
  32.     private EntityRepositoryInterface $orderDeliveryRepository;
  33.     private EntityRepositoryInterface $orderRepository;
  34.     private Logger $logger;
  35.     public function __construct(
  36.         EntityRepositoryInterface $orderDeliveryRepository,
  37.         EntityRepositoryInterface $orderRepository,
  38.         ConfigService $configService,
  39.         PaymentDeliverService $paymentDeliverService,
  40.         PaymentCancelService $paymentCancelService,
  41.         PaymentReturnService $paymentReturnService,
  42.         Logger $logger
  43.     )
  44.     {
  45.         $this->orderDeliveryRepository $orderDeliveryRepository;
  46.         $this->orderRepository $orderRepository;
  47.         $this->configService $configService;
  48.         $this->paymentDeliverService $paymentDeliverService;
  49.         $this->paymentCancelService $paymentCancelService;
  50.         $this->paymentReturnService $paymentReturnService;
  51.         $this->logger $logger;
  52.     }
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             StateMachineTransitionEvent::class => 'onTransition',
  57.         ];
  58.     }
  59.     public function onTransition(StateMachineTransitionEvent $event): void
  60.     {
  61.         if ($event->getEntityName() !== 'order_delivery' || $this->configService->isBidirectionalityEnabled() === false) {
  62.             return;
  63.         }
  64.         /** @var OrderDeliveryEntity $orderDelivery */
  65.         $orderDelivery $this->orderDeliveryRepository->search(new Criteria([$event->getEntityId()]), $event->getContext())->first();
  66.         /** @var OrderEntity $order */
  67.         $order $this->orderRepository->search(CriteriaHelper::getCriteriaForOrder($orderDelivery->getOrderId()), $event->getContext())->first();
  68.         if (!MethodHelper::isRatepayOrder($order)) {
  69.             return;
  70.         }
  71.         /** @var RatepayOrderDataEntity $ratepayData */
  72.         $ratepayData $order->getExtension(OrderExtension::EXTENSION_NAME);
  73.         if ($ratepayData === null) {
  74.             $this->logger->warning('Error during bidirectionality: No Ratepay Data was found.', [
  75.                 'order' => $order->getId(),
  76.                 'orderNumber' => $order->getOrderNumber(),
  77.             ]);
  78.             return;
  79.         }
  80.         switch ($event->getToPlace()->getTechnicalName()) {
  81.             case $this->configService->getBidirectionalityFullDelivery():
  82.                 $operation OrderOperationData::OPERATION_DELIVER;
  83.                 $service $this->paymentDeliverService;
  84.                 break;
  85.             case $this->configService->getBidirectionalityFullCancel():
  86.                 $operation OrderOperationData::OPERATION_CANCEL;
  87.                 $service $this->paymentCancelService;
  88.                 break;
  89.             case $this->configService->getBidirectionalityFullReturn():
  90.                 $operation OrderOperationData::OPERATION_RETURN;
  91.                 $service $this->paymentReturnService;
  92.                 break;
  93.             default:
  94.                 // do nothing
  95.                 return;
  96.         }
  97.         $orderOperationData = new OrderOperationData($event->getContext(), $order$operationnullfalse);
  98.         try {
  99.             $response $service->doRequest($orderOperationData);
  100.             if ($response->getResponse()->isSuccessful() === false) {
  101.                 $this->logger->error('Error during bidirectionality. (Exception: ' $response->getResponse()->getReasonMessage() . ')', [
  102.                     'order' => $order->getId(),
  103.                     'transactionId' => $ratepayData->getTransactionId(),
  104.                     'orderNumber' => $order->getOrderNumber(),
  105.                     'itemsToProcess' => $orderOperationData->getItems(),
  106.                 ]);
  107.             }
  108.         } catch (Exception $e) {
  109.             $this->logger->critical('Exception during bidirectionality. (Exception: ' $e->getMessage() . ')', [
  110.                 'order' => $order->getId(),
  111.                 'transactionId' => $ratepayData->getTransactionId(),
  112.                 'orderNumber' => $order->getOrderNumber(),
  113.                 'itemsToProcess' => $orderOperationData->getItems(),
  114.             ]);
  115.         }
  116.     }
  117. }