PHP code example of clubedev / pagbank-php

1. Go to this page and download the library: Download clubedev/pagbank-php 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/ */

    

clubedev / pagbank-php example snippets




use ClubeDev\PagBank\PagBank;

$payment = new PagBank(
    pagBankToken: 'SEU_TOKEN_PAGBANK',
    clubeDevToken: 'SEU_TOKEN_CLUBEDEV',
    sandbox: true // true = ambiente de testes
);

use ClubeDev\PagBank\Domain\Client;
use ClubeDev\PagBank\Domain\Phone;
use ClubeDev\PagBank\Domain\Item;
use ClubeDev\PagBank\Domain\Shipping;
use ClubeDev\PagBank\Domain\Address;
use ClubeDev\PagBank\Domain\Payment;
use ClubeDev\PagBank\Domain\Payment\Pix;

$response = $payment->createPayment(
    reference: "identificação-informada-por-você", // Geralmente utilizado algum id
    client: new Client(
        document: "12345678909", 
        name: "João", 
        email: "[email protected]",
        phones: [ // Opcional
            new Phone(
                country: 55,
                area: 21,
                number: 999121314
            ),
            new Phone(
                country: 55, 
                area: 21, 
                number: 999151617
            ),
        ]
    ),
    items: [ // Opcional
        new Item(
            name: 'Item X',
            quantity: 1,
            unit_amount: 12.99
        )
    ],
    shipping: new Shipping( // Opcional
        address: new Address(
            street: 'Rua 01',
            number: '01', // Opcional
            complement: 'Q01 L01', // Opcional
            locality: 'Bairro',
            city: 'Cidade',
            state_code: 'SP',
            postal_code: '01310930'
        )
    ),
    payment: new Payment(
        pix: new Pix(
            expiration_date: "2025-12-01 12:00:00",
            amount: 25.90
        )
    ),
    webhook_url: 'https://exemplo.com/webhook' // Opcional
);


$amount = $response->payment()?->pix?->amount;
$expirationDate = $response->payment()?->pix?->expiration_date;
$chargeId = $response->payment()?->pix?->charge_id;
$qrcode = $response->payment()?->pix?->qrcode;
$qrcodeImage = $response->payment()?->pix?->qrcode_image;

use ClubeDev\PagBank\Domain\Client;
use ClubeDev\PagBank\Domain\Phone;
use ClubeDev\PagBank\Domain\Item;
use ClubeDev\PagBank\Domain\Shipping;
use ClubeDev\PagBank\Domain\Address;
use ClubeDev\PagBank\Domain\Payment;
use ClubeDev\PagBank\Domain\Payment\CreditCard;
use ClubeDev\PagBank\Domain\Payment\Holder;

$response = $payment->createPayment(
    reference: "identificação-informada-por-você", // Geralmente utilizado algum id
    client: new Client(
        document: "12345678909", 
        name: "João", 
        email: "[email protected]",
        phones: [ // Opcional
            new Phone(
                country: 55,
                area: 21,
                number: 999121314
            ),
            new Phone(
                country: 55, 
                area: 21, 
                number: 999151617
            ),
        ]
    ),
    items: [ // Opcional
        new Item(
            name: 'Item X',
            quantity: 1,
            unit_amount: 21.36
        )
    ],
    shipping: new Shipping( // Opcional
        address: new Address(
            street: 'Rua 01',
            number: '01', // Opcional
            complement: 'Q01 L01', // Opcional
            locality: 'Bairro',
            city: 'Cidade',
            state_code: 'SP',
            postal_code: '01310930'
        )
    ),
    payment: new Payment(
        credit_card: new CreditCard(
            amount: 21.36,
            description: 'Descrição do pagamento',
            soft_descriptor: 'NOMEFATURACLIENTE',
            card_token: 'TOKEN_GERADO_PELO_SDK_JS', // Utilize ClubeDev PagBank Card Encrypt
            holder: new Holder(
                document: '123.456.789-09',
                name: 'Wanderson Teste',
            )
        )
    ),
    webhook_url: 'https://exemplo.com/webhook' // Opcional
);

$amount = $response->payment()?->credit_card?->amount;
$description = $response->payment()?->credit_card?->description;
$softDescriptor = $response->payment()?->credit_card?->soft_descriptor;
$chargeId = $response->payment()?->credit_card?->charge_id;
$reference = $response->payment()?->credit_card?->reference;
$authorizationCode = $response->payment()?->credit_card?->authorization_code;
$nsu = $response->payment()?->credit_card?->nsu;
$brand = $response->payment()?->credit_card?->brand;
$cardNumber = $response->payment()?->credit_card?->card_number;
$expiration = $response->payment()?->credit_card?->expiration;
$holderName = $response->payment()?->credit_card?->holder?->name;
$holderDocument = $response->payment()?->credit_card?->holder?->document;

