<?php
namespace App\Controller\Api;
use App\Entity\Membership;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use App\Helper\StripeHelper;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\Entity\Customer;
class MembershipController extends ApiController
{
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/membership/list", name="api_membership_list_get", methods={"POST"})
*/
public function listMembership(Request $request)
{
$prefixFile = $this->getParameter("app.path.membership_images");
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
/** @var Membership $membership */
$memberships = [];
foreach ($this->em->getRepository(Membership::class)->findAll() as $membership){
$memberships[] = $membership->__toArray($baseurl . $prefixFile . "/");
}
return $this->response($memberships);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/membership/create_checkout", name="api_membership_create_checkout", methods={"POST"})
*/
public function createCheckout(Request $request)
{
$stripe = new StripeHelper();
$customer = $this->getUser();
$result = $stripe->createSessionMonthlyTest($customer);
return $this->json($result);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/membership/create_subscription", name="api_membership_create_subscription", methods={"POST"})
*/
public function createSubscription(Request $request)
{
$stripe = new StripeHelper();
$customer = $this->getUser();
$req = $this->transformJsonBody($request);
$paymentId = $req->get('payment_id');
$period = $req->get('period');
$result = [];
if($period === 'monthly'){
$result = $stripe->createMonthlySubscriptionTest($customer,$paymentId);
}else if($period === 'yearly'){
$result = $stripe->createYearlySubscriptionTest($customer,$paymentId);
}
return $this->json($result);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/membership/create_subscription_intent", name="api_membership_create_subscription_intent", methods={"POST"})
*/
public function createSubscriptionIntent(Request $request)
{
$stripe = new StripeHelper();
$customer = $this->getUser();
$req = $this->transformJsonBody($request);
$period = $req->get('period');
$result = [];
if($period === 'monthly'){
$result = $stripe->createMonthlySubscriptionIntentTest($customer);
}else if($period === 'yearly'){
$result = $stripe->createYearlySubscriptionIntentTest($customer);
}
return $this->json($result);
}
}