src/Subscriber/Payment/WebhookSubscriber.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Subscriber\Payment;
  4. use App\Event\Cinema\CinemaContextOnDemandEvent;
  5. use App\Event\Order\OrderPaymentTransactionHandledEvent;
  6. use App\Event\Payment\WebhookNotificationEvent;
  7. use App\Exception\Order\BasketDoesNotExistException;
  8. use App\Exception\Order\BasketOrderLockedException;
  9. use App\Manager\OrderManager;
  10. use App\Payment\Model\Response as PaymentProviderResponse;
  11. use App\Payment\PaymentFactory;
  12. use App\Service\Payment\PaymentProviderService;
  13. use Ramsey\Uuid\Uuid;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  16. class WebhookSubscriber implements EventSubscriberInterface
  17. {
  18.     private PaymentFactory $paymentFactory;
  19.     private PaymentProviderService $paymentProviderService;
  20.     private EventDispatcherInterface $eventDispatcher;
  21.     private OrderManager $orderManager;
  22.     public function __construct(
  23.         PaymentFactory           $paymentFactory,
  24.         PaymentProviderService   $paymentProviderService,
  25.         EventDispatcherInterface $eventDispatcher,
  26.         OrderManager             $orderManager
  27.     )
  28.     {
  29.         $this->paymentFactory $paymentFactory;
  30.         $this->paymentProviderService $paymentProviderService;
  31.         $this->eventDispatcher $eventDispatcher;
  32.         $this->orderManager $orderManager;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         return [
  37.             WebhookNotificationEvent::class => 'onNotification'
  38.         ];
  39.     }
  40.     public function onNotification(WebhookNotificationEvent $event): void
  41.     {
  42.         $client $this->paymentFactory->get(
  43.             $this->paymentProviderService->getPaymentProviderServiceIdentifier($event->getPaymentProvider())
  44.         );
  45.         $paymentProviderResponse $client->dispatch($event->getRequest());
  46.         if ($paymentProviderResponse !== null) {
  47.             $this->eventDispatcher->dispatch(
  48.                 new CinemaContextOnDemandEvent($paymentProviderResponse->getCinemaId())
  49.             );
  50.             $order $this->orderManager->get(Uuid::fromString($paymentProviderResponse->getId()));
  51.             $client->flushLogger();
  52.         } else {
  53.             return;
  54.         }
  55.         if ($order === null) {
  56.             throw new BasketDoesNotExistException();
  57.         }
  58.         if ($paymentProviderResponse->getStatus() !== PaymentProviderResponse::PENDING_TRANSACTION) {
  59.             if ($this->orderManager->isOrderLocked(Uuid::fromString($paymentProviderResponse->getId()))) {
  60.                 throw new BasketOrderLockedException();
  61.             }
  62.             $this->orderManager->lockOrder(Uuid::fromString($paymentProviderResponse->getId()));
  63.         }
  64.         try {
  65.             $this->orderManager->handlePaymentProviderResponse(
  66.                 $paymentProviderResponse,
  67.                 $order,
  68.                 $client,
  69.                 $event->getPaymentChannel(),
  70.                 ['isProcessingExpired' => false'isProcessingAllowed' => true],
  71.             );
  72.         } catch (\Exception $exception) {
  73.             throw $exception;
  74.         } finally {
  75.             $this->eventDispatcher->dispatch(
  76.                 new OrderPaymentTransactionHandledEvent($order$paymentProviderResponse,
  77.                     $client$event->getPaymentChannel()
  78.                 )
  79.             );
  80.         }
  81.     }
  82. }