src/Controller/WidgetElectronicInvoiceController.php line 153

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ElectronicInvoiceContact;
  4. use App\Repository\AccountingFirmRepository;
  5. use App\Repository\AuthorizedDomainRepository;
  6. use App\Services\WidgetMailing;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Qferrer\Mjml\Twig\MjmlExtension;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  11. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\HttpKernel\KernelInterface;
  17. use Symfony\Component\Mailer\MailerInterface;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Twig\Environment;
  20. class WidgetElectronicInvoiceController extends AbstractController
  21. {
  22.     protected MjmlExtension $mjmlRenderer;
  23.     protected Environment $twig;
  24.     private MailerInterface $mailer;
  25.     public function __construct(MailerInterface $mailerMjmlExtension $mjmlRendererEnvironment $twig)
  26.     {
  27.         $this->mailer $mailer;
  28.         $this->mjmlRenderer $mjmlRenderer;
  29.         $this->twig $twig;
  30.     }
  31.     #[Route('/check-electronic-invoice'name'w_electronic_invoice_check')]
  32.     public function ctrl_checkElectronicInvoice(Request $requestAccountingFirmRepository $repositorystring $prefix null): JsonResponse
  33.     {
  34.         $token $request->query->get('token');
  35.         $host $request->get('host');
  36.         if ($token && $host) {
  37.             $accountingFirm $repository->findOneBy(['electronicInvoiceToken' => $token]);
  38.             $isAuthorized false;
  39.             if ($accountingFirm && in_array($host$accountingFirm->getListAuthorizedDomains())) {
  40.                 $isAuthorized true;
  41.             }
  42.             $url $isAuthorized $accountingFirm->getElectronicInvoiceWidgetUrl() : null;
  43.         } else {
  44.             $accountingFirm $repository->findOneBy(['host' => $host]);
  45.             $url $accountingFirm $prefix $this->generateUrl('w_electronic_invoice_get') : null;
  46.         }
  47.         return $this->json($url);
  48.     }
  49.     #[Route('/widget-electronic-invoice'name'w_electronic_invoice_get')]
  50.     public function ctrl_widgetElectronicInvoice(Request $requestAccountingFirmRepository $repositoryAuthorizedDomainRepository $domainsParameterBagInterface $params)
  51.     {
  52.         $token $request->query->get('token');
  53.         $host $request->get('host');
  54.         if ($host != "preview") {
  55.              $accountingFirm $repository->findOneBy(['host' => $host]);
  56.             if (is_null($accountingFirm)) {
  57.                 $domains $domains->findBy(['name' => $host]);
  58.                 if (is_null($domains)) {
  59.                     throw new NotFoundHttpException();
  60.                 }
  61.                 foreach ($domains as $dom) {
  62.                     $tmp_ac $dom->getAccountingFirm();
  63.                     if ($tmp_ac->getElectronicInvoiceToken() == $token) {
  64.                         $accountingFirm $tmp_ac;
  65.                         break;
  66.                     }
  67.                 }
  68.             }
  69.             if (is_null($accountingFirm)) {
  70.                 throw new NotFoundHttpException();
  71.             }
  72.         }
  73.         return $this->render('widget_electronic_invoice/content.html.twig', [
  74.             'cabinet' => $accountingFirm,
  75.             'serverUrl' => $params->get('domain_widget_electronic_invoice')
  76.         ]);
  77.     }
  78.     #[Route('/widget-electronic-invoice-popup'name'w_electronic_invoice_popup_get')]
  79.     public function ctrl_widgetElectronicInvoicePopup(Request $requestAccountingFirmRepository $repositoryAuthorizedDomainRepository $domainsParameterBagInterface $params)
  80.     {
  81.         $token $request->query->get('token');
  82.         $host $request->get('host');
  83.         if ($host != "preview") {
  84.             $accountingFirm $repository->findOneBy(['host' => $host]);
  85.             if (is_null($accountingFirm)) {
  86.                 $domains $domains->findBy(['name' => $host]);
  87.                 if (is_null($domains)) {
  88.                     throw new NotFoundHttpException();
  89.                 }
  90.                 foreach ($domains as $dom) {
  91.                     $tmp_ac $dom->getAccountingFirm();
  92.                     if ($tmp_ac->getElectronicInvoiceToken() == $token) {
  93.                         $accountingFirm $tmp_ac;
  94.                         break;
  95.                     }
  96.                 }
  97.             }
  98.             if (is_null($accountingFirm)) {
  99.                 throw new NotFoundHttpException();
  100.             }
  101.         }
  102.         $electronicInvoiceUrl $accountingFirm->getUrl() . $accountingFirm->getElectronicInvoiceWidgetUrl();
  103.         return $this->render('widget_electronic_invoice/popup.html.twig', [
  104.             'cabinet' => $accountingFirm,
  105.             'serverUrl' => $params->get('domain_widget_electronic_invoice'),
  106.             'electronicInvoiceUrl' => $electronicInvoiceUrl
  107.         ]);
  108.     }
  109.     #[Route('/embed'name'w_electronic_invoice_embed')]
  110.     public function embed()
  111.     {
  112.         $root $this->getParameter('kernel.project_dir');
  113.         $path $root '/public/widgets/widget_electronic_invoice/widget_electronic_invoice.js';
  114.         return new BinaryFileResponse($path);
  115.     }
  116.     #[Route('/{filename}.{format}'name'w_electronic_invoice_files')]
  117.     public function widget_files(string $filenamestring $format)
  118.     {
  119.         $allowed = array('png''jpg''jpeg''gif''js''svg''eot''ttf''woff');
  120.         if (in_array($format$allowed)) {
  121.             $root $this->getParameter('kernel.project_dir');
  122.             $path $root '/public/widget_electronic_invoice/' $filename '.' $format;
  123.             return new BinaryFileResponse($path);
  124.         }
  125.         return false;
  126.     }
  127.     #[Route('/send-electronic-invoice'name'widget_electronic_invoice_send_form'methods: ['POST'])]
  128.     public function sendForm(
  129.         Request $request,
  130.         EntityManagerInterface $em,
  131.         AccountingFirmRepository $accountingFirmRepository,
  132.         WidgetMailing $widgetMailing
  133.     ): JsonResponse {
  134.         // Vérifie le JSON reçu
  135.         $data json_decode($request->getContent(), true);
  136.         if (!$data) {
  137.             return new JsonResponse([
  138.                 "success" => false,
  139.                 "error"   => "invalid_json"
  140.             ], 400);
  141.         }
  142.         //--------------------------------------
  143.         // 1) ANTI-SPAM : Honeypot
  144.         //--------------------------------------
  145.         if (!empty($data['website'] ?? null)) {
  146.             // On répond comme si tout était OK, pour ne rien révéler
  147.             return new JsonResponse(["success" => true]);
  148.         }
  149.         //--------------------------------------
  150.         // 2) ANTI-SPAM : Timestamp minimal
  151.         //--------------------------------------
  152.         if (!isset($data['form_ts']) || (time() * 1000 - (int)$data['form_ts']) < 1000) {
  153.             // Pareil : réponse neutre
  154.             return new JsonResponse(["success" => true]);
  155.         }
  156.         //--------------------------------------
  157.         // 3) Validation des champs
  158.         //--------------------------------------
  159.         $required = ['company''firstname''lastname''phone''email''message'];
  160.         foreach ($required as $field) {
  161.             if (empty(trim($data[$field] ?? ''))) {
  162.                 return new JsonResponse([
  163.                     "success" => false,
  164.                     "error"   => "missing_field",
  165.                     "field"   => $field
  166.                 ], 400);
  167.             }
  168.         }
  169.         // Email valide ?
  170.         if (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
  171.             return new JsonResponse([
  172.                 "success" => false,
  173.                 "error"   => "invalid_email"
  174.             ], 400);
  175.         }
  176.         $accountingFirm $accountingFirmRepository->findOneBy(['id' => $data['accountingFirmId']]);
  177.         if (!$accountingFirm) {
  178.             return new JsonResponse([
  179.                 "success" => false,
  180.                 "error"   => "invalid_accounting_firm"
  181.             ], 400);
  182.         }
  183.         //--------------------------------------
  184.         // 4) Création de l'entité ElectronicInvoiceContact
  185.         //--------------------------------------
  186.         $eiContact = new ElectronicInvoiceContact();
  187.         $eiContact->setAccountingFirm($accountingFirm);
  188.         $eiContact->setCompany($data['company']);
  189.         $eiContact->setFirstname($data['firstname']);
  190.         $eiContact->setLastname($data['lastname']);
  191.         $eiContact->setPhone($data['phone']);
  192.         $eiContact->setEmail($data['email']);
  193.         $eiContact->setMessage($data['message']);
  194.         $eiContact->setCreatedAt(new \DateTimeImmutable());
  195.         $em->persist($eiContact);
  196.         $em->flush();
  197.         //--------------------------------------
  198.         // 5) Envoi email via ton service WidgetMailing
  199.         //--------------------------------------
  200.         try {
  201.             $widgetMailing->sendElectronicInvoiceContact($eiContact);
  202.         } catch (\Throwable $e) {
  203.             // Sentry le capturera automatiquement puisque tu l’as configuré côté front
  204.             return new JsonResponse([
  205.                 "success" => false,
  206.                 "error"   => "mail_error",
  207.                 "details" => $e->getMessage()
  208.             ], 500);
  209.         }
  210.         //--------------------------------------
  211.         // 6) Réponse finale
  212.         //--------------------------------------
  213.         return new JsonResponse([
  214.             "success" => true
  215.         ]);
  216.     }
  217. }