Creare una destinazione

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"
}'

Salva il webhook.secret restituito alla creazione in CIRCUSPAY_WEBHOOK_SECRET. Serve a verificare le firme ed è distinto dalla chiave API.

Verificare la firma

X-ONIPAY-Signature contiene l’HMAC-SHA256 esadecimale del corpo HTTP originale, calcolato con il segreto del webhook. Verifica il corpo esatto ricevuto prima di ricodificare il JSON.

PHP · verifica della firma
<?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.

Elaborare gli eventi

ParametroDescrizione
payment.succeededVerifica il pagamento e conferma l’ordine una sola volta.
payment.failedRegistra il tentativo fallito senza confermare l’ordine.
payment.refundedSincronizza lo stato del rimborso.
webhook.testTest tecnico della destinazione. Non avvia l’evasione di ordini.

Gli eventi di pagamento usano una struttura con type, created e data. Il contenuto di data dipende dal flusso. Non presumere uno schema identico per tutti gli eventi; riconcilia i riferimenti con le transazioni.

Testare la ricezione

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
}'

Consultare le consegne

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"

Sostituisci 123 con il tuo ID webhook. La risposta logs contiene le ultime 50 consegne, con codice HTTP, durata ed eventuale errore.