Skip to content

Commit

Permalink
trocando gerador de qrcode
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardokum committed Nov 24, 2023
1 parent 176d392 commit 20b52d1
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"ext-curl": "*",
"ext-json": "*",
"ext-openssl": "*",
"chillerlan/php-qrcode": "*"
"simplesoftwareio/simple-qrcode": "^4.2"
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0|^7.0|^8.0|^9.0|^10.0",
Expand Down
50 changes: 42 additions & 8 deletions src/Boleto/AbstractBoleto.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use Throwable;
use Carbon\Carbon;
use Illuminate\Support\Str;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use Eduardokum\LaravelBoleto\Util;
use SimpleSoftwareIO\QrCode\Generator;
use BaconQrCode\Common\ErrorCorrectionLevel;
use Eduardokum\LaravelBoleto\Boleto\Render\Pdf;
use Eduardokum\LaravelBoleto\Boleto\Render\Html;
use Eduardokum\LaravelBoleto\Boleto\Render\PdfCaixa;
Expand All @@ -32,6 +32,9 @@ abstract class AbstractBoleto implements BoletoContract
const TIPO_CHAVEPIX_CELULAR = 'celular';
const TIPO_CHAVEPIX_EMAIL = 'email';
const TIPO_CHAVEPIX_ALEATORIA = 'aleatoria';
const QRCODE_ESTILO_QUADRADO = 'square';
const QRCODE_ESTILO_PONTO = 'dot';
const QRCODE_ESTILO_ARREDONDADO = 'round';

/**
* Campos necessários para o boleto
Expand Down Expand Up @@ -393,6 +396,11 @@ abstract class AbstractBoleto implements BoletoContract
*/
public $valorRecebido;

/**
* @var string
*/
private $qrCodeStyle = self::QRCODE_ESTILO_QUADRADO;

/**
* Recebe a imagem em base 64 do QR Code do PIX
*
Expand Down Expand Up @@ -1833,6 +1841,30 @@ public function setPixChaveTipo($pixChaveTipo)
return $this;
}

/**
* @return string
*/
public function getQrCodeStyle()
{
return $this->qrCodeStyle;
}

/**
* @param string $qrCodeStyle
* @return AbstractBoleto
* @throws ValidationException
*/
public function setQrCodeStyle($qrCodeStyle)
{
if (! in_array($qrCodeStyle, [self::QRCODE_ESTILO_QUADRADO, self::QRCODE_ESTILO_PONTO, self::QRCODE_ESTILO_ARREDONDADO])) {
throw new ValidationException(sprintf('Estilo QRCODE %s não é válido', $qrCodeStyle));
}

$this->qrCodeStyle = $qrCodeStyle;

return $this;
}

/**
* @return ?string
*/
Expand All @@ -1849,13 +1881,14 @@ public function getPixQrCodeBase64(): ?string
return $this->getPixQrCode();
}

$options = new QROptions([
'eccLevel' => QRCode::ECC_M,
'outputType' => QRCode::OUTPUT_IMAGE_PNG,
'quietzoneSize' => 0,
]);
$qrCode = new Generator();
$qrCode->format('png');
$qrCode->style($this->getQrCodeStyle());
$qrCode->eye('circle');
$qrCode->errorCorrection(ErrorCorrectionLevel::M());
$qrCode->size(400);

return (new QRCode($options))->render($this->getPixQrCode());
return 'data:image/png;base64,' . base64_encode($qrCode->generate($this->getPixQrCode()));
}

