src/Controller/Blo/OrderController.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Blo;
  4. use App\Entity\Blo\Order;
  5. use App\Repository\Blo\OrderRepository;
  6. use App\Service\Blo\CountriesHelper;
  7. use App\Service\Blo\PaymentService;
  8. use Knp\Component\Pager\PaginatorInterface;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. #[Route('/orders')]
  14. class OrderController extends AbstractController
  15. {
  16.     public function __construct(
  17.         private readonly OrderRepository $orderRepository,
  18.         private readonly PaginatorInterface $paginator,
  19.         private readonly PaymentService $paymentService,
  20.     ) {
  21.     }
  22.     #[Route(''name'blo_orders'methods: ['GET'])]
  23.     public function index(Request $request): Response
  24.     {
  25.         $this->denyAccessUnlessGranted('ROLE_USER');
  26.         $user $this->getUser();
  27.         $orders $this->paginator->paginate(
  28.             $this->orderRepository->createQueryBuilder('o')
  29.                 ->where('o.customer = :user')
  30.                 ->setParameter('user'$user)
  31.                 ->orderBy('o.createdAt''DESC'),
  32.             $request->query->getInt('page'1),
  33.             20
  34.         );
  35.         return $this->render('blo/order/index.html.twig', [
  36.             'orders' => $orders,
  37.         ]);
  38.     }
  39.     #[Route('/{id}'name'blo_order_show'methods: ['GET'], requirements: ['id' => '\d+'])]
  40.     public function show(Order $order): Response
  41.     {
  42.         $this->denyAccessUnlessGranted('blo_order_view'$order);
  43.         return $this->render('blo/order/show.html.twig', [
  44.             'order' => $order,
  45.             'countryLabel' => CountriesHelper::getLabel($order->getShippingCountry()),
  46.             'paymentMethods' => $this->paymentService->getRegistry()->getAvailableGateways(),
  47.         ]);
  48.     }
  49.     #[Route('/{id}/pay'name'blo_order_pay'methods: ['GET''POST'], requirements: ['id' => '\d+'])]
  50.     public function pay(Order $orderRequest $request): Response
  51.     {
  52.         $this->denyAccessUnlessGranted('blo_order_view'$order);
  53.         if ($order->getPaymentStatus() === Order::PAYMENT_PAID) {
  54.             $this->addFlash('info''Cette commande est déjà payée.');
  55.             return $this->redirectToRoute('blo_order_show', ['id' => $order->getId()]);
  56.         }
  57.         if ($request->isMethod('POST')) {
  58.             if (!$this->isCsrfTokenValid('blo_order_pay_' $order->getId(), $request->request->get('_csrf_token'))) {
  59.                 $this->addFlash('error''Jeton de sécurité invalide.');
  60.                 return $this->redirectToRoute('blo_order_pay', ['id' => $order->getId()]);
  61.             }
  62.             $method $request->request->get('payment_method''dummy');
  63.             $result $this->paymentService->initPayment($order$method);
  64.             if ($result->success && $result->redirectUrl) {
  65.                 return $this->redirect($result->redirectUrl);
  66.             }
  67.             if ($result->success && $result->clientSecret && $result->publishableKey) {
  68.                 $confirmUrl $this->generateUrl('blo_checkout_confirm', ['ref' => $result->paymentRef], \Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL);
  69.                 return $this->render('blo/order/pay_stripe.html.twig', [
  70.                     'order' => $order,
  71.                     'stripeClientSecret' => $result->clientSecret,
  72.                     'stripePublishableKey' => $result->publishableKey,
  73.                     'confirmUrl' => $confirmUrl,
  74.                 ]);
  75.             }
  76.             $this->addFlash('error'$result->errorMessage ?? 'Impossible d\'initier le paiement.');
  77.             return $this->redirectToRoute('blo_order_pay', ['id' => $order->getId()]);
  78.         }
  79.         return $this->render('blo/order/pay.html.twig', [
  80.             'order' => $order,
  81.             'paymentMethods' => $this->paymentService->getRegistry()->getAvailableGateways(),
  82.         ]);
  83.     }
  84. }