| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace App\Services\Integrations\Asaas;
- use App\Exceptions\AsaasException;
- use Illuminate\Http\Client\PendingRequest;
- use Illuminate\Support\Facades\Http;
- class AsaasClient
- {
- protected string $baseUrl;
- protected string $apiKey;
- public function __construct(?string $apiKey = null)
- {
- $this->baseUrl = config('services.asaas.base_url');
- // Chave explícita (ex.: conta da unidade) tem prioridade. Sem ela, usa a
- // chave da matriz resolvida do banco (UI) com fallback para o .env.
- $this->apiKey = $apiKey ?? \App\Models\CompanyPaymentAccount::resolveApiKey();
- }
- protected function request(): PendingRequest
- {
- return Http::withHeaders([
- 'access_token' => $this->apiKey,
- ])->baseUrl($this->baseUrl);
- }
- public function get(string $endpoint, array $query = [])
- {
- $response = $this->request()->get($endpoint, $query);
- if ($response->failed()) {
- throw new AsaasException($response);
- }
- return $response->json();
- }
- public function post(string $endpoint, array $data = [])
- {
- $response = $this->request()->post($endpoint, $data);
- if ($response->failed()) {
- throw new AsaasException($response);
- }
- return $response->json();
- }
- public function delete(string $endpoint)
- {
- $response = $this->request()->delete($endpoint);
- if ($response->failed()) {
- throw new AsaasException($response);
- }
- return $response->json();
- }
- }
|