src/Controller/CheckoutController.php line 180

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Exception;
  4. use App\Entity\Order;
  5. use App\Entity\Package;
  6. use App\Models\InfoClient;
  7. use App\Dto\PaymentModeDTO;
  8. use App\Form\InfoClientType;
  9. use App\Service\MailService;
  10. use App\Service\OrderService;
  11. use App\Service\StripeService;
  12. use App\Service\CatalogueService;
  13. use App\Exception\CustomException;
  14. use App\Repository\PackageRepository;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Component\Form\FormFactoryInterface;
  20. use Symfony\Component\Validator\Constraints\NotBlank;
  21. use Symfony\Component\Form\Extension\Core\Type\TextType;
  22. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  23. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  24. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  25. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  26. class CheckoutController extends AbstractController
  27. {
  28.     public function __construct(
  29.         private CatalogueService $catalogueService,
  30.         // private StripeService $stripeService,
  31.         // private OrderService $orderService,
  32.         // private PackageRepository $packageRepository,
  33.         // private EntityManagerInterface $em,
  34.         // private MailService $mailService
  35.         private SessionInterface $session
  36.     )
  37.     {}
  38.     #[Route(path'/package/{id}/payment'name'app_our_catalogues_payment')]
  39.     public function client_package_payment($idRequest $request): Response
  40.     {
  41.         $paymentInfo $this->catalogueService->getPaymentInfo();
  42.         $package $this->catalogueService->findPackage($id);
  43.         $packagePaymentModes $package['tempPaymentModes'];
  44.         $choicePaymentModes array_map(fn($item) => new PaymentModeDTO($item['id'], $item['name'], $item['sectionRef']), $packagePaymentModes);
  45.         if($package==null) throw new \Exception("Le package n'existe pas.");
  46.         $infoClient = new InfoClient();
  47.         $infoClient->setReferenceVente(trim($this->session->get('ref''')));
  48.         $form $this->createForm(InfoClientType::class, $infoClient);
  49.         $form
  50.             ->add('paymentMethod'HiddenType::class, [
  51.                 'mapped' => false,
  52.                 'required' => false
  53.             ])
  54.             ->add('paymentIntent'HiddenType::class, [
  55.                 'mapped' => false,
  56.                 'required' => false
  57.             ])
  58.             ->add('subscriptionId'HiddenType::class, [
  59.                 'mapped' => false,
  60.                 'required' => false
  61.             ])
  62.             ->add('free'HiddenType::class, [
  63.                 'mapped' => false,
  64.                 'required' => false,
  65.             ])
  66.             ->add('codePromo'TextType::class, [
  67.                 'mapped' => false,
  68.                 'required' => false,
  69.                 'trim' => true,
  70.             ])
  71.              ->add('checkNumber'TextType::class, [
  72.                 'mapped' => false,
  73.                 "label" => "Numero chèque",
  74.                 'required' => false,
  75.                 'trim' => true,
  76.             ])
  77.              ->add('refTransfer'TextType::class, [
  78.                 'mapped' => false,
  79.                 "label" => "Référence du virement",
  80.                 'required' => false,
  81.                 'trim' => true,
  82.             ])
  83.             ->add('paymentMode'ChoiceType::class, [
  84.                 'mapped' => false,
  85.                 "label" => false,
  86.                 'choices' => $choicePaymentModes,
  87.                 'choice_label' => fn($choice) => $choice->name,
  88.                 'choice_value' => fn($choice) => $choice ? (string)$choice->id '',
  89.                 'choice_attr' => fn($choice) => [
  90.                     'data-section' => $choice->sectionRef,
  91.                     'class' => 'payment-radio',
  92.                 ],
  93.                 'data' => $choicePaymentModes[0] ?? null
  94.                 "constraints" => [
  95.                     new NotBlank(["message" => "Mode de payement obligatoire"])
  96.                 ],
  97.                 'expanded' => true
  98.                 'multiple' => false,    
  99.             ])
  100.             
  101.             
  102.         ;
  103.         $withTypeContrat count($package['packagePriceByTypeContrats']) > 0;
  104.         if($withTypeContrat){
  105.             
  106.             $typeContrats $this->catalogueService->getTypeContrats();
  107.             $typeContratId $request->get('typeContratId'$typeContrats[0]['id']);
  108.             $typeContratChoices = [];
  109.             for($i=0$i<count($typeContrats);$i++){
  110.                 $typeContratChoices[$typeContrats[$i]['label']] =  $typeContrats[$i]['id'];
  111.             }
  112.             $form->add('typeContrat'ChoiceType::class, [
  113.                 'mapped' => false,
  114.                 "label" => "Durée du contrat",
  115.                 'choices' => $typeContratChoices,
  116.                 'required' => true,
  117.                 'data' => $typeContratId
  118.             ]);
  119.         }
  120.         $form->handleRequest($request);
  121.         if ($form->isSubmitted() && $form->isValid()) {
  122.             try {
  123.                 $subscriptionId $request->request->get('info_client')['subscriptionId'];
  124.                 $paymentIntentId_req $request->request->get('info_client')['paymentIntent'];
  125.                 $free $request->request->get('info_client')['free'];
  126.                 $typeContratId null;
  127.                 if($withTypeContrat$typeContratId $request->request->get('info_client')['typeContrat'];
  128.                 $data = [
  129.                     'infoClient' => $infoClient
  130.                     'subscriptionId' => $subscriptionId,
  131.                     'paymentIntentId_req' => $paymentIntentId_req,
  132.                     'free' => $free
  133.                     'typeContratId' => $typeContratId,
  134.                     'codePromo' => $request->request->get('info_client')['codePromo'] ,
  135.                     'ipClient' =>  $request->getClientIp(),
  136.                     'paymentMode' => $form->get('paymentMode')->getData()->getId(),
  137.                     'refTransfer' => $form->get('refTransfer')->getData(),
  138.                     'checkNumber' => $form->get('checkNumber')->getData(),
  139.                 ];
  140.                 $this->catalogueService->saveOrder($package['id'], $data);
  141.                 return $this->redirectToRoute('app_thank_you');
  142.             } catch (CustomException $ex) {
  143.                 //throw ($ex);
  144.                  $this->addFlash('danger'$ex->getMessage());
  145.             }catch (Exception $e) {
  146.                 $this->addFlash('danger'$_ENV['CUSTOM_ERROR_MESSAGE']);
  147.             }
  148.         }
  149.         return $this->render('catalogues/checkout.html.twig',[
  150.             'stripe_public_key' => $this->getParameter('stripe_public_key'),
  151.             // 'intentSecretData' => $this->catalogueService->getPackageIntentSecret($id),
  152.             'form' => $form->createView(),
  153.             'package' => $package,
  154.             'TTC' => $package['ttc'],
  155.             'withTypeContrat' => $withTypeContrat,
  156.             'paymentInfo' => $paymentInfo
  157.         ]);
  158.         
  159.     }
  160.     #[Route('/remerciement'name'app_thank_you')]
  161.     public function thank_you()
  162.     {
  163.         return $this->render('thank_you/thank-you.html.twig', []);
  164.     }
  165.     #[Route(path'/api/check-code-promo'name'client_api_check_code_promo')]
  166.     public function client_api_check_code_promo(Request $request): Response
  167.     {
  168.         try{
  169.             $code $request->get('code''');
  170.             $ipAddress $request->getClientIp();
  171.             return $this->json([
  172.                 "status"=>true,
  173.                 "data"=> $this->catalogueService->checkCodePromoValidity($code,$ipAddress)
  174.             ], 200);
  175.         }
  176.         catch(\Exception $ex){
  177.             return $this->json([
  178.                 "status"=>false,
  179.                 "message"=>$ex->getMessage()
  180.             ], 500);
  181.         }
  182.     }
  183.     
  184. }