如何从 Stripe charge.succeeded 事件中识别特定产品?

How can I identify specific products from Stripe charge.succeeded events?

我已经设置了一个 Pipedream 工作流,它使用 webhook 监听 Stripe 事件,然后将购买者添加到 mailerlite 组。

我想销售多种产品,但已根据用户购买的产品将用户添加到不同的 mailerlite 组。

我以为我可以通过将目标 mailerlite 组 ID 添加到产品元数据来做到这一点,但是当我对此进行测试时,元数据对象进入 pipedream 时是空的。

如何从条纹 charge.succeeded 事件中识别产品?

没有从 charge.succeeded 检索产品的操作,charge.succeeded 事件中也没有任何数据可以引导您返回源产品。

本例中的元数据不是与产品关联的元数据,而是与 charge.succeeded 事件关联的元数据。

为了做我想做的事,我必须:

  1. 更改触发器以查找 checkout.session.completed,它在结帐完成时发生。
  2. 获取结帐会话 ID 并使用它来调用条带 API 并获取订单项:
import { axios } from "@pipedream/platform"

export default defineComponent({
  props: {
    stripe: {
      type: "app",
      app: "stripe",
    }
  },
  async run({steps, $}) {
    return await axios($, {
      url: `https://api.stripe.com/v1/checkout/sessions/${steps.trigger.event.data.object.id}/line_items?limit=5`,
      auth: {
        username: `${this.stripe.$auth.api_key}`,
        password: ``,
      },
    })
  },
})

  1. 订单项包含价格,而价格又引用产品,因此使用结帐会话订单项请求的输出来获取产品 ID:
import { axios } from "@pipedream/platform"
export default defineComponent({
  props: {
    stripe: {
      type: "app",
      app: "stripe",
    }
  },
  async run({steps, $}) {
    return await axios($, {
      url: `https://api.stripe.com/v1/products/${steps.stripe.$return_value.data[0].price.product}`,
      auth: {
        username: `${this.stripe.$auth.api_key}`,
        password: ``,
      },
    })
  },
})
  1. 使用 that 的输出获取元数据并在 mailerlite _subscribe_to_group 操作中使用它。

使用标准的 Checkout 集成,您可以创建 Checkout Session,并且您知道将购买哪些产品,因为您将它们传递到 line_items (api ref) you pass in. Instead of setting metadata on the Product, you could add logic on your end to know which "target mailerlite group id" before you create the Checkout Session and then pass that information into metadata (see apiref)。您将从 checkout.session.completed 事件中获得所有这些信息。

如果您正在做 cross-sells 这样的事情,这将不起作用,但它可能是一个不错的选择,并且可以避免您在 re-retrieve 来自 stripe 的产品时遇到的麻烦想检查元数据。