接收艾克索拉Webhook

处理购买时,艾克索拉会发送Webhook来通知各种事件的发生(如取消支付、获取购物车内容等)。通过使用这些Webhook,您可以:

  • 向玩家物品库添加购买项(无论使用自己的解决方案还是第三方解决方案)
  • 实现验证和处理订单的自有逻辑以记录信息、发放奖励、给予订单折扣等。

要在BaaS测接收Webhook,请按照以下用于FirebasePlayFab的说明将现成即用的函数添加到您的项目。

向Firebase项目添加云函数

  1. 初始化您的Firebase项目
  2. 导入Webhook接收函数,其中<WebhookSecretKey>是在发布商帐户项目设置 > Webhoo部分找到的密钥

接收Webhook的函数代码:

Copy
Full screen
Small screen
 1const webhookSecretKey = "<WebhookSecretKey>";
 2
 3exports.webhook = functions.https.onRequest((request, response) => {
 4  const requestRawBody = request.rawBody.toString("utf-8");
 5  const crypto = require("crypto");
 6  const sig = "Signature " + crypto
 7      .createHash("sha1")
 8      .update(requestRawBody + webhookSecretKey)
 9      .digest("hex");
10  if (request.headers.authorization !== sig) {
11    response.status(401).send();
12    return;
13  }
14
15  // TODO Replace this block with your game's logic for purchases handling
16  switch (request.body.notification_type) {
17    case "order_paid": {
18      const userId = request.body.user.external_id;
19      const skus = request.body.items.map(function(it) {
20        return it.sku;
21      }).join(", ");
22      const price =
23          `${request.body.order.amount} ${request.body.order.currency}`;
24      functions.logger.log(
25          "Order Paid\n",
26          `A user ${userId} has just paid ${price} for ${skus}\n`,
27          "Full Data\n",
28          request.body
29      );
30      break;
31    }
32    case "order_canceled": {
33      const userId = request.body.user.external_id;
34      const orderId = request.body.order.id;
35      functions.logger.log(
36          "Order Canceled\n",
37          `A user ${userId} has just canceled order ${orderId}\n`,
38          "Full Data\n",
39          request.body
40      );
41      break;
42    }
43  }
44
45  response.status(204).send();
46});
  1. 实现Webhook处理的逻辑。
注:
该函数用于接收与购买时获取购物车内容取消支付相关的Webhook。您可以自行实现支付页面其他事件的相关Webhook
  1. 参考此示例将函数部署到生产环境。
  2. 在Firebase控制台中,前往Build > Functions并复制Webhook接收函数的URL。
  3. 发布商帐户中打开您的项目。
  4. 前往项目设置 > Webhook
  5. Webhook服务器字段,指定Webhook接收函数的URL。

向PlayFab项目添加云脚本

注:
PlayFab云脚本不直接支持使用HTTP触发器的函数,因此这里使用Azure函数来实现Webhook的接收。

  1. 准备与Azure函数交互的开发环境
  2. 参考示例添加接收Webhook的函数,其中<WebhookSecretKey>是在发布商帐户项目设置 > Webhook部分找到的密钥

接收Webhook的函数代码:

Copy
Full screen
Small screen
 1const webhookSecretKey = "<WebhookSecretKey>";
 2
 3module.exports = async function (context, request) {
 4    const requestRawBody = request.rawBody.toString("utf-8");
 5    const crypto = require("crypto");
 6    const sig = "Signature " + crypto
 7        .createHash("sha1")
 8        .update(requestRawBody + webhookSecretKey)
 9        .digest("hex");
10    if (request.headers.authorization !== sig) {
11        context.res = {
12            status: 401
13        };
14        return;
15    }
16
17    // TODO Replace this block with your game's logic for purchases handling
18    switch (request.body.notification_type) {
19        case "order_paid": {
20            const userId = request.body.user.external_id;
21            const skus = request.body.items.map(function (it) {
22                return it.sku;
23            }).join(", ");
24            const price =
25                `${request.body.order.amount} ${request.body.order.currency}`;
26            context.log(
27                "Order Paid\n" +
28                `A user ${userId} has just paid ${price} for ${skus}\n` +
29                "Full Data\n" +
30                JSON.stringify(request.body)
31            );
32            break;
33        }
34        case "order_canceled": {
35            const userId = request.body.user.external_id;
36            const orderId = request.body.order.id;
37            context.log(
38                "Order Canceled\n" +
39                `A user ${userId} has just canceled order ${orderId}\n` +
40                "Full Data\n" +
41                JSON.stringify(request.body)
42            );
43            break;
44        }
45    }
46
47    context.res = {
48        status: 204
49    };
50}
  1. 实现Webhook的处理逻辑。
注:
该函数用于接收与购买时获取购物车内容取消支付相关的Webhook。您可以自行实现支付页面其他事件的相关Webhook
  1. 将函数部署到生产环境
  2. 复制函数URL
  3. 前往您的PlayFab项目。
  4. 注册云脚本函数
  5. 发布商帐户中打开您的项目。
  6. 前往项目设置 > Webhooks
  7. Webhook服务器字段,指定Webhook接收函数的URL。
本文对您的有帮助吗?
谢谢!
我们还有其他可改进之处吗? 留言
非常抱歉
请说明为何本文没有帮助到您。 留言
感谢您的反馈!
我们会查看您的留言并运用它改进用户体验。
上次更新时间: 2025年7月9日

发现了错别字或其他内容错误? 请选择文本,然后按Ctrl+Enter。

报告问题
我们非常重视内容质量。您的反馈将帮助我们做得更好。
请留下邮箱以便我们后续跟进
感谢您的反馈!
无法发送您的反馈
请稍后重试或发送邮件至doc_feedback@xsolla.com与我们联系。