Created
June 1, 2020 17:37
-
-
Save vitorvargasdev/dcdd388d06d282e6e1ecbedc0f09c44e to your computer and use it in GitHub Desktop.
This file contains 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\Domains\Asaas; | |
use GuzzleHttp\Client; | |
use GuzzleHttp\RequestOptions; | |
class Connection | |
{ | |
private $base_url; | |
private $api; | |
private $api_key; | |
private $mode; | |
public function __construct() | |
{ | |
$this->api_key = env('ASAAS_API_KEY'); | |
$this->mode = env('ASAAS_MODE'); | |
$this->api = new Client([ | |
'headers' => [ | |
'Content-Type' => 'application/json', | |
'access_token' => $this->api_key | |
] | |
]); | |
if ($this->mode === 'sandbox') { | |
$this->base_url = 'https://sandbox.asaas.com/api/v3/'; | |
} | |
if ($this->mode === 'production') { | |
$this->base_url = 'https://www.asaas.com/api/v3/'; | |
} | |
} | |
public function get($url) | |
{ | |
$response = $this->api->get($this->base_url . $url); | |
return $response->getBody()->getContents(); | |
} | |
public function post($url, $params, $json) | |
{ | |
if ($json === true) { | |
$response = $this->api->post($this->base_url . $url, [ | |
\GuzzleHttp\RequestOptions::JSON => $params | |
]); | |
} else { | |
$response = $this->api->post($this->base_url . $url, $params); | |
} | |
return [ | |
'code' => $response->getStatusCode(), | |
'response' => json_decode($response->getBody()->getContents()) | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment