| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?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');
- // Allows overriding the API key for subaccounts
- $this->apiKey = $apiKey ?? config('services.asaas.api_key');
- }
- 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();
- }
- }
|