custom/plugins/PayrexxPaymentGatewaySW6/src/Subscriber/BackendSubscriber.php line 40

  1. <?php declare(strict_types=1);
  2. namespace PayrexxPaymentGateway\Subscriber;
  3. use PayrexxPaymentGateway\Service\PayrexxApiService;
  4. use Shopware\Core\System\StateMachine\Aggregation\StateMachineState\StateMachineStateEntity;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryEntity;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Checkout\Order\OrderDefinition;
  11. use Shopware\Core\Checkout\Order\OrderEvents;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. class BackendSubscriber implements EventSubscriberInterface
  16. {
  17.     private $container;
  18.     /**
  19.      * @var PayrexxApiService
  20.      */
  21.     protected $payrexxApiService;
  22.     public function __construct(ContainerInterface $container,  PayrexxApiService $payrexxApiService)
  23.     {
  24.         $this->container $container;
  25.         $this->payrexxApiService $payrexxApiService;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             OrderEvents::ORDER_DELIVERY_WRITTEN_EVENT => 'onUpdateOrder'
  31.         ];
  32.     }
  33.     public function onUpdateOrder(EntityWrittenEvent $event): void
  34.     {
  35.         $context $event->getContext();
  36.         foreach ($event->getWriteResults() as $writeResult) {
  37.             $payload $writeResult->getPayload();
  38.             /** @var EntityRepositoryInterface $stateRepository */
  39.             $deliveryRepository $this->container->get('order_delivery.repository');
  40.             try {
  41.                 $deliveryResult $deliveryRepository->search(
  42.                     (new Criteria([$payload['id']]))->addAssociation('order''stateMachineState'),
  43.                     $context
  44.                 );
  45.             } catch (InconsistentCriteriaIdsException $e) {
  46.             }
  47.             /** @var OrderDeliveryEntity|null $deliveryInfo */
  48.             $deliveryInfo =  $deliveryResult->first();
  49.             if (!($deliveryInfo instanceof OrderDeliveryEntity)) {
  50.                 continue;
  51.             }
  52.             $deliveryState $deliveryInfo->getStateMachineState();
  53.             if(!$deliveryState || $deliveryState->getTechnicalName() !== 'shipped') {
  54.                 continue;
  55.             }
  56.             /** @var EntityRepositoryInterface $orderRepo */
  57.             $orderRepo $this->container->get('order.repository');
  58.             /** @var EntitySearchResult $orderR */
  59.             $orderResult $orderRepo->search(
  60.                 (new Criteria([$deliveryInfo->getOrderId()]))->addAssociation('transactions.paymentMethod''transactions.customFields'),
  61.                 $context
  62.             );
  63.             /** @var OrderEntity|null $order */
  64.             $order $orderResult->first();
  65.             if (!($order instanceof OrderEntity)) {
  66.                 continue;
  67.             }
  68.             $salesChannelId $order->getSalesChannelId();
  69.             $transaction $order->getTransactions()->first();
  70.             $transactionCustomFields $transaction->getCustomFields();
  71.             if (!$transaction || !$transactionCustomFields || !$transactionCustomFields['gateway_id']) {
  72.                 continue;
  73.             }
  74.             if (!$payrexxGateway $this->payrexxApiService->getPayrexxGateway($transactionCustomFields['gateway_id'], $salesChannelId)) {
  75.                 continue;
  76.             }
  77.             if (!$payrexxTransaction $this->payrexxApiService->getTransactionByGateway($payrexxGateway$salesChannelId)) {
  78.                 continue;
  79.             }
  80.             if ($payrexxTransaction->getStatus() == 'uncaptured') {
  81.                 $this->payrexxApiService->captureTransaction($payrexxTransaction->getId(), $salesChannelId);
  82.             }
  83.         }
  84.     }
  85. }