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

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\Service\OrderService;
  5. use Kiener\MolliePayments\Service\SettingsService;
  6. use Kiener\MolliePayments\Struct\PaymentMethod\PaymentMethodAttributes;
  7. use Psr\Log\LoggerInterface;
  8. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  9. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  15. use Shopware\Core\System\StateMachine\Aggregation\StateMachineTransition\StateMachineTransitionActions;
  16. use Shopware\Core\System\StateMachine\Event\StateMachineStateChangeEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class OrderDeliverySubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var SettingsService
  22.      */
  23.     private $settings;
  24.     /**
  25.      * @var ShipmentManager
  26.      */
  27.     private $mollieShipment;
  28.     /**
  29.      * @var OrderService
  30.      */
  31.     private $orderService;
  32.     /**
  33.      * @var EntityRepository
  34.      */
  35.     private $repoOrderTransactions;
  36.     /**
  37.      * @var LoggerInterface
  38.      */
  39.     private $logger;
  40.     /**
  41.      * @param SettingsService $settings
  42.      * @param ShipmentManager $mollieShipment
  43.      * @param OrderService $orderService
  44.      * @param EntityRepository $repoOrderTransactions
  45.      * @param LoggerInterface $logger
  46.      */
  47.     public function __construct(SettingsService $settingsShipmentManager $mollieShipmentOrderService $orderServiceEntityRepository $repoOrderTransactionsLoggerInterface $logger)
  48.     {
  49.         $this->settings $settings;
  50.         $this->mollieShipment $mollieShipment;
  51.         $this->orderService $orderService;
  52.         $this->repoOrderTransactions $repoOrderTransactions;
  53.         $this->logger $logger;
  54.     }
  55.     /**
  56.      * @return array<mixed>
  57.      */
  58.     public static function getSubscribedEvents(): array
  59.     {
  60.         return [
  61.             'state_machine.order_delivery.state_changed' => 'onOrderDeliveryChanged',
  62.         ];
  63.     }
  64.     /**
  65.      * @param StateMachineStateChangeEvent $event
  66.      */
  67.     public function onOrderDeliveryChanged(StateMachineStateChangeEvent $event): void
  68.     {
  69.         if ($event->getTransitionSide() !== StateMachineStateChangeEvent::STATE_MACHINE_TRANSITION_SIDE_ENTER) {
  70.             return;
  71.         }
  72.         $transitionName $event->getTransition()->getTransitionName();
  73.         if ($transitionName !== StateMachineTransitionActions::ACTION_SHIP) {
  74.             return;
  75.         }
  76.         # get the configuration of the sales channel from the order
  77.         $configSalesChannel $this->settings->getSettings($event->getSalesChannelId());
  78.         # if we don't even configure automatic shipping
  79.         # then don't even look into our order to find out if we should actually starts
  80.         if (!$configSalesChannel->getAutomaticShipping()) {
  81.             return;
  82.         }
  83.         $orderDeliveryId $event->getTransition()->getEntityId();
  84.         try {
  85.             $order $this->orderService->getOrderByDeliveryId($orderDeliveryId$event->getContext());
  86.             $swTransaction $this->getLatestOrderTransaction($order->getId(), $event->getContext());
  87.             if (!$swTransaction) {
  88.                 throw new \Exception('Order '.$order->getOrderNumber().' does not have transactions');
  89.             }
  90.             # verify if the customer really paid with Mollie in the end
  91.             $paymentMethod $swTransaction->getPaymentMethod();
  92.             if (!$paymentMethod instanceof PaymentMethodEntity) {
  93.                 throw new \Exception('Transaction ' $swTransaction->getId() . ' has no payment method!');
  94.             }
  95.             $paymentMethodAttributes = new PaymentMethodAttributes($paymentMethod);
  96.             if (!$paymentMethodAttributes->isMolliePayment()) {
  97.                 # just skip it if it has been paid
  98.                 # with another payment provider
  99.                 # do NOT throw an error
  100.                 return;
  101.             }
  102.             $this->logger->info('Starting Shipment through Order Delivery Transition for order: ' $order->getOrderNumber());
  103.             $this->mollieShipment->shipOrderRest($ordernull$event->getContext());
  104.         } catch (\Throwable $ex) {
  105.             $this->logger->error('Failed to transfer delivery state to mollie: '.$ex->getMessage(), ['exception' => $ex]);
  106.             return;
  107.         }
  108.     }
  109.     private function getLatestOrderTransaction(string $orderIdContext $context):?OrderTransactionEntity
  110.     {
  111.         $criteria = new Criteria();
  112.         $criteria->addFilter(new EqualsFilter('order.id'$orderId));
  113.         $criteria->addAssociation('order');
  114.         $criteria->addAssociation('stateMachineState');
  115.         $criteria->addAssociation('paymentMethod');
  116.         $criteria->addSorting(new FieldSorting('createdAt'FieldSorting::DESCENDING));
  117.         $result $this->repoOrderTransactions->search($criteria$context);
  118.         return $result->first();
  119.     }
  120. }