src/Controller/CarteCadeauController.php line 69

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Code;
  4. use App\Entity\Contact;
  5. use App\Entity\Notification;
  6. use App\Entity\Payment\Payplug;
  7. use App\Entity\Token;
  8. use App\Entity\Service;
  9. use App\Form\ContactPaymentType;
  10. use App\Form\ContactTunnelType;
  11. use App\Repository\LogRepository;
  12. use App\Service\Formater;
  13. use App\Entity\Reservation;
  14. use App\Form\CarteCadeauType;
  15. use App\Service\CodeService;
  16. use App\Service\MetaConversionsApiService;
  17. use App\Service\PaymentService;
  18. use App\Service\TokenService;
  19. use App\Service\PayplugService;
  20. use App\Service\CarteCadeauService;
  21. use App\Repository\ServiceRepository;
  22. use Doctrine\ORM\EntityManagerInterface;
  23. use PhpOffice\PhpSpreadsheet\Style\Fill;
  24. use App\Service\ExtraReservationService;
  25. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  26. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  27. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  28. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpFoundation\Response;
  31. use Symfony\Component\Routing\Annotation\Route;
  32. use Symfony\Component\HttpFoundation\JsonResponse;
  33. use Symfony\Component\HttpFoundation\StreamedResponse;
  34. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  35. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  36. class CarteCadeauController extends AbstractController
  37. {
  38.     public function __construct(
  39.         private ServiceRepository  $serviceRepository,
  40.         private CarteCadeauService $carteCadeauService,
  41.         private string             $payplugSecretKey,
  42.         private TokenService       $tokenService,
  43.         private PayplugService      $payplugService,
  44.         private PaymentService      $paymentService,
  45.         private EntityManagerInterface $entityManager,
  46.         private ExtraReservationService $extraReservationService,
  47.     )
  48.     {}
  49.     /**
  50.      * @deprecated message="Gardé pour les utilisateurs ayant des anciens liens, et est remplacé par la route create_card"
  51.      */
  52.     #[Route(path'/offrir/carte'name'create-card')]
  53.     public function oldDisplayCard(Request $requestTokenService $tokenService)
  54.     {
  55.         $token $tokenService->findOneByValue(['value' => $request->query->get('token')]);
  56.         if (!$token) {
  57.             return new JsonResponse("Carte non disponible"404);
  58.         } else {
  59.             return $this->redirectToRoute('carte_cadeau_pdf', ['value' => $token->getValue()]);
  60.         }
  61.     }
  62.     #[Route(path'/offrir'name'carte_cadeau_tunnel')]
  63.     public function tunnel(ExtraReservationService $extraReservationService)
  64.     {
  65.         $token = new Token();
  66.         $extraReservationService->generateExtraReservations($tokentrue);
  67.         $form $this->createForm(CarteCadeauType::class, $token);
  68.         return $this->renderForm('carteCadeau/tunnel.html.twig', [
  69.             'templates' => $this->carteCadeauService->getAllTemplate(),
  70.             'form' => $form,
  71.         ]);
  72.     }
  73.     #[Route(path'/carte-cadeau/paiement'name'carte_cadeau_paiement')]
  74.     public function createPayment(
  75.         Request $request,
  76.         TokenService $tokenService,
  77.         PayplugService $payplugService,
  78.         MetaConversionsApiService $metaConversionsApi
  79.     ) {
  80.         $token = new Token;
  81.         $this->extraReservationService->generateExtraReservations($tokentrue);
  82.         $form $this->createForm(CarteCadeauType::class, $token);
  83.         $form->handleRequest($request);
  84.         $deliveryAddress $token->getDeliveryAddress();
  85.         if ($deliveryAddress && $deliveryAddress->getUseAdresseFacturation()) {
  86.             $contact $token->getContact();
  87.             $deliveryAddress->setPrenom($contact->getPrenom());
  88.             $deliveryAddress->setNom($contact->getNom());
  89.             $deliveryAddress->setAdresse($contact->getAdresse());
  90.             $deliveryAddress->setVille($contact->getVille());
  91.             $deliveryAddress->setCodePostal($contact->getCodePostal());
  92.             $deliveryAddress->setPays('FRANCE');
  93.         }
  94.         $tokenService->save($token);
  95.         // Send InitiateCheckout event to Meta Conversions API (server-side)
  96.         if ($metaConversionsApi->isConfigured()) {
  97.             $contact $token->getContact();
  98.             $totalPrice $tokenService->computePrice($token);
  99.             $externalId 'contact_' $contact->getId();
  100.             $contentId 'gift_card_' $token->getId();
  101.             $eventId $metaConversionsApi->generateEventId();
  102.             $metaConversionsApi->sendInitiateCheckoutEvent(
  103.                 eventId$eventId,
  104.                 value$totalPrice,
  105.                 email$contact->getEmail(),
  106.                 phone$contact->getTelephone(),
  107.                 firstName$contact->getPrenom(),
  108.                 lastName$contact->getNom(),
  109.                 clientIpAddress$request->getClientIp(),
  110.                 clientUserAgent$request->headers->get('User-Agent'),
  111.                 fbc$request->cookies->get('_fbc'),
  112.                 fbp$request->cookies->get('_fbp'),
  113.                 eventSourceUrl$request->headers->get('referer'),
  114.                 externalId$externalId,
  115.                 contentId$contentId,
  116.                 city$contact->getVille(),
  117.                 zipCode$contact->getCodePostal()
  118.             );
  119.         }
  120.         $payment $payplugService->generatePayment($token'carteCadeau');
  121.         return new JsonResponse($payment->hosted_payment->payment_url);
  122.     }
  123.     #[Route(path: ['/carte-cadeau/{value}''/offrir/carte/{value}'], name'carte_cadeau_pdf')]
  124.     public function createCard(Token $tokenCarteCadeauService $carteCadeauServiceCodeService $codeServiceTokenService $tokenService)
  125.     {
  126.         $code $codeService->findOneByValue($token->getCodePromo()->getValue());
  127.         if (!$code) {
  128.             return new JsonResponse("Carte non disponible"404);
  129.         }
  130.         if( $tokenService->isAlreadyPaid($token) || $token->getContact()->getAdresse() == "admin" ) {
  131.             $pdf $carteCadeauService->generatePdfCarteCadeau($code$token);
  132.             $pdfContent $pdf->Output('S''generated.pdf'); // 'S' returns the PDF as a string
  133.             return new Response($pdfContent200, [
  134.                 'Content-Type' => 'application/pdf',
  135.                 'Content-Disposition' => 'inline; filename="generated.pdf"',
  136.             ]);
  137.         } else {
  138.             return new JsonResponse("Cette carte cadeau n a pas ete completement regle, veuillez contacter notre equipe en indiquant le code suivant : TID"$token->getId() , 403);
  139.         }
  140.     }
  141.     #[Route(path'/carte-cadeau/extend-validity/{id}'name'carte_cadeau_extend_validity')]
  142.     public function askCode($idRequest $request): Response
  143.     {
  144.         $token  $this->carteCadeauService->findById($id);
  145.         $code   $token->getCodePromo();
  146.         $error  "";
  147.         if (!$token || !$code) {
  148.             return $this->render('reservation/tunnel/ask-code.html.twig', [
  149.                 "error" => "Erreur lors de la récupération de votre code"
  150.             ]);
  151.         }
  152.         if (!$code->isExpired()) {
  153.             return $this->redirect($this->generateUrl('reservation_carte_cadeau_tunnel', [], UrlGeneratorInterface::ABSOLUTE_URL). "?code=".$code->getValue());
  154.         }
  155.         $payment        $this->paymentService->find($request->query->get('payment'));
  156.         $months         $request->query->get('months');
  157.         $notification   $request->query->get('notification');
  158.         if ($payment && $months && !$payment->getDatePayment()) {
  159.             if (!$this->payplugService->isPaid($payment)) {
  160.                 $error "Le paiement n'a pas été validé, veuillez réessayer";
  161.             } else {
  162.                 $payment->setDatePayment(new \DateTime());
  163.                 $newValidity = clone $code->getValidity();
  164.                 $code->setValidity($newValidity->modify('+'.$months.' months'));
  165.                 if ($notification == "true") {
  166.                     $payplugNotification = new Notification();
  167.                     $payplugNotification->setToken($token->getId());
  168.                     $payplugNotification->setType('Prolongation carte cadeau');
  169.                     $payplugNotification->setIsPaid(true);
  170.                     $payplugNotification->setInfo('Carte cadeau prolongé via notification');
  171.                     $payplugNotification->setError(false);
  172.                     $this->entityManager->persist($payplugNotification);
  173.                 }
  174.                 $this->entityManager->persist($payment);
  175.                 $this->entityManager->persist($code);
  176.                 $this->entityManager->flush();
  177.                 $this->addFlash('success'"Carte cadeau prolongée jusqu'au "$code->getValidityFormated());
  178.                 return $this->redirect($this->generateUrl('reservation_carte_cadeau_tunnel', [], UrlGeneratorInterface::ABSOLUTE_URL). "?code=".$code->getValue());
  179.             }
  180.        }
  181.         $form $this->createFormBuilder()
  182.             ->add('months'ChoiceType::class, [
  183.                 'block_prefix' => 'custom_choice',
  184.                 'label' => 'Mois',
  185.                 'expanded' => true,
  186.                 'multiple' => false,
  187.                 'required' => true,
  188.                 'attr' => [
  189.                     'render_mode' => 'widget-only'
  190.                 ],
  191.                 'choices' => [
  192.                     '3 mois' => 3,
  193.                     '6 mois' => 6,
  194.                 ],
  195.                 'choice_attr' => [
  196.                     '3 mois' => [
  197.                         'label' => "3 mois",
  198.                         'description' => "Expire le "$code->getValidityFormated("+3 month"),
  199.                         'price' =>  $this->carteCadeauService->computeExtendValidityPrice(
  200.                                 $this->tokenService->computePrice($token),
  201.                                 3
  202.                             ). "€"
  203.                     ],
  204.                     '6 mois' => [
  205.                         'label' => "6 mois",
  206.                         'description' => "Expire le "$code->getValidityFormated("+6 month"),
  207.                         'price' =>  $this->carteCadeauService->computeExtendValidityPrice(
  208.                                 $this->tokenService->computePrice($token),
  209.                                 6
  210.                             ). "€"
  211.                     ]
  212.                 ],
  213.             ])
  214.             ->add('contact'ContactPaymentType::class, [
  215.                 'block_prefix' => 'contact',
  216.                 'block_name' => 'contact',
  217.                 'label' => false,
  218.             ])
  219.             ->setAction($this->generateUrl('carte_cadeau_extend_validity', ['id' => $token->getId()]))
  220.             ->getForm();
  221.         $form->handleRequest($request);
  222.         if ($form->isSubmitted() && $form->isValid()) {
  223.             $months $form->get('months')->getData();
  224.             $contact $form->get('contact')->getData();
  225.             $price $this->carteCadeauService->computeExtendValidityPrice($this->tokenService->computePrice($token), $months);
  226.             $payplugPayment = new Payplug();
  227.             $payplugPayment->setToken($token);
  228.             $payplugPayment->setcontact($contact);
  229.             $payplugPayment->setAmount($price);
  230.             $this->paymentService->save($payplugPayment);
  231.             $returnUrl $this->generateUrl('carte_cadeau_extend_validity', ['id' => $token->getId()], UrlGeneratorInterface::ABSOLUTE_URL).
  232.                 "?payment="$payplugPayment->getId().
  233.                 "&months="$months;
  234.             $notificationUrl $returnUrl "&notification=true";
  235.             $payment $this->payplugService->generateCustomPayment($payplugPayment$contact$returnUrl$notificationUrl$price);
  236.             return $this->render('reservation/tunnel/extend-code.html.twig', [
  237.                 'paymentUrl' => $payment->hosted_payment->payment_url,
  238.                 'description' => 'Celle-ci est expiré depuis le '$code->getValidityFormated(),
  239.                 'form' => $form->createView(),
  240.             ]);
  241.         }
  242.         return $this->render('reservation/tunnel/extend-code.html.twig', [
  243.             'error' => $error,
  244.             'description' => 'Celle-ci est expiré depuis le '$code->getValidityFormated(),
  245.             'form' => $form->createView(),
  246.         ]);
  247.     }
  248. }