The EcomPHP\TiktokShop\Webhook class is used to capture, verify, and parse incoming notification payloads from TikTok Shop.
To use it, instantiate the class with your existing Client instance, then use capture() to load the payload and verify() to ensure the request is authentic using the HMAC signature.
Workflow
- Initialize: Create a
new Webhook($client). - Capture: Call
capture() without arguments to automatically read from php://input, or pass an array of parameters manually. - Verify: Call
verify() to validate the signature against your AppSecret. By default, it looks for the signature in the HTTP_AUTHORIZATION server variable. - Extract: Use getter methods to access the event type, shop ID, timestamp, and the event data.
use EcomPHP\TiktokShop\Webhook;
// Assuming $client is already initialized
$webhook = new Webhook($client);
try {
// 1. Capture the payload
$webhook->capture();
// 2. Verify the signature (checks HTTP_AUTHORIZATION by default)
if ($webhook->verify()) {
// 3. Process the data
$type = $webhook->getType();
$shopId = $webhook->getShopId();
$data = $webhook->getData();
if ($type === Webhook::ORDER_STATUS_UPDATE) {
// Handle order status change
}
}
} catch (TiktokShopException $e) {
// Handle invalid signature or invalid data
echo 'Webhook error: ' . $e->getMessage();
}