PHP code example of joshua-barbosa / erede-sdk

1. Go to this page and download the library: Download joshua-barbosa/erede-sdk 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/ */

    

joshua-barbosa / erede-sdk example snippets


// config/logging.php
'channels' => [
    'erede' => [
        'driver' => 'stack',
        'channels' => ['daily', 'slack'],
    ],
],

use eRede\eRede;

class CobrancaService
{
    public function __construct(private eRede $erede) {}
}

$erede = new eRede(pv: '...', token: '...', env: 'production');

use eRede\Classes\Transaction;

$transaction = (new Transaction(amount: 149.90, reference: 'pedido-1234'))
    ->creditCard('5448280000000007', '123', 12, 2030, 'JOAO DA SILVA')
    ->setInstallments(3)
    ->setSoftDescriptor('MINHA LOJA');

$response = $erede->transactions()->create($transaction);

$response->getTid();
$response->getReturnCode();     // '00' = aprovada
$response->getAuthorizationCode();

$transaction->capture(false);
$autorizada = $erede->transactions()->create($transaction);

// mais tarde
$erede->transactions($autorizada->getTid())->capture(new Amount(149.90));

$erede->transactions('30161009000000000001')->get();
$erede->transactions()->getByReference('pedido-1234');

use eRede\Classes\Amount;
use eRede\Classes\Url;

$erede->transactions($tid)->refunds()->create(
    new Amount(149.90),
    new Url('https://minhaloja.com.br/webhooks/erede'),   // opcional
);

$erede->transactions($tid)->refunds()->getByTid()->getRefundsList();
$erede->transactions($tid)->refunds()->get($refundId);

use eRede\Classes\Transaction;

$transaction = (new Transaction(amount: 39.00, reference: 'pedido-1234'))
    ->pix(now()->addMinutes(30));          // expiração: futura e de no máximo 15 dias

$response = $erede->transactions()->create($transaction);

$response->getTid();                              // guarde para consultar e devolver
$response->getQrCodeResponse()->getQrCodeData();  // Pix copia e cola (EMV)
$response->getQrCodeResponse()->getQrCodeImage(); // imagem do QR Code em base64

$consulta = $erede->transactions($tid)->get();

if ($qr = $consulta->getQrCodeResponse()) {
    $qr->isPending();                              // ainda não pago; QR Code segue disponível
} else {
    $consulta->getAuthorization()->getStatus();    // 'Approved' (pago) ou 'Canceled' (devolvido)
    $consulta->getAuthorization()->getEndToEndId();
    $consulta->getRefunds();                       // devoluções, se houver
}

$erede->transactions($tid)->refunds()->create(new Amount(39.00));

use eRede\Exceptions\eRedeException;

try {
    $response = $erede->transactions()->create($transaction);
} catch (eRedeException $e) {
    $e->getMessage();   // mensagem da Rede já traduzida
    $e->returnCode();   // ex.: '51' (saldo insuficiente)
    $e->getCode();      // status HTTP
    $e->context();      // dados já sanitizados
}

Http::fake([
    '*oauth2/token' => Http::response(['access_token' => 'fake', 'token_type' => 'Bearer', 'expires_in' => 3600]),
    '*/v2/transactions' => Http::response(['tid' => '123', 'returnCode' => '00']),
]);
bash
php artisan vendor:publish --tag=erede-config