src/Controller/UserController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use App\Middleware\UserMiddleware;
  10. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  11. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. class UserController extends AbstractController
  14. {
  15.   private $session;
  16.   private $callbackurl;
  17.   protected $container;
  18.   public function __construct(
  19.     SessionInterface $session,
  20.     ContainerInterface $container
  21.   ) {
  22.     $this->session $session;
  23.     $this->container $container;
  24.     $this->callbackurl $_SERVER['REQUEST_SCHEME'] . '://' $_SERVER['HTTP_HOST'];
  25.   }
  26.   /**
  27.    * @Route("/", name="index", methods={"GET"}, priority=1)
  28.    */
  29.   public function index(Request $request)
  30.   {
  31.     if ($this->session->get('user_id')) {
  32.       return new RedirectResponse($this->generateUrl('orders_list'));
  33.     }
  34.     return $this->render('link/signin.html.twig');
  35.   }
  36.   /**
  37.    * @Route("/expired", name="expired", methods={"GET"}, priority=1)
  38.    */
  39.   public function expired(Request $requestUserMiddleware $userMiddleware)
  40.   {
  41.     if (!$this->session->get('user_id')) {
  42.       return new RedirectResponse($this->generateUrl('orders_list'));
  43.     }
  44.     return $this->render('upgrade/expired.html.twig');
  45.   }
  46.   /**
  47.    * @Route("/upgrade", name="upgrade", methods={"GET"}, priority=1)
  48.    */
  49.   public function upgrade(Request $requestUserMiddleware $userMiddleware)
  50.   {
  51.     if (!$this->session->get('user_id')) {
  52.       return new RedirectResponse($this->generateUrl('orders_list'));
  53.     }
  54.     $state bin2hex(random_bytes(16)); // Random string for CSRF protection
  55.     $parameter = array(
  56.       'apiKey' => $this->getParameter('SHOPIFY_API_KEY'),
  57.       'secret' => $this->getParameter('SHOPIFY_SECRET'),
  58.       'store' => $this->session->get("store"),
  59.       'userId' => $this->session->get("user_id"),
  60.       'accessToken' => $this->session->get("access_token"),
  61.       'callbackUrl' => $this->callbackurl "/shopify/upgrade/callback?state=" $state,
  62.       'test' => $this->getParameter('TEST_PAYMENT'),
  63.       'state' => $state,
  64.     );
  65.     $userMiddleware->upgrade($parameter);
  66.   }
  67.   /**
  68.    * @Route("/shopify/upgrade/callback", name="upgrade_callback", methods={"GET"}, priority=1)
  69.    */
  70.   public function shopifyUpgradeCallback(Request $requestUserMiddleware $userMiddleware): RedirectResponse
  71.   {
  72.     $state $request->get('state');
  73.     $chargeId $request->get('charge_id');
  74.     if (!$chargeId || !$state) {
  75.       throw new NotFoundHttpException(constant("App\\Constant\\Globals::PARAMS_MISSING"));
  76.     }
  77.     if ($request->get('state') !== $this->session->get('state')) {
  78.       throw new NotFoundHttpException(constant("App\\Constant\\Globals::ERR_CSRF_TOKEN"));
  79.     }
  80.     $parameter = array(
  81.       'shop' => $this->session->get("store"),
  82.       'chargeId' => $chargeId,
  83.       'apiKey' => $this->getParameter('SHOPIFY_API_KEY'),
  84.       'secret' => $this->getParameter('SHOPIFY_SECRET'),
  85.       'userId' => $this->session->get("user_id"),
  86.       'accessToken' => $this->session->get("access_token")
  87.     );
  88.     $userMiddleware->checkSubscriptionStatus($parameter);
  89.     return new RedirectResponse($this->generateUrl('orders_list'));
  90.   }
  91. }