src/Controller/MainController.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\MangoCall;
  4. use App\Entity\SberbankAcquiringOrder;
  5. use App\Entity\SberbankAcquiringOrderWebhook;
  6. use App\Repository\CustomerRepository;
  7. use App\Repository\SberbankAcquiringOrderRepository;
  8. use App\Repository\SberbankAcquiringOrderWebhookRepository;
  9. use App\Repository\TransportCompanyRepository;
  10. use App\Service\Helper\NormalizerHelper;
  11. use App\Service\Api;
  12. use App\Service\Mango;
  13. use App\Service\PaymentOrder;
  14. use App\Service\ProductAction;
  15. use Doctrine\ORM\ORMException;
  16. use Exception;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  23. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  24. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  25. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  26. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  27. /**
  28.  * @Route("/")
  29.  */
  30. class MainController extends AbstractController
  31. {
  32.     /**
  33.      * @Route("", methods="GET")
  34.      */
  35.     public function main(): Response
  36.     {
  37.         $params $this->getBaseParams();
  38.         $params['js_version'] = filemtime(__DIR__ '/../../public/assets/js/index_search.js');
  39.         return $this->render('main.html.twig'$params);
  40.     }
  41.     /**
  42.      * @Route("contacts", methods="GET")
  43.      */
  44.     public function contacts(): Response
  45.     {
  46.         return $this->render('contacts.html.twig'$this->getBaseParams());
  47.     }
  48.     /**
  49.      * @Route("about", methods="GET")
  50.      */
  51.     public function about(): Response
  52.     {
  53.         return $this->render('about.html.twig'$this->getBaseParams());
  54.     }
  55.     /**
  56.      * @Route("payment", methods="GET")
  57.      */
  58.     public function payment(): Response
  59.     {
  60.         return $this->render('payment.html.twig'$this->getBaseParams());
  61.     }
  62.     /**
  63.      * @Route("delivery", methods="GET")
  64.      */
  65.     public function delivery(): Response
  66.     {
  67.         return $this->render('delivery.html.twig'$this->getBaseParams());
  68.     }
  69.     /**
  70.      * @Route("search", methods="GET")
  71.      */
  72.     public function search(): Response
  73.     {
  74.         $params $this->getBaseParams();
  75.         $params['js_version'] = filemtime(__DIR__ '/../../public/assets/js/search_page.js');
  76.         return $this->render('search.html.twig'$params);
  77.     }
  78.     /**
  79.      * @Route("cart_page", methods="GET")
  80.      */
  81.     public function cartPage(): Response
  82.     {
  83.         $params $this->getBaseParams();
  84.         $params['js_version_cart_page'] = filemtime(__DIR__ '/../../public/assets/js/cart_page.js');
  85.         return $this->render('cart_page.html.twig'$params);
  86.     }
  87.     /**
  88.      * @Route("orders", methods="GET")
  89.      */
  90.     public function orders(): Response
  91.     {
  92.         $params $this->getBaseParams();
  93.         $params['js_version_orders'] = filemtime(__DIR__ '/../../public/assets/js/orders.js');
  94.         return $this->render('orders.html.twig'$params);
  95.     }
  96.     /**
  97.      * @Route("request/{hash}", methods="GET")
  98.      */
  99.     public function order(): Response
  100.     {
  101.         $params $this->getBaseParams();
  102.         $params['js_version_order'] = filemtime(__DIR__ '/../../public/assets/js/order.js');
  103.         return $this->render('order.html.twig'$params);
  104.     }
  105.     /**
  106.      * @Route("payment/success", methods="GET")
  107.      */
  108.     public function paymentSuccess(Request $requestSberbankAcquiringOrderRepository $sberbankAcquiringOrderRepositoryPaymentOrder $paymentOrderService): Response
  109.     {
  110.         /** @var SberbankAcquiringOrder $sberbankAcquiringOrder */
  111.         $sberbankAcquiringOrder $sberbankAcquiringOrderRepository->findOneBy(['acquiringOrder' => $request->query->get('orderId')]);
  112.         $paymentOrderService->executePaymentBySberbank($sberbankAcquiringOrder);
  113.         $params $this->getBaseParams();
  114.         $params['order'] = ['hash' => $sberbankAcquiringOrder->getOrder()->getHash()];
  115.         return $this->render('success_payment.html.twig'$params);
  116.     }
  117.     /**
  118.      * @Route("payment/failed", methods="GET")
  119.      */
  120.     public function paymentFailed(Request $requestSberbankAcquiringOrderRepository $sberbankAcquiringOrderRepositoryPaymentOrder $paymentOrderService): Response
  121.     {
  122.         /** @var SberbankAcquiringOrder $sberbankAcquiringOrder */
  123.         $sberbankAcquiringOrder $sberbankAcquiringOrderRepository->findOneBy(['acquiringOrder' => $request->query->get('orderId')]);
  124.         $params $this->getBaseParams();
  125.         return $this->render('failed_payment.html.twig'$params);
  126.     }
  127.     /**
  128.      * @Route("sberbank/webhook", methods="POST")
  129.      */
  130.     public function sberbankWebhook(Request $requestSberbankAcquiringOrderWebhookRepository $acquiringOrderWebhookRepository): Response
  131.     {
  132.         $acquiringOrderWebhookRepository->save(new SberbankAcquiringOrderWebhook(json_encode($request->toArray())));
  133.         return new JsonResponse(['result' => 'success']);
  134.     }
  135.     /**
  136.      * @Route("transport_companies", methods="GET")
  137.      */
  138.     public function getTransportCompanies(TransportCompanyRepository $transportCompanyRepository): JsonResponse
  139.     {
  140.         return new JsonResponse($transportCompanyRepository->getList());
  141.     }
  142.     /**
  143.      * @Route("/handle_mango/events/call", methods="POST")
  144.      * @throws Exception
  145.      */
  146.     public function handleMangoCall(
  147.         Request $request,
  148.         CustomerRepository $customerRepository,
  149.         Api $apiService,
  150.         Mango $mangoService,
  151.     ): JsonResponse
  152.     {
  153.         try {
  154.             $mangoCall $mangoService->handleCall($request->request->all());
  155.             $mangoContactId $mangoCall && $mangoCall->getCustomer() ? $mangoCall->getCustomer()->getMangoContactId() : null;
  156.             if($mangoCall && !$mangoContactId) {
  157.                 $mangoContactId $apiService->createMangoClient($mangoCall);
  158.                 if($mangoContactId$customerRepository->update($mangoCall->getCustomer()->setMangoContactId($mangoContactId));
  159.             }
  160.             return new JsonResponse(['result' => 'success']);
  161.         } catch (Exception TransportExceptionInterface $exception) {
  162.             return new JsonResponse(['result' => 'failed']);
  163.         }
  164.     }
  165.     /**
  166.      * @Route("/handle_mango/events/summary", methods="POST")
  167.      * @throws Exception
  168.      */
  169.     public function handleMangoSummary(
  170.         Request $request,
  171.         Api $apiService,
  172.         Mango $mangoService,
  173.     ): JsonResponse
  174.     {
  175.         $call $mangoService->handleSummary($request->request->all());
  176.         if($call && $call->getCustomer()) $apiService->updateMangoClient($call->getCustomer());
  177.         return new JsonResponse(['result' => 'success']);
  178.     }
  179.     /**
  180.      * @Route("/handle_mango/events/recording", methods="POST")
  181.      * @throws Exception
  182.      */
  183.     public function handleMangoRecord(Request $requestMango $mangoService): JsonResponse
  184.     {
  185.         sleep(3);
  186.         $mangoService->handleRecord($request->request->all());
  187.         return new JsonResponse(['result' => 'success']);
  188.     }
  189.     private function getBaseParams(): array
  190.     {
  191.         return [
  192.             'js_version_base' => filemtime(__DIR__ '/../../public/assets/js/base.js'),
  193.             'js_version_user_header' => filemtime(__DIR__ '/../../public/assets/js/user_header.js'),
  194.             'css_version' => filemtime(__DIR__ '/../../public/assets/css/styles.css')
  195.         ];
  196.     }
  197. }