跳到主要内容
将当前页面内容以 Markdown 格式复制到剪贴板

Webhooks

Webhooks API 允许您创建、查看、更新和删除单个或批量 Webhook。

Webhook 可以通过 WooCommerce 设置屏幕或使用 REST API 端点进行管理。WC_Webhook 类负责管理所有与自定义文章类型相关的 Webhook 数据存储和检索,以及排队 Webhook 操作并处理/传递/记录 Webhook。在 woocommerce_init 时,加载已激活的 Webhook。

每个 Webhook 具有以下内容:

  • status: 状态 (active: 交付有效载荷; paused: 由管理员暂停交付; disabled: 因失败而暂停交付)。
  • topic: 确定触发 Webhook 的资源事件。
  • delivery URL: 交付有效载荷的 URL,必须是 HTTP 或 HTTPS。
  • secret: 一个可选的密钥,用于生成请求体的 HMAC-SHA256 哈希值,以便接收方可以验证 Webhook 的真实性。
  • hooks: 添加到 Webhook 并绑定以进行处理的钩子名称数组。

主题

主题是资源(例如:订单)和事件(例如:创建)的组合,并且映射到一个或多个钩子名称(例如:woocommerce_checkout_order_processed)。 可以使用主题名称创建 Webhook,并且会自动添加相应的钩子。

核心主题包括:

  • 优惠券: coupon.created, coupon.updatedcoupon.deleted.
  • 客户: customer.created, customer.updatedcustomer.deleted.
  • 订单: order.created, order.updatedorder.deleted.
  • 产品: product.created, product.updatedproduct.deleted.

也可以使用自定义主题,它们映射到一个钩子名称。 例如,您可以添加一个以 action.woocommerce_add_to_cart 为主题的 Webhook,该 Webhook 在发生该事件时触发。 自定义主题将第一个钩子参数传递给负载,因此在这个例子中,cart_item_key 将包含在负载中。

Delivery/payload

Delivery 是通过 wp_remote_post() (HTTP POST) 完成的,并且默认在后台使用 wp-cron 进行处理。为了帮助接收方处理 webhook,会添加一些自定义页眉到请求中:

  • X-WC-Webhook-Source: http://example.com/.
  • X-WC-Webhook-Topic - 例如: order.updated.
  • X-WC-Webhook-Resource - 例如: order.
  • X-WC-Webhook-Event - 例如: updated.
  • X-WC-Webhook-Signature - payload 的 base64 编码的 HMAC-SHA256 哈希值。
  • X-WC-Webhook-ID - webhook 的 post ID。
  • X-WC-Webhook-Delivery-ID - delivery 日志 ID (一个评论)。

payload 是 JSON 编码的,并且对于 API 资源(优惠券、客户、订单、产品),响应与通过 REST API 请求时完全相同。

日志

请求/回复使用 WooCommerce 的日志系统进行记录。每个交付日志包括:

  • 请求持续时间。
  • 请求网址、方法、页眉和正文。
  • 回复代码、消息、页眉和正文。

在出现 5 次连续的失败交付(由非 HTTP 2xx 回复码定义)后,该钩子将被禁用,必须通过 REST API 进行编辑才能重新启用。

可以访问 "WooCommerce" > "状态" > "日志" 中的交付日志。

可视化界面

您可以在 "WooCommerce" > "设置" > "高级" > "Webhooks" 中找到 Webhooks 界面,有关详细信息,请参阅我们的 可视化 Webhooks 文档

Webhook properties

| Attribute | Type | Description | | ------------------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | | id | integer | Unique identifier for the resource. READ-ONLY | | name | string | A friendly name for the webhook. | | status | string | Webhook status. Options: active, paused and disabled. Default is active. | | topic | string | Webhook topic. MANDATORY | | resource | string | Webhook resource. READ-ONLY | | event | string | Webhook event. READ-ONLY | | hooks | array | WooCommerce action names associated with the webhook. READ-ONLY | | delivery_url | string | The URL where the webhook payload is delivered. READ-ONLY MANDATORY | | secret | string | Secret key used to generate a hash of the delivered webhook and provided in the request headers. This will default is a MD5 hash from the current user's ID | username if not provided. WRITE-ONLY MANDATORY | | date_created | date-time | The date the webhook was created, in the site's timezone. READ-ONLY | | date_created_gmt | date-time | The date the webhook was created, as GMT. READ-ONLY | | date_modified | date-time | The date the webhook was last modified, in the site's timezone. READ-ONLY | | date_modified_gmt | date-time | The date the webhook was last modified, as GMT. READ-ONLY |

创建一个钩子

这个 API 可以帮助您创建新的钩子。

POST /wp-json/wc/v3/webhooks
curl -X POST https://example.com/wp-json/wc/v3/webhooks \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"name": "Order updated",
"topic": "order.updated",
"delivery_url": "http://requestb.in/1g0sxmo1"
}'

获取一个钩子

这个 API 允许你检索和查看特定的钩子。

GET /wp-json/wc/v3/webhooks/<id>
curl https://example.com/wp-json/wc/v3/webhooks/142 \
-u consumer_key:consumer_secret

查看全部 Webhooks

此 API 帮助您查看所有 Webhooks。

GET /wp-json/wc/v3/webhooks
curl https://example.com/wp-json/wc/v3/webhooks \
-u consumer_key:consumer_secret

Available parameters

ParameterTypeDescription
contextstringScope under which the request is made; determines fields present in response. Options: view and edit. Default is view.
pageintegerCurrent page of the collection. Default is 1.
per_pageintegerMaximum number of items to be returned in result set. Default is 10.
searchstringLimit results to those matching a string.
afterstringLimit response to resources published after a given ISO8601 compliant date.
beforestringLimit response to resources published before a given ISO8601 compliant date.
dates_are_gmtbooleanWhether to interpret dates as GMT dates when limiting response by published date.
excludearrayEnsure result set excludes specific IDs.
includearrayLimit result set to specific ids.
offsetintegerOffset the result set by a specific number of items.
orderstringOrder sort attribute ascending or descending. Options: asc and desc. Default is desc.
orderbystringSort collection by object attribute. Options: date, id, include, title and slug. Default is date.
statusstringLimit result set to webhooks assigned a specific status. Options: all, active, paused and disabled. Default is all.

Update a webhook

This API lets you make changes to a webhook.

PUT /wp-json/wc/v3/webhooks/<id>
curl -X PUT https://example.com/wp-json/wc/v3/webhooks/142 \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"status": "paused"
}'

删除一个钩子

这个 API 可以帮助您删除一个钩子。

DELETE /wp-json/wc/v3/webhooks/<id>
curl -X DELETE https://example.com/wp-json/wc/v3/webhooks/142 \
-u consumer_key:consumer_secret

可用参数

参数类型描述
force字符串使用 true 是否永久删除 Webhook,默认为 false

批量更新 Webhooks

此 API 可帮助您批量创建、更新和删除多个 Webhooks。

备注

注意:默认情况下,限制为最多 100 个对象可以被创建、更新或 删除。

POST /wp-json/wc/v3/webhooks/batch
curl -X POST https://example.com/wp-json/wc/v3/webhooks/batch \
-u consumer_key:consumer_secret \
-H "Content-Type: application/json" \
-d '{
"create": [
{
"name": "Coupon created",
"topic": "coupon.created",
"delivery_url": "http://requestb.in/1g0sxmo1"
},
{
"name": "Customer deleted",
"topic": "customer.deleted",
"delivery_url": "http://requestb.in/1g0sxmo1"
}
],
"delete": [
143
]
}'