Skip to content
Digisys Alpha
Menu

WooCommerce · webhooks · PHP · security

Verify a WooCommerce webhook signature in PHP (HMAC)

WooCommerce signs each webhook with the secret you set. Read the raw request body, compute a base64-encoded HMAC-SHA256 of it with that secret, and compare it to the X-WC-Webhook-Signature header using hash_equals. Reject the request if they differ.

By Ahmed Memon · Published 2026-11-30 · Updated 2026-11-30 · 5 min read

A webhook endpoint is a public URL. Without a signature check, anyone who finds it can post fake orders or events. WooCommerce gives you what you need to verify that a delivery really came from your store.

What WooCommerce sends

Each delivery carries the event as JSON and a signature in the X-WC-Webhook-Signature header. It is a base64-encoded HMAC-SHA256 hash of the request body, made with the secret you entered when you created the webhook. Other headers name the topic and the webhook ID.

The check in PHP

$raw      = file_get_contents('php://input');           // raw body, before json_decode
$received = $_SERVER['HTTP_X_WC_WEBHOOK_SIGNATURE'] ?? '';
$expected = base64_encode(hash_hmac('sha256', $raw, $secret, true));

if (!hash_equals($expected, $received)) {
    http_response_code(401);
    exit;
}

Four mistakes that break it

  • Hashing the decoded array or re-encoded JSON. Hash the exact raw bytes you received.
  • Forgetting the true flag in hash_hmac, which returns hex instead of raw bytes before base64.
  • Comparing with == or ===. Use hash_equals so the comparison takes the same time whatever the input.
  • Letting a framework or a security plugin read and change the body before your code sees it.

When the header never arrives

Some hosts and proxies strip custom headers. If your signature is always empty, confirm that first. A fallback is to put a long random token in the webhook URL and check it as well, but treat it as a second layer, not a replacement for the signature.

Process each event once

A valid signature does not mean the event is new. WooCommerce can deliver again after a failure, so store the delivery or event ID and take a lock before you act. We cover that in our post on processing webhooks once.

Test it

  • Send a request with a wrong secret and expect a 401.
  • Send the same valid event twice and expect one effect.
  • Return a 2xx quickly and do slow work in a queue, so WooCommerce does not see a timeout as a failure.

Questions

Which header does WooCommerce use for the webhook signature?

X-WC-Webhook-Signature. It holds a base64-encoded HMAC-SHA256 hash of the request body, made with the webhook secret.

Why does my signature check always fail?

Usually the body was changed before hashing, the raw flag was missing in hash_hmac, or the wrong secret is in use. Hash the exact raw body and compare with hash_equals.

Is a secret in the URL enough?

It is a useful extra layer where headers are stripped, but the HMAC signature is the stronger check. Use both where you can.

Need this built or reviewed?

Book a 30-minute discovery call or message us on WhatsApp. We reply within one business day.