src/Services/CallApiService.php line 17

  1. <?php
  2. namespace App\Services;
  3. use Symfony\Component\Routing\RouterInterface;
  4. use Symfony\Component\HttpFoundation\RedirectResponse;
  5. class CallApiService
  6. {
  7.   private $apiUrl;
  8.   private $domainName;
  9.   /* @var $router RouterInterface */
  10.   private $router;
  11.   public function __construct($apiUrl$domainNameRouterInterface $router)
  12.   {
  13.     $this->apiUrl $apiUrl;
  14.     $this->httpHost $domainName;
  15.     $this->router $router;
  16.   }
  17.   //Authentification Admin
  18.   public function auth(string $loginstring $password): array
  19.   {
  20.     $endpoint "auth";
  21.     $url "$this->apiUrl"$endpoint";
  22.     $curl curl_init($url);
  23.     curl_setopt($curlCURLOPT_REFERER$this->httpHost);
  24.     curl_setopt($curlCURLOPT_POSTtrue);
  25.     curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  26.     curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
  27.     $headers = array(
  28.       "Accept: application/json",
  29.       "Content-Type: application/json",
  30.     );
  31.     $data = [
  32.       "login" => $login,
  33.       "password" => $password
  34.     ];
  35.     $dataEncoded json_encode($data);
  36.     curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  37.     curl_setopt($curlCURLOPT_POSTFIELDS$dataEncoded);
  38.     $response curl_exec($curl);
  39.     if ($response === false) {
  40.       throw new \Exception(curl_error($curl), curl_errno($curl));
  41.       return ["null" => null];
  42.     } else {
  43.       $results = [];
  44.       if (curl_getinfo($curlCURLINFO_HTTP_CODE) === 200) {
  45.         $results json_decode($responsetrue);
  46.       } else if (curl_getinfo($curlCURLINFO_HTTP_CODE) === 401) {
  47.         throw $results =  new \Exception('identifiant incorrecte');
  48.       } else if (curl_getinfo($curlCURLINFO_HTTP_CODE) === 404) {
  49.         $results =  [
  50.           'code' => 404,
  51.           'message' => $response['message'],
  52.         ];
  53.       }
  54.     }
  55.     curl_close($curl);
  56.     return $results;
  57.   }
  58.   //Authentification Admin
  59.   public function postApi($endpoint, array $data)
  60.   {
  61.     $url "$this->apiUrl"$endpoint";
  62.     $dataEncoded json_encode($data);
  63.     $headers = array(
  64.       "Accept: application/json",
  65.       "Content-Type: application/json",
  66.     );
  67.     $curl curl_init($url);
  68.     curl_setopt($curlCURLOPT_REFERER$this->httpHost);
  69.     curl_setopt($curlCURLOPT_POSTtrue);
  70.     curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  71.     curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
  72.     curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  73.     curl_setopt($curlCURLOPT_POSTFIELDS$dataEncoded);
  74.     $response curl_exec($curl);
  75.     $curl_http_code curl_getinfo($curlCURLINFO_HTTP_CODE);
  76.     $curl_error curl_error($curl);
  77.     $curl_errno curl_errno($curl);
  78.     curl_close($curl);
  79.     if ($response === false) {
  80.       throw new \Exception($curl_error$curl_errno);
  81.       return ["null" => null];
  82.     } else {
  83.       $results = [];
  84.       // if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200) {
  85.       //   $results = json_decode($response, true);
  86.       // } else if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 401) {
  87.       //   throw $results =  new \Exception('identifiant incorrecte');
  88.       // } else if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 404) {
  89.       //   $response = json_decode($response, true);
  90.       //   $results =  $response;
  91.       // } else if (curl_getinfo($curl, CURLINFO_HTTP_CODE) === 403) {
  92.       //   $response = json_decode($response, true);
  93.       //   $results = $response;
  94.       // }
  95.       switch ($curl_http_code) {
  96.         case 200:
  97.           $results json_decode($responsetrue);
  98.           break;
  99.         case 401// Accès non autorisé, l'apiKey n'existe pas
  100.           // a rediriger vers un page d'erreur qui dirait joindre le support, car c'est une erreur qui ne devrait jamais arrivée
  101.           die(new RedirectResponse($this->router->generate('app_login')));
  102.           //throw $results =  new \Exception('identifiant incorrecte');
  103.           break;
  104.         case 403:
  105.         case 404:
  106.           $response json_decode($responsetrue);
  107.           $results =  $response;
  108.           break;
  109.         case 409// Conflit avec le compte ID ou service ID, l'apiKey existe, mais un conflit avec les valeurs configurées
  110.           // a rediriger vers un page d'erreur qui dirait joindre le support, car c'est une erreur qui ne devrait jamais arrivée
  111.           die(new RedirectResponse($this->router->generate('app_login')));
  112.           break;
  113.         case 410// apiToken n'exite pas ou la date est dépassée, vous devez démarrer une nouvelle session
  114.           die(new RedirectResponse($this->router->generate('app_login')));
  115.           break;
  116.         default:
  117.           $response json_decode($responsetrue);
  118.           $results =  $response;
  119.           break;
  120.       }
  121.     }
  122.     // $data = [ 'message' => "Accès non autorisé", 'responseCode' => "401" ];  l'apiKey n'existe pas
  123.     // $data = [ 'message' => "Conflit avec le référent HTTP", 'responseCode' => "409" ]; l'apiKey existe, mais un conflit avec les valeurs configurées
  124.     // $data = [ 'message' => "Conflit avec le compte ID", 'responseCode' => "409" ];
  125.     // $data = [ 'message' => "Conflit avec le service ID", 'responseCode' => "409" ];
  126.     // $data = [ 'message' => "token n’est plus disponible, vous devez démarrer une nouvelle session", "responseCode" => "410"}]
  127.     return $results;
  128.   }
  129.   public function postRefund($url): array
  130.   {
  131.     $curl curl_init($url);
  132.     curl_setopt($curlCURLOPT_REFERER$this->httpHost);
  133.     curl_setopt($curlCURLOPT_POSTtrue);
  134.     curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
  135.     curl_setopt($curlCURLOPT_SSL_VERIFYPEERfalse);
  136.     $headers = array(
  137.       "Accept: application/json",
  138.       "Content-Type: application/json",
  139.     );
  140.     curl_setopt($curlCURLOPT_HTTPHEADER$headers);
  141.     $response curl_exec($curl);
  142.     $curl_http_code curl_getinfo($curlCURLINFO_HTTP_CODE);
  143.     $curl_error curl_error($curl);
  144.     $curl_errno curl_errno($curl);
  145.     curl_close($curl);
  146.     if ($response === false) {
  147.       throw new \Exception($curl_error$curl_errno);
  148.       return ["null" => null];
  149.     } else {
  150.       $results = [];
  151.       switch ($curl_http_code) {
  152.         case 200:
  153.           $results json_decode($responsetrue);
  154.           break;
  155.         case 401// Accès non autorisé, l'apiKey n'existe pas
  156.           throw $results =  new \Exception('identifiant incorrecte');
  157.           break;
  158.         case 403:
  159.         case 404:
  160.           $response json_decode($responsetrue);
  161.           $results =  $response;
  162.           break;
  163.         case 409// Conflit avec le compte ID ou service ID, l'apiKey existe, mais un conflit avec les valeurs configurées
  164.           $response json_decode($responsetrue);
  165.           $results =  $response;
  166.           break;
  167.         case 410// apiToken n'exite pas ou la date est dépassée, vous devez démarrer une nouvelle session
  168.           throw $results =  new \Exception('identifiant incorrecte');
  169.           break;
  170.         default:
  171.           $response json_decode($responsetrue);
  172.           $results =  $response;
  173.           break;
  174.       }
  175.     }
  176.     return $results;
  177.   }
  178. }