<?php
namespace App\Controller\Api;
use App\Entity\Customer;
use App\Entity\CustomerDevice;
use App\Entity\Season;
use App\Services\Api\CustomerManager;
use App\Services\ConfigManager;
use Doctrine\ORM\EntityManagerInterface;
use Sonata\GoogleAuthenticator\GoogleAuthenticator;
use Sonata\GoogleAuthenticator\GoogleQrUrl;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Helper\RevenueCatHelper;
use App\Helper\AppleHelper;
use App\Helper\StripeHelper;
class CustomerController extends ApiController
{
/** @var CustomerManager $customerManager */
protected $customerManager;
/** @var ConfigManager $configManager */
protected $configManager;
/**
* @param CustomerManager $customerManager
* @param ConfigManager $configManager
*/
public function __construct(
EntityManagerInterface $em,
ParameterBagInterface $parameterBag,
CustomerManager $customerManager,
ConfigManager $configManager
) {
parent::__construct($em, $parameterBag);
$this->customerManager = $customerManager;
$this->configManager = $configManager;
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/user_data", name="api_user_data", methods={"POST"})
*/
public function userData(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
/** @var Season $season */
$season = $customer->getSeasonActive();
$prefixFile = $this->getParameter("app.path.customer_images");
$baseurl = $request->getScheme() . '://' . $request->getHttpHost() . $request->getBasePath();
$path = !empty($customer->getImageTeam()) ? $baseurl . $prefixFile . "/" . $customer->getImageTeam() : "";
$path_trainer_logo = !empty($customer->getImageTrainerTeam()) ? $baseurl . $prefixFile . "/" . $customer->getImageTrainerTeam() : "";
$data = [
"id" => $customer->getId(),
"isPro" => $customer->getIsPro(),
"name" => $customer->getName(),
"surname" => $customer->getSurname(),
"email" => $customer->getEmail(),
"teamName" => $customer->getNameTeam(),
"stadiumName" => $customer->getStadiumName(),
"monthlyCost" => $customer->getMonthlyCostTeam(),
"kitPaymentCost" => $customer->getKitPaymentCostTeam(),
"otherPaymentCost" => $customer->getOtherPaymentCostTeam(),
"teamCategory" => $customer->getCategoryTeam(),
"teamLogo" => $path,
"trainerLogo" => $path_trainer_logo,
//"countryId" => ! empty($customer->getCountry())? $customer->getCountry()->getId() : null,
"countryId" => !empty($season->getCountry()) ? $season->getCountry()->getId() : null,
"season" => $customer->getSeasonActive() ? $customer->getSeasonActive()->getYear() : "",
"seasonId" => $customer->getSeasonActive() ? $customer->getSeasonActive()->getId() : "",
"timeFree" => $this->customerManager->remainingTimeFree($customer),
"enabledToGo" => $this->customerManager->isEnabledToGo($customer),
"typeSoccer" => $customer->getTypeSoccer() ? $customer->getTypeSoccer()->__toArray() : null,
"alignmentDefaultU7" => $customer->getAlignmentDefaultU7() ? $customer->getAlignmentDefaultU7()->__toArray() : null,
"alignmentDefaultU8" => $customer->getAlignmentDefaultU8() ? $customer->getAlignmentDefaultU8()->__toArray() : null,
"alignmentDefaultU11" => $customer->getAlignmentDefaultU11() ? $customer->getAlignmentDefaultU11()->__toArray() : null,
"phone" => $customer->getPhone(),
"dateOfBirth" => $customer->getDateOfBirth() ? $customer->getDateOfBirth()->format('Y-m-d') : null,
"googleAuthCodeVerified" => $customer->getGoogleAuthCodeVerified() ? true : false,
"numberClubesLeague" => $customer->getNumberClubesLeague(),
];
return $this->response($data);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/purchase", name="api_user_purchase", methods={"POST"})
*/
public function customerPurchase(Request $request)
{
$req = $this->transformJsonBody($request);
$data = $this->getDataFromRequest($req);
$response = $this->customerManager->updatePurchase($data,$request);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/stripe/subscription", name="api_user_purchase_get_all", methods={"POST"})
*/
public function getStipeSubscription(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$stripe = new StripeHelper();
$this->setStatusCode(200);
$result = $stripe->getBySubscriptionKey($customer->getPurchase());
return $this->response($result);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/subscription/info", name="api_user_purchase_get_info", methods={"POST"})
*/
public function getStipeSubscriptionInfo(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$this->setStatusCode(200);
// Define the whitelist array
$whitelist = ["contactodtvct@gmail.com","Jotaindie@gmail.com","pepmalagrava@gmail.com","oliver_parr@hotmail.com","estebanherrera@icloud.com","alvarobenitezcalvente@gmail.com","juventudcirculo28@gmail.com","kikeemcna@gmail.com","alpebra99@gmail.com","Adrian_Palacio10@hotmail.com","xianarv2010@gmail.com","raulmar419@gmail.com","orozco1titi@gmail.com","jvcc@telefonica.net","angeldiazo1996@gmail.com","adrian_palacio10@hotmail.com","Pperagalo@gmail.com"];
// Get the email parameter from the request
$email = strtolower($customer->getEmail()); // Convert to lowercase
// TODO: REMOVE LATER
if (in_array($email, $whitelist)) {
$result = [
'active'=> true
];
return $this->response($result);
}
$purchase = $customer->getPurchase();
if (strpos($purchase, 'sub_') === 0) {
$stripe = new StripeHelper();
$result = $stripe->info($customer->getPurchase());
return $this->response($result);
}
if (strtolower($customer->getPurchasePlatform()) === 'ios'){
$apple = new AppleHelper();
$result = $apple->info($customer);
return $this->response($result);
}
if (strpos($purchase, 'MII') === 0) {
$apple = new AppleHelper();
$result = $apple->info($customer);
return $this->response($result);
}
$verifypayment = new RevenueCatHelper();
$result = $verifypayment->validateSubscription($customer);
return $this->response($result);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/stripe/purchase/cancel", name="api_user_purchase_cancel", methods={"POST"})
*/
public function stripePurchaseCancel(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$stripe = new StripeHelper();
$this->setStatusCode(200);
$result = $stripe->cancel($customer->getPurchase());
return $this->response($result);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/check_purchase", name="api_user_purchase_check", methods={"POST"})
*/
public function checkPurchase(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$verifypayment = new AppleHelper();
$result = $verifypayment->fetchAppleInformation($customer->getPurchase());
$this->setStatusCode(200);
return $this->response($result);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/check_purchase_status", name="api_user_purchase_check_status", methods={"POST"})
*/
public function checkPurchaseStatus(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$verifypayment = new AppleHelper();
$result = $verifypayment->validateSubscription($customer);
$this->setStatusCode(200);
return $this->response($result);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/stripe/check_purchase_status", name="api_user_stripe_purchase_check_status", methods={"POST"})
*/
public function checkStripePurchaseStatus(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$verifypayment = new StripeHelper();
$result = $verifypayment->validateSubscription($customer->getPurchase());
$this->setStatusCode(200);
return $this->response($result);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/refresh_purchase", name="api_user_purchase_refresh", methods={"POST"})
*/
public function customerRefreshPurchase(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$result = false;
// Define the whitelist array
$whitelist = ["contactodtvct@gmail.com","Jotaindie@gmail.com","pepmalagrava@gmail.com","oliver_parr@hotmail.com","estebanherrera@icloud.com","alvarobenitezcalvente@gmail.com","juventudcirculo28@gmail.com","kikeemcna@gmail.com","alpebra99@gmail.com","Adrian_Palacio10@hotmail.com","xianarv2010@gmail.com","raulmar419@gmail.com","orozco1titi@gmail.com","jvcc@telefonica.net","angeldiazo1996@gmail.com","adrian_palacio10@hotmail.com","Pperagalo@gmail.com"];
// Get the email parameter from the request
$email = strtolower($customer->getEmail()); // Convert to lowercase
// TODO: REMOVE LATER
if (in_array($email, $whitelist)) {
$result = true;
} else {
$purchase = $customer->getPurchase();
$platform = $customer->getPurchasePlatform();
if ($purchase === null){
/// if purchase if null we still need to check if if's under revenuecat helper
$verifypayment = new RevenueCatHelper();
$result = $verifypayment->validateSubscription($customer);
} else {
/// IOS CHECK
if (strtolower($platform) === 'ios') {
$verifyApplePayment = new AppleHelper();
$result = $verifyApplePayment->validateSubscription($customer);
}
else if (strpos($purchase, 'MII') === 0) {
$verifyApplePayment = new AppleHelper();
$result = $verifyApplePayment->validateSubscription($customer);
}
/// STRIPE WEB CHECK
else if (strpos($purchase, 'sub_') === 0) {
$stripePayment = new StripeHelper();
$result = $stripePayment->validateSubscription($purchase);
}
/// ANDROID CHECK
else {
$verifypayment = new RevenueCatHelper();
$result = $verifypayment->validateSubscription($customer);
}
}
}
$response = $this->customerManager->changeStatusSuscription($result);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/coupon_swap", name="api_user_coupon_swap", methods={"POST"})
*/
public function customerUseCoupon(Request $request)
{
$request = $this->transformJsonBody($request);
$data = [
'coupon_code' => $request->get('coupon_code'),
];
$response = $this->customerManager->useCoupon($data);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/device/add", name="api_user_device_add", methods={"POST"})
*/
public function addDevice(Request $request)
{
$request = $this->transformJsonBody($request);
$data = [
'refresh_token' => $request->get('refresh_token'),
'device_id' => $request->get('device_id'),
'device_model' => $request->get('device_model'),
'device_type' => $request->get('device_type'),
'device_brand' => $request->get('device_brand'),
];
$response = $this->customerManager->addDevice($data);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/device/delete", name="api_user_device_delete", methods={"POST"})
*/
public function deleteDevice(Request $request)
{
$request = $this->transformJsonBody($request);
$data = [
'device_id' => $request->get('device_id'),
'skipFingerPrinter' => $request->get('skipFingerPrinter', 0),
];
$response = $this->customerManager->deleteDevice($data);
$this->setStatusCode($response["code"]);
return $this->response($response);
}
protected function getDataFromRequest(Request $request)
{
$data = [
"purchase" => $request->get('purchase'),
"product_id" => $request->get('product_id')
];
return $data;
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/device/list", name="api_user_device_list", methods={"POST"})
*/
public function listDevice(Request $request)
{
$request = $this->transformJsonBody($request);
$devices = [];
/** @var Customer $customer */
$customer = $this->getUser();
$customer_device = $this->em->getRepository(CustomerDevice::class)->findBy([
'customerId' => $customer->getId(),
'status' => true,
]);
/** @var ExerciseUser $exercise */
foreach ($customer_device as $device) {
$devices[] = $device->__toArray();
}
return $this->response($devices);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/customer/remove_account", name="api_user_remove_account", methods={"POST"})
*/
public function removeAccount(Request $request)
{
$devices = [];
/** @var Customer $customer */
$customer = $this->getUser();
$customerRemove = $this->em->getRepository(Customer::class)->find($customer->getId());
$customerRemove->setDeletedAt(new \DateTime());
$this->em->persist($customerRemove);
$customer_device = $this->em->getRepository(CustomerDevice::class)->findBy([
'customerId' => $customer->getId(),
'status' => true,
]);
foreach ($customer_device as $key => $customerDevice) {
$customerDevice->setStatus(false);
$this->em->persist($customerDevice);
}
$this->em->flush();
return $this->response($devices);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/create_step_verification", name="api_create_step_verification", methods={"POST"})
*/
public function CreateStepVerification(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$existCode = $customer->getGoogleAuthCode();
if ($existCode) {
$qr = GoogleQrUrl::generate($customer->getEmail(), $existCode, 'Golmanager');
return $this->json([
'qr' => $qr,
'secret' => $existCode
]);
}
$g = new GoogleAuthenticator();
$secret = $g->generateSecret();
$qr = GoogleQrUrl::generate($customer->getEmail(), $secret, 'Golmanager');
$customer->setGoogleAuthCode($secret);
$this->em->persist($customer);
$this->em->flush();
return $this->json([
'qr' => $qr,
'secret' => $secret
]);
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/verify_step_verification", name="api_verify_step_verification", methods={"POST"})
*/
public function VerifyStepVerification(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$code = $request->get('code');
$g = new GoogleAuthenticator();
if ($g->checkCode($customer->getGoogleAuthCode(), $code)) {
$customer->setGoogleAuthCodeVerified(true);
$this->em->persist($customer);
$this->em->flush();
return $this->json([
'success' => true
]);
} else {
return $this->json([
'success' => false
]);
}
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/verify_otp", name="api_verify_otp", methods={"POST"})
*/
public function VerifyOtp(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$code = $request->get('code');
$g = new GoogleAuthenticator();
if ($g->checkCode($customer->getGoogleAuthCode(), $code)) {
return $this->json([
'success' => true
]);
} else {
return $this->json([
'success' => false
]);
}
}
/**
* @param Request $request
* @return JsonResponse
*
* @Route("/disable_step_verification", name="api_disable_step_verification", methods={"POST"})
*/
public function DisableStepVerification(Request $request)
{
/** @var Customer $customer */
$customer = $this->getUser();
$code = $request->get('code');
$g = new GoogleAuthenticator();
if ($g->checkCode($customer->getGoogleAuthCode(), $code)) {
$customer->setGoogleAuthCodeVerified(false);
$this->em->persist($customer);
$this->em->flush();
return $this->json([
'success' => true
]);
}
return $this->json([
'success' => false
]);
}
}