Integração com PHP

Exemplo usando curl nativo do PHP — sem SDK, sem dependências.

Criar cobrança

php
<?php
function createPixCharge(string $apiKey, array $data): array {
    $ch = curl_init('https://api.hubpay.dev/v1/charges');

    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST           => true,
        CURLOPT_HTTPHEADER     => [
            'Authorization: Bearer ' . $apiKey,
            'Content-Type: application/json',
            'Idempotency-Key: ' . uniqid('', true),
        ],
        CURLOPT_POSTFIELDS => json_encode($data),
    ]);

    $response = curl_exec($ch);
    $status   = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    $body = json_decode($response, true);
    if ($status !== 201) {
        throw new RuntimeException('Hubpay error: ' . $body['error']['message']);
    }
    return $body;
}

// Uso:
$charge = createPixCharge($_ENV['HUBPAY_API_KEY'], [
    'amount'      => 5000,
    'provider'    => 'asaas',
    'description' => 'Pedido #1234',
    'payer'       => [
        'name'     => 'João Silva',
        'document' => '12345678901',
        'email'    => 'joao@exemplo.com',
    ],
]);

echo $charge['pix']['copy_paste']; // Pix copia e cola

Webhook handler

php
<?php
function verifyHubpaySignature(string $payload, string $signature, string $secret): bool {
    $expected = 'v1=' . hash_hmac('sha256', $payload, $secret);
    return hash_equals($expected, $signature);
}

$payload   = file_get_contents('php://input');
$signature = $_SERVER['HTTP_X_HUBPAY_SIGNATURE'] ?? '';

if (!verifyHubpaySignature($payload, $signature, $_ENV['HUBPAY_WEBHOOK_SECRET'])) {
    http_response_code(401);
    exit('Invalid signature');
}

$event = json_decode($payload, true);

if ($event['type'] === 'charge.paid') {
    $orderId = $event['data']['metadata']['order_id'] ?? null;
    // marcar pedido como pago...
}

http_response_code(200);
echo 'ok';

Buscar cobrança

php
<?php
function getCharge(string $apiKey, string $chargeId): array {
    $ch = curl_init("https://api.hubpay.dev/v1/charges/{$chargeId}");

    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => [
            'Authorization: Bearer ' . $apiKey,
        ],
    ]);

    $response = curl_exec($ch);
    curl_close($ch);

    return json_decode($response, true);
}

$charge = getCharge($_ENV['HUBPAY_API_KEY'], 'chg_abc123');
echo $charge['status']; // 'pending', 'paid', etc.