use ClubeDev\PagBank\Domain\Client;
use ClubeDev\PagBank\Domain\Phone;
use ClubeDev\PagBank\Domain\Item;
use ClubeDev\PagBank\Domain\Shipping;
use ClubeDev\PagBank\Domain\Address;
use ClubeDev\PagBank\Domain\Payment;
use ClubeDev\PagBank\Domain\Payment\Title;
use ClubeDev\PagBank\Domain\Payment\Holder;

$response = $payment->createPayment(
    reference: "identificação-informada-por-você", // Geralmente utilizado algum id
    client: new Client(
        document: "12345678909", 
        name: "João", 
        email: "[email protected]",
        phones: [ // Opcional
            new Phone(
                country: 55,
                area: 21,
                number: 999121314
            ),
            new Phone(
                country: 55, 
                area: 21, 
                number: 999151617
            ),
        ]
    ),
    items: [ // Opcional
        new Item(
            name: 'Item X',
            quantity: 1,
            unit_amount: 21.36
        )
    ],
    shipping: new Shipping( // Opcional
        address: new Address(
            street: 'Rua 01',
            number: '01', // Opcional
            complement: 'Q01 L01', // Opcional
            locality: 'Bairro',
            city: 'Cidade',
            state_code: 'SP',
            postal_code: '01310930'
        )
    ),
    payment: new Payment(
        title: new Title(
            description: 'Descrição do boleto',
            amount: 14.98,
            due_date: '2025-11-20',
            holder: new Holder(
                document: '12345678909',
                name: 'Wanderson Teste',
                email: '[email protected]',
                address: new Address(
                    street: 'Rua 01',
                    number: '01', // Opcional
                    locality: 'Bairro',
                    city: 'Cidade',
                    state: 'São Paulo',
                    state_code: 'SP',
                    postal_code: '01310930'
                )
            )
        )
    ),
    webhook_url: 'https://exemplo.com/webhook' // Opcional
);

$amount = $response->payment()?->title?->amount;
$description = $response->payment()?->title?->description;
$dueDate = $response->payment()?->title?->due_date;
$chargeId = $response->payment()?->title?->charge_id;
$barCode = $response->payment()?->title?->bar_code;
$urlPdf = $response->payment()?->title?->url;
$holderName = $response->payment()?->title?->holder?->name;
$holderDocument = $response->payment()?->title?->holder?->document;
$holderEmail = $response->payment()?->title?->holder?->email;
$holderAddressStreet = $response->payment()?->title?->holder?->address?->street;
$holderAddressNumber = $response->payment()?->title?->holder?->address?->number;
$holderAddressLocality = $response->payment()?->title?->holder?->address?->locality;
$holderAddressCity = $response->payment()?->title?->holder?->address?->city;
$holderAddressState = $response->payment()?->title?->holder?->address?->state;
$holderAddressStateCode = $response->payment()?->title?->holder?->address?->state_code;
$holderAddressPostal_code = $response->payment()?->title?->holder?->address?->postal_code;

$response = $payment->getPayment('ORDER_ID');

$amount = $response->payment()?->pix?->amount;
$expirationDate = $response->payment()?->pix?->expiration_date;
$chargeId = $response->payment()?->pix?->charge_id;
$qrcode = $response->payment()?->pix?->qrcode;
$qrcodeImage = $response->payment()?->pix?->qrcode_image;

$amount = $response->payment()?->credit_card?->amount;
$description = $response->payment()?->credit_card?->description;
$softDescriptor = $response->payment()?->credit_card?->soft_descriptor;
$chargeId = $response->payment()?->credit_card?->charge_id;
$reference = $response->payment()?->credit_card?->reference;
$authorizationCode = $response->payment()?->credit_card?->authorization_code;
$nsu = $response->payment()?->credit_card?->nsu;
$brand = $response->payment()?->credit_card?->brand;
$cardNumber = $response->payment()?->credit_card?->card_number;
$expiration = $response->payment()?->credit_card?->expiration;
$holderName = $response->payment()?->credit_card?->holder?->name;
$holderDocument = $response->payment()?->credit_card?->holder?->document;

