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

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