/**
Expand Down Expand Up @@ -1977,6 +2010,7 @@ public function validarPix()

/**
* @return string|null
* @throws ValidationException
*/
public function gerarPixCopiaECola()
{
Expand Down
16 changes: 12 additions & 4 deletions src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/
final class Util
{
/**
* @var string[]
*/
public static $bancos = [
'246' => 'Banco ABC Brasil S.A.',
'025' => 'Banco Alfa S.A.',
Expand Down Expand Up @@ -864,7 +867,7 @@ public static function isCnab240($content)
{
$content = is_array($content) ? $content[0] : $content;

return mb_strlen(rtrim($content, "\r\n")) == 240 ? true : false;
return mb_strlen(rtrim($content, "\r\n")) == 240;
}

/**
Expand All @@ -877,7 +880,7 @@ public static function isCnab400($content)
{
$content = is_array($content) ? $content[0] : $content;

return mb_strlen(rtrim($content, "\r\n")) == 400 ? true : false;
return mb_strlen(rtrim($content, "\r\n")) == 400;
}

/**
Expand Down Expand Up @@ -1230,9 +1233,14 @@ public static function formatarUUID($uuid)
* @param $id
* @param Pessoa $beneficiario
* @return string
* @throws ValidationException
*/
public static function gerarPixCopiaECola($pix, $valor, $id, Pessoa $beneficiario)
{
if ($id != Util::normalizeChars($id)) {
throw new ValidationException('ID inválido, não pode possuir caracteres especiais');
}

$crc16 = function ($payload) {
$payload .= '6304';

Expand Down Expand Up @@ -1269,8 +1277,8 @@ public static function gerarPixCopiaECola($pix, $valor, $id, Pessoa $beneficiari
$payload .= $line('53', '986');
$payload .= $line('54', $valor);
$payload .= $line('58', 'BR');
$payload .= $line('59', $beneficiario->getNome());
$payload .= $line('60', $beneficiario->getCidade());
$payload .= $line('59', Util::normalizeChars($beneficiario->getNome()));
$payload .= $line('60', Util::normalizeChars($beneficiario->getCidade()));
$payload .= $line('62', $txId);

return $payload . $crc16($payload);
Expand Down
16 changes: 16 additions & 0 deletions tests/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ public function testFuncaoGerarPixCopiaECola()
$this->assertEquals('00020101021226370014br.gov.bcb.pix0115teste@teste.com52040000530398654031005802BR5914NOME_DA_PESSOA6006CIDADE62160512id-transacao6304DA67', $pixCopiaECola);
}

public function testFuncaoGerarPixCopiaEColaErro()
{
$this->expectException(Exception::class);
Util::gerarPixCopiaECola('teste@teste.com', 100.00, 'id-transação', new Pessoa(['nome' => 'NOME_DA_PESSOA', 'cidade' => 'CIDADE']));
}

public function testFuncaoDecodePixCopiaECola()
{
$pixDecoded = Util::decodePixCopiaECola('00020101021226370014br.gov.bcb.pix0115teste@teste.com52040000530398654031005802BR5914NOME_DA_PESSOA6006CIDADE62160512id-transacao6304DA67');
Expand All @@ -192,6 +198,16 @@ public function testFuncaoDecodePixCopiaECola()
$this->assertEquals('CIDADE', Arr::get($pixDecoded, '60'));
}

public function testFuncaoGerarPixCopiaEColaEDecodePixCopiaECola()
{
$pixDecoded = Util::decodePixCopiaECola(Util::gerarPixCopiaECola('teste@teste.com.br', 123.45, 'id-transacao-tx', new Pessoa(['nome' => 'NOME DA PESSOA', 'cidade' => 'SÃO PAULO'])));
$this->assertEquals('teste@teste.com.br', Arr::get($pixDecoded, '26.01'));
$this->assertEquals('id-transacao-tx', Arr::get($pixDecoded, '62.05'));
$this->assertEquals(123.45, Arr::get($pixDecoded, '54'));
$this->assertEquals('NOME DA PESSOA', Arr::get($pixDecoded, '59'));
$this->assertEquals('SAO PAULO', Arr::get($pixDecoded, '60'));
}

public function testFuncaoTipoChavePix()
{
$this->assertEquals(AbstractBoleto::TIPO_CHAVEPIX_EMAIL, Util::tipoChavePix('teste@teste.com'));
Expand Down

0 comments on commit 20b52d1

Please sign in to comment.