$amount = $response->payment()?->title?->amount;
$description = $response->payment()?->title?->description;
$dueDate = $response->payment()?->title?->due_date;
$chargeId = $response->payment()?->title?->charge_id;
$barCode = $response->payment()?->title?->bar_code;
$urlPdf = $response->payment()?->title?->url;
$holderName = $response->payment()?->title?->holder?->name;
$holderDocument = $response->payment()?->title?->holder?->document;
$holderEmail = $response->payment()?->title?->holder?->email;
$holderAddressStreet = $response->payment()?->title?->holder?->address?->street;
$holderAddressNumber = $response->payment()?->title?->holder?->address?->number;
$holderAddressLocality = $response->payment()?->title?->holder?->address?->locality;
$holderAddressCity = $response->payment()?->title?->holder?->address?->city;
$holderAddressState = $response->payment()?->title?->holder?->address?->state;
$holderAddressStateCode = $response->payment()?->title?->holder?->address?->state_code;
$holderAddressPostal_code = $response->payment()?->title?->holder?->address?->postal_code;

$response = $payment->cancelPayment(
    charge_id: 'CHARGE_ID', 
    amount: 16.99
);

$body = file_get_contents('php://input');
$paymentWebhook = $payment->webhook(json_decode($body, true));

use ClubeDev\PagBank\Domain\Phone;

new Phone(
    country: 55, 
    area: 21, 
    number: 999999999
);

use ClubeDev\PagBank\Domain\Client;

new Client(
    document: "12345678900",
    name: "Fulano Teste",
    email: "[email protected]",
    phones: [ 
        new Phone(country: 55, area: 21, number: 999999999),
        new Phone(country: 55, area: 21, number: 999999999),
    ]
);

use ClubeDev\PagBank\Domain\Item;

new Item(
    name: 'Item teste',
    quantity: 1,
    unit_amount: 12.99
);

use ClubeDev\PagBank\Domain\Address;

new Address(
    street: 'Rua 01',
    number: '01',
    complement: 'Q01 L01',
    locality: 'Bairro',
    city: 'Cidade',
    state: 'São Paulo',
    state_code: 'SP',
    postal_code: '01310930'
);

use ClubeDev\PagBank\Domain\Shipping;

new Shipping(
    address: new Address(...),
);

use ClubeDev\PagBank\Domain\Payment\Holder;

new Holder(
    document: '123.456.789-09',
    name: 'Fulano Teste',
    email: '[email protected]',
    address: new Address(...)
)

use ClubeDev\PagBank\Domain\Payment\CreditCard;

new CreditCard(
    amount: 11.50,
    description: 'Descrição do pagamento',
    soft_descriptor: 'NOMEFATURA', // Descrição que aparecerá na fatura do cartão
    holder: new Holder(...),
    card_token: 'TOKEN_GERADO_PELO_SDK_JS',
    installments: 1,
    charge_id: null, // Preenchido automaticamente pelo backend
    reference: null, // Preenchido automaticamente pelo backend
    authorization_code: null, // Preenchido automaticamente pelo backend
    nsu: null, // Preenchido automaticamente pelo backend
    brand: null, // Preenchido automaticamente pelo backend
    card_number: null, // Preenchido automaticamente pelo backend
    expiration: null, // Preenchido automaticamente pelo backend
)

use ClubeDev\PagBank\Domain\Payment\Title;

new Title(
    amount: 11.50,
    description: 'Descrição do boleto para controle interno',
    due_date: '2025-12-01',
    holder: new Holder(...),
    instruction_line_1: 'Não receber após vencimento',
    instruction_line_2: 'Pagável apenas na loterica',
    charge_id: null, // Preenchido automaticamente pelo backend
    bar_code: null, // Preenchido automaticamente pelo backend
    url: null, // Preenchido automaticamente pelo backend
)

use ClubeDev\PagBank\Domain\Payment\Pix;

new Pix(
    amount: 11.50,
    expiration_date: '2025-12-01 12:30:00',
    charge_id: null, // Preenchido automaticamente pelo backend
    qrcode: null, // Preenchido automaticamente pelo backend
    qrcode_image: null, // Preenchido automaticamente pelo backend
)

use ClubeDev\PagBank\Domain\Payment;

new Payment(
    pix: new Pix(...),
    title: new Title(...),
    credit_card: new CreditCard(...),
    paid_at: null, // Preenchido automaticamente pelo backend
    status: null, // Preenchido automaticamente pelo backend
    full_refunded: null, // Preenchido automaticamente pelo backend
    paid: null, // Preenchido automaticamente pelo backend
    refunded: null, // Preenchido automaticamente pelo backend
)

use ClubeDev\PagBank\Exceptions\ClubedevException;
use ClubeDev\PagBank\Exceptions\PagBankException;

try {
    $response = $payment->getPayment('ORDER_ID');
} catch(ClubedevException $e) {
    $exception = json_decode($e->getMessage());
    echo $exception?->error ?? $exception?->message ?? $e->getMessage();
} catch(PagBankException $e) {
    $exception = json_decode($e->getMessage());
    echo $exception?->error ?? $exception?->message ?? $e->getMessage();
} catch (\Throwable $th) {
    echo $th->getMessage();
}
bash
composer