如何将自定义数据传递给 Stripe webhook?

How can I pass custom data to Stripe webhook?

我正在创建和处理这样的条带付款:

    // Create payment intent
    const { data } = await axios.post('/api/payments/get-payment-intent', { slug: post.slug })

    // Use payment intent to charge the card
    const result = await stripe.confirmCardPayment(data.paymentIntentSecert, {
      payment_method: {
        card: elements.getElement(CardElement),
      },
    })

为了能够完成订单,我需要能够将一些数据(产品的 id 和买家的 username)传递给付款后执行的网络钩子已成功完成(payment_intent.succeeded 事件)。

我该怎么做?

我尝试像这样向 confirmCardPayment() 添加 metadata 键:

    const result = await stripe.confirmCardPayment(data.paymentIntentSecert, {
      payment_method: {
        card: elements.getElement(CardElement),
        metadata: {
          username: user.username,
          postId: post.id
        }
      },
    })

但是元数据没有显示在 webhook 接收到的对象上。

无法使用 confirmCardPayment() 更新 PaymentIntent 元数据。

您首先要将用户名和 postId 传递给您的后端服务器。

例子

const { data } = await axios.post('/api/payments/get-payment-intent', { 
    username:user.username, 
    postId: post.Id 
});

随后 create the PaymentIntent with the metadata

Node.js 创建 PaymentIntent 的示例

const paymentIntent = await stripe.paymentIntents.create({
  amount: 2000,
  currency: 'usd',
  metadata: {
      username,
      postId
  },
});