Skip to content

Commit

Permalink
Merge pull request #1 from diorgesl/analysis-pe1j62
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
diorgesl authored Sep 30, 2020
2 parents 69cc102 + 8714107 commit b2bb416
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 93 deletions.
8 changes: 4 additions & 4 deletions config/diorgesbb.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
'developer_application_key' => '',
'client_id' => '',
'client_secret' => '',
'basic' => ''
'basic' => '',
],
'producao' => [
'agenciaBeneficiario' => '', // SEM DIFITO VERIFICADOR
Expand All @@ -36,7 +36,7 @@
'developer_application_key' => '',
'client_id' => '',
'client_secret' => '',
'basic' => ''
]
]
'basic' => '',
],
],
];
15 changes: 7 additions & 8 deletions src/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@

namespace Diorgesl\DiorgesBB;

use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;

class Auth
{
public static function obtainAccessToken() {
public static function obtainAccessToken()
{
$client = new Client();
$uri = !config('diorgesbb.production') ? 'https://oauth.hm.bb.com.br' : 'https://oauth.bb.com.br';
$secrets = !config('diorgesbb.production') ? config('diorgesbb.api.homologa') : config('diorgesbb.api.producao');
$uri = ! config('diorgesbb.production') ? 'https://oauth.hm.bb.com.br' : 'https://oauth.bb.com.br';
$secrets = ! config('diorgesbb.production') ? config('diorgesbb.api.homologa') : config('diorgesbb.api.producao');

$basic = $secrets['basic'];
$formaBasic = base64_encode($secrets['client_id'] . ':' . $secrets['client_secret']);
$formaBasic = base64_encode($secrets['client_id'].':'.$secrets['client_secret']);
if ($basic != $formaBasic) {
$basic = $formaBasic;
}

$res = $client->request('POST', $uri.'/oauth/token?grant_type=client_credentials&scope=cobrancas.boletos-info cobrancas.boletos-requisicao', [
'headers' => [
'Authorization' => 'Basic '. $basic,
'Authorization' => 'Basic '.$basic,
'Accept' => 'application/json',

]
],
]);
$access_token = json_decode($res->getBody()->getContents())->access_token;

return $access_token;

}
}
29 changes: 18 additions & 11 deletions src/Avalista.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,50 @@ public function __construct($params = [])
if (method_exists($this, 'getProtectedFields') && in_array(lcfirst($param), $this->getProtectedFields())) {
continue;
}
if (method_exists($this, 'set' . ucwords($param))) {
$this->{'set' . ucwords($param)}($value);
if (method_exists($this, 'set'.ucwords($param))) {
$this->{'set'.ucwords($param)}($value);
}
}
}

