Skip to main content

Verify Webhook

Verify the integrity of webhook requests using the signature header.

Overview

To ensure the integrity of webhook data, every webhook request includes a digital signature in the header. You should verify this signature using your Secret Key before processing the request.

Verification Steps

  1. Get the JSON payload from the request.
  2. Retrieve the signature from the request headers.
  3. Alphabetically sort the payload by keys.
  4. Convert the sorted payload to a JSON string.
  5. Generate an HMAC-SHA256 signature using your Secret Key (from API Config) as the key.
  6. Compare your generated signature with the received signature.

Code Examples

// Payload received from webhook
$payload = [
"status" => "failed",
"type" => "sale",
"transaction_id" => "A49dfkqvw",
// ... other fields
];

// Fetch API Secret
$apiSecret = getenv('API_SECRET') ?: 'default_secret';

// Received signature
$receivedSignature = $_SERVER['HTTP_SIGNATURE']; // Adjust based on your header name

// Step 1: Sort payload
ksort($payload);

// Step 2: Convert to JSON
$payloadJson = json_encode($payload, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

// Step 3: Generate HMAC
$generatedSignature = hash_hmac('sha256', $payloadJson, $apiSecret);

// Step 4: Verify
if (hash_equals($generatedSignature, $receivedSignature)) {
echo "Signature is valid!";
} else {
echo "Signature is invalid!";
}

Important Notes

JSON Formatting

Ensure you use standard JSON formatting with no extra spaces when generating the signature string (e.g., in Python use separators=(',', ':')).