Laravel Boleto

repository·master·Indexed 20 days ago

https://github.com/eduardokum/laravel-boleto

A PHP package for Laravel applications to manage the full lifecycle of Brazilian bank slips (boletos), including generation, remittance file creation, and processing bank return files. It supports banks such as Banco do Brasil, Banco do Nordeste, and Bancoob. Compatible with PHP 7.2+ and Laravel 6+.

Tokens
29.1K
Snippets
88
Records
105
Agent score
67%

What's inside laravel-boleto

  1. Overview of Laravel Boleto

    master

    Laravel Boleto is a PHP package designed for generating boletos (Brazilian bank slips), creating remittance files (remessas), and reading return files (leitura de retorno).

    Compatibility Requirements:

    • PHP: 7.2 or higher (always use the latest version).
    • Laravel: Support for Laravel 6 and newer versions.
  2. Understand Remessa types and bank compatibility

    master

    Remessas (remittance files) in this library come in two formats: 240 positions and 400 positions.

    Availability varies by bank. Some banks support both, while others only support one or the other. Note that some banks marked with an asterisk (*) require homologation (official testing/approval with the bank) before use.

    Bank240 Positions400 Positions
    Bancoobyesyes
    Banrisulyes*yes
    Banco do Brasilyes*yes
    Banco do Nordestenoyes*
    Bradescoyes*yes
    Caixa Econômica Federalyes*yes
    HSBCnoyes
    Itaúyes*yes
    Santanderyes*yes
    Sicrediyes*yes
  3. Create a Boleto instance

    master

    To create a Boleto instance, you must first instantiate two Pessoa objects: the recipient (beneficiário) and the payer (pagador). Because different banks have unique requirements and logic, you should consult the specific documentation for the bank you are integrating with after setting up these entities.

    // Conceptual workflow:
    // 1. Create Recipient (Pessoa)
    // 2. Create Payer (Pessoa)
    // 3. Create Boleto using both Pessoa instances and bank-specific configuration
  4. Generate a Boleto for Caixa Econômica Federal

    master

    To create a boleto for Caixa Econômica Federal, use the Eduardokum\LaravelBoleto\Boleto\Banco\Caixa class. This bank requires specific mandatory fields: numero (Bill number, size 15), agencia (Account keeping agency, size 4), and codigoCliente (Recipient number, size 6).

    You can instantiate the object using a fluent setter pattern or by passing a configuration array to the constructor.

    $caixa = new Eduardokum\LaravelBoleto\Boleto\Banco\Caixa;
    $caixa->setLogo('/path/to/logo.png')
        ->setDataVencimento('1997-10-07')
        ->setValor('100')
        ->setNumero(1)
        ->setNumeroDocumento(1)
        ->setPagador($pagador)
        ->setBeneficiario($beneficiario)
        ->setCarteira('RG')
        ->setAgencia(1111)
        ->setCodigoCliente(222222)
        ->setDescricaoDemonstrativo(['demonstrativo 1', 'demonstrativo 2', 'demonstrativo 3'])
        ->setInstrucoes(['instrucao 1', 'instrucao 2', 'instrucao 3']);
  5. Configure a Bradesco Boleto

    master

    To create a Bradesco boleto, you can use the Eduardokum\LaravelBoleto\Boleto\Banco\Bradesco class. You can initialize it using a fluent setter pattern or by passing a configuration array to the constructor.

    Mandatory Fields

    • numero: Bill number (size: 11).
    • agencia: Account keeping agency (size: 4).
    • conta: Account number (size: 7).
    • cip: Code used to identify specific messages to the recipient (size: 3, defaults to 000).

    Available Bank Contracts (carteira)

    • 09: Com Registro
    • 21: Com Registro - Pagável somente no Bradesco
    • 26: Com Registro – Emissão na Internet
    $bradesco = new Eduardokum\LaravelBoleto\Boleto\Banco\Bradesco;
    $bradesco->setLogo('/path/to/logo.png')
        ->setDataVencimento('1997-10-07')
        ->setValor('100')
        ->setNumero(1)
        ->setNumeroDocumento(1)
        ->setPagador($pagador)
        ->setBeneficiario($beneficiario)
        ->setCarteira('09')
        ->setAgencia(1111)
        ->setConta(2222222)
        ->setDescricaoDemonstrativo(['demonstrativo 1', 'demonstrativo 2', 'demonstrativo 3'])
        ->setInstrucoes(['instrucao 1', 'instrucao 2', 'instrucao 3']);
  6. Render multiple Boletos as HTML or Carnê

    master

    To generate HTML content for multiple boletos, use the Eduardokum\LaravelBoleto\Boleto\Render\Html class.

    Workflow

    1. Instantiate Html.
    2. Add boletos using addBoleto($boleto) or addBoletos([$b1, $b2]).
    3. Configure display options using showPrint() or hideInstrucoes().
    4. Execute rendering using gerarBoleto() for standard HTML or gerarCarne() for the 'carnê' format.

    Both methods return an HTML string.

    use Eduardokum\LaravelBoleto\Boleto\Render\Html;
    
    $html = new Html();
    $html->addBoletos([$boleto1, $boleto2]);
    
    $html->showPrint();
    $html->hideInstrucoes();
    
    // Returns standard HTML string
    $htmlString = $html->gerarBoleto();
    
    // Returns 'carnê' format HTML string
    $carneString = $html->gerarCarne();
  7. Generate Sicredi CNAB remessa files

    master

    To generate a CNAB remessa file for Sicredi, use the Eduardokum\LaravelBoleto\Cnab\Remessa classes. You can choose between the Cnab400 format or the Cnab240 format depending on your bank's requirements.

    Mandatory fields for Sicredi include:

    • idremessa: Sequence number of the send (size: 7).
    • agencia: Account keeping agency (size: 4).
    • conta: Account number (size: 5).
    • codigoCliente: Customer/Beneficiary code with the banking institution (size: 5). Note: This is often the account number without the check digit, but may vary.

    You can instantiate the class using either a fluent setter pattern or by passing an associative array to the constructor.

    // Using Cnab400
    $send = new Eduardokum\LaravelBoleto\Cnab\Remessa\Cnab400\Banco\Sicredi;
    $send->setBeneficiario($beneficiario)
        ->setIdremessa(1)
        ->setCarteira(1)
        ->setAgencia(1111)
        ->setConta(22222)
        ->setCodigoCliente(12345);
    
    // Using Cnab240
    $send = new Eduardokum\LaravelBoleto\Cnab\Remessa\Cnab240\Banco\Sicredi;
    $send->setBeneficiario($beneficiario)
        ->setIdremessa(1)
        ->setCarteira(1)
        ->setAgencia(1111)
        ->setConta(22222)
        ->setCodigoCliente(12345);
  8. Generate and manage Remessa files

    master

    All banks share common methods for file generation, regardless of whether they use 240 or 400 positions. To generate a file, you must first add Boleto instances to a Remessa object.

    Common operations include:

    • Adding a single boleto: addBoleto(Boleto $boleto)
    • Adding multiple boletos: addBoletos(array $boletos)
    • Generating the file content as a string: gerar()
    • Saving the file to a specific disk path: save($path)
    • Forcing a file download in a web context: download($filename = null)
    // Add a single bill to a send object. Here need a instance of Boleto.
    $send->addBoleto(Boleto $detalhe);
    
    // Add multiples bill to a send object. Here need a array of instances of Boleto.
    $send->addBoletos(Boleto[] $boletos);
    
    // Return a string of file.
    // It depends on the instance, 240 or 400 positions.
    $send->gerar();
    
    // Saves the string to a file on the disk whose path was passed in $path argument.
    $send->save($path);
    
    // Force file download.
    // If you pass the $filename argument it overwrites the name in the download.
    $send->download($filename = null);
  9. Create a Pagador (Payer) instance

    master

    In laravel-boleto, a Pagador (the person receiving the charge) is represented by an instance of the \Eduardokum\LaravelBoleto\Pessoa class. You can instantiate it using fluent setter methods or by passing an associative array to the constructor.

    Note that a bill requires an instance of Pagador to function correctly.

    // Option 1: Using fluent setter methods
    $pagador = new \Eduardokum\LaravelBoleto\Pessoa;
    $pagador->setDocumento('00.000.000/0000-00')
        ->setNome('Company co.')
        ->setCep('00000-000')
        ->setEndereco('Street name, 123')
        ->setBairro('district')
        ->setUf('UF')
        ->setCidade('City');
    
    // Option 2: Using the constructor with an array
    $pagador = new \Eduardokum\LaravelBoleto\Pessoa([
        'documento' => '00.000.000/0000-00',
        'nome'      => 'Company co.',
        'cep'       => '00000-000',
        'endereco'  => 'Street name, 123',
        'bairro'    => 'district',
        'uf'        => 'UF',
        'cidade'    => 'City',
    ]);
  10. Process CNAB return files using the Factory

    master

    The Factory class can automatically detect the bank and the CNAB format (240 or 400 positions) from your input. This is the recommended way to instantiate a return object without manually specifying the bank or format.

    Supported input types for the constructor:

    • A file path (string)
    • String content (newline separated lines)
    • An array of strings (one per line)

    After instantiation, use processar() to parse the file. You can identify the bank using getTipo() and getCodigoBanco().

    // The Factory will guess what the return is (240 or 400) and which bank,
    // then return the instantiated object.
    $return = \Eduardokum\LaravelBoleto\Cnab\Retorno\Factory::make($argument);
    
    // To process the file
    $return->processar();
    
    // You can know the type of bank after instantiation
    $type = $return->getTipo();
    $code = $return->getCodigoBanco();
  11. Install laravel-boleto via Composer

    master

    To install the laravel-boleto package, use Composer. You can either run the require command directly from your terminal or manually add the dependency to your composer.json file.

    Requirements

    • PHP intl extension
    • PHP 5.6 or 7.0+ (PHP 7.0 or higher is strongly recommended)
    composer require "eduardokum/laravel-boleto"
  12. Generate Banrisul CNAB remessa files

    master

    To generate a Banrisul remessa file, you can use either the CNAB400 or CNAB240 format classes. You can instantiate these classes using a fluent setter pattern or by passing an associative array to the constructor.

    Mandatory Fields

    When configuring Banrisul, the following fields are required:

    • agencia: Account keeping agency (size: 4).
    • conta: Account number (size: 5).
    • codigoCliente: Recipient number (size: 13).
    • codigoClienteOfficeBanking: Recipient number (size: 10). Note: This field is required when the Bank contract is 'R', 'S', or 'X'.
    // Using the fluent setter pattern for CNAB400
    $send = new Eduardokum\LaravelBoleto\Cnab\Remessa\Cnab400\Banco\Banrisul;
    
    $send->setBeneficiario($beneficiario)
        ->setCarteira(1)
        ->setAgencia(1111)
        ->setCodigoCliente(1234567)
        // ->setCodigoClienteOfficeBanking('1234567890')
        ->setConta(22222);