<?php
namespace App\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use App\Middleware\UserMiddleware;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UserController extends AbstractController
{
private $session;
private $callbackurl;
protected $container;
public function __construct(
SessionInterface $session,
ContainerInterface $container
) {
$this->session = $session;
$this->container = $container;
$this->callbackurl = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'];
}
/**
* @Route("/", name="index", methods={"GET"}, priority=1)
*/
public function index(Request $request)
{
if ($this->session->get('user_id')) {
return new RedirectResponse($this->generateUrl('orders_list'));
}
return $this->render('link/signin.html.twig');
}
/**
* @Route("/expired", name="expired", methods={"GET"}, priority=1)
*/
public function expired(Request $request, UserMiddleware $userMiddleware)
{
if (!$this->session->get('user_id')) {
return new RedirectResponse($this->generateUrl('orders_list'));
}
return $this->render('upgrade/expired.html.twig');
}
/**
* @Route("/upgrade", name="upgrade", methods={"GET"}, priority=1)
*/
public function upgrade(Request $request, UserMiddleware $userMiddleware)
{
if (!$this->session->get('user_id')) {
return new RedirectResponse($this->generateUrl('orders_list'));
}
$state = bin2hex(random_bytes(16)); // Random string for CSRF protection
$parameter = array(
'apiKey' => $this->getParameter('SHOPIFY_API_KEY'),
'secret' => $this->getParameter('SHOPIFY_SECRET'),
'store' => $this->session->get("store"),
'userId' => $this->session->get("user_id"),
'accessToken' => $this->session->get("access_token"),
'callbackUrl' => $this->callbackurl . "/shopify/upgrade/callback?state=" . $state,
'test' => $this->getParameter('TEST_PAYMENT'),
'state' => $state,
);
$userMiddleware->upgrade($parameter);
}
/**
* @Route("/shopify/upgrade/callback", name="upgrade_callback", methods={"GET"}, priority=1)
*/
public function shopifyUpgradeCallback(Request $request, UserMiddleware $userMiddleware): RedirectResponse
{
$state = $request->get('state');
$chargeId = $request->get('charge_id');
if (!$chargeId || !$state) {
throw new NotFoundHttpException(constant("App\\Constant\\Globals::PARAMS_MISSING"));
}
if ($request->get('state') !== $this->session->get('state')) {
throw new NotFoundHttpException(constant("App\\Constant\\Globals::ERR_CSRF_TOKEN"));
}
$parameter = array(
'shop' => $this->session->get("store"),
'chargeId' => $chargeId,
'apiKey' => $this->getParameter('SHOPIFY_API_KEY'),
'secret' => $this->getParameter('SHOPIFY_SECRET'),
'userId' => $this->session->get("user_id"),
'accessToken' => $this->session->get("access_token")
);
$userMiddleware->checkSubscriptionStatus($parameter);
return new RedirectResponse($this->generateUrl('orders_list'));
}
}