Skip to content

Verify webhook signatures

Anyone can send a POST request to your webhook URL. Before marking an order as paid, check that the request really comes from Senfenico, that its content wasn’t modified, and that it is recent.

Header Content
X-Senfenico-Timestamp When this attempt was sent, in Unix seconds (e.g. 1795675230)
X-Senfenico-Signature v1= followed by the hex HMAC-SHA256 signature
X-Senfenico-Delivery Event identifier, the same on every retry: use it to drop duplicates
X-Webhook-Hash Legacy signature, still sent for existing integrations (see below)
X-Senfenico-Signature = "v1=" + hex( HMAC-SHA256( webhook_secret_key, timestamp + "." + raw_body ) )
Element Value
webhook_secret_key The key shown under the Signing secret label in Developer › Webhooks in the dashboard. The same key is used in test and live mode.
timestamp The exact value of the X-Senfenico-Timestamp header
raw_body The exact bytes received, before any JSON decoding
  1. Read the request’s raw body and the X-Senfenico-Timestamp and X-Senfenico-Signature headers.
  2. Reject the request if the timestamp is more than 5 minutes away from your server’s clock. This check is what stops an intercepted request from being replayed.
  3. Compute HMAC-SHA256(secret_key, timestamp + "." + raw_body) as hex.
  4. Compare it with the value after v1= using a constant-time comparison (hash_equals, hmac.compare_digest, crypto.timingSafeEqual). If any check fails, return 400 and do nothing else.

Each example verifies the signature, then decodes data, which is a JSON string. The PHP and Laravel examples use str_starts_with, available from PHP 8.0.

<?php
const SENFENICO_WEBHOOK_SECRET = 'your_webhook_secret_key';
function verify_senfenico(
string $payload,
?string $timestamp,
?string $signature,
string $secret,
int $tolerance = 300
): bool {
if (!$timestamp || !ctype_digit($timestamp)) return false;
if (!$signature || !str_starts_with($signature, 'v1=')) {
return false;
}
if (abs(time() - (int) $timestamp) > $tolerance) return false;
$expected = hash_hmac('sha256', $timestamp . '.' . $payload, $secret);
return hash_equals($expected, substr($signature, 3));
}
$payload = file_get_contents('php://input'); // raw body
$timestamp = $_SERVER['HTTP_X_SENFENICO_TIMESTAMP'] ?? null;
$signature = $_SERVER['HTTP_X_SENFENICO_SIGNATURE'] ?? null;
$ok = verify_senfenico(
$payload, $timestamp, $signature, SENFENICO_WEBHOOK_SECRET
);
if (!$ok) {
http_response_code(400);
exit;
}
$event = json_decode($payload, true);
$data = is_string($event['data'])
? json_decode($event['data'], true)
: $event['data'];
switch ($event['event']) {
case 'charge.success':
case 'checkout.success':
// mark order $data['reference'] as paid (once)
break;
case 'charge.failed':
// ...
break;
}
http_response_code(200);

Compute the expected signature for a body saved in body.json, using the timestamp you received:

Fenêtre de terminal
printf '%s.' "$TIMESTAMP" | cat - body.json \
| openssl dgst -sha256 -hmac "$SENFENICO_WEBHOOK_SECRET"

The output must equal the value after v1= in X-Senfenico-Signature. To receive real test events, create a charge with an sk_test_ key and a test number.

Symptom Likely cause
Signature never matches Re-serialized body instead of raw body, or a JSON middleware consumed the body before your code
Matches only without accents JSON was decoded and re-encoded (é became é)
Rejected as “too old” although the signature is right Your server clock drifts: enable NTP time sync
Fails with the right key A space or newline copied with the secret, or v1= not stripped before comparing
No webhook received Private URL, 301/302 redirect, or a response outside 2xx (see requirements)

Existing integrations and the PHP and Python SDKs (Webhook::constructEvent / Webhook.construct_event) use the X-Webhook-Hash header. It is still sent with every webhook:

X-Webhook-Hash = hex( SHA-256( raw_body + webhook_secret_key ) )

It has no timestamp, so it does not protect against replay. Use X-Senfenico-Signature for new integrations. If you keep the legacy header, still compare in constant time and process each X-Senfenico-Delivery only once.

The same event can arrive more than once: store each processed X-Senfenico-Delivery (or event + reference pair) and skip duplicates.

Before you fulfill a valuable order, fetch the object from the API and check its status, amount and live_mode.

Keep the secret server-side, in an environment variable, never in Git.