PHP code example of gildonei / unicred

1. Go to this page and download the library: Download gildonei/unicred library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gildonei / unicred example snippets





use \Unicred\Environment;
use \Unicred\UnicredApi;
use \Unicred\Entity\Assignor;
use \Unicred\Entity\Payer;
use \Unicred\Entity\Address;
use \Unicred\Entity\BankSlip;
use \Unicred\Request\AuthenticationRequest;
use \Unicred\Exception\UnicredRequestException;

try {
    #Preparando o ambiente
    $ambiente = Environment::sandbox();

    #Dados do Cedente (Cliente Unicred)
    $cedente = new Assignor();
    $cedente->setBankAgency('BANK AGENCY NUMBER')
        ->setApiKey('SUA API KEY')
        ->setUser('SEU LOGIN')
        ->setPassword('SUA SENHA')
        ->setPayeeCode('SEU CÓDIGO DE BENEFICIÁRIO')
        ->setPortifolio('CÓDIGO VARIAÇÃO DA CARTEIRA');

    #Obter o Token de Acesso
    $autenticacao = new AuthenticationRequest($cedente, $ambiente);
    $autenticacao->execute();

    #Dados do boleto
    $parcelaId = 12304;
    $boleto = new BankSlip();
    $boleto->setDueDate(new DateTime())
        ->setValue(10.45)
        ->setYourNumber($parcelaId)
        ->setBankSlipNumber($parcelaId)
		->getPayer()
            ->setName('Fulano de Tal')
            ->setPayerType(PAYER::PERSON)
            ->setDocument('44675831141')
            ->setEmail('[email protected]')
            ->setCorporateName('Fulano de Tal')
        ->getAddress()
            ->setState('SC')
            ->setDistrict('Bairro')
            ->setCity('Cidade')
            ->setAddress('Logradouro com Número')
            ->setZip('99999-999');
	$boleto->getDiscount()
		->setIndicator(0)	// Opções válidas no manual da Unicred
		->setDateLimit(new DateTime())
		->setValue(0.00);
	$boleto->getFine()
		->setIndicator(0)
		->setDateLimit(new DateTime())
		->setValue(0.00);
	$boleto->getInterest()
		->setIndicator(0)
		->setDateLimit(new DateTime())
		->setValue(0.00);

    #Instancia a API
    $unicred = new UnicredApi($cedente, $ambiente);

    #Obtém o ID do boleto gerado
    $boletoId = $unicred->createBankSlip($boleto);

    #Atribui o ID do boleto Unicred ao objeto boleto
    $boleto->setBankSlipId($boletoId);

    # Consulta o objeto boleto na API - obtém código de barras e linha digitável
    $boleto = $unicred->consultBankSlip($boleto);

    #Output do objeto boleto (BankSlip)
    print_r($boleto);

} catch (UnicredRequestException $e) {
    echo $e->getUnicredError()->getMessage();

} catch (Exception $e) {
    echo $e->getMessage();
}




use \Unicred\Environment;
use \Unicred\UnicredApi;
use \Unicred\Entity\Assignor;
use \Unicred\Entity\BankSlip;
use \Unicred\Request\AuthenticationRequest;
use \Unicred\Exception\UnicredRequestException;

try {
    #Preparando o ambiente
    $ambiente = Environment::sandbox();

    #Dados do Cedente (Cliente Unicred)
    $cedente = new Assignor();
    $cedente->setBankAgency('BANK AGENCY NUMBER')
        ->setApiKey('SUA API KEY')
        ->setUser('SEU LOGIN')
        ->setPassword('SUA SENHA')
        ->setPayeeCode('SEU CÓDIGO DE BENEFICIÁRIO')
        ->setPortifolio('CÓDIGO VARIAÇÃO DA CARTEIRA');

    #Obter o Token de Acesso
    $autenticacao = new AuthenticationRequest($cedente, $ambiente);
    $autenticacao->execute();

    #Dados do boleto
    $boleto = new BankSlip();
    $boleto->setBankSlipId('TOKEN DO BOLETO');

    # Consulta o objeto boleto na API - obtém código de barras e linha digitável
    $boleto = $unicred->consultBankSlip($boleto);

    #Output do objeto boleto (BankSlip)
    print_r($boleto);

} catch (UnicredRequestException $e) {
    echo $e->getUnicredError()->getMessage();

} catch (Exception $e) {
    echo $e->getMessage();
}