custom/plugins/MolliePayments/src/Subscriber/OrderDeliverySubscriber.php line 73

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Kiener\MolliePayments\Subscriber;
  3. use Kiener\MolliePayments\Components\ShipmentManager\ShipmentManager;
  4. use Kiener\MolliePayments\Repository\OrderTransaction\OrderTransactionRepositoryInterface;
  5. use Kiener\MolliePayments\Service\OrderService;
  6. use Kiener\MolliePayments\Service\SettingsService;
  7. use Kiener\MolliePayments\Struct\PaymentMethod\PaymentMethodAttributes;
  8. use Psr\Log\LoggerInterface;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  10. use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionActions;
  11. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class OrderDeliverySubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var SettingsService
  17.      */
  18.     private $settings;
  19.     /**
  20.      * @var ShipmentManager
  21.      */
  22.     private $mollieShipment;
  23.     /**
  24.      * @var OrderService
  25.      */
  26.     private $orderService;
  27.     /**
  28.      * @var OrderTransactionRepositoryInterface
  29.      */
  30.     private $repoOrderTransactions;
  31.     /**
  32.      * @var LoggerInterface
  33.      */
  34.     private $logger;
  35.     /**
  36.      * @param SettingsService $settings
  37.      * @param ShipmentManager $mollieShipment
  38.      * @param OrderService $orderService
  39.      * @param OrderTransactionRepositoryInterface $repoOrderTransactions
  40.      * @param LoggerInterface $logger
  41.      */
  42.     public function __construct(SettingsService $settingsShipmentManager $mollieShipmentOrderService $orderServiceOrderTransactionRepositoryInterface $repoOrderTransactionsLoggerInterface $logger)
  43.     {
  44.         $this->settings $settings;
  45.         $this->mollieShipment $mollieShipment;
  46.         $this->orderService $orderService;
  47.         $this->repoOrderTransactions $repoOrderTransactions;
  48.         $this->logger $logger;
  49.     }
  50.     /**
  51.      * @return array<mixed>
  52.      */
  53.     public static function getSubscribedEvents(): array
  54.     {
  55.         return [
  56.             'state_machine.order_delivery.state_changed' => 'onOrderDeliveryChanged',
  57.         ];
  58.     }
  59.     /**
  60.      * @param StateMachineStateChangeEvent $event
  61.      */
  62.     public function onOrderDeliveryChanged(StateMachineStateChangeEvent $event): void
  63.     {
  64.         if ($event->getTransitionSide() !== StateMachineStateChangeEvent::STATE_MACHINE_TRANSITION_SIDE_ENTER) {
  65.             return;
  66.         }
  67.         $transitionName $event->getTransition()->getTransitionName();
  68.         if ($transitionName !== StateMachineTransitionActions::ACTION_SHIP) {
  69.             return;
  70.         }
  71.         # get the configuration of the sales channel from the order
  72.         $configSalesChannel $this->settings->getSettings($event->getSalesChannelId());
  73.         # if we don't even configure automatic shipping
  74.         # then don't even look into our order to find out if we should actually starts
  75.         if (!$configSalesChannel->getAutomaticShipping()) {
  76.             return;
  77.         }
  78.         $orderDeliveryId $event->getTransition()->getEntityId();
  79.         try {
  80.             $order $this->orderService->getOrderByDeliveryId($orderDeliveryId$event->getContext());
  81.             $swTransaction $this->repoOrderTransactions->getLatestOrderTransaction($order->getId(), $event->getContext());
  82.             # verify if the customer really paid with Mollie in the end
  83.             $paymentMethod $swTransaction->getPaymentMethod();
  84.             if (!$paymentMethod instanceof PaymentMethodEntity) {
  85.                 throw new \Exception('Transaction ' $swTransaction->getId() . ' has no payment method!');
  86.             }
  87.             $paymentMethodAttributes = new PaymentMethodAttributes($paymentMethod);
  88.             if (!$paymentMethodAttributes->isMolliePayment()) {
  89.                 # just skip it if it has been paid
  90.                 # with another payment provider
  91.                 # do NOT throw an error
  92.                 return;
  93.             }
  94.             $this->logger->info('Starting Shipment through Order Delivery Transition for order: ' $order->getOrderNumber());
  95.             $this->mollieShipment->shipOrderRest($ordernull$event->getContext());
  96.         } catch (\Throwable $ex) {
  97.             $this->logger->error('Failed to transfer delivery state to mollie: '.$ex->getMessage(), ['exception' => $ex]);
  98.             return;
  99.         }
  100.     }
  101. }