public function setNomeRegistro($nomeRegistro){
public function setNomeRegistro($nomeRegistro)
{
$this->nomeRegistro = $nomeRegistro;
}

public function setNumeroRegistro($numeroRegistro){
public function setNumeroRegistro($numeroRegistro)
{
$this->numeroRegistro = $numeroRegistro;
}

public function setTipoRegistro($tipoRegistro){
public function setTipoRegistro($tipoRegistro)
{
$this->tipoRegistro = $tipoRegistro;
}

public function getNomeRegistro(){
public function getNomeRegistro()
{
return $this->nomeRegistro;
}

public function getNumeroRegistro(){
public function getNumeroRegistro()
{
return $this->numeroRegistro;
}

public function getTipoRegistro(){
public function getTipoRegistro()
{
return strlen($this->numeroRegistro) <= 11 ? 1 : 2;
}

public function jsonSerialize (){
public function jsonSerialize()
{
$arr = [];
foreach(get_class_methods($this) as $method){
foreach (get_class_methods($this) as $method) {
if (strpos($method, 'get') !== false) {
$value = $this->{$method}();

if(!empty($value) || strlen(trim($value)) > 0){
if (! empty($value) || strlen(trim($value)) > 0) {
$arr[lcfirst(str_replace(['set', 'get'], ['', ''], $method))] = $value;
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/Boleto.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@

namespace Diorgesl\DiorgesBB;

use Diorgesl\DiorgesBB\Avalista;
use Diorgesl\DiorgesBB\Pagador;

class Boleto implements \JsonSerializable
{

/*
* Identificador determinado pelo sistema Cobrança para controlar a emissão
* de boletos, liquidação, crédito de valores ao Beneficiário e intercâmbio
Expand Down Expand Up @@ -237,13 +233,13 @@ class Boleto implements \JsonSerializable
public function __construct($params = [])
{
$api = config('diorgesbb.api.homologa');
if(config('diorgesbb.production')){
if (config('diorgesbb.production')) {
$api = config('diorgesbb.api.producao');
}

foreach($api as $k => $v){
if (method_exists($this, 'set' . ucwords($k))) {
$this->{'set' . ucwords($k)}($v);
foreach ($api as $k => $v) {
if (method_exists($this, 'set'.ucwords($k))) {
$this->{'set'.ucwords($k)}($v);
}
}

Expand All @@ -252,8 +248,8 @@ public function __construct($params = [])
if (method_exists($this, 'getProtectedFields') && in_array(lcfirst($param), $this->getProtectedFields())) {
continue;
}
if (method_exists($this, 'set' . ucwords($param))) {
$this->{'set' . ucwords($param)}($value);
if (method_exists($this, 'set'.ucwords($param))) {
$this->{'set'.ucwords($param)}($value);
}
}
}
Expand Down Expand Up @@ -535,8 +531,9 @@ public function setTextoCampoUtilizacaoBeneficiario($textoCampoUtilizacaoBenefic
*/
public function getNumeroTituloCliente()
{
$convenio = str_pad($this->getNumeroConvenio(), 10, "0", STR_PAD_LEFT);
$numeroTituloCliente = str_pad($this->numeroTituloCliente, 10, "0", STR_PAD_LEFT);
$convenio = str_pad($this->getNumeroConvenio(), 10, '0', STR_PAD_LEFT);
$numeroTituloCliente = str_pad($this->numeroTituloCliente, 10, '0', STR_PAD_LEFT);

return $convenio.$numeroTituloCliente;
}

Expand Down Expand Up @@ -611,6 +608,7 @@ public function getPagador()
public function setPagador($pagador)
{
$this->pagador = new Pagador($pagador);

return $this;
}

Expand All @@ -629,21 +627,23 @@ public function getAvalista()
public function setAvalista($avalista)
{
$this->avalista = new Avalista($avalista);

return $this;
}

public function jsonSerialize () {
public function jsonSerialize()
{
$arr = [];
foreach(get_class_methods($this) as $method){
foreach (get_class_methods($this) as $method) {
if (strpos($method, 'get') !== false) {
$value = $this->{$method}();

if(is_numeric($value)){
if($value > 0){
if (is_numeric($value)) {
if ($value > 0) {
$arr[lcfirst(str_replace(['set', 'get'], ['', ''], $method))] = $value;
}
}else{
if(!empty($value) || strlen(trim($value)) > 0){
} else {
if (! empty($value) || strlen(trim($value)) > 0) {
$arr[lcfirst(str_replace(['set', 'get'], ['', ''], $method))] = $value;
}
}
Expand Down
63 changes: 32 additions & 31 deletions src/Boletos.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

namespace Diorgesl\DiorgesBB;

use Diorgesl\DiorgesBB\Auth;
use Diorgesl\DiorgesBB\Boleto;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;

use GuzzleHttp\Exception\GuzzleException;

class Boletos
{
Expand All @@ -19,79 +16,83 @@ public function __construct(Client $client)
{
$token = Auth::obtainAccessToken();
$this->client = $client;
$this->token = $token;
$this->uri = !config('diorgesbb.production') ? 'https://api.hm.bb.com.br/cobrancas/v1/boletos' : 'https://api.bb.com.br/cobrancas/v1/boletos';
$this->secrets = !config('diorgesbb.production') ? config('diorgesbb.api.homologa') : config('diorgesbb.api.producao');
$this->token = $token;
$this->uri = ! config('diorgesbb.production') ? 'https://api.hm.bb.com.br/cobrancas/v1/boletos' : 'https://api.bb.com.br/cobrancas/v1/boletos';
$this->secrets = ! config('diorgesbb.production') ? config('diorgesbb.api.homologa') : config('diorgesbb.api.producao');
}

public function boleto($id){
$convenio = str_pad($this->secrets['numeroConvenio'], 10, "0", STR_PAD_LEFT);
$boleto = str_pad($id, 10, "0", STR_PAD_LEFT);
public function boleto($id)
{
$convenio = str_pad($this->secrets['numeroConvenio'], 10, '0', STR_PAD_LEFT);
$boleto = str_pad($id, 10, '0', STR_PAD_LEFT);
try {
$res = $this->client->request('GET', $this->uri . '/' . $convenio . $boleto, [
$res = $this->client->request('GET', $this->uri.'/'.$convenio.$boleto, [
'query' => [
'gw-dev-app-key' => $this->secrets['developer_application_key'],
'numeroConvenio' => $this->secrets['numeroConvenio'],
],
'headers' => [
'Authorization' => 'Bearer ' . $this->token
]
'Authorization' => 'Bearer '.$this->token,
],
]);

return json_decode($res->getBody()->getContents());
}catch (GuzzleException $e) {
} catch (GuzzleException $e) {
$this->__responseException($e);
}
}

public function boletos($params = []) {
public function boletos($params = [])
{
$query = [
'gw-dev-app-key' => $this->secrets['developer_application_key'],
'agenciaBeneficiario' => $this->secrets['agenciaBeneficiario'],
'contaBeneficiario' => $this->secrets['contaBeneficiario'],
'indicadorSituacao' => 'A'
'indicadorSituacao' => 'A',
];

$query = array_merge($query, $params);

try {
$request = $this->client->get($this->uri. '/', [
$request = $this->client->get($this->uri.'/', [
'query' => $query,
'headers' => [
'Authorization' => 'Bearer '. $this->token
]
'Authorization' => 'Bearer '.$this->token,
],
]);

return json_decode($request->getBody()->getContents());
}catch (GuzzleException $e) {
} catch (GuzzleException $e) {
$this->__responseException($e);
}
}

public function registrar(Boleto $boleto)
{
try {
$res = $this->client->request("POST", $this->uri, [
"query" => [
"gw-dev-app-key" => $this->secrets["developer_application_key"],
$res = $this->client->request('POST', $this->uri, [
'query' => [
'gw-dev-app-key' => $this->secrets['developer_application_key'],
],
'body' => json_encode($boleto),
'headers' => [
'Authorization' => 'Bearer '.$this->token,
'Content-Type' => 'application/json',
],
"body" => json_encode($boleto),
"headers" => [
"Authorization" => "Bearer " . $this->token,
"Content-Type" => "application/json",
]
]);

return json_decode($res->getBody()->getContents());
}catch (GuzzleException $e){
} catch (GuzzleException $e) {
$this->__responseException($e);
}
}

public function verificarRegisto(array $boletos)
{

}

protected function __responseException(GuzzleException $e){
protected function __responseException(GuzzleException $e)
{
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
throw new \Exception($responseBodyAsString);
Expand Down
Loading

0 comments on commit b2bb416

Please sign in to comment.