src/Controller/Front/Order/OrderPaymentController.php line 40

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Front\Order;
  4. use App\Application\Service\Helper\ToolsService;
  5. use App\Application\Service\Order\OrderService;
  6. use App\Application\Service\Session\SessionService;
  7. use App\Entity\System\Cart;
  8. use App\Entity\System\PaymentMethod;
  9. use App\Exception\PaymentException;
  10. use App\Factory\Order\OrderPaymentViewFactory;
  11. use App\Manager\System\CartManager;
  12. use App\Manager\System\OrderManager;
  13. use App\Service\Order\OrderPaymentService;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class OrderPaymentController extends AbstractController
  19. {
  20. public function __construct(
  21. private readonly OrderManager $orderManager,
  22. private readonly CartManager $cartManager,
  23. private readonly SessionService $sessionService,
  24. private readonly OrderPaymentService $orderPaymentService,
  25. private readonly OrderService $orderService,
  26. private readonly OrderPaymentViewFactory $orderPaymentViewFactory,
  27. private readonly ToolsService $toolsService,
  28. ) {
  29. }
  30. /**
  31. * @IsGranted("IS_AUTHENTICATED_FULLY")
  32. *
  33. * @Route("/order/{cartParentId}/payment", name="order_payment", methods={"GET", "POST"})
  34. */
  35. public function payOrder(int $cartParentId): Response
  36. {
  37. $requestedCartParent = $this->cartManager->findOneById($cartParentId);
  38. if (!$requestedCartParent) {
  39. return $this->redirectToRoute('homepage');
  40. }
  41. $orderPendingToPay = $this->orderManager->findNextNotVisibleOrderByParentCart($cartParentId);
  42. if (!$orderPendingToPay) {
  43. $cartParent = $this->cartManager->findOneById($cartParentId);
  44. $totalPaidOrders = $this->orderManager->findAllByParentCartId($cartParentId);
  45. if (empty($totalPaidOrders)) {
  46. $totalPaidOrders = [$cartParent->getOrder()];
  47. }
  48. $this->sessionService->delete('id_cart');
  49. $this->sessionService->delete('id_cart_services');
  50. return $this->renderTemplate($cartParent, count($totalPaidOrders), $totalPaidOrders);
  51. }
  52. $parentCart = $orderPendingToPay->getCart()->getCartParent();
  53. try {
  54. $paymentUrl = $this->orderPaymentService->payOrder($orderPendingToPay, $parentCart);
  55. return $this->redirect($paymentUrl);
  56. } catch (PaymentException $exception) {
  57. [
  58. $twigName,
  59. $parentCart,
  60. $ordersCount,
  61. $paidOrders,
  62. $exceptionMessages,
  63. ] = $this->orderPaymentService->managePaymentException($parentCart, [$orderPendingToPay], $exception->getMessage(), []);
  64. $this->orderService->sendOrderToPubSub($orderPendingToPay);
  65. return $this->renderTemplate($parentCart, $ordersCount, $paidOrders, $exceptionMessages);
  66. }
  67. }
  68. private function renderTemplate(
  69. Cart $cart,
  70. ?int $totalCreatedOrders = 0,
  71. ?array $successOrders = null,
  72. ?array $errors = null,
  73. ): Response {
  74. $twigName = $cart->getPaymentMethod()->getId() === PaymentMethod::TRANSFER_PAYMENT_METHOD_ID ? 'success_bankwire.html.twig' : 'success.html.twig';
  75. if ($cart->isVirtual()) {
  76. $cartType = 'services';
  77. $orderPaymentView = $this->orderPaymentViewFactory->buildForServices(
  78. $cart->getCustomer(),
  79. $cart->getId(),
  80. $totalCreatedOrders,
  81. $successOrders,
  82. $errors
  83. );
  84. } else {
  85. $cartType = 'products';
  86. $orderPaymentView = $this->orderPaymentViewFactory->buildForProducts(
  87. $cart->getCustomer()->getId(),
  88. $cart->getId(),
  89. $totalCreatedOrders,
  90. $successOrders,
  91. $errors
  92. );
  93. }
  94. if ($this->toolsService::isMobile()) {
  95. return $this->render(
  96. 'front/order/'.$cartType.'/mobile/'.$twigName,
  97. $this->orderPaymentViewFactory->createViewParameters($orderPaymentView)
  98. );
  99. }
  100. return $this->render(
  101. 'front/order/'.$cartType.'/'.$twigName,
  102. $this->orderPaymentViewFactory->createViewParameters($orderPaymentView)
  103. );
  104. }
  105. }