GateTokenController.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\CallApiService;
  5. use Exception;
  6. use Illuminate\Support\Str;
  7. class GateTokenController extends Controller
  8. {
  9. private $callApiService;
  10. public function __construct(CallApiService $callApiService)
  11. {
  12. $this->callApiService = $callApiService;
  13. }
  14. public function store()
  15. {
  16. if (empty(request('AppBase64'))) {
  17. return $this->unauthorizedResponse();
  18. }
  19. $apiType = Str::snake(request('ApiType', 'main'));
  20. try {
  21. $query = [
  22. 'ClientId' => config("app.api.{$apiType}.ClientId"),
  23. 'BeforeBase64' => config("app.api.{$apiType}.BeforeBase64"),
  24. 'AppBase64' => request('AppBase64')
  25. ];
  26. $response = $this->callApiService->getGateToken($query, $apiType);
  27. } catch (Exception $e) {
  28. return $this->tokenNotFoundResponse();
  29. }
  30. if (isset($response['GateToken']) && $gateToken = $response['GateToken']) {
  31. return [ 'ApiUrl' => env(Str::upper($apiType) . '_API_URL'), 'GateToken' => $gateToken ];
  32. }
  33. return $this->unauthorizedResponse();
  34. }
  35. public function test()
  36. {
  37. $apiType = Str::snake(request('ApiType', 'main'));
  38. try {
  39. $query = [
  40. 'ClientId' => config("app.api.{$apiType}.ClientId"),
  41. 'BeforeBase64' => config("app.api.{$apiType}.BeforeBase64"),
  42. ];
  43. $response = $this->callApiService->getGateToken($query, 'main');
  44. } catch (Exception $e) {
  45. return $this->tokenNotFoundResponse();
  46. }
  47. if (isset($response['GateToken']) && $gateToken = $response['GateToken']) {
  48. return [ 'GateToken' => $gateToken ];
  49. }
  50. return $this->unauthorizedResponse();
  51. }
  52. public function unauthorizedResponse()
  53. {
  54. return response()->json('Unauthorized', 401);
  55. }
  56. public function tokenNotFoundResponse()
  57. {
  58. return response()->json('GateToken Not Found', 505);
  59. }
  60. }