PHP code example of nkpay / pix-sdk

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

    

nkpay / pix-sdk example snippets


use NkPay\Pix\PixClient;
use NkPay\Pix\PixConfig;

$client = new PixClient(new PixConfig(
    baseUri: 'https://api.sandbox.banrisul.com.br/pix/v2',
    tokenUrl: 'https://api.sandbox.banrisul.com.br/oauth/token',
    clientId: getenv('PIX_CLIENT_ID'),
    clientSecret: getenv('PIX_CLIENT_SECRET'),
    pfxPath: __DIR__ . '/certs/certificado.pfx',
    pfxPassphrase: getenv('PIX_PFX_PASSPHRASE'), // '' se o .pfx não tiver senha
));

use NkPay\Pix\PixClient;
use NkPay\Pix\PixConfig;

$client = new PixClient(new PixConfig(
    baseUri: 'https://api.sandbox.banrisul.com.br/pix/v2',
    tokenUrl: 'https://api.sandbox.banrisul.com.br/oauth/token',
    clientId: getenv('PIX_CLIENT_ID'),
    clientSecret: getenv('PIX_CLIENT_SECRET'),
    certPath: __DIR__ . '/certs/certificado.pem',
    certKeyPath: __DIR__ . '/certs/chave.key',
    // certPassphrase: 'opcional, se a chave for protegida por senha',
));

use NkPay\Pix\Util\TxidGenerator;

$txid = TxidGenerator::generateWithPrefix('pedido1234');

$cobranca = $client->cobrancaImediata()->criar($txid, [
    'calendario' => ['expiracao' => 3600],
    'devedor' => ['cpf' => '12345678909', 'nome' => 'Fulano de Tal'],
    'valor' => ['original' => '49.90'],
    'chave' => '[email protected]',
    'solicitacaoPagador' => 'Pagamento NkPay - pedido #1234',
]);

echo $cobranca['pixCopiaECola']; // string BR Code -> renderize como QR Code

$resultado = $client->pix()->recebidos([
    'inicio' => '2026-06-01T00:00:00Z',
    'fim'    => '2026-06-13T23:59:59Z',
    'txid'   => $txid,
]);

foreach ($resultado['pix'] ?? [] as $pix) {
    // $pix['endToEndId'], $pix['valor'], $pix['horario'], ...
}

$client->webhook()->configurar('[email protected]', 'https://nkpay.com.br/webhooks/pix');

$client->pix()->solicitarDevolucao($e2eid, 'devolucao-001', [
    'valor' => '10.00',
    'natureza' => 'ORIGINAL',
    'descricao' => 'Reembolso parcial - pedido #1234',
]);

$client->cobrancaVencimento()->criar($txid, [
    'calendario' => [
        'dataDeVencimento' => '2026-07-10',
        'validadeAposVencimento' => 30,
    ],
    'devedor' => ['cpf' => '12345678909', 'nome' => 'Fulano de Tal'],
    'valor' => ['original' => '199.90'],
    'chave' => '[email protected]',
]);

use NkPay\Pix\Exception\PixException;

try {
    $client->cobrancaImediata()->criar($txid, $payload);
} catch (PixException $e) {
    foreach ($e->violacoes() as $v) {
        // $v['razao'], $v['propriedade']
    }
}

src/
├── PixClient.php          # cliente HTTP (OAuth2 + mTLS)
├── PixConfig.php           # configuração de conexão (PEM ou PFX/P12)
├── Exception/PixException.php
├── Resource/
│   ├── CobrancaImediata.php   # /cob
│   ├── CobrancaVencimento.php # /cobv
│   ├── Pix.php                # /pix, /pix/{e2eid}/devolucao/{id}
│   ├── Webhook.php             # /webhook
│   └── Location.php             # /loc
└── Util/
    ├── TxidGenerator.php
    └── PfxCertificateLoader.php   # converte .pfx/.p12 -> PEM em runtime