AsaasClient.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Services\Integrations\Asaas;
  3. use App\Exceptions\AsaasException;
  4. use Illuminate\Http\Client\PendingRequest;
  5. use Illuminate\Support\Facades\Http;
  6. class AsaasClient
  7. {
  8. protected string $baseUrl;
  9. protected string $apiKey;
  10. public function __construct(?string $apiKey = null)
  11. {
  12. $this->baseUrl = config('services.asaas.base_url');
  13. // Allows overriding the API key for subaccounts
  14. $this->apiKey = $apiKey ?? config('services.asaas.api_key');
  15. }
  16. protected function request(): PendingRequest
  17. {
  18. return Http::withHeaders([
  19. 'access_token' => $this->apiKey,
  20. ])->baseUrl($this->baseUrl);
  21. }
  22. public function get(string $endpoint, array $query = [])
  23. {
  24. $response = $this->request()->get($endpoint, $query);
  25. if ($response->failed()) {
  26. throw new AsaasException($response);
  27. }
  28. return $response->json();
  29. }
  30. public function post(string $endpoint, array $data = [])
  31. {
  32. $response = $this->request()->post($endpoint, $data);
  33. if ($response->failed()) {
  34. throw new AsaasException($response);
  35. }
  36. return $response->json();
  37. }
  38. }