custom/plugins/MolliePayments/src/Subscriber/OrderStateSubscriber.php line 52

Open in your IDE?
  1. <?php
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Kiener\MolliePayments\Service\CustomFieldService;
  4. use Kiener\MolliePayments\Service\OrderService;
  5. use Kiener\MolliePayments\Service\PaymentMethodService;
  6. use Mollie\Api\MollieApiClient;
  7. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionCollection;
  8. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  9. use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  10. use Shopware\Core\Checkout\Order\OrderEntity;
  11. use Shopware\Core\Checkout\Order\OrderStates;
  12. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  13. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class OrderStateSubscriber implements EventSubscriberInterface
  16. {
  17.     public static function getSubscribedEvents()
  18.     {
  19.         return [
  20.             'state_machine.order.state_changed' => ['onKlarnaOrderCancelledAsAdmin']
  21.         ];
  22.     }
  23.     /** @var MollieApiClient $apiClient */
  24.     private $apiClient;
  25.     /** @var OrderService */
  26.     private $orderService;
  27.     /** @var PaymentMethodService */
  28.     private $paymentMethodService;
  29.     public function __construct(
  30.         MollieApiClient      $apiClient,
  31.         OrderService         $orderService,
  32.         PaymentMethodService $paymentMethodService
  33.     ) {
  34.         $this->apiClient $apiClient;
  35.         $this->orderService $orderService;
  36.         $this->paymentMethodService $paymentMethodService;
  37.     }
  38.     /**
  39.      * @param StateMachineStateChangeEvent $event
  40.      * @throws \Mollie\Api\Exceptions\ApiException
  41.      */
  42.     public function onKlarnaOrderCancelledAsAdmin(StateMachineStateChangeEvent $event): void
  43.     {
  44.         if (!($event->getContext()->getSource() instanceof AdminApiSource)) {
  45.             return;
  46.         }
  47.         // Build order state change to cancelled event name
  48.         $orderStateCancelled implode('.', [
  49.             StateMachineStateChangeEvent::STATE_MACHINE_TRANSITION_SIDE_ENTER,
  50.             OrderStates::STATE_MACHINE,
  51.             OrderStates::STATE_CANCELLED
  52.         ]);
  53.         if ($event->getStateEventName() !== $orderStateCancelled) {
  54.             return;
  55.         }
  56.         $order $this->orderService->getOrder($event->getTransition()->getEntityId(), $event->getContext());
  57.         if (!$order instanceof OrderEntity) {
  58.             return;
  59.         }
  60.         if (!$order->getTransactions() instanceof OrderTransactionCollection) {
  61.             return;
  62.         }
  63.         // use filterByState(OrderTransactionStates::STATE_OPEN)?
  64.         $lastTransaction $order->getTransactions()->last();
  65.         if (!$lastTransaction instanceof OrderTransactionEntity) {
  66.             return;
  67.         }
  68.         $paymentMethod $lastTransaction->getPaymentMethod();
  69.         if (is_null($paymentMethod) && $lastTransaction->getPaymentMethodId() !== '') {
  70.             $paymentMethod $this->paymentMethodService->getPaymentMethodById($lastTransaction->getPaymentMethodId());
  71.         }
  72.         $molliePaymentMethod null;
  73.         if (!is_null($paymentMethod) && !is_null($paymentMethod->getCustomFields())
  74.             && array_key_exists('mollie_payment_method_name'$paymentMethod->getCustomFields())) {
  75.             $molliePaymentMethod $paymentMethod->getCustomFields()['mollie_payment_method_name'];
  76.         }
  77.         if (is_null($molliePaymentMethod) ||
  78.             !in_array($molliePaymentMethod, ['klarnapaylater''klarnasliceit'])) {
  79.             return;
  80.         }
  81.         $customFields $order->getCustomFields();
  82.         $mollieOrderId null;
  83.         if (!is_null($customFields) &&
  84.             array_key_exists(CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS$customFields) &&
  85.             array_key_exists('order_id'$customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS])) {
  86.             $mollieOrderId $customFields[CustomFieldService::CUSTOM_FIELDS_KEY_MOLLIE_PAYMENTS]['order_id'];
  87.         }
  88.         if (is_null($mollieOrderId)) {
  89.             return;
  90.         }
  91.         $mollieOrder $this->apiClient->orders->get($mollieOrderId);
  92.         if (in_array($mollieOrder->status, ['created''authorized''shipping'])) {
  93.             $this->apiClient->orders->cancel($mollieOrderId);
  94.         }
  95.     }
  96. }