custom/plugins/MolliePayments/src/Subscriber/CancelMollieOrderSubscriber.php line 61

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Kiener\MolliePayments\Service\OrderService;
  4. use Mollie\Api\Exceptions\ApiException;
  5. use Mollie\Api\MollieApiClient;
  6. use Mollie\Api\Types\OrderStatus;
  7. use Psr\Log\LoggerInterface;
  8. use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionActions;
  9. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class CancelMollieOrderSubscriber implements EventSubscriberInterface
  12. {
  13.     public const MOLLIE_CANCEL_ORDER_STATES = [
  14.         OrderStatus::STATUS_CREATED,
  15.         OrderStatus::STATUS_AUTHORIZED,
  16.         OrderStatus::STATUS_SHIPPING
  17.     ];
  18.     /**
  19.      * @var string
  20.      */
  21.     private $shopwareVersion;
  22.     /**
  23.      * @var MollieApiClient
  24.      */
  25.     private $apiClient;
  26.     /**
  27.      * @var OrderService
  28.      */
  29.     private $orderService;
  30.     /**
  31.      * @var LoggerInterface
  32.      */
  33.     private $logger;
  34.     /**
  35.      * @param MollieApiClient $apiClient
  36.      * @param OrderService $orderService
  37.      * @param LoggerInterface $loggerService
  38.      * @param string $shopwareVersion
  39.      */
  40.     public function __construct(MollieApiClient $apiClientOrderService $orderServiceLoggerInterface $loggerServicestring $shopwareVersion)
  41.     {
  42.         $this->apiClient $apiClient;
  43.         $this->orderService $orderService;
  44.         $this->shopwareVersion $shopwareVersion;
  45.         $this->logger $loggerService;
  46.     }
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             'state_machine.order.state_changed' => ['onOrderStateChanges']
  51.         ];
  52.     }
  53.     public function onOrderStateChanges(StateMachineStateChangeEvent $event): void
  54.     {
  55.         if ($event->getTransitionSide() !== StateMachineStateChangeEvent::STATE_MACHINE_TRANSITION_SIDE_ENTER) {
  56.             return;
  57.         }
  58.         $allowedStates = [StateMachineTransitionActions::ACTION_CANCEL => true];
  59.         if (version_compare($this->shopwareVersion'6.2''>=')) {
  60.             $allowedStates[StateMachineTransitionActions::ACTION_FAIL] = true;
  61.         }
  62.         $transitionName $event->getTransition()->getTransitionName();
  63.         if (!isset($allowedStates[$transitionName])) {
  64.             return;
  65.         }
  66.         $order $this->orderService->getOrder($event->getTransition()->getEntityId(), $event->getContext());
  67.         $customFields $order->getCustomFields() ?? [];
  68.         $mollieOrderId $customFields['mollie_payments']['order_id'] ?? '';
  69.         if (empty($mollieOrderId)) {
  70.             return;
  71.         }
  72.         try {
  73.             $mollieOrder $this->apiClient->orders->get($mollieOrderId);
  74.             if (in_array($mollieOrder->status, [self::MOLLIE_CANCEL_ORDER_STATES])) {
  75.                 $this->apiClient->orders->cancel($mollieOrderId);
  76.             }
  77.         } catch (ApiException $e) {
  78.             $this->logger->warning(
  79.                 $e->getMessage()
  80.             );
  81.         }
  82.     }
  83. }