Skip to main content

Kev tuav webhook

Tsim cov webhook endpoints tshwj xeeb kom txais cov xwm txheej los ntawm cov kev pabcuam sab nraud thiab sync cov ntaub ntawv nrog Ultimate Multisite.

Webhook Endpoint Tshwj Xeeb

// Register webhook endpoint
add_action('rest_api_init', function() {
register_rest_route('my-addon/v1', '/webhook', [
'methods' => 'POST',
'callback' => 'handle_my_webhook',
'permission_callback' => 'verify_webhook_signature'
]);
});

function handle_my_webhook($request) {
$payload = $request->get_json_params();

switch ($payload['event_type']) {
case 'customer.updated':
$customer = wu_get_customer($payload['customer_id']);
if ($customer) {
$customer->set_vip($payload['data']['is_vip']);
$customer->save();
}
break;

case 'subscription.cancelled':
$membership = wu_get_membership_by_hash($payload['subscription_id']);
if ($membership) {
$membership->cancel();
}
break;
}

return ['status' => 'processed'];
}

function verify_webhook_signature($request) {
$signature = $request->get_header('X-Webhook-Signature');
$payload = $request->get_body();
$secret = get_option('my_webhook_secret');

return hash_hmac('sha256', $payload, $secret) === $signature;
}

Yam Yuav Tsum Xav Txog Txog Kev Ruaj Ntseg

  • Ib txwm kuaj xyuas webhook signatures siv HMAC lossis ib qho shared secret
  • Siv permission_callback rau koj txoj REST route — tsis txhob teeb nws ua __return_true hauv production
  • Tshawb xyuas thiab ntxuav kom huv txhua yam ntaub ntawv payload uas tuaj txog ua ntej yuav siv nws
  • Xa rov qab HTTP status codes kom tsim nyog (200 rau kev ua tiav, 400 rau cov lus thov tsis raug)