src/Controller/Front/Stripe/StripeResponseController.php line 118

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Front\Stripe;
  4. use App\Application\Service\Helper\LogWriterService;
  5. use App\Application\Service\Session\SessionService;
  6. use App\Manager\System\OrderManager;
  7. use App\Message\StripeResponseMessage;
  8. use App\Service\Order\OrderPaymentService;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Messenger\MessageBusInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Stripe\Webhook;
  15. use Stripe\Exception\SignatureVerificationException;
  16. use Stripe\Exception\UnexpectedValueException;
  17. class StripeResponseController extends AbstractController
  18. {
  19.     private LogWriterService $logWriterService;
  20.     private MessageBusInterface $messageBus;
  21.     private OrderManager $orderManager;
  22.     private SessionService $sessionService;
  23.     private OrderPaymentService $orderPaymentService;
  24.     public function __construct(
  25.         LogWriterService $logWriterService,
  26.         MessageBusInterface $messageBus,
  27.         OrderManager $orderManager,
  28.         SessionService $sessionService,
  29.         OrderPaymentService $orderPaymentService
  30.     ) {
  31.         $this->logWriterService $logWriterService;
  32.         $this->messageBus $messageBus;
  33.         $this->orderManager $orderManager;
  34.         $this->sessionService $sessionService;
  35.         $this->orderPaymentService $orderPaymentService;
  36.     }
  37.     /**
  38.      * @Route("stripe/{orderId}/checkout", methods={"GET"}, name="stripe_checkout_session_response")
  39.      */
  40.     public function checkoutSessionResponse(int $orderId): Response
  41.     {
  42.         $message __METHOD__.' - Procesamos la respuesta del CheckoutSession de Stripe para el pedido '.$orderId;
  43.         $this->logWriterService->logOrderPayment($message);
  44.         $order $this->orderManager->findOneById($orderId);
  45.         if (!$order) {
  46.             $this->logWriterService->logOrderPaymentError(__METHOD__.' - El pedido '.$orderId.' no existe.');
  47.             return $this->redirect(BASE_URL);
  48.         }
  49.         $paidOrderIds $this->sessionService->get('paidOrderIds') ? (array)$this->sessionService->get('paidOrderIds') : [];
  50.         $paidOrderIds[] = $order->getId();
  51.         $paidOrders = \array_map(function (int $paidOrderId) {
  52.             return $this->orderManager->getOneById($paidOrderId);
  53.         }, $paidOrderIds);
  54.         $this->orderPaymentService->manageOrdersInSessionOnPaymentSuccess(
  55.             $paidOrders,
  56.             $order,
  57.             (bool)$order->getCart()->getCartParent()
  58.         );
  59.         if ($order->getCart()->getVirtual()) {
  60.             $this->logWriterService->logOrderPayment(__METHOD__.' - Redirecting to services_order_creation route');
  61.             return $this->redirectToRoute('services_order_creation', ['validateCart' => 0]);
  62.         }
  63.         $this->logWriterService->logOrderPayment(__METHOD__.' - Redirecting to products_order_creation route');
  64.         return $this->redirectToRoute('products_order_creation', ['validateCart' => 0]);
  65.     }
  66.     /**
  67.      * @Route("stripe/webhook/notification", name="webhook_stripe_notification", methods={"POST"})
  68.      */
  69.     public function checkNotification(Request $request): Response
  70.     {
  71.         $webhookSecret $this->getParameter('stripe_webhook_key');
  72.         $sigHeader $request->headers->get('Stripe-Signature');
  73.         $notificationRequest = \json_decode($request->getContent(), true);
  74.         $this->logWriterService->logOrderPayment('LLega webhook Stripe. Request: '.$request->getContent().' y la firma es: '.$sigHeader);
  75.         $sandbox = (bool)$this->getParameter('stripe_sandbox');
  76.         $livemode $notificationRequest['data']['object']['livemode'];
  77.         if ($sandbox !== ($livemode === false)) {
  78.             $this->logWriterService->logOrderPaymentError('Error Webhook Stripe: La variable stripe_sandbox con valor '.(int)$sandbox.' no coincide con el valor de livemode en el webhook Stripe: '.$livemode);
  79.             return new Response('Invalid livemode'Response::HTTP_BAD_REQUEST);
  80.         }
  81.         try {
  82.             $event Webhook::constructEvent($request->getContent(), $sigHeader$webhookSecret);
  83.         } catch (UnexpectedValueException $exception) {
  84.             $this->logWriterService->logOrderPaymentError('Error Webhook Stripe: Invalid payload: '.$exception->getMessage());
  85.             return new Response('Invalid payload'Response::HTTP_BAD_REQUEST);
  86.         } catch (SignatureVerificationException $exception) {
  87.             $this->logWriterService->logOrderPaymentError('Error Webhook Stripe: Invalid signature: '.$exception->getMessage());
  88.             return new Response('Invalid signature'Response::HTTP_BAD_REQUEST);
  89.         }
  90.         $this->logWriterService->logOrderPayment('Webhook Stripe vĂ¡lido. Mandamos mensaje a Rabbit');
  91.         $stripeResponseMessage = new StripeResponseMessage($event);
  92.         $this->messageBus->dispatch($stripeResponseMessage);
  93.         return new Response(
  94.             null,
  95.             Response::HTTP_OK
  96.         );
  97.     }
  98. }