src/Controller/Api/CustomerController.php line 407

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\Customer;
  4. use App\Entity\CustomerDevice;
  5. use App\Entity\Season;
  6. use App\Services\Api\CustomerManager;
  7. use App\Services\ConfigManager;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Sonata\GoogleAuthenticator\GoogleAuthenticator;
  10. use Sonata\GoogleAuthenticator\GoogleQrUrl;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use App\Helper\RevenueCatHelper;
  17. use App\Helper\AppleHelper;
  18. use App\Helper\StripeHelper;
  19. class CustomerController extends ApiController
  20. {
  21.   /** @var CustomerManager $customerManager */
  22.   protected $customerManager;
  23.   /** @var  ConfigManager $configManager */
  24.   protected $configManager;
  25.   /**
  26.    * @param CustomerManager $customerManager
  27.    * @param ConfigManager $configManager
  28.    */
  29.   public function __construct(
  30.     EntityManagerInterface $em,
  31.     ParameterBagInterface $parameterBag,
  32.     CustomerManager $customerManager,
  33.     ConfigManager $configManager
  34.   ) {
  35.     parent::__construct($em$parameterBag);
  36.     $this->customerManager $customerManager;
  37.     $this->configManager $configManager;
  38.   }
  39.   /**
  40.    * @param Request $request
  41.    * @return JsonResponse
  42.    *
  43.    * @Route("/user_data", name="api_user_data", methods={"POST"})
  44.    */
  45.   public function userData(Request $request)
  46.   {
  47.     /** @var Customer $customer */
  48.     $customer $this->getUser();
  49.     /** @var Season $season */
  50.     $season $customer->getSeasonActive();
  51.     $prefixFile $this->getParameter("app.path.customer_images");
  52.     $baseurl $request->getScheme() . '://' $request->getHttpHost() . $request->getBasePath();
  53.     $path = !empty($customer->getImageTeam()) ? $baseurl $prefixFile "/" $customer->getImageTeam() : "";
  54.     $path_trainer_logo = !empty($customer->getImageTrainerTeam()) ? $baseurl $prefixFile "/" $customer->getImageTrainerTeam() : "";
  55.     $data = [
  56.       "id" => $customer->getId(),
  57.       "isPro" => $customer->getIsPro(),
  58.       "name" => $customer->getName(),
  59.       "surname" => $customer->getSurname(),
  60.       "email" => $customer->getEmail(),
  61.       "teamName" => $customer->getNameTeam(),
  62.       "stadiumName" => $customer->getStadiumName(),
  63.       "monthlyCost" => $customer->getMonthlyCostTeam(),
  64.       "kitPaymentCost" => $customer->getKitPaymentCostTeam(),
  65.       "otherPaymentCost" => $customer->getOtherPaymentCostTeam(),
  66.       "teamCategory" => $customer->getCategoryTeam(),
  67.       "teamLogo" => $path,
  68.       "trainerLogo" => $path_trainer_logo,
  69.       //"countryId" => ! empty($customer->getCountry())? $customer->getCountry()->getId() : null,
  70.       "countryId" => !empty($season->getCountry()) ? $season->getCountry()->getId() : null,
  71.       "season" => $customer->getSeasonActive() ? $customer->getSeasonActive()->getYear() : "",
  72.       "seasonId" => $customer->getSeasonActive() ? $customer->getSeasonActive()->getId() : "",
  73.       "timeFree" => $this->customerManager->remainingTimeFree($customer),
  74.       "enabledToGo" => $this->customerManager->isEnabledToGo($customer),
  75.       "typeSoccer" => $customer->getTypeSoccer() ? $customer->getTypeSoccer()->__toArray() : null,
  76.       "alignmentDefaultU7" => $customer->getAlignmentDefaultU7() ? $customer->getAlignmentDefaultU7()->__toArray() : null,
  77.       "alignmentDefaultU8" => $customer->getAlignmentDefaultU8() ? $customer->getAlignmentDefaultU8()->__toArray() : null,
  78.       "alignmentDefaultU11" => $customer->getAlignmentDefaultU11() ? $customer->getAlignmentDefaultU11()->__toArray() : null,
  79.       "phone" => $customer->getPhone(),
  80.       "dateOfBirth" => $customer->getDateOfBirth() ?  $customer->getDateOfBirth()->format('Y-m-d') : null,
  81.       "googleAuthCodeVerified" => $customer->getGoogleAuthCodeVerified() ? true false,
  82.       "numberClubesLeague" => $customer->getNumberClubesLeague(),
  83.     ];
  84.     return $this->response($data);
  85.   }
  86.   /**
  87.    * @param Request $request
  88.    * @return JsonResponse
  89.    *
  90.    * @Route("/customer/purchase", name="api_user_purchase", methods={"POST"})
  91.    */
  92.   public function customerPurchase(Request $request)
  93.   {
  94.     $req $this->transformJsonBody($request);
  95.     $data $this->getDataFromRequest($req);
  96.     
  97.     $response $this->customerManager->updatePurchase($data,$request);
  98.     
  99.     $this->setStatusCode($response["code"]);
  100.     return $this->response($response);
  101.   }
  102.    /**
  103.    * @param Request $request
  104.    * @return JsonResponse
  105.    *
  106.    * @Route("/customer/stripe/subscription", name="api_user_purchase_get_all", methods={"POST"})
  107.    */
  108.   public function getStipeSubscription(Request $request)
  109.   {
  110.     /** @var Customer $customer */
  111.     $customer $this->getUser();
  112.     $stripe = new StripeHelper();
  113.     $this->setStatusCode(200);
  114.     $result $stripe->getBySubscriptionKey($customer->getPurchase());
  115.     return $this->response($result);
  116.   }
  117.      /**
  118.    * @param Request $request
  119.    * @return JsonResponse
  120.    *
  121.    * @Route("/customer/subscription/info", name="api_user_purchase_get_info", methods={"POST"})
  122.    */
  123.   public function getStipeSubscriptionInfo(Request $request)
  124.   {
  125.     /** @var Customer $customer */
  126.     $customer $this->getUser();
  127.     $this->setStatusCode(200);
  128.     // Define the whitelist array
  129.     $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"];
  130.     // Get the email parameter from the request
  131.     $email strtolower($customer->getEmail()); // Convert to lowercase
  132.     // TODO: REMOVE LATER
  133.     if (in_array($email$whitelist)) {
  134.       $result = [
  135.         'active'=> true
  136.       ];
  137.       return $this->response($result);
  138.     }
  139.     $purchase $customer->getPurchase();
  140.     if (strpos($purchase'sub_') === 0) {
  141.       $stripe = new StripeHelper();
  142.       $result $stripe->info($customer->getPurchase());
  143.       return $this->response($result);
  144.     } 
  145.     
  146.     if (strtolower($customer->getPurchasePlatform()) === 'ios'){
  147.       $apple = new AppleHelper();
  148.       $result $apple->info($customer);
  149.       return $this->response($result);
  150.     }
  151.     
  152.     if (strpos($purchase'MII') === 0) {
  153.       $apple = new AppleHelper();
  154.       $result $apple->info($customer);
  155.       return $this->response($result);
  156.     }  
  157.     $verifypayment = new RevenueCatHelper();
  158.     $result $verifypayment->validateSubscription($customer);
  159.     return $this->response($result);
  160.   }
  161.    /**
  162.    * @param Request $request
  163.    * @return JsonResponse
  164.    *
  165.    * @Route("/customer/stripe/purchase/cancel", name="api_user_purchase_cancel", methods={"POST"})
  166.    */
  167.   public function stripePurchaseCancel(Request $request)
  168.   {
  169.     /** @var Customer $customer */
  170.     $customer $this->getUser();
  171.     $stripe = new StripeHelper();
  172.     $this->setStatusCode(200);
  173.     $result $stripe->cancel($customer->getPurchase());
  174.     return $this->response($result);
  175.   }
  176.    /**
  177.    * @param Request $request
  178.    * @return JsonResponse
  179.    *
  180.    * @Route("/customer/check_purchase", name="api_user_purchase_check", methods={"POST"})
  181.    */
  182.   public function checkPurchase(Request $request)
  183.   {
  184.     /** @var Customer $customer */
  185.     $customer $this->getUser();
  186.     $verifypayment = new AppleHelper();
  187.     $result $verifypayment->fetchAppleInformation($customer->getPurchase());
  188.     
  189.     $this->setStatusCode(200);
  190.     return $this->response($result);
  191.   }
  192.     /**
  193.    * @param Request $request
  194.    * @return JsonResponse
  195.    *
  196.    * @Route("/customer/check_purchase_status", name="api_user_purchase_check_status", methods={"POST"})
  197.    */
  198.   public function checkPurchaseStatus(Request $request)
  199.   {
  200.     /** @var Customer $customer */
  201.     $customer $this->getUser();
  202.     $verifypayment = new AppleHelper();
  203.     $result $verifypayment->validateSubscription($customer);
  204.     
  205.     $this->setStatusCode(200);
  206.     return $this->response($result);
  207.   }
  208.      /**
  209.    * @param Request $request
  210.    * @return JsonResponse
  211.    *
  212.    * @Route("/customer/stripe/check_purchase_status", name="api_user_stripe_purchase_check_status", methods={"POST"})
  213.    */
  214.   public function checkStripePurchaseStatus(Request $request)
  215.   {
  216.     /** @var Customer $customer */
  217.     $customer $this->getUser();
  218.     $verifypayment = new StripeHelper();
  219.     $result $verifypayment->validateSubscription($customer->getPurchase());
  220.     
  221.     $this->setStatusCode(200);
  222.     return $this->response($result);
  223.   }
  224.   /**
  225.    * @param Request $request
  226.    * @return JsonResponse
  227.    *
  228.    * @Route("/customer/refresh_purchase", name="api_user_purchase_refresh", methods={"POST"})
  229.    */
  230.   public function customerRefreshPurchase(Request $request)
  231.   {
  232.     /** @var Customer $customer */
  233.     $customer $this->getUser();
  234.     $result false;
  235.     // Define the whitelist array
  236.     $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"];
  237.     // Get the email parameter from the request
  238.     $email strtolower($customer->getEmail()); // Convert to lowercase
  239.     // TODO: REMOVE LATER
  240.     if (in_array($email$whitelist)) {
  241.       $result true;
  242.     } else {
  243.       $purchase $customer->getPurchase();
  244.       $platform $customer->getPurchasePlatform();
  245.   
  246.       if ($purchase === null){
  247.         /// if purchase if null we still need to check if if's under revenuecat helper
  248.         $verifypayment = new RevenueCatHelper();
  249.         $result $verifypayment->validateSubscription($customer);
  250.       } else {
  251.         /// IOS CHECK
  252.         if (strtolower($platform) === 'ios') {
  253.           $verifyApplePayment = new AppleHelper();
  254.           $result $verifyApplePayment->validateSubscription($customer);
  255.         } 
  256.         else if (strpos($purchase'MII') === 0) {
  257.           $verifyApplePayment = new AppleHelper();
  258.           $result $verifyApplePayment->validateSubscription($customer);
  259.         }
  260.         /// STRIPE WEB CHECK
  261.         else if (strpos($purchase'sub_') === 0) {
  262.           $stripePayment = new StripeHelper();
  263.           $result $stripePayment->validateSubscription($purchase);
  264.         }
  265.         /// ANDROID CHECK
  266.         else {
  267.           $verifypayment = new RevenueCatHelper();
  268.           $result $verifypayment->validateSubscription($customer);
  269.         }
  270.       }
  271.     }
  272.     $response $this->customerManager->changeStatusSuscription($result);
  273.     $this->setStatusCode($response["code"]);
  274.     return $this->response($response);
  275.   }
  276.   /**
  277.    * @param Request $request
  278.    * @return JsonResponse
  279.    *
  280.    * @Route("/customer/coupon_swap", name="api_user_coupon_swap", methods={"POST"})
  281.    */
  282.   public function customerUseCoupon(Request $request)
  283.   {
  284.     $request $this->transformJsonBody($request);
  285.     $data = [
  286.       'coupon_code' => $request->get('coupon_code'),
  287.     ];
  288.     $response $this->customerManager->useCoupon($data);
  289.     $this->setStatusCode($response["code"]);
  290.     return $this->response($response);
  291.   }
  292.   /**
  293.    * @param Request $request
  294.    * @return JsonResponse
  295.    *
  296.    * @Route("/customer/device/add", name="api_user_device_add", methods={"POST"})
  297.    */
  298.   public function addDevice(Request $request)
  299.   {
  300.     $request $this->transformJsonBody($request);
  301.     $data = [
  302.       'refresh_token' => $request->get('refresh_token'),
  303.       'device_id' => $request->get('device_id'),
  304.       'device_model' => $request->get('device_model'),
  305.       'device_type' => $request->get('device_type'),
  306.       'device_brand' => $request->get('device_brand'),
  307.     ];
  308.     $response $this->customerManager->addDevice($data);
  309.     $this->setStatusCode($response["code"]);
  310.     return $this->response($response);
  311.   }
  312.   /**
  313.    * @param Request $request
  314.    * @return JsonResponse
  315.    *
  316.    * @Route("/customer/device/delete", name="api_user_device_delete", methods={"POST"})
  317.    */
  318.   public function deleteDevice(Request $request)
  319.   {
  320.     $request $this->transformJsonBody($request);
  321.     $data = [
  322.       'device_id' => $request->get('device_id'),
  323.       'skipFingerPrinter' => $request->get('skipFingerPrinter'0),
  324.     ];
  325.     $response $this->customerManager->deleteDevice($data);
  326.     $this->setStatusCode($response["code"]);
  327.     return $this->response($response);
  328.   }
  329.   protected function getDataFromRequest(Request $request)
  330.   {
  331.     $data = [
  332.       "purchase" => $request->get('purchase'),
  333.       "product_id" => $request->get('product_id')
  334.     ];
  335.     return $data;
  336.   }
  337.   /**
  338.    * @param Request $request
  339.    * @return JsonResponse
  340.    *
  341.    * @Route("/customer/device/list", name="api_user_device_list", methods={"POST"})
  342.    */
  343.   public function listDevice(Request $request)
  344.   {
  345.     $request $this->transformJsonBody($request);
  346.     $devices = [];
  347.     /** @var Customer $customer */
  348.     $customer $this->getUser();
  349.     $customer_device $this->em->getRepository(CustomerDevice::class)->findBy([
  350.       'customerId' => $customer->getId(),
  351.       'status' => true,
  352.     ]);
  353.     /** @var ExerciseUser $exercise */
  354.     foreach ($customer_device as $device) {
  355.       $devices[] = $device->__toArray();
  356.     }
  357.     return $this->response($devices);
  358.   }
  359.   /**
  360.    * @param Request $request
  361.    * @return JsonResponse
  362.    *
  363.    * @Route("/customer/remove_account", name="api_user_remove_account", methods={"POST"})
  364.    */
  365.   public function removeAccount(Request $request)
  366.   {
  367.     $devices = [];
  368.     /** @var Customer $customer */
  369.     $customer $this->getUser();
  370.     $customerRemove $this->em->getRepository(Customer::class)->find($customer->getId());
  371.     $customerRemove->setDeletedAt(new \DateTime());
  372.     $this->em->persist($customerRemove);
  373.     $customer_device $this->em->getRepository(CustomerDevice::class)->findBy([
  374.       'customerId' => $customer->getId(),
  375.       'status' => true,
  376.     ]);
  377.     foreach ($customer_device as $key => $customerDevice) {
  378.       $customerDevice->setStatus(false);
  379.       $this->em->persist($customerDevice);
  380.     }
  381.     $this->em->flush();
  382.     return $this->response($devices);
  383.   }
  384.   /**
  385.    * @param Request $request
  386.    * @return JsonResponse
  387.    *
  388.    * @Route("/create_step_verification", name="api_create_step_verification", methods={"POST"})
  389.    */
  390.   public function CreateStepVerification(Request $request)
  391.   {
  392.     /** @var Customer $customer */
  393.     $customer $this->getUser();
  394.     $existCode $customer->getGoogleAuthCode();
  395.     if ($existCode) {
  396.       $qr GoogleQrUrl::generate($customer->getEmail(), $existCode'Golmanager');
  397.       return $this->json([
  398.         'qr' => $qr,
  399.         'secret' => $existCode
  400.       ]);
  401.     }
  402.     $g = new GoogleAuthenticator();
  403.     $secret $g->generateSecret();
  404.     $qr GoogleQrUrl::generate($customer->getEmail(), $secret'Golmanager');
  405.     $customer->setGoogleAuthCode($secret);
  406.     $this->em->persist($customer);
  407.     $this->em->flush();
  408.     return $this->json([
  409.       'qr' => $qr,
  410.       'secret' => $secret
  411.     ]);
  412.   }
  413.   /**
  414.    * @param Request $request
  415.    * @return JsonResponse
  416.    *
  417.    * @Route("/verify_step_verification", name="api_verify_step_verification", methods={"POST"})
  418.    */
  419.   public function VerifyStepVerification(Request $request)
  420.   {
  421.     /** @var Customer $customer */
  422.     $customer $this->getUser();
  423.     $code $request->get('code');
  424.     $g = new GoogleAuthenticator();
  425.     if ($g->checkCode($customer->getGoogleAuthCode(), $code)) {
  426.       $customer->setGoogleAuthCodeVerified(true);
  427.       $this->em->persist($customer);
  428.       $this->em->flush();
  429.       return $this->json([
  430.         'success' => true
  431.       ]);
  432.     } else {
  433.       return $this->json([
  434.         'success' => false
  435.       ]);
  436.     }
  437.   }
  438.   /**
  439.    * @param Request $request
  440.    * @return JsonResponse
  441.    *
  442.    * @Route("/verify_otp", name="api_verify_otp", methods={"POST"})
  443.    */
  444.   public function VerifyOtp(Request $request)
  445.   {
  446.     /** @var Customer $customer */
  447.     $customer $this->getUser();
  448.     $code $request->get('code');
  449.     $g = new GoogleAuthenticator();
  450.     if ($g->checkCode($customer->getGoogleAuthCode(), $code)) {
  451.       return $this->json([
  452.         'success' => true
  453.       ]);
  454.     } else {
  455.       return $this->json([
  456.         'success' => false
  457.       ]);
  458.     }
  459.   }
  460.   /**
  461.    * @param Request $request
  462.    * @return JsonResponse
  463.    *
  464.    * @Route("/disable_step_verification", name="api_disable_step_verification", methods={"POST"})
  465.    */
  466.   public function DisableStepVerification(Request $request)
  467.   {
  468.     /** @var Customer $customer */
  469.     $customer $this->getUser();
  470.     $code $request->get('code');
  471.     $g = new GoogleAuthenticator();
  472.     if ($g->checkCode($customer->getGoogleAuthCode(), $code)) {
  473.       $customer->setGoogleAuthCodeVerified(false);
  474.       $this->em->persist($customer);
  475.       $this->em->flush();
  476.       return $this->json([
  477.         'success' => true
  478.       ]);
  479.     }
  480.     return $this->json([
  481.       'success' => false
  482.     ]);
  483.   }
  484. }