<?php
namespace App\Controller;
use Exception;
use App\Entity\Order;
use App\Entity\Package;
use App\Models\InfoClient;
use App\Dto\PaymentModeDTO;
use App\Form\InfoClientType;
use App\Service\MailService;
use App\Service\OrderService;
use App\Service\StripeService;
use App\Service\CatalogueService;
use App\Exception\CustomException;
use App\Repository\PackageRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class CheckoutController extends AbstractController
{
public function __construct(
private CatalogueService $catalogueService,
// private StripeService $stripeService,
// private OrderService $orderService,
// private PackageRepository $packageRepository,
// private EntityManagerInterface $em,
// private MailService $mailService
private SessionInterface $session
)
{}
#[Route(path: '/package/{id}/payment', name: 'app_our_catalogues_payment')]
public function client_package_payment($id, Request $request): Response
{
$paymentInfo = $this->catalogueService->getPaymentInfo();
$package = $this->catalogueService->findPackage($id);
$packagePaymentModes = $package['tempPaymentModes'];
$choicePaymentModes = array_map(fn($item) => new PaymentModeDTO($item['id'], $item['name'], $item['sectionRef']), $packagePaymentModes);
if($package==null) throw new \Exception("Le package n'existe pas.");
$infoClient = new InfoClient();
$infoClient->setReferenceVente(trim($this->session->get('ref', '')));
$form = $this->createForm(InfoClientType::class, $infoClient);
$form
->add('paymentMethod', HiddenType::class, [
'mapped' => false,
'required' => false
])
->add('paymentIntent', HiddenType::class, [
'mapped' => false,
'required' => false
])
->add('subscriptionId', HiddenType::class, [
'mapped' => false,
'required' => false
])
->add('free', HiddenType::class, [
'mapped' => false,
'required' => false,
])
->add('codePromo', TextType::class, [
'mapped' => false,
'required' => false,
'trim' => true,
])
->add('checkNumber', TextType::class, [
'mapped' => false,
"label" => "Numero chèque",
'required' => false,
'trim' => true,
])
->add('refTransfer', TextType::class, [
'mapped' => false,
"label" => "Référence du virement",
'required' => false,
'trim' => true,
])
->add('paymentMode', ChoiceType::class, [
'mapped' => false,
"label" => false,
'choices' => $choicePaymentModes,
'choice_label' => fn($choice) => $choice->name,
'choice_value' => fn($choice) => $choice ? (string)$choice->id : '',
'choice_attr' => fn($choice) => [
'data-section' => $choice->sectionRef,
'class' => 'payment-radio',
],
'data' => $choicePaymentModes[0] ?? null,
"constraints" => [
new NotBlank(["message" => "Mode de payement obligatoire"])
],
'expanded' => true,
'multiple' => false,
])
;
$withTypeContrat = count($package['packagePriceByTypeContrats']) > 0;
if($withTypeContrat){
$typeContrats = $this->catalogueService->getTypeContrats();
$typeContratId = $request->get('typeContratId', $typeContrats[0]['id']);
$typeContratChoices = [];
for($i=0; $i<count($typeContrats);$i++){
$typeContratChoices[$typeContrats[$i]['label']] = $typeContrats[$i]['id'];
}
$form->add('typeContrat', ChoiceType::class, [
'mapped' => false,
"label" => "Durée du contrat",
'choices' => $typeContratChoices,
'required' => true,
'data' => $typeContratId
]);
}
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$subscriptionId = $request->request->get('info_client')['subscriptionId'];
$paymentIntentId_req = $request->request->get('info_client')['paymentIntent'];
$free = $request->request->get('info_client')['free'];
$typeContratId = null;
if($withTypeContrat) $typeContratId = $request->request->get('info_client')['typeContrat'];
$data = [
'infoClient' => $infoClient,
'subscriptionId' => $subscriptionId,
'paymentIntentId_req' => $paymentIntentId_req,
'free' => $free,
'typeContratId' => $typeContratId,
'codePromo' => $request->request->get('info_client')['codePromo'] ,
'ipClient' => $request->getClientIp(),
'paymentMode' => $form->get('paymentMode')->getData()->getId(),
'refTransfer' => $form->get('refTransfer')->getData(),
'checkNumber' => $form->get('checkNumber')->getData(),
];
$this->catalogueService->saveOrder($package['id'], $data);
return $this->redirectToRoute('app_thank_you');
} catch (CustomException $ex) {
//throw ($ex);
$this->addFlash('danger', $ex->getMessage());
}catch (Exception $e) {
$this->addFlash('danger', $_ENV['CUSTOM_ERROR_MESSAGE']);
}
}
return $this->render('catalogues/checkout.html.twig',[
'stripe_public_key' => $this->getParameter('stripe_public_key'),
// 'intentSecretData' => $this->catalogueService->getPackageIntentSecret($id),
'form' => $form->createView(),
'package' => $package,
'TTC' => $package['ttc'],
'withTypeContrat' => $withTypeContrat,
'paymentInfo' => $paymentInfo
]);
}
#[Route('/remerciement', name: 'app_thank_you')]
public function thank_you()
{
return $this->render('thank_you/thank-you.html.twig', []);
}
#[Route(path: '/api/check-code-promo', name: 'client_api_check_code_promo')]
public function client_api_check_code_promo(Request $request): Response
{
try{
$code = $request->get('code', '');
$ipAddress = $request->getClientIp();
return $this->json([
"status"=>true,
"data"=> $this->catalogueService->checkCodePromoValidity($code,$ipAddress)
], 200);
}
catch(\Exception $ex){
return $this->json([
"status"=>false,
"message"=>$ex->getMessage()
], 500);
}
}
}