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

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