Last active
July 28, 2024 14:19
-
-
Save LucasHayashi/f20eb7928d60a47b5813caab5ef2b37a to your computer and use it in GitHub Desktop.
Controlador para API's de atenticação da Bling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Api; | |
use App\Http\Controllers\Controller; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Http; | |
use App\Models\OAuthToken; | |
class BlingApiController extends Controller | |
{ | |
private $apiUrl = "https://www.bling.com.br/Api/v3"; | |
public function callback(Request $request) | |
{ | |
$code = $request->query('code'); | |
if ($code) { | |
$response = Http::withBasicAuth(env('BLING_CLIENT_ID'), env('BLING_CLIENT_SECRET')) | |
->withHeaders([ | |
'Content-Type' => 'application/x-www-form-urlencoded', | |
'Accept' => '1.0' | |
]) | |
->asForm() | |
->post($this->apiUrl . '/oauth/token', [ | |
'grant_type' => 'authorization_code', | |
'code' => $code | |
]); | |
if ($response->successful()) { | |
$data = $response->json(); | |
if (isset($data['access_token']) && isset($data['refresh_token'])) { | |
OAuthToken::truncate(); | |
OAuthToken::create([ | |
'access_token' => $data['access_token'], | |
'refresh_token' => $data['refresh_token'] | |
]); | |
return response()->json(['message' => 'Tokens salvos com sucesso.']); | |
} | |
} | |
return response()->json(['error' => 'Erro ao renovar tokens.', 'detalhes' => $response->json('error')], 400); | |
} | |
return response()->json(['error' => 'Código de autorização não fornecido.'], 400); | |
} | |
public function renovarTokens($refresh_token) | |
{ | |
if ($refresh_token) { | |
$response = Http::withBasicAuth(env('BLING_CLIENT_ID'), env('BLING_CLIENT_SECRET')) | |
->withHeaders([ | |
'Content-Type' => 'application/x-www-form-urlencoded', | |
'Accept' => '1.0' | |
]) | |
->asForm() | |
->post($this->apiUrl . '/oauth/token', [ | |
'grant_type' => 'refresh_token', | |
'refresh_token' => $refresh_token | |
]); | |
if ($response->successful()) { | |
$data = $response->json(); | |
if (isset($data['access_token']) && isset($data['refresh_token'])) { | |
OAuthToken::truncate(); | |
OAuthToken::create([ | |
'access_token' => $data['access_token'], | |
'refresh_token' => $data['refresh_token'] | |
]); | |
return ['access_token' => $data['access_token']]; | |
} | |
} | |
return ['error' => 'Erro ao obter tokens.', 'detalhes' => $response->json('error')]; | |
} | |
return ['error' => 'Refresh token não fornecido.']; | |
} | |
private function obterTokens() | |
{ | |
return OAuthToken::all()->first(); | |
} | |
public function categorias() | |
{ | |
$token = $this->obterTokens(); | |
if (!$token) { | |
return response()->json(['error' => 'Token não encontrado.'], 400); | |
} | |
$access_token = $token['access_token']; | |
$refresh_token = $token['refresh_token']; | |
$categorias = Http::withToken($access_token) | |
->get($this->apiUrl . '/categorias/produtos'); | |
if ($categorias->failed()) { | |
if ($categorias->status() === 401) { | |
$new_access_token = $this->renovarTokens($refresh_token); | |
if ($new_access_token['error']) { | |
return response()->json($new_access_token, 400); | |
} else { | |
$categorias = Http::withToken($new_access_token['access_token']) | |
->get($this->apiUrl . '/categorias/produtos'); | |
} | |
} else { | |
return response()->json(['error' => 'Erro ao obter categorias.', 'detalhes' => $categorias->json('error')], 400); | |
} | |
} | |
return $categorias->json(); | |
} | |
} |
Código desenvolvido em Larave 11.x
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Como vou implementar no máximo 2 endpoints não me preocupei muito com a separação de dependencias, espero que ajude!