reactjs 如何在Shopify中设置订单创建webhook?

kq4fsx7k  于 5个月前  发布在  React
关注(0)|答案(2)|浏览(98)

我已经开始在shopify-app-remix上构建应用程序。我无法设置订单以在此包上创建webhook。我尝试了以下代码以在订单时触发webhook。当我使用npm run dev运行应用程序时,在命令提示符下,我只能看到“应用程序卸载程序”。
在shopify.server.js文件中:

import "@shopify/shopify-app-remix/adapters/node";
import {
  AppDistribution,
  DeliveryMethod,
  shopifyApp,
  LATEST_API_VERSION,
} from "@shopify/shopify-app-remix/server";
import { PrismaSessionStorage } from "@shopify/shopify-app-session-storage-prisma";
import { restResources } from "@shopify/shopify-api/rest/admin/2023-07";

import prisma from "./db.server";

const shopify = shopifyApp({
  apiKey: process.env.SHOPIFY_API_KEY,
  apiSecretKey: process.env.SHOPIFY_API_SECRET || "",
  apiVersion: LATEST_API_VERSION,
  scopes: process.env.SCOPES?.split(","),
  appUrl: process.env.SHOPIFY_APP_URL || "",
  authPathPrefix: "/auth",
  sessionStorage: new PrismaSessionStorage(prisma),
  distribution: AppDistribution.AppStore,
  restResources,
  webhooks: {
    APP_UNINSTALLED: {
      deliveryMethod: DeliveryMethod.Http,
      callbackUrl: "/webhooks",
    },
    ORDERS_CREATE: {
      deliveryMethod: DeliveryMethod.Http,
      callbackUrl: "/webhooks",
    },
  },
  hooks: {
    afterAuth: async ({ session }) => {
      shopify.registerWebhooks({ session });
    },
  },
  ...(process.env.SHOP_CUSTOM_DOMAIN
    ? { customShopDomains: [process.env.SHOP_CUSTOM_DOMAIN] }
    : {}),
});

export default shopify;
export const apiVersion = LATEST_API_VERSION;
export const addDocumentResponseHeaders = shopify.addDocumentResponseHeaders;
export const authenticate = shopify.authenticate;
export const unauthenticated = shopify.unauthenticated;
export const login = shopify.login;
export const registerWebhooks = shopify.registerWebhooks;
export const sessionStorage = shopify.sessionStorage;

字符串
在webhooks.jsx文件中:

import { authenticate } from "../shopify.server";
import db from "../db.server";

export const action = async ({ request }) => {
  const { topic, shop, session } = await authenticate.webhook(request);

  switch (topic) {
    case "APP_UNINSTALLED":
      if (session) {
        await db.session.deleteMany({ where: { shop } });
      }
      break;
    case "CUSTOMERS_DATA_REQUEST":
    case "CUSTOMERS_REDACT":
    case "SHOP_REDACT":
    case "ORDERS_CREATE": console.log(' Order create webhook run'); break;
    default:
      throw new Response("Unhandled webhook topic", { status: 404 });
  }

  throw new Response();
};

9nvpjoqh

9nvpjoqh1#

假设您使用的是CLI生成的最新Shopify Boiler模板。
额外的webhook在初始安装时创建。即使它被正确安装,也不建议依赖它。
从长远来看,你会想自己开发添加webhooks的方法。

  • 如果您的应用程序增长,您需要更多的webhook,该怎么办?
  • 如果您的应用程序崩溃,Shopify删除了您的webhooks。

以下是https://github.com/Shopify/shopify-api-js/blob/main/docs/guides/webhooks.md的官方文档

brgchamk

brgchamk2#

order/create webhook的正确名称是ORDER_TRANSACTIONS_CREATE
根据这份文件。
https://shopify.dev/docs/api/admin-graphql/2023-07/enums/WebhookSubscriptionTopic

相关问题