接收艾克索拉Webhook

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

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

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

向Firebase项目添加云函数

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

接收Webhook的函数代码:

Copy
Full screen
Small screen
const webhookSecretKey = "<WebhookSecretKey>";

exports.webhook = functions.https.onRequest((request, response) => {
  const requestRawBody = request.rawBody.toString("utf-8");
  const crypto = require("crypto");
  const sig = "Signature " + crypto
      .createHash("sha1")
      .update(requestRawBody + webhookSecretKey)
      .digest("hex");
  if (request.headers.authorization !== sig) {
    response.status(401).send();
    return;
  }

  // TODO Replace this block with your game's logic for purchases handling
  switch (request.body.notification_type) {
    case "order_paid": {
      const userId = request.body.user.external_id;
      const skus = request.body.items.map(function(it) {
        return it.sku;
      }).join(", ");
      const price =
          `${request.body.order.amount} ${request.body.order.currency}`;
      functions.logger.log(
          "Order Paid\n",
          `A user ${userId} has just paid ${price} for ${skus}\n`,
          "Full Data\n",
          request.body
      );
      break;
    }
    case "order_canceled": {
      const userId = request.body.user.external_id;
      const orderId = request.body.order.id;
      functions.logger.log(
          "Order Canceled\n",
          `A user ${userId} has just canceled order ${orderId}\n`,
          "Full Data\n",
          request.body
      );
      break;
    }
  }

  response.status(204).send();
});
  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
const webhookSecretKey = "<WebhookSecretKey>";

module.exports = async function (context, request) {
    const requestRawBody = request.rawBody.toString("utf-8");
    const crypto = require("crypto");
    const sig = "Signature " + crypto
        .createHash("sha1")
        .update(requestRawBody + webhookSecretKey)
        .digest("hex");
    if (request.headers.authorization !== sig) {
        context.res = {
            status: 401
        };
        return;
    }

    // TODO Replace this block with your game's logic for purchases handling
    switch (request.body.notification_type) {
        case "order_paid": {
            const userId = request.body.user.external_id;
            const skus = request.body.items.map(function (it) {
                return it.sku;
            }).join(", ");
            const price =
                `${request.body.order.amount} ${request.body.order.currency}`;
            context.log(
                "Order Paid\n" +
                `A user ${userId} has just paid ${price} for ${skus}\n` +
                "Full Data\n" +
                JSON.stringify(request.body)
            );
            break;
        }
        case "order_canceled": {
            const userId = request.body.user.external_id;
            const orderId = request.body.order.id;
            context.log(
                "Order Canceled\n" +
                `A user ${userId} has just canceled order ${orderId}\n` +
                "Full Data\n" +
                JSON.stringify(request.body)
            );
            break;
        }
    }

    context.res = {
        status: 204
    };
}
  1. 实现Webhook的处理逻辑。
注:
该函数用于接收与购买时获取购物车内容取消支付相关的Webhook。您可以自行实现支付页面其他事件的相关Webhook
  1. 将函数部署到生产环境
  2. 复制函数URL
  3. 前往您的PlayFab项目。
  4. 注册云脚本函数
  5. 发布商帐户中打开您的项目。
  6. 前往项目设置 > Webhooks
  7. Webhook服务器字段,指定Webhook接收函数的URL。
本文对您的有帮助吗?
谢谢!
我们还有其他可改进之处吗? 留言
非常抱歉
请说明为何本文没有帮助到您。 留言
感谢您的反馈!
我们会查看您的留言并运用它改进用户体验。
为此页面评分
为此页面评分
我们还有其他可改进之处吗?

不想回答

感谢您的反馈!
上次更新时间: 2024年1月22日

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

报告问题
我们非常重视内容质量。您的反馈将帮助我们做得更好。
请留下邮箱以便我们后续跟进
感谢您的反馈!