src/Controller/RegisterController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Order;
  4. use App\Entity\Payment;
  5. use App\Entity\User;
  6. use App\Form\User\ActivationFeeType;
  7. use App\Form\User\GetStartedType;
  8. use App\Service\Api\StripeApi;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  14. class RegisterController extends AbstractController
  15. {
  16.     //const ACTIVATION_FEE_PER_RESTAURANT = 1200;
  17.     const ACTIVATION_FEE_PER_RESTAURANT 0;
  18.     #[Route(path'/get-started'name'register'methods: ['POST''GET'])]
  19.     public function index(Request $requestUserPasswordHasherInterface $encoder): Response
  20.     {
  21.         if ($this->getUser()) {
  22.             return $this->redirectToRoute('default');
  23.         }
  24.         $user null;
  25.         if ($request->request->has('get_started')) {
  26.             $r $request->request->get('get_started');
  27.             if (isset($r['email'])) {
  28.                 $user $this->getDoctrine()->getRepository(User::class)
  29.                     ->findOneBy(['email'=>$r['email']]);
  30.                 if ($user && $user->getState()!=User::STATE_PAYMENT) {
  31.                     $user=null;
  32.                 }
  33.             }
  34.         }
  35.         if (!$user) {
  36.             $user = new User();
  37.             $user
  38.                 ->setManager(null)
  39.                 ->setState(User::STATE_PAYMENT);
  40.         }
  41.         $form $this->createForm(GetStartedType::class, $user);
  42.         $form->handleRequest($request);
  43.         if ($form->isSubmitted() && $form->isValid()) {
  44.             $user->setPassword($encoder->hashPassword(
  45.                 $user,
  46.                 sha1($user->getId().random_bytes(5))
  47.             ));
  48.             try {
  49.                 $user->addRole('ROLE_ADMIN_USER');
  50.                 $this->getDoctrine()->getManager()->persist($user);
  51.                 $this->getDoctrine()->getManager()->flush();
  52.                 return $this->redirectToRoute('register_subscription',['id'=>$user->getId()]);
  53.             } catch (\Exception $e) {
  54.                 $this->addFlash('danger','It looks like you already have an account in REMS');
  55.             }
  56.         }
  57.         return $this->render('register/index.html.twig', [
  58.             'controller_name' => self::class,
  59.             'user' => $user,
  60.             'form' => $form->createView(),
  61.         ]);
  62.     }
  63.     #[Route(path'/get-started/subscription/{id}'name'register_subscription'methods: ['POST''GET'])]
  64.     #[Route(path'/get-started/subscription/{id}/{token}'name'register_subscription_retry'methods: ['POST''GET'])]
  65.     public function requestActivationFee(Request $requestUser $userStripeApi $stripeApistring $token=''):Response
  66.     {
  67.         $order null;
  68.         if ($request->attributes->get('_route')=='register_subscription_retry') {
  69.             $order $this->getDoctrine()->getRepository(Order::class)
  70.                 ->findOneBy(['token'=>$token]);
  71.         }
  72.         if (!$order) {
  73.             $order = new Order();
  74.             $order
  75.                 ->setUser($user)
  76.                 ->setState(Order::STATE_CART)
  77.                 ->setAmount($user->getMaxRestaurantsCount() * self::ACTIVATION_FEE_PER_RESTAURANT)
  78.                 ->setMemo('Activation fee. '.$user->getMaxRestaurantsCount().' restaurant(s)');
  79.         }
  80.         if ($order->getState()==Order::STATE_PAID) {
  81.             //
  82.         }
  83.         $form $this->createForm(ActivationFeeType::class, $order);
  84.         $form->handleRequest($request);
  85.         if ($form->isSubmitted() && $form->isValid()) {
  86.             $order->setState(Order::STATE_WAITING);
  87.             $this->getDoctrine()->getManager()->persist($user);
  88.             $this->getDoctrine()->getManager()->flush();
  89.             $stripeApi->initializeClient();
  90.             $stripeApi->createSession($order);
  91.             if ($order->getAmount()==0) {
  92.                 $order->setState(Order::STATE_PAID);
  93.                 $order->setToken('');
  94.                 $order->getUser()->setState(User::STATE_PENDING);
  95.                 $this->getDoctrine()->getManager()->persist($order->getUser());
  96.                 $this->getDoctrine()->getManager()->persist($order);
  97.                 $this->getDoctrine()->getManager()->flush();
  98.                 //ToDo: send notification
  99.                 return $this->redirectToRoute('register-thanks');
  100.             }
  101.             if ($order->getPayments()->last()->getState()!=Payment::SESSION_CREATED) {
  102.                 $this->addFlash('danger''Failed to create payment session. Please try again or contact our support team.');
  103.                 return $this->redirectToRoute('register_subscription_retry', ['id'=>$user->getId(),'token'=>$order->getToken()]);
  104.             }
  105.             return $this->render('register/payment.html.twig', [
  106.                 'order' => $order
  107.             ]);
  108.         }
  109.         return $this->render('register/confirmActivation.html.twig', [
  110.             'controller_name' => self::class,
  111.             'user' => $user,
  112.             'order' => $order,
  113.             'form' => $form->createView()
  114.         ]);
  115.     }
  116.     #[Route(path'/get-started/success/{token}'name'register_payment_success')]
  117.     public function paymentCapture(Request $requeststring $tokenStripeApi $stripeApi): Response
  118.     {
  119.         $order $this->getDoctrine()->getRepository(Order::class)
  120.             ->findOneBy(['token'=>$token]);
  121.         if (!$order) {
  122.             $this->addFlash('danger''Something went wrong. System is not able to locate your purchase order.');
  123.             return $this->redirectToRoute('register');
  124.         }
  125.         $stripeApi->initializeClient();
  126.         $stripeApi->retrieveSession($order);
  127.         if ($order->getState()!=Order::STATE_PAID) {
  128.             $this->addFlash('danger''Payment failed! Unfortunately something went wrong during payment. You haven\'t been charged. Please try again.');
  129.             return $this->redirectToRoute('register_subscription_retry', ['id'=>$order->getUser()->getId()]);
  130.         }
  131.         $order->getUser()->setState(User::STATE_PENDING);
  132.         $order->setToken('');
  133.         $this->getDoctrine()->getManager()->persist($order->getUser());
  134.         $this->getDoctrine()->getManager()->persist($order);
  135.         $this->getDoctrine()->getManager()->flush();
  136.         //ToDo: send notification
  137.         return $this->redirectToRoute('register-thanks');
  138.     }
  139.     #[Route(path'/get-started/cancel/{token}'name'register_payment_cancel')]
  140.     public function paymentCancel(Request $requeststring $token): Response
  141.     {
  142.         $this->addFlash('warning''Payment cancelled! You haven\'t been charged. You can try again payment again.');
  143.         $order $this->getDoctrine()->getRepository(Order::class)
  144.             ->findOneBy(['token'=>$token]);
  145.         if (!$order) {
  146.             $this->addFlash('danger''Something went wrong. System is not able to locate your purchase order.');
  147.             return $this->redirectToRoute('register');
  148.         }
  149.         $order->setState(Order::STATE_CANCELLED);
  150.         $order->getPayments()->last()->setState(Payment::STATE_CANCEL);
  151.         $this->getDoctrine()->getManager()->persist($order);
  152.         $this->getDoctrine()->getManager()->flush();
  153.         return $this->redirectToRoute('register_subscription_retry', ['id'=>$order->getUser()->getId()]);
  154.     }
  155.     #[Route(path'/get-started/thank-you'name'register-thanks')]
  156.     public function thankYou(Request $request): Response
  157.     {
  158.         if ($this->getUser()) {
  159.             return $this->redirectToRoute('default');
  160.         }
  161.         return $this->render('register/thankyou.html.twig', [
  162.             'controller_name' => 'RegisterController',
  163.         ]);
  164.     }
  165. }