Api23GateTokenService.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Services;
  3. use App\Services\CallApiService;
  4. use Exception;
  5. class Api23GateTokenService
  6. {
  7. private $callApiService;
  8. public function __construct(CallApiService $callApiService)
  9. {
  10. $this->callApiService = $callApiService;
  11. }
  12. public function getToken($api23Key)
  13. {
  14. try {
  15. $response = $this->callApiService->callApi([
  16. 'url' => 'gate-token-api23hash-get',
  17. 'data' => [
  18. 'Api23Key' => $api23Key
  19. ],
  20. 'headers' => $this->buildHeader()
  21. ], 'true');
  22. if ($this->callApiService->verifyApiError($response)) {
  23. if ($response['apiStatus'] === 505) {
  24. $response = $this->callGateTokenApi($api23Key);
  25. if ($this->callApiService->verifyApiError($response)) {
  26. return ['error' => true, 'message' => $response];
  27. }
  28. } else {
  29. return ['error' => true, 'message' => 'Unauthorized'];
  30. }
  31. }
  32. } catch (Exception $e) {
  33. return ['error' => true, 'message' => 'Unauthorized'];
  34. }
  35. return ['error' => false, 'data' => $response['GateToken']];
  36. }
  37. public function callGateTokenApi($api23Key)
  38. {
  39. return $this->callApiService->getGateToken([
  40. 'ClientId' => config('app.api.main.ClientId'),
  41. 'BeforeBase64' => config('app.api.main.BeforeBase64'),
  42. 'Api23eKeyPair' => env('API23E_KEY_PAIR'),
  43. 'Api23Key' => $api23Key,
  44. ], 'main');
  45. }
  46. private function buildHeader($gateToken = null): array
  47. {
  48. $headers = $this->callApiService->basicHeader();
  49. if ($gateToken) {
  50. $headers['GateToken'] = $gateToken;
  51. }
  52. return $headers;
  53. }
  54. }