src/Controller/FrontendCompetitionController.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  8. use Symfony\Component\Form\FormError;
  9. use Stripe\Stripe;
  10. use Stripe\PaymentIntent;
  11. use App\Form\Type\ParticipantFormType;
  12. use App\Entity\Competition;
  13. use App\Entity\Participant;
  14. class FrontendCompetitionController extends AbstractController {
  15.     /**
  16.      * @Route("/competition/{slug}", name="competition")
  17.      */
  18.     public function single(EntityManagerInterface $emRequest $request$slug) {
  19.         $competition $em->getRepository(Competition::class)->findOneBy(['slug' => $slug]);
  20.         if (null === $competition) {
  21.             return $this->redirectToRoute('events');
  22.         }
  23.         $participant = new Participant();
  24.         $form $this->createForm(ParticipantFormType::class, $participant, ['minimum_down_payment' => $competition->getMinimumDownPayment()]);
  25.         $form->handleRequest($request);
  26.         if ($form->isSubmitted() && $form->isValid()) {
  27.             $recaptchaResponse $request->get('g-recaptcha-response');
  28.             if ($this->captchaVerify($recaptchaResponse) == true) {
  29.                 $participant->setCompetition($competition);
  30.                 // save changes to db
  31.                 $em->persist($participant);
  32.                 $em->flush();
  33.                 // redirect to listing page
  34.                 return $this->redirectToRoute('competition_payment', ['slug' => $slug'hash' => $participant->getHash()]);
  35.             } else {
  36.                 $form->get('downPaymentAmount')->addError(new FormError('Invalid reCaptcha'));
  37.             }
  38.         }
  39.         return $this->render('frontend/competition/single.html.twig', [
  40.                     'competition' => $competition,
  41.                     'form' => $form->createView()
  42.         ]);
  43.     }
  44.     /**
  45.      * @Route("/competition/{slug}/payment/{hash}", name="competition_payment")
  46.      */
  47.     public function payment(EntityManagerInterface $emRequest $request$slug$hash) {
  48.         $competition $em->getRepository(Competition::class)->findOneBy(['slug' => $slug]);
  49.         if (null === $competition) {
  50.             return $this->redirectToRoute('events');
  51.         }
  52.         $participant $em->getRepository(Participant::class)->findOneBy(['hash' => $hash]);
  53.         if (null === $participant) {
  54.             return $this->redirectToRoute('competition', ['slug' => $slug]);
  55.         }
  56.         if (!$participant->isPaymentPending()) {
  57.             return $this->redirectToRoute('competition', ['slug' => $slug]);
  58.         }
  59.         // Stripe config
  60.         Stripe::setApiKey($this->getParameter('stripe_secret_key'));
  61.         // send payment intent request
  62.         try {
  63.             $amount $participant->getDownPaymentAmount();
  64.             $intent PaymentIntent::create([
  65.                         'amount' => floatval($amount 100),
  66.                         'currency' => 'USD',
  67.                         'payment_method_types' => ['card'],
  68.                         'metadata' => [
  69.                             'payment_hash' => $hash
  70.                         ]
  71.             ]);
  72.         } catch (\Exception $e) {
  73.             $this->get('session')->getFlashBag()->set('danger'$e->getMessage());
  74.             return $this->redirectToRoute('competition', ['slug' => $slug]);
  75.         }
  76.         return $this->render('frontend/competition/payment.html.twig', [
  77.                     'competition' => $competition,
  78.                     'participant' => $participant,
  79.                     'client_secret' => $intent->client_secret,
  80.                     'publishable_key' => $this->getParameter('stripe_publishable_key'),
  81.                     'amount' => $amount,
  82.                     'home' => $this->generateUrl('homepage', ['registered' => 1], UrlGeneratorInterface::ABSOLUTE_URL)
  83.         ]);
  84.     }
  85.     /**
  86.      * Validate reCaptcha
  87.      *
  88.      * @param $recaptcha
  89.      * @return mixed
  90.      */
  91.     private function captchaVerify($recaptcha) {
  92.         $url "https://www.google.com/recaptcha/api/siteverify";
  93.         $ch curl_init();
  94.         curl_setopt($chCURLOPT_URL$url);
  95.         curl_setopt($chCURLOPT_HEADER0);
  96.         curl_setopt($chCURLOPT_RETURNTRANSFERTRUE);
  97.         curl_setopt($chCURLOPT_POSTtrue);
  98.         curl_setopt($chCURLOPT_POSTFIELDS, array(
  99.             "secret" => $this->getParameter('recaptcha_secret_key'),
  100.             "response" => $recaptcha
  101.                 )
  102.         );
  103.         $response curl_exec($ch);
  104.         curl_close($ch);
  105.         $data json_decode($response);
  106.         if (JSON_ERROR_NONE !== json_last_error()) {
  107.             return false;
  108.         }
  109.         return $data->success;
  110.     }
  111. }