| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?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();
- }
- public function delete(string $endpoint)
- {
- $response = $this->request()->delete($endpoint);
- if ($response->failed()) {
- throw new AsaasException($response);
- }
- return $response->json();
- }
- }
|