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

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