Criar um destino

POST/webhooks.php
Terminal
curl "https://circus-pay.com/api/webhooks.php" \
  -H "Authorization: Bearer $CIRCUSPAY_SECRET_KEY" \
  -H "Content-Type: application/json" \
  --data '{
  "url": "https://example.com/webhooks/circuspay",
  "events": [
    "payment.succeeded",
    "payment.failed",
    "payment.refunded"
  ],
  "description": "Boutique principale"
}'

Guarde o webhook.secret devolvido na criação em CIRCUSPAY_WEBHOOK_SECRET. Serve para verificar assinaturas e é distinto da chave API.

Verificar a assinatura

X-ONIPAY-Signature contém o HMAC-SHA256 hexadecimal do corpo HTTP original, calculado com o segredo do webhook. Verifique o corpo exato recebido antes de voltar a codificar o JSON.

PHP · verificação de assinatura
<?php
$raw = file_get_contents('php://input');
$received = $_SERVER['HTTP_X_ONIPAY_SIGNATURE'] ?? '';
$secret = getenv('CIRCUSPAY_WEBHOOK_SECRET');
if (!$secret || !preg_match('/^[a-f0-9]{64}$/', $received)) {
    http_response_code(400); exit;
}
$expected = hash_hmac('sha256', $raw, $secret);
if (!hash_equals($expected, $received)) {
    http_response_code(400); exit;
}
$event = json_decode($raw, true);
if (!is_array($event)) { http_response_code(400); exit; }
// Persist the event durably before returning 2xx.
// Deduplicate fulfilment by your order + transaction reference.
// Verify order, amount, currency and final status server-side.
// Return 5xx if durable processing/queuing fails.

Processar os eventos

ParâmetroDescrição
payment.succeededVerifique o pagamento e confirme a encomenda uma única vez.
payment.failedRegiste a tentativa falhada sem confirmar a encomenda.
payment.refundedSincronize o estado do reembolso.
webhook.testTeste técnico do destino. Não desencadeia a entrega de encomendas.

Os eventos de pagamento usam um envelope com type, created e data. O conteúdo de data depende do fluxo. Não presuma que todos os eventos têm o mesmo esquema; reconcilie as referências com as transações.

Testar a receção

POST/webhooks.php?action=test
Terminal
curl "https://circus-pay.com/api/webhooks.php?action=test" \
  -H "Authorization: Bearer $CIRCUSPAY_SECRET_KEY" \
  -H "Content-Type: application/json" \
  --data '{
  "id": 123
}'

Consultar as entregas

GET/webhooks.php?id=123&logs=1
Terminal
curl "https://circus-pay.com/api/webhooks.php?id=123&logs=1" \
  -H "Authorization: Bearer $CIRCUSPAY_SECRET_KEY"

Substitua 123 pelo ID do webhook. A resposta logs contém as últimas 50 entregas, com código HTTP, duração e eventual erro.