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

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