Sviluppatori
Webhook
Quando una fattura cambia stato, CryBit invia una richiesta POST firmata con un corpo JSON all'URL webhook del tuo merchant.
Configurazione
Imposta l'URL webhook e seleziona gli eventi nelle impostazioni del merchant, nella scheda integrazione: paid, expired, refunded, cancelled. L'URL deve essere un indirizzo pubblico HTTP o HTTPS sulla porta 80, 443, 8080 o 8443; i redirect non vengono seguiti. Viene impostato sul merchant, non nella richiesta della fattura.
Header
| Header | Valore |
|---|---|
X-CryBit-Event | Il nome dell'evento, per esempio payment.paid. |
X-CryBit-Signature | t=<unix time>,v1=<hex>: HMAC-SHA256 di "<t>.<raw body>" con il tuo signing secret. |
Verifica la firma
Il signing secret è mostrato nelle impostazioni del merchant accanto all'URL webhook. Calcola la firma sul corpo grezzo, confrontala a tempo costante e rifiuta le richieste più vecchie di cinque minuti.
PHP
$secret = getenv('CRYBIT_WEBHOOK_SECRET');
$body = file_get_contents('php://input');
$header = $_SERVER['HTTP_X_CRYBIT_SIGNATURE'] ?? '';
parse_str(str_replace(',', '&', $header), $p); // t=…&v1=…
$t = (int) ($p['t'] ?? 0);
$expected = hash_hmac('sha256', $t . '.' . $body, $secret);
if (abs(time() - $t) > 300 || !hash_equals($expected, $p['v1'] ?? '')) {
http_response_code(400);
exit;
}
// the request is genuine: store the event, answer 200, do the rest later
http_response_code(200);Node.js
import crypto from 'node:crypto'
export function verify(rawBody, header, secret) {
const parts = Object.fromEntries(header.split(',').map((kv) => kv.split('=')))
const t = Number(parts.t)
const expected = crypto.createHmac('sha256', secret).update(t + '.' + rawBody).digest('hex')
const ok = parts.v1 && parts.v1.length === expected.length
&& crypto.timingSafeEqual(Buffer.from(parts.v1), Buffer.from(expected))
return ok && Math.abs(Date.now() / 1000 - t) <= 300
}Consegna e tentativi
- Rispondi rapidamente con uno stato 2xx: CryBit attende circa dodici secondi.
- Se non rispondi, CryBit riprova dopo 15 secondi, 1 minuto, 5, 15 e 30 minuti, poi 2, 6 e 24 ore.
- Una risposta 4xx, tranne 429, interrompe i tentativi.
- Lo stesso evento può arrivare più di una volta: gestiscilo in modo idempotente, usando come chiave order_id o l'uuid della fattura.
- Se hai perso un webhook, leggi lo stato con GET /api/v1/payments/{uuid}: è sempre la fonte di verità.
FAQ
Posso vedere cosa è stato consegnato?
Sì. La pagina dei log API nell'account merchant elenca le richieste e le consegne dei webhook con il codice di stato e il corpo.
