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",
"amount" => "55.00",
"reference" => "DazuknyVyG",
"transaction_id" => "A49dfkqvw",
"payment_method" => "Card (VISA)",
"created_at" => "2025-02-25 05:18:56",
"processor_name" => "Trust Payments",
"currency" => "USD"
];

// Fetch API Secret Text from settings/config
$apiSecret = getenv('API_SECRET') ?: 'default_secret'; // Replace with your actual config fetching logic

// Received signature (from the request headers or body)
$receivedSignature = "your_received_signature_here"; // Replace with the actual signature

// Step 1: Sort the payload by keys alphabetically
ksort($payload);

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

// Step 3: Generate HMAC signature using SHA-256
$generatedSignature = hash_hmac('sha256', $payloadJson, $apiSecret);

// Step 4: Verify the signature
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=(',', ':')).