Skip to content

Commit

Permalink
inter api 3 and pix
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardokum committed Nov 13, 2023
1 parent d00a724 commit 7147f29
Show file tree
Hide file tree
Showing 10 changed files with 391 additions and 98 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"setasign/fpdf": "^1.8",
"ext-curl": "*",
"ext-json": "*",
"ext-openssl": "*"
"ext-openssl": "*",
"chillerlan/php-qrcode": "^4.3"
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0",
Expand Down
90 changes: 61 additions & 29 deletions src/Api/AbstractAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

namespace Eduardokum\LaravelBoleto\Api;

use stdClass;
use Exception;
use Illuminate\Support\Str;
use Eduardokum\LaravelBoleto\Util;
use Eduardokum\LaravelBoleto\Pessoa;
use Eduardokum\LaravelBoleto\Api\Exception\CurlException;
use Eduardokum\LaravelBoleto\Api\Exception\HttpException;
use Eduardokum\LaravelBoleto\Api\Exception\MissingDataException;
use Eduardokum\LaravelBoleto\Contracts\Pessoa as PessoaContract;
use Eduardokum\LaravelBoleto\Api\Exception\UnauthorizedException;
use Eduardokum\LaravelBoleto\Contracts\Boleto\BoletoAPI as BoletoAPIContract;

abstract class AbstractAPI
Expand Down Expand Up @@ -66,7 +72,7 @@ abstract class AbstractAPI
*
* @param array $params
*
* @throws \Exception
* @throws MissingDataException
*/
public function __construct($params = [])
{
Expand All @@ -79,7 +85,7 @@ public function __construct($params = [])
}
}
if (count($missing) > 0) {
throw new Exception\MissingDataException($missing);
throw new MissingDataException($missing);
}
}

Expand All @@ -90,28 +96,34 @@ public function __destruct()
}
}

//
// public function __construct($baseUrl, $conta, $certificado, $certificadoChave, $certificadoSenha = null)
// {
// $this->baseUrl = $baseUrl;
// $this->conta = $conta;
// $this->certificado = $certificado;
// $this->certificadoChave = $certificadoChave;
// $this->certificadoSenha = $certificadoSenha;
// }

abstract protected function headers();

abstract public function createBoleto(BoletoAPIContract $boleto);

abstract public function retrieveNossoNumero($nossoNumero);

abstract public function retrieveID($id);

abstract public function cancelNossoNumero($nossoNumero, $motivo);

abstract public function cancelID($id, $motivo);

abstract public function retrieveList($inputedParams = []);

abstract public function getPdfNossoNumero($nossoNumero);

abstract public function getPdfID($id);

/**
* @param $url
* @param $type
* @return mixed
* @throws Exception
*/
public function createWebhook($url, $type = 'all') {
throw new Exception('Método não disponível no banco');
}

public function retrieve(BoletoAPIContract $boleto)
{
return $this->retrieveNossoNumero($boleto->getNossoNumero());
Expand Down Expand Up @@ -349,7 +361,7 @@ public function getBeneficiario()
* @param array $beneficiario
*
* @return AbstractAPI
* @throws \Exception
* @throws Exception
*/
public function setBeneficiario($beneficiario): AbstractAPI
{
Expand Down Expand Up @@ -446,8 +458,7 @@ protected function setRequestInfo($requestInfo)
}

/**
* @throws Exception\CurlException
* @throws Exception\HttpException|Exception\UnauthorizedException
* @throws HttpException|UnauthorizedException|CurlException
*/
protected function post($url, array $post, $raw = false)
{
Expand All @@ -471,12 +482,36 @@ protected function post($url, array $post, $raw = false)
return $this->execute();
}

/**
* @throws HttpException|UnauthorizedException|CurlException
*/
protected function put($url, array $post, $raw = false)
{
$url = ltrim($url, '/');
$this->init()
->setHeaders(array_filter([
'Accept' => $raw ? null : 'application/json',
'Content-type' => $raw ? 'application/x-www-form-urlencoded' : 'application/json',
]));

// clean string
$post = $this->arrayMapRecursive(function ($data) {
return Util::normalizeChars($data);
}, $post);

curl_setopt($this->curl, CURLOPT_URL, $this->getBaseUrl().$url);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $raw ? http_build_query($post) : json_encode($post));

return $this->execute();
}

/**
* @param $url
*
* @return \stdClass
* @throws Exception\HttpException
* @throws Exception\CurlException|Exception\UnauthorizedException
* @return stdClass
* @throws HttpException|UnauthorizedException|CurlException
*/
protected function get($url)
{
Expand Down Expand Up @@ -563,11 +598,11 @@ private function convertHeaders(array $headers)
/**
* @param $response
*
* @return \stdClass
* @return stdClass
*/
private function parseResponse($response)
{
$retorno = new \stdClass();
$retorno = new stdClass();
$retorno->headers_text = substr($response, 0, strpos($response, "\r\n\r\n"));
$retorno->body_text = substr($response, strpos($response, "\r\n\r\n"));

Expand All @@ -588,25 +623,22 @@ private function parseResponse($response)
/**
* @param $retorno
*
* @throws Exception\HttpException
* @throws Exception\UnauthorizedException
* @throws HttpException|UnauthorizedException
*/
private function handleException($retorno)
{
if ($this->getResponseHttpCode() < 200 || $this->getResponseHttpCode() > 299) {
if (in_array($this->getResponseHttpCode(), [401, 403]) && empty($retorno->body_text)) {
throw new Exception\UnauthorizedException($this->getBaseUrl(), $this->getCertificado(), $this->getCertificadoChave(), $this->getCertificadoSenha());
throw new UnauthorizedException($this->getBaseUrl(), $this->getCertificado(), $this->getCertificadoChave(), $this->getCertificadoSenha());
}

throw new Exception\HttpException($this->getResponseHttpCode(), $this->getRequestInfo(), $retorno->body_text);
throw new HttpException($this->getResponseHttpCode(), $this->getRequestInfo(), $retorno->body_text);
}
}

/**
* @return false|\stdClass
* @throws Exception\CurlException
* @throws Exception\HttpException
* @throws Exception\UnauthorizedException
* @return false|stdClass
* @throws CurlException|HttpException|UnauthorizedException
*/
private function execute()
{
Expand Down Expand Up @@ -652,7 +684,7 @@ private function execute()
curl_close($this->curl);
$this->curl = null;
if (! $this->getResponseHttpCode() && $error) {
throw new Exception\CurlException($error);
throw new CurlException($error);
}

return false;
Expand Down
Loading

0 comments on commit 7147f29

Please sign in to comment.