【问题标题】:Stripe webhooks automatically erases metadata after a few calls几次调用后,Stripe webhook 会自动删除元数据
【发布时间】:2022-01-16 03:53:06
【问题描述】:

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

这是我直接从 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',
 
}
{}
{}
{}
{}
{}

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

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

【问题讨论】:

    标签: stripe-payments


    【解决方案1】:

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

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

    请分享您在何处设置元数据以及您希望在哪些特定对象/事件中接收元数据的详细信息。

    【讨论】:

    • 你是对的。我只在结帐会话对象中获取数据。因此在那里进行了供应。谢谢
    猜你喜欢
    • 2020-06-03
    • 1970-01-01
    • 2021-12-05
    • 2022-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2018-10-20
    相关资源
    最近更新 更多