$npx -y skills add hookdeck/webhook-skills --skill woocommerce-webhooksReceive and verify WooCommerce webhooks. Use when setting up WooCommerce webhook handlers, debugging signature verification, or handling e-commerce events like order.created, order.updated, product.created, or customer.created.
| 1 | # WooCommerce Webhooks |
| 2 | |
| 3 | ## When to Use This Skill |
| 4 | |
| 5 | - Setting up WooCommerce webhook handlers |
| 6 | - Debugging signature verification failures |
| 7 | - Understanding WooCommerce event types and payloads |
| 8 | - Handling order, product, or customer events |
| 9 | - Integrating with WooCommerce stores |
| 10 | |
| 11 | ## Essential Code (USE THIS) |
| 12 | |
| 13 | ### WooCommerce Signature Verification (JavaScript) |
| 14 | |
| 15 | ```javascript |
| 16 | const crypto = require('crypto'); |
| 17 | |
| 18 | function verifyWooCommerceWebhook(rawBody, signature, secret) { |
| 19 | if (!signature || !secret) return false; |
| 20 | |
| 21 | const hash = crypto |
| 22 | .createHmac('sha256', secret) |
| 23 | .update(rawBody) |
| 24 | .digest('base64'); |
| 25 | |
| 26 | try { |
| 27 | return crypto.timingSafeEqual( |
| 28 | Buffer.from(signature), |
| 29 | Buffer.from(hash) |
| 30 | ); |
| 31 | } catch { |
| 32 | return false; |
| 33 | } |
| 34 | } |
| 35 | ``` |
| 36 | |
| 37 | ### Express Webhook Handler |
| 38 | |
| 39 | ```javascript |
| 40 | const express = require('express'); |
| 41 | const app = express(); |
| 42 | |
| 43 | // CRITICAL: Use raw body for signature verification |
| 44 | app.use('/webhooks/woocommerce', express.raw({ type: 'application/json' })); |
| 45 | |
| 46 | app.post('/webhooks/woocommerce', (req, res) => { |
| 47 | const signature = req.headers['x-wc-webhook-signature']; |
| 48 | const secret = process.env.WOOCOMMERCE_WEBHOOK_SECRET; |
| 49 | |
| 50 | if (!verifyWooCommerceWebhook(req.body, signature, secret)) { |
| 51 | return res.status(400).send('Invalid signature'); |
| 52 | } |
| 53 | |
| 54 | const payload = JSON.parse(req.body); |
| 55 | const topic = req.headers['x-wc-webhook-topic']; |
| 56 | |
| 57 | console.log(`Received ${topic} event:`, payload.id); |
| 58 | res.status(200).send('OK'); |
| 59 | }); |
| 60 | ``` |
| 61 | |
| 62 | ### Next.js API Route (App Router) |
| 63 | |
| 64 | ```typescript |
| 65 | import crypto from 'crypto'; |
| 66 | import { NextRequest } from 'next/server'; |
| 67 | |
| 68 | export async function POST(request: NextRequest) { |
| 69 | const signature = request.headers.get('x-wc-webhook-signature'); |
| 70 | const secret = process.env.WOOCOMMERCE_WEBHOOK_SECRET; |
| 71 | |
| 72 | const rawBody = await request.text(); |
| 73 | |
| 74 | if (!verifyWooCommerceWebhook(rawBody, signature, secret)) { |
| 75 | return new Response('Invalid signature', { status: 400 }); |
| 76 | } |
| 77 | |
| 78 | const payload = JSON.parse(rawBody); |
| 79 | const topic = request.headers.get('x-wc-webhook-topic'); |
| 80 | |
| 81 | console.log(`Received ${topic} event:`, payload.id); |
| 82 | return new Response('OK', { status: 200 }); |
| 83 | } |
| 84 | ``` |
| 85 | |
| 86 | ### FastAPI Handler |
| 87 | |
| 88 | ```python |
| 89 | import hmac |
| 90 | import hashlib |
| 91 | import base64 |
| 92 | from fastapi import FastAPI, Request, HTTPException |
| 93 | |
| 94 | app = FastAPI() |
| 95 | |
| 96 | def verify_woocommerce_webhook(raw_body: bytes, signature: str, secret: str) -> bool: |
| 97 | if not signature or not secret: |
| 98 | return False |
| 99 | |
| 100 | hash_digest = hmac.new( |
| 101 | secret.encode(), |
| 102 | raw_body, |
| 103 | hashlib.sha256 |
| 104 | ).digest() |
| 105 | expected_signature = base64.b64encode(hash_digest).decode() |
| 106 | |
| 107 | return hmac.compare_digest(signature, expected_signature) |
| 108 | |
| 109 | @app.post('/webhooks/woocommerce') |
| 110 | async def handle_webhook(request: Request): |
| 111 | raw_body = await request.body() |
| 112 | signature = request.headers.get('x-wc-webhook-signature') |
| 113 | secret = os.getenv('WOOCOMMERCE_WEBHOOK_SECRET') |
| 114 | |
| 115 | if not verify_woocommerce_webhook(raw_body, signature, secret): |
| 116 | raise HTTPException(status_code=400, detail='Invalid signature') |
| 117 | |
| 118 | payload = await request.json() |
| 119 | topic = request.headers.get('x-wc-webhook-topic') |
| 120 | |
| 121 | print(f"Received {topic} event: {payload.get('id')}") |
| 122 | return {'status': 'success'} |
| 123 | ``` |
| 124 | |
| 125 | ## Common Event Types |
| 126 | |
| 127 | | Event | Triggered When | Common Use Cases | |
| 128 | |-------|----------------|------------------| |
| 129 | | `order.created` | New order placed | Send confirmation emails, update inventory | |
| 130 | | `order.updated` | Order status changed | Track fulfillment, send notifications | |
| 131 | | `order.deleted` | Order deleted | Clean up external systems | |
| 132 | | `product.created` | Product added | Sync to external catalogs | |
| 133 | | `product.updated` | Product modified | Update pricing, inventory | |
| 134 | | `customer.created` | New customer registered | Welcome emails, CRM sync | |
| 135 | | `customer.updated` | Customer info changed | Update profiles, preferences | |
| 136 | |
| 137 | ## Environment Variables |
| 138 | |
| 139 | ```bash |
| 140 | WOOCOMMERCE_WEBHOOK_SECRET=your_webhook_secret_key |
| 141 | ``` |
| 142 | |
| 143 | ## Headers Reference |
| 144 | |
| 145 | WooCommerce webhooks include these headers: |
| 146 | |
| 147 | - `X-WC-Webhook-Signature` - HMAC SHA256 signature (base64) |
| 148 | - `X-WC-Webhook-Topic` - Event type (e.g., "order.created") |
| 149 | - `X-WC-Webhook-Resource` - Resource type (e.g., "order") |
| 150 | - `X-WC-Webhook-Event` - Action (e.g., "created") |
| 151 | - `X-WC-Webhook-Source` - Store URL |
| 152 | - `X-WC-Webhook-ID` - Webhook ID |
| 153 | - `X-WC-Webhook-Delivery-ID` - Unique delivery ID |
| 154 | |
| 155 | ## Local Development |
| 156 | |
| 157 | For local webhook testing, install Hookdeck CLI: |
| 158 | |
| 159 | ```bash |
| 160 | ``` |
| 161 | |
| 162 | Then start the tunnel: |
| 163 | |
| 164 | ```bash |
| 165 | npx hookdeck-cli listen 3000 woocommerce --path /webhooks/woocommerce |