src/Controller/Api/ApiController.php line 79

  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\Country;
  4. use App\Entity\Input;
  5. use App\Entity\InputType;
  6. use App\Entity\LandingPage;
  7. use App\Entity\PaymentConnector;
  8. use App\Entity\PaymentType;
  9. use App\Entity\Product;
  10. use App\Entity\Source;
  11. use App\Entity\User;
  12. use App\Repository\CountryRepository;
  13. use App\Repository\InputRepository;
  14. use App\Repository\InputTypeRepository;
  15. use App\Repository\LandingPageRepository;
  16. use App\Repository\PaymentConnectorRepository;
  17. use App\Repository\PaymentTypeRepository;
  18. use App\Repository\ProductRepository;
  19. use App\Repository\SourceApiCallRepository;
  20. use App\Repository\SourceRepository;
  21. use App\Service\PaymentConnectorManagerInterface;
  22. use App\Service\PaymentProcess;
  23. use Doctrine\ORM\EntityManagerInterface;
  24. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  25. use Symfony\Component\HttpClient\CurlHttpClient;
  26. use Symfony\Component\HttpFoundation\InputBag;
  27. use Symfony\Component\HttpFoundation\JsonResponse;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\Routing\Annotation\Route;
  31. use Symfony\Component\Security\Http\Attribute\IsGranted;
  32. #[Route('/api')]
  33. class ApiController extends AbstractController
  34. {
  35. //    #[Route('/test', name: 'test')]
  36. //    public function test()
  37. //    {
  38. //        $data = [
  39. //            'publicKey' => 'xQ6Syq0U',
  40. //            'privateKey' => 'YhulnvwXmyxAG8KmcZQM1MKagOCF0POmJeLKFFLDcJj6Cqr3wcfvitGmVhy3jk8IS4DBJXY9LFIHtKtffy0wo4sMTx7p6rfHg7Oj',
  41. //            'product' => 'ii6je3Lh',
  42. //            'value' => '777',
  43. //            'pubid' => 44,
  44. //            'clickid' => 'stoto',
  45. //            'visitorTag' => '3a1088f99164479800692e6',
  46. //        ];
  47. //
  48. //        // Initialiser une session CurlHttpClient
  49. //        $customer = new CurlHttpClient();
  50. //
  51. //        $headers = [
  52. //            'Content-Type' => 'application/json',
  53. //            'toto' => 'titi',
  54. //        ];
  55. //
  56. //        // Exécuter la requête
  57. //        $time = microtime(true);
  58. //
  59. //        ///notif/212f45fac563ea2306b6623/subscription/new/6101
  60. //        $response = $customer->request('POST', 'https://dev-sub.phonismart.com/api/pin', [
  61. ////        $response = $customer->request('POST', 'https://dev-sub.phonismart.com/api/phone-number?source=xQ6Syq0U&product=ii6je3Lh&value=0548961969&pubid=42&clickid=toto', [
  62. ////        $response = $customer->request('POST', 'https://dev-sub.phonismart.com'.$this->generateUrl('app_public_notif_subscription_new_param', [
  63. ////                'param' => 6101,
  64. ////                'uniqueKey' => $aggregator->getUniqueKey(),
  65. ////            ]), [
  66. //            'headers' => $headers,
  67. //            'body' => $data
  68. //        ]);
  69. //
  70. ////        dd($response);
  71. //        $content = json_decode($response->getContent());
  72. //        dd($content);
  73. //    }
  74.     #[Route('/{inputTypeNameCanonical}'name'call')]
  75.     public function api(
  76.         string $inputTypeNameCanonical,
  77.         Request $request,
  78.         SourceRepository $sourceRepository,
  79.         ProductRepository $productRepository,
  80.         InputTypeRepository $inputTypeRepository,
  81.         PaymentProcess $paymentProcess,
  82.         InputRepository $inputRepository,
  83.         SourceApiCallRepository $sourceApiCallRepository,
  84.         EntityManagerInterface $em,
  85.     ): Response
  86.     {
  87.         $sourceApiCall $sourceApiCallRepository->log(
  88.             $request,
  89.         );
  90.         $source $this->getSourceByRequest($em$sourceRepository$request);
  91.         $sourceApiCall->setSource($source);
  92.         $sourceApiCallRepository->save($sourceApiCall);
  93.         $product $this->getProductByRequest($em$productRepository$request);
  94.         $sourceApiCall->setProduct($product);
  95.         $sourceApiCallRepository->save($sourceApiCall);
  96.         $inputType $this->getInputTypeByNameCanonical($em$inputTypeRepository$inputTypeNameCanonical);
  97.         $sourceApiCall->setInputType($inputType);
  98.         $sourceApiCallRepository->save($sourceApiCalltrue);
  99.         $pubid Input::getPubidByRequest($requesttrue);
  100.         $sourceApiCall->setPubid($pubid);
  101.         $sourceApiCallRepository->save($sourceApiCalltrue);
  102.         $clickid Input::getClickidByRequest($requesttrue);
  103.         $sourceApiCall->setClickid($clickid);
  104.         $sourceApiCallRepository->save($sourceApiCalltrue);
  105.         if ($product->getPaymentType()->isFirstInputType($inputType) === false && Input::getInputBag($request)->get('visitorTag') === null) {
  106.             return new JsonResponse(PaymentProcess::getApiResponseTemplate(
  107.                 PaymentProcess::ERROR,
  108.                 null,
  109.                 'visitorTag empty'
  110.             ));
  111.         }
  112.         $visitorTag Input::getInputBag($request)->get('visitorTag'Input::generateNewVisitorTag());
  113.         $sourceApiCall->setVisitorTag($visitorTag);
  114.         $sourceApiCallRepository->save($sourceApiCalltrue);
  115.         $input $inputRepository->getCurrentInput($visitorTag$product$sourcenull$pubidnull$clickid$request->getClientIp());
  116.         while ($input->getInputTypeId() !== $inputType->getId() && $input->getPreviousInput() !== null) {
  117.             $input $input->getPreviousInput();
  118.         }
  119.         if ($input->getInputTypeId() !== $inputType->getId()) {
  120.             return new JsonResponse(PaymentProcess::getApiResponseTemplate(
  121.                 PaymentProcess::ERROR,
  122.                 $product->getPaymentType()->isLastInputType($inputType) === false $visitorTag null,
  123.                 'visitorTag not found'
  124.             ));
  125.         }
  126.         $input
  127.             ->setIsFromApi(true)
  128.             ->setIsSubmitted(true)
  129.             ->setValue(Input::getInputBag($request)->get('value'))
  130.         ;
  131.         $sourceApiCall->setInput($input);
  132.         $sourceApiCallRepository->save($sourceApiCalltrue);
  133.         $paymentProcess->execute($input);
  134.         $sourceApiCall->setSubscription($input->getActiveSubscription());
  135.         $sourceApiCallRepository->save($sourceApiCalltrue);
  136.         $apiResponse $paymentProcess->getApiResponse();
  137.         $sourceApiCall->setResponse($apiResponse);
  138.         $sourceApiCallRepository->save($sourceApiCalltrue);
  139.         return new JsonResponse($apiResponse);
  140.     }
  141.     private function getInputTypeByNameCanonical(EntityManagerInterface $emInputTypeRepository $inputTypeRepositorystring $nameCanonical): InputType
  142.     {
  143.         $inputType $inputTypeRepository->findOneByNameCanonical($nameCanonical);
  144.         if ($inputType === null) {
  145.             $em->flush();
  146.             throw $this->createNotFoundException('input type not found');
  147.         }
  148.         return $inputType;
  149.     }
  150.     private function getProductByRequest(EntityManagerInterface $emProductRepository $productRepositoryRequest $request): Product
  151.     {
  152.         $uniqueKey Input::getInputBag($request)->get('product');
  153.         if ($uniqueKey === null || ($product $productRepository->getProductByUniqueKey($uniqueKeyfalse)) === null) {
  154.             $em->flush();
  155.             throw $this->createNotFoundException('product not found');
  156.         }
  157.         return $product;
  158.     }
  159.     private function getSourceByRequest(EntityManagerInterface $emSourceRepository $sourceRepositoryRequest $request): Source
  160.     {
  161.         $publicKey Input::getInputBag($request)->get('publicKey');
  162.         $privateKey Input::getInputBag($request)->get('privateKey');
  163.         if ($publicKey === null || $privateKey === null || ($source $sourceRepository->getSourceByKeys($publicKey$privateKey)) === null) {
  164.             $em->flush();
  165.             throw $this->createNotFoundException('source not found');
  166.         }
  167.         return $source;
  168.     }
  169. }