custom/plugins/CkoShopware6/src/Subscriber/OrderStateMachineSubscriber.php line 58

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Cko\Shopware6\Subscriber;
  3. use Checkout\CheckoutApiException;
  4. use Cko\Shopware6\Factory\SettingsFactory;
  5. use Cko\Shopware6\Handler\PaymentHandler;
  6. use Cko\Shopware6\Service\CheckoutApi\CheckoutPaymentService;
  7. use Cko\Shopware6\Service\Order\AbstractOrderService;
  8. use Cko\Shopware6\Service\Order\AbstractOrderTransactionService;
  9. use Cko\Shopware6\Service\Order\OrderService;
  10. use Cko\Shopware6\Service\PaymentMethodService;
  11. use Cko\Shopware6\Struct\CheckoutApi\Resources\Payment;
  12. use Exception;
  13. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionCollection;
  14. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  15. use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent;
  16. use Shopware\Core\Checkout\Order\OrderEntity;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class OrderStateMachineSubscriber implements EventSubscriberInterface
  19. {
  20.     private PaymentMethodService $paymentMethodService;
  21.     private SettingsFactory $settingsFactory;
  22.     private CheckoutPaymentService $checkoutPaymentService;
  23.     private AbstractOrderService $orderService;
  24.     private AbstractOrderTransactionService $orderTransactionService;
  25.     public function __construct(
  26.         PaymentMethodService $paymentMethodService,
  27.         SettingsFactory $settingsFactory,
  28.         CheckoutPaymentService $checkoutPaymentService,
  29.         AbstractOrderService $orderService,
  30.         AbstractOrderTransactionService $orderTransitionService
  31.     ) {
  32.         $this->paymentMethodService $paymentMethodService;
  33.         $this->settingsFactory $settingsFactory;
  34.         $this->checkoutPaymentService $checkoutPaymentService;
  35.         $this->orderService $orderService;
  36.         $this->orderTransactionService $orderTransitionService;
  37.     }
  38.     public static function getSubscribedEvents(): array
  39.     {
  40.         return [
  41.             'state_enter.order_delivery.state.shipped' => 'onOrderDeliveryEnterStateShipped',
  42.         ];
  43.     }
  44.     /**
  45.      * @throws CheckoutApiException
  46.      * @throws Exception
  47.      */
  48.     public function onOrderDeliveryEnterStateShipped(OrderStateMachineStateChangeEvent $event): void
  49.     {
  50.         $order $event->getOrder();
  51.         $orderTransaction $this->getOrderTransaction($order);
  52.         if (!$orderTransaction instanceof OrderTransactionEntity) {
  53.             return;
  54.         }
  55.         $paymentHandler $this->paymentMethodService->getPaymentHandlerByOrderTransaction($orderTransaction);
  56.         if (!$paymentHandler instanceof PaymentHandler || !$paymentHandler->shouldCaptureAfterShipping()) {
  57.             return;
  58.         }
  59.         $checkoutOrderCustomFields OrderService::getCheckoutOrderCustomFields($order);
  60.         $checkoutPaymentId $checkoutOrderCustomFields->getCheckoutPaymentId();
  61.         // If empty payment detail we skip it
  62.         if (empty($checkoutPaymentId)) {
  63.             return;
  64.         }
  65.         // We get payment detail from checkout.com API
  66.         $payment $this->checkoutPaymentService->getPaymentDetails($checkoutPaymentId$order->getSalesChannelId());
  67.         if ($payment->getStatus() !== CheckoutPaymentService::STATUS_AUTHORIZED) {
  68.             return;
  69.         }
  70.         // Get plugin settings
  71.         $settings $this->settingsFactory->getSettings($order->getSalesChannelId());
  72.         // Capture the payment from Checkout.com
  73.         $actionId $paymentHandler->capturePayment($payment->getId(), $order);
  74.         $checkoutOrderCustomFields->setLastCheckoutActionId($actionId);
  75.         $this->orderService->updateCheckoutCustomFields($order$checkoutOrderCustomFields$event->getContext());
  76.         $this->orderTransactionService->processTransition($orderTransactionCheckoutPaymentService::STATUS_CAPTURED$event->getContext());
  77.         $this->orderService->processTransition($order$settingsCheckoutPaymentService::STATUS_CAPTURED$event->getContext());
  78.     }
  79.     private function getOrderTransaction(OrderEntity $order): ?OrderTransactionEntity
  80.     {
  81.         $orderTransactions $order->getTransactions();
  82.         if (!$orderTransactions instanceof OrderTransactionCollection) {
  83.             return null;
  84.         }
  85.         $orderTransactions->sort(function (OrderTransactionEntity $aOrderTransactionEntity $b): int {
  86.             return $a->getCreatedAt() <=> $b->getCreatedAt();
  87.         });
  88.         return $orderTransactions->last();
  89.     }
  90. }