custom/plugins/KlarnaPayment/src/Components/EventListener/OrderStatusTransitionEventListener.php line 66

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace KlarnaPayment\Components\EventListener;
  4. use KlarnaPayment\Components\Helper\OrderFetcher;
  5. use KlarnaPayment\Components\Helper\StateHelper\Cancel\CancelStateHelper;
  6. use KlarnaPayment\Components\Helper\StateHelper\Capture\CaptureStateHelperInterface;
  7. use KlarnaPayment\Components\Helper\StateHelper\Refund\RefundStateHelperInterface;
  8. use KlarnaPayment\Components\Helper\StateHelper\StateData\StateDataHelper;
  9. use KlarnaPayment\Components\PaymentHandler\AbstractKlarnaPaymentHandler;
  10. use KlarnaPayment\Components\Validator\OrderTransitionChangeValidator;
  11. use KlarnaPayment\Core\Framework\ContextScope;
  12. use Shopware\Core\Checkout\Order\Aggregate\OrderDelivery\OrderDeliveryDefinition;
  13. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionDefinition;
  14. use Shopware\Core\Checkout\Order\OrderDefinition;
  15. use Shopware\Core\Checkout\Order\OrderEntity;
  16. use Shopware\Core\Framework\Context;
  17. use Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class OrderStatusTransitionEventListener implements EventSubscriberInterface
  20. {
  21.     /** @var OrderTransitionChangeValidator */
  22.     private $orderStatusValidator;
  23.     /** @var CaptureStateHelperInterface */
  24.     private $captureStateHelper;
  25.     /** @var RefundStateHelperInterface */
  26.     private $refundStateHelper;
  27.     /** @var OrderFetcher */
  28.     private $orderFetcher;
  29.     /** @var CancelStateHelper */
  30.     private $cancelStateHelper;
  31.     /** @var StateDataHelper */
  32.     private $stateDataHelper;
  33.     public function __construct(
  34.         OrderTransitionChangeValidator $orderStatusValidator,
  35.         CaptureStateHelperInterface $captureStateHelper,
  36.         RefundStateHelperInterface $refundStateHelper,
  37.         CancelStateHelper $cancelStateHelper,
  38.         OrderFetcher $orderFetcher,
  39.         StateDataHelper $stateDataHelper
  40.     ) {
  41.         $this->orderStatusValidator $orderStatusValidator;
  42.         $this->captureStateHelper   $captureStateHelper;
  43.         $this->refundStateHelper    $refundStateHelper;
  44.         $this->cancelStateHelper    $cancelStateHelper;
  45.         $this->orderFetcher         $orderFetcher;
  46.         $this->stateDataHelper      $stateDataHelper;
  47.     }
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             StateMachineTransitionEvent::class => 'onStateMachineTransition',
  52.         ];
  53.     }
  54.     public function onStateMachineTransition(StateMachineTransitionEvent $transitionEvent): void
  55.     {
  56.         $context $transitionEvent->getContext();
  57.         if ($transitionEvent->getContext()->getScope() === ContextScope::INTERNAL_SCOPE) {
  58.             return;
  59.         }
  60.         $order $this->getOrder($transitionEvent$context);
  61.         if (!$order) {
  62.             return;
  63.         }
  64.         if ($this->isFraudStatusAccepted($order)
  65.             && $this->orderStatusValidator->isAutomaticCapture(
  66.                 $transitionEvent->getEntityName(),
  67.                 $transitionEvent->getToPlace()->getTechnicalName(),
  68.                 $order->getSalesChannelId(),
  69.                 $transitionEvent->getContext()
  70.             )
  71.         ) {
  72.             $this->captureStateHelper->processOrderCapture($order$context);
  73.         } elseif ($this->orderStatusValidator->isAutomaticRefund($transitionEvent$order->getSalesChannelId())) {
  74.             $this->refundStateHelper->processOrderRefund($order$context);
  75.         }
  76.         if ($this->orderStatusValidator->isAutomaticCancel($transitionEvent)) {
  77.             if ($transitionEvent->getEntityName() === OrderDefinition::ENTITY_NAME) {
  78.                 $this->cancelStateHelper->processOrderCancellation($order$context);
  79.             } elseif ($transitionEvent->getEntityName() === OrderTransactionDefinition::ENTITY_NAME) {
  80.                 $this->cancelTransaction($order$transitionEvent->getEntityId(), $context);
  81.             }
  82.         }
  83.     }
  84.     private function isFraudStatusAccepted(OrderEntity $orderEntity): bool
  85.     {
  86.         foreach ($this->stateDataHelper->getValidTransactions($orderEntity) as $transaction) {
  87.             if (is_array($transaction->getCustomFields())
  88.                 && isset($transaction->getCustomFields()['klarna_fraud_status'])
  89.                 && strtolower($transaction->getCustomFields()['klarna_fraud_status']) === strtolower(AbstractKlarnaPaymentHandler::FRAUD_STATUS_ACCEPTED)) {
  90.                 return true;
  91.             }
  92.         }
  93.         return false;
  94.     }
  95.     private function getOrder(StateMachineTransitionEvent $transitionEventContext $context): ?OrderEntity
  96.     {
  97.         if ($transitionEvent->getEntityName() === OrderDefinition::ENTITY_NAME) {
  98.             return $this->orderFetcher->getOrderFromOrder($transitionEvent->getEntityId(), $context);
  99.         }
  100.         if ($transitionEvent->getEntityName() === OrderDeliveryDefinition::ENTITY_NAME) {
  101.             return $this->orderFetcher->getOrderFromOrderDelivery($transitionEvent->getEntityId(), $context);
  102.         }
  103.         if ($transitionEvent->getEntityName() === OrderTransactionDefinition::ENTITY_NAME) {
  104.             return $this->orderFetcher->getOrderFromOrderTransaction($transitionEvent->getEntityId(), $context);
  105.         }
  106.         return null;
  107.     }
  108.     private function cancelTransaction(OrderEntity $orderstring $transactionIdContext $context): void
  109.     {
  110.         if ($order->getTransactions() === null) {
  111.             return;
  112.         }
  113.         $transaction $order->getTransactions()->get($transactionId);
  114.         if ($transaction === null) {
  115.             return;
  116.         }
  117.         $this->cancelStateHelper->processOrderTransactionCancellation($transaction$order$context);
  118.     }
  119. }