custom/plugins/MolliePayments/src/Subscriber/WebhookTimezoneSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Kiener\MolliePayments\Subscriber;
  4. use Kiener\MolliePayments\Service\Router\RoutingDetector;
  5. use Kiener\MolliePayments\Service\TransactionService;
  6. use Kiener\MolliePayments\Struct\Order\OrderAttributes;
  7. use Psr\Log\LoggerInterface;
  8. use Shopware\Core\Checkout\Order\Aggregate\OrderTransaction\OrderTransactionEntity;
  9. use Shopware\Core\Checkout\Order\OrderEntity;
  10. use Shopware\Core\Framework\Uuid\Uuid;
  11. use Shopware\Storefront\Framework\Twig\TwigDateRequestListener;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\RequestEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. class WebhookTimezoneSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var TransactionService
  19.      */
  20.     private $transactionService;
  21.     /**
  22.      * @var RoutingDetector
  23.      */
  24.     private $routeDetector;
  25.     /**
  26.      * @var LoggerInterface
  27.      */
  28.     private $logger;
  29.     public function __construct(TransactionService $transactionServiceRoutingDetector $routeDetectorLoggerInterface $logger)
  30.     {
  31.         $this->transactionService $transactionService;
  32.         $this->routeDetector $routeDetector;
  33.         $this->logger $logger;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             // Route gets matched in a subscriber with priority 32, so we need to have a lower priority than that.
  39.             // But priority needs to be higher than 0 as Shopware's timezone listener will run at that priority.
  40.             KernelEvents::REQUEST => ['fixWebhookTimezone'31],
  41.         ];
  42.     }
  43.     public function fixWebhookTimezone(RequestEvent $event): void
  44.     {
  45.         // we only fix the timezone when being called from the
  46.         // Storefront Webhook Route or API Webhook Route (headless).
  47.         if (! $this->routeDetector->isStorefrontWebhookRoute() && ! $this->routeDetector->isApiWebhookRoute()) {
  48.             return;
  49.         }
  50.         $request $event->getRequest();
  51.         $routeParams $request->get('_route_params');
  52.         $transactionId $routeParams['swTransactionId'] ?? '';
  53.         if (! Uuid::isValid($transactionId)) {
  54.             $this->logger->warning(sprintf('Webhook Timezone Fixer: TransactionId %s is not valid'$transactionId), [
  55.                 'transactionId' => $transactionId,
  56.             ]);
  57.             return;
  58.         }
  59.         $transaction $this->transactionService->getTransactionById($transactionId);
  60.         if (! $transaction instanceof OrderTransactionEntity) {
  61.             $this->logger->error(sprintf('Transaction for id %s does not exist'$transactionId));
  62.             return;
  63.         }
  64.         $order $transaction->getOrder();
  65.         if (! $order instanceof OrderEntity) {
  66.             $this->logger->error(sprintf('Could not get order from transaction %s'$transactionId));
  67.             return;
  68.         }
  69.         $orderAttributes = new OrderAttributes($order);
  70.         if (empty($orderAttributes->getTimezone())) {
  71.             return;
  72.         }
  73.         // Set the timezone cookie on the request and let Shopware handle the rest.
  74.         $request->cookies->set(TwigDateRequestListener::TIMEZONE_COOKIE$orderAttributes->getTimezone());
  75.     }
  76. }