WooCommerce Webhook API: Real-Time Order Notifications with HMAC Verification

WooCommerce webhooks with HMAC signature verification

WooCommerce can push events to another system instead of being polled: when an order is created or updated, it sends a POST request with the order as JSON to a delivery URL. Every request is signed with a secret, and the receiver must check that signature before trusting the data. A receiver without the check accepts fake orders from anyone who knows the URL.

Problem: A CRM should receive every new WooCommerce order. The first version of the receiver accepted any POST to its URL, and test requests from a scanner created fake customers in the CRM.

Solution: Create the webhook once (in WooCommerce → Settings → Advanced → Webhooks or on plugin activation), with a random secret that the receiver also knows. In the receiver, compute the base64-encoded HMAC-SHA256 of the raw body with that secret and compare it with the X-WC-Webhook-Signature header using hash_equals(), answer quickly with 200, and ignore duplicate deliveries.

Create the webhook

In the admin: WooCommerce → Settings → Advanced → Webhooks → Add webhook, topic “Order created”, delivery URL of the receiver, a secret, status “Active”. From code, the same happens once on plugin activation, never on every request:

register_activation_hook( __FILE__, 'ha_create_crm_webhook' );
function ha_create_crm_webhook(): void {
    if ( ! class_exists( 'WC_Webhook' ) || get_option( 'ha_crm_webhook_id' ) ) {
        return;
    }
    $secret  = wp_generate_password( 40, false );
    $webhook = new WC_Webhook();
    $webhook->set_name( 'CRM: order created' );
    $webhook->set_user_id( get_current_user_id() );
    $webhook->set_topic( 'order.created' );
    $webhook->set_delivery_url( 'https://crm.example.com/hooks/woocommerce' );
    $webhook->set_secret( $secret );
    $webhook->set_status( 'active' );
    $webhook_id = $webhook->save();

    update_option( 'ha_crm_webhook_id', $webhook_id, false );
    // Give the same secret to the receiver (environment variable there), not to the browser.
}

The user ID matters: the payload is built with that user’s permissions, like a REST API request. Topics are resource.event: order.created, order.updated, order.deleted, order.restored, the same for product, customer and coupon.

Verify the signature in the receiver

<?php
// receiver.php on the CRM side (plain PHP, no WordPress)
$secret    = getenv( 'WC_WEBHOOK_SECRET' );
$body      = file_get_contents( 'php://input' );
$signature = $_SERVER['HTTP_X_WC_WEBHOOK_SIGNATURE'] ?? '';

// WooCommerce sends a test ping "webhook_id=123" without a signature when the webhook is saved.
if ( '' === $signature && str_starts_with( $body, 'webhook_id=' ) ) {
    http_response_code( 200 );
    exit;
}

$expected = base64_encode( hash_hmac( 'sha256', $body, $secret, true ) );
if ( ! hash_equals( $expected, $signature ) ) {
    http_response_code( 401 );
    exit;
}

$delivery = $_SERVER['HTTP_X_WC_WEBHOOK_DELIVERY_ID'] ?? '';
$order    = json_decode( $body, true );

// Store the raw event and process it later; answer fast.
ha_queue_event( $delivery, $_SERVER['HTTP_X_WC_WEBHOOK_TOPIC'] ?? '', $order );
http_response_code( 200 );

Three details:

  • The HMAC is computed over the raw body exactly as received. Decoding and re-encoding the JSON first changes spaces and escaping, and the signature no longer matches.
  • The fourth argument true of hash_hmac() returns raw bytes, which are then base64-encoded; that is the format WooCommerce uses. A hex string never matches.
  • hash_equals() compares in constant time, so the response time does not leak how many characters of a guessed signature were right.

Deliveries, retries and duplicates

WooCommerce sends webhooks through Action Scheduler, in the background after the order is saved. A delivery counts as failed when the receiver does not answer with a 2xx status within the timeout; after five consecutive failures WooCommerce disables the webhook and its status becomes “Disabled”. The delivery logs are under WooCommerce → Status → Logs (source webhooks-delivery).

The same event can arrive twice, for example when the answer of the receiver was lost. The receiver should store X-WC-Webhook-Delivery-ID or the order ID with the topic and skip what it has already processed.

NOTE: The payload contains personal data (names, addresses, emails), so the delivery URL must use HTTPS. If the receiver is a WordPress site itself, register a REST route and read the body with $request->get_body() for the signature check. The opposite direction, a service sending events to WooCommerce: order notes from a warehouse webhook.