<?php
namespace App\Controller;
use App\Entity\MangoCall;
use App\Entity\SberbankAcquiringOrder;
use App\Entity\SberbankAcquiringOrderWebhook;
use App\Repository\CustomerRepository;
use App\Repository\SberbankAcquiringOrderRepository;
use App\Repository\SberbankAcquiringOrderWebhookRepository;
use App\Repository\TransportCompanyRepository;
use App\Service\Helper\NormalizerHelper;
use App\Service\Api;
use App\Service\Mango;
use App\Service\PaymentOrder;
use App\Service\ProductAction;
use Doctrine\ORM\ORMException;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
/**
* @Route("/")
*/
class MainController extends AbstractController
{
/**
* @Route("", methods="GET")
*/
public function main(): Response
{
$params = $this->getBaseParams();
$params['js_version'] = filemtime(__DIR__ . '/../../public/assets/js/index_search.js');
return $this->render('main.html.twig', $params);
}
/**
* @Route("contacts", methods="GET")
*/
public function contacts(): Response
{
return $this->render('contacts.html.twig', $this->getBaseParams());
}
/**
* @Route("about", methods="GET")
*/
public function about(): Response
{
return $this->render('about.html.twig', $this->getBaseParams());
}
/**
* @Route("payment", methods="GET")
*/
public function payment(): Response
{
return $this->render('payment.html.twig', $this->getBaseParams());
}
/**
* @Route("delivery", methods="GET")
*/
public function delivery(): Response
{
return $this->render('delivery.html.twig', $this->getBaseParams());
}
/**
* @Route("search", methods="GET")
*/
public function search(): Response
{
$params = $this->getBaseParams();
$params['js_version'] = filemtime(__DIR__ . '/../../public/assets/js/search_page.js');
return $this->render('search.html.twig', $params);
}
/**
* @Route("cart_page", methods="GET")
*/
public function cartPage(): Response
{
$params = $this->getBaseParams();
$params['js_version_cart_page'] = filemtime(__DIR__ . '/../../public/assets/js/cart_page.js');
return $this->render('cart_page.html.twig', $params);
}
/**
* @Route("orders", methods="GET")
*/
public function orders(): Response
{
$params = $this->getBaseParams();
$params['js_version_orders'] = filemtime(__DIR__ . '/../../public/assets/js/orders.js');
return $this->render('orders.html.twig', $params);
}
/**
* @Route("request/{hash}", methods="GET")
*/
public function order(): Response
{
$params = $this->getBaseParams();
$params['js_version_order'] = filemtime(__DIR__ . '/../../public/assets/js/order.js');
return $this->render('order.html.twig', $params);
}
/**
* @Route("payment/success", methods="GET")
*/
public function paymentSuccess(Request $request, SberbankAcquiringOrderRepository $sberbankAcquiringOrderRepository, PaymentOrder $paymentOrderService): Response
{
/** @var SberbankAcquiringOrder $sberbankAcquiringOrder */
$sberbankAcquiringOrder = $sberbankAcquiringOrderRepository->findOneBy(['acquiringOrder' => $request->query->get('orderId')]);
$paymentOrderService->executePaymentBySberbank($sberbankAcquiringOrder);
$params = $this->getBaseParams();
$params['order'] = ['hash' => $sberbankAcquiringOrder->getOrder()->getHash()];
return $this->render('success_payment.html.twig', $params);
}
/**
* @Route("payment/failed", methods="GET")
*/
public function paymentFailed(Request $request, SberbankAcquiringOrderRepository $sberbankAcquiringOrderRepository, PaymentOrder $paymentOrderService): Response
{
/** @var SberbankAcquiringOrder $sberbankAcquiringOrder */
$sberbankAcquiringOrder = $sberbankAcquiringOrderRepository->findOneBy(['acquiringOrder' => $request->query->get('orderId')]);
$params = $this->getBaseParams();
return $this->render('failed_payment.html.twig', $params);
}
/**
* @Route("sberbank/webhook", methods="POST")
*/
public function sberbankWebhook(Request $request, SberbankAcquiringOrderWebhookRepository $acquiringOrderWebhookRepository): Response
{
$acquiringOrderWebhookRepository->save(new SberbankAcquiringOrderWebhook(json_encode($request->toArray())));
return new JsonResponse(['result' => 'success']);
}
/**
* @Route("transport_companies", methods="GET")
*/
public function getTransportCompanies(TransportCompanyRepository $transportCompanyRepository): JsonResponse
{
return new JsonResponse($transportCompanyRepository->getList());
}
/**
* @Route("/handle_mango/events/call", methods="POST")
* @throws Exception
*/
public function handleMangoCall(
Request $request,
CustomerRepository $customerRepository,
Api $apiService,
Mango $mangoService,
): JsonResponse
{
try {
$mangoCall = $mangoService->handleCall($request->request->all());
$mangoContactId = $mangoCall && $mangoCall->getCustomer() ? $mangoCall->getCustomer()->getMangoContactId() : null;
if($mangoCall && !$mangoContactId) {
$mangoContactId = $apiService->createMangoClient($mangoCall);
if($mangoContactId) $customerRepository->update($mangoCall->getCustomer()->setMangoContactId($mangoContactId));
}
return new JsonResponse(['result' => 'success']);
} catch (Exception | TransportExceptionInterface $exception) {
return new JsonResponse(['result' => 'failed']);
}
}
/**
* @Route("/handle_mango/events/summary", methods="POST")
* @throws Exception
*/
public function handleMangoSummary(
Request $request,
Api $apiService,
Mango $mangoService,
): JsonResponse
{
$call = $mangoService->handleSummary($request->request->all());
if($call && $call->getCustomer()) $apiService->updateMangoClient($call->getCustomer());
return new JsonResponse(['result' => 'success']);
}
/**
* @Route("/handle_mango/events/recording", methods="POST")
* @throws Exception
*/
public function handleMangoRecord(Request $request, Mango $mangoService): JsonResponse
{
sleep(3);
$mangoService->handleRecord($request->request->all());
return new JsonResponse(['result' => 'success']);
}
private function getBaseParams(): array
{
return [
'js_version_base' => filemtime(__DIR__ . '/../../public/assets/js/base.js'),
'js_version_user_header' => filemtime(__DIR__ . '/../../public/assets/js/user_header.js'),
'css_version' => filemtime(__DIR__ . '/../../public/assets/css/styles.css')
];
}
}