Stripe webhooks 在几次调用后自动删除元数据

Stripe webhooks automatically erases metadata after a few calls

我正在创建一个会话并传递一些元数据。然后使用 webhooks 我捕获它以配置用户。

这是我直接从 Stripe 文档复制的 webhook 代码。

app.post("/webhook", async (req, res) => {
  let data;
  let eventType;
  // Check if webhook signing is configured.
  const webhookSecret = {{'STRIPE_WEBHOOK_SECRET'}}
  if (webhookSecret) {
    // Retrieve the event by verifying the signature using the raw body and secret.
    let event;
    let signature = req.headers["stripe-signature"];

    try {
      event = stripe.webhooks.constructEvent(
        req.body,
        signature,
        webhookSecret
      );
    } catch (err) {
      console.log(`⚠️  Webhook signature verification failed.`);
      return res.sendStatus(400);
    }
    // Extract the object from the event.
    data = event.data;
    eventType = event.type;
  } else {
  
    data = req.body.data;
    eventType = req.body.type;
    console.log(data.object.metadata)// ----------------> This is the only line I have added.

  }

  switch (eventType) {
      case 'checkout.session.completed':
      
        break;
      case 'invoice.paid':
       
        break;
      case 'invoice.payment_failed':
       
        break;
      default:
      // Unhandled event type
    }

  res.sendStatus(200);
});

data.object.metadata 的输出如下所示。

{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{}
{
  userId: '61e1d34343223e6828ac3935a8',
  postNumber: '8',
  accountType: 'growth',
 
}
{}
{}
{}
{}
{}

我想保留元数据值,以便以后可以将它们用于发票完成事件。但是当我在发票完成事件中访问它时它是空的。

我是编程新手。非常感谢任何帮助

您应该将事件类型与元数据一起记录下来。您很可能正在接收和显示具有不同对象负载的几种不同类型的事件。

您在哪里设置元数据,在结帐会话本身?那只会出现在结帐会话对象和事件上,而不是发票 objects/events。

请详细说明您在哪里设置元数据,以及您希望在哪个特定 objects/events 中收到元数据。