Node.js 创建订单时在NodeJ上注册Webhook

Node.js 创建订单时在NodeJ上注册Webhook,node.js,shopify,webhooks,Node.js,Shopify,Webhooks,我有一个shopify商店mystore和一个nodejs应用myapp。我需要做的是,当mystore上发生某些事情时,将在我的nodejs应用程序中创建/注册一个webhook。我试过这个软件包,但它对我不起作用,我不认为这是我想要的东西我只想假设在商店中创建订单时,注册了webhook您不能在创建订单时创建/注册新的webhook Webhooks是一种从特定事件中检索和存储数据的工具。它们允许您注册一个https://URL,事件数据可以在其中以JSON或XML格式存储。Webhook通

我有一个shopify商店mystore和一个nodejs应用myapp。我需要做的是,当mystore上发生某些事情时,将在我的nodejs应用程序中创建/注册一个webhook。我试过这个软件包,但它对我不起作用,我不认为这是我想要的东西我只想假设在商店中创建订单时,注册了webhook

您不能在创建订单时创建/注册新的webhook

Webhooks是一种从特定事件中检索和存储数据的工具。它们允许您注册一个https://URL,事件数据可以在其中以JSON或XML格式存储。Webhook通常用于:

  • 下订单
  • 改变产品价格
  • 脱机时通知IM客户端或寻呼机
  • 为数据仓库收集数据
  • 集成您的会计软件
  • 筛选订单项目并将订单通知各个托运人
  • 卸载应用程序时从数据库中删除客户数据

如果您只需要注册webhook,您可以使用此代码。 您只需更改webhook主题和端点

这用于订单/创建webhook注册

添加shopify api节点请求承诺包并要求它们

const ShopifyAPIClient = require("shopify-api-node");
const request = require("request-promise");
然后

添加registerWebhook函数

const registerWebhook = async function (shopDomain, accessToken, webhook) {
  const shopify = new ShopifyAPIClient({
    shopName: shopDomain,
    accessToken: accessToken,
  });
  const isCreated = await checkWebhookStatus(shopDomain, accessToken, webhook);
  if (!isCreated) {
    shopify.webhook.create(webhook).then(
      (response) => console.log(`webhook '${webhook.topic}' created`),
      (err) =>
        console.log(
          `Error creating webhook '${webhook.topic}'. ${JSON.stringify(
            err.response.body
          )}`
        )
    );
  }
};
要检查Shopify上尚未创建的webhook,可以使用以下代码

const checkWebhookStatus = async function (shopDomain, accessToken, webhook) {
  try {
    const shopifyWebhookUrl =
      "https://" + shopDomain + "/admin/api/2020-07/webhooks.json";
    const webhookListData = {
      method: "GET",
      url: shopifyWebhookUrl,
      json: true,
      headers: {
        "X-Shopify-Access-Token": accessToken,
        "content-type": "application/json",
      },
    };
    let response = await request.get(webhookListData);
    if (response) {
      let webhookTopics = response.webhooks.map((webhook) => {
        return webhook.topic;
      });
      return webhookTopics.includes(webhook.topic);
    } else {
      return false;
    }
  } catch (error) {
    console.log("This is the error", error);
    return false;
  }
};
快乐编码:)

const checkWebhookStatus = async function (shopDomain, accessToken, webhook) {
  try {
    const shopifyWebhookUrl =
      "https://" + shopDomain + "/admin/api/2020-07/webhooks.json";
    const webhookListData = {
      method: "GET",
      url: shopifyWebhookUrl,
      json: true,
      headers: {
        "X-Shopify-Access-Token": accessToken,
        "content-type": "application/json",
      },
    };
    let response = await request.get(webhookListData);
    if (response) {
      let webhookTopics = response.webhooks.map((webhook) => {
        return webhook.topic;
      });
      return webhookTopics.includes(webhook.topic);
    } else {
      return false;
    }
  } catch (error) {
    console.log("This is the error", error);
    return false;
  }
};