src/Controller/PlanController.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Components\PaymentStatus;
  4. use App\Entity\Account;
  5. use App\Entity\Payment;
  6. use App\Entity\Plan;
  7. use App\Paypal\PaypalHandler;
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\Exception\GuzzleException;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class PlanController extends AbstractController
  16. {
  17.     private $paypalHandler;
  18.     /**
  19.      * PlanController constructor.
  20.      * @param $paypalHandler
  21.      */
  22.     public function __construct(PaypalHandler $paypalHandler)
  23.     {
  24.         $this->paypalHandler $paypalHandler;
  25.     }
  26.     /**
  27.      * @return Response
  28.      *
  29.      * @Route("/plans", name="plans_redirect")
  30.      */
  31.     public function redirectPlans(): Response
  32.     {
  33.         return $this->redirectToRoute("pricing", [], Response::HTTP_MOVED_PERMANENTLY);
  34.     }
  35.     /**
  36.      * @Route("/pricing", name="pricing")
  37.      */
  38.     public function index(): Response
  39.     {
  40.         $plans $this->getDoctrine()->getRepository(Plan::class)->findBy([
  41.             'version' => 2,
  42.             'show' => 1
  43.         ], ['price' => 'DESC']);
  44.         return $this->render('plans/index.html.twig', [
  45.             'plans' => $plans,
  46.         ]);
  47.     }
  48.     /**
  49.      * @return Response
  50.      *
  51.      * @Route("/account/plans", name="account_plans")
  52.      */
  53.     public function accountPlans(): Response
  54.     {
  55.         /** @var Account $user */
  56.         $user $this->getDoctrine()->getRepository(Account::class)->findOneByEmail($this->getUser()->getUsername());
  57.         /** @var Plan[] $plans */
  58.         $plans $this->getDoctrine()->getRepository(Plan::class)->findBy([
  59.             'version' => 2,
  60.             'show' => 1
  61.         ], ['price' => 'DESC']);
  62.         return $this->render('plans/account_plans.html.twig', [
  63.             'plans' => $plans,
  64.             'user' => $user
  65.         ]);
  66.     }
  67.     /**
  68.      * @return Response
  69.      *
  70.      * @Route("/account/plans/cancel", name="account_plans_cancel")
  71.      */
  72.     public function cancelPlan(): Response
  73.     {
  74.         /** @var Account $user */
  75.         $user $this->getDoctrine()->getRepository(Account::class)->findOneByEmail($this->getUser()->getUsername());
  76.         /** @var Payment $payment */
  77.         $payment $this->getDoctrine()->getRepository(Payment::class)->findBy(['account' => $user],
  78.             ['created' => 'DESC'], 1)[0];
  79.         // Get the Paypal Auth Token
  80.         $authToken $this->paypalHandler->getAuthToken();
  81.         if (!$authToken) {
  82.             $this->addFlash("flash_error""Unable to connect to Paypal at this time.");
  83.             return $this->redirectToRoute("account_plans");
  84.         }
  85.         // With the Auth token cancel the plan
  86.         $cancelSubscription $this->paypalHandler->cancelSubscription($payment$authToken);
  87.         if (!$cancelSubscription) {
  88.             $this->addFlash("flash_error""Unable to cancel current plan with Paypal. Please try again.");
  89.             return $this->redirectToRoute("account_plans");
  90.         }
  91.         $plan $this->getDoctrine()->getRepository(Plan::class)->findOneBy([
  92.             'version' => 2,
  93.             'name' => 'Starter'
  94.         ]);
  95.         $user->setPlan($plan);
  96.         $entityManager $this->getDoctrine()->getManager();
  97.         $entityManager->persist($user);
  98.         $entityManager->flush();
  99.         return $this->redirectToRoute("account_plans");
  100.     }
  101.     /**
  102.      * @param Request $request
  103.      * @param Plan $plan
  104.      * @return Response
  105.      *
  106.      * @Route("/account/plans/{plan}", name="account_plans_create")
  107.      */
  108.     public function selectPlan(Request $requestPlan $plan): JsonResponse
  109.     {
  110.         /** @var Account $user */
  111.         $user $this->getDoctrine()->getRepository(Account::class)->findOneByEmail($this->getUser()->getUsername());
  112.         try {
  113.             $payment = new Payment($request->get('subscriptionID'), PaymentStatus::INITIALIZED$user$plan);
  114.             if ($request->get('billingToken')) {
  115.                 $payment->setBillingToken($request->get('billingToken'));
  116.             }
  117.             if ($request->get('orderID')) {
  118.                 $payment->setOrderId($request->get('orderID'));
  119.             }
  120.             $entityManager $this->getDoctrine()->getManager();
  121.             $entityManager->persist($payment);
  122.             // Update to set the new plan
  123.             $user->setPlan($plan);
  124.             $entityManager->persist($user);
  125.             $entityManager->flush();
  126.             $this->addFlash("flash_success""Thank you! Your plan is now active on your account.");
  127.             return new JsonResponse("Successfully stored");
  128.         } catch (\Exception $e) {
  129.             $this->addFlash("flash_error""Unable to activate this plan on your account.");
  130.             return new JsonResponse(
  131.                 sprintf("Unable to complete request: %s"$e->getMessage()),
  132.                 Response::HTTP_INTERNAL_SERVER_ERROR
  133.             );
  134.         }
  135.     }
  136. }