Stripe payments Webhook错误:无法从header-Netlify函数和条带提取时间戳和签名

Stripe payments Webhook错误:无法从header-Netlify函数和条带提取时间戳和签名,stripe-payments,netlify,stripes,netlify-function,Stripe Payments,Netlify,Stripes,Netlify Function,一旦checkout.session.completed事件完成,我将尝试控制台记录条带customerID。但我收到了400状态码错误“无法从标题提取时间戳和签名”。我似乎无法找到一个解决方案来将客户ID条带化到控制台日志中 感谢您的帮助或指点。这是我的密码: const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); exports.handler = async ({ body, headers }) => {

一旦checkout.session.completed事件完成,我将尝试控制台记录条带customerID。但我收到了400状态码错误“无法从标题提取时间戳和签名”。我似乎无法找到一个解决方案来将客户ID条带化到控制台日志中

感谢您的帮助或指点。这是我的密码:

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

exports.handler = async ({ body, headers }) => {
  try {
    const stripeEvent = stripe.webhooks.constructEvent(
      body,
      headers['stripe-signature'],
      process.env.STRIPE_WEBHOOK_SECRET
    );

    if (stripeEvent.type === 'checkout.session.completed') {
      const eventObject = stripeEvent.data.object;
      const customerID = eventObject.customer;
      console.log(customerID);
      console.log(headers);
    }
    return {
      statusCode: 200,
      body: JSON.stringify(customerID),
    };
  } catch (err) {
    console.log(`Stripe webhook failed with ${err}`);
    return {
      statusCode: 400,
      body: `Webhook Error: ${err.message}`,
    };
  }
};
当我在控制台中按照“在终端中获取此”中的注释建议记录标题时:


我现在可以在终端中看到客户ID(cus_JOqbW95ulF3zx0),但是我需要什么来将其记录为customerID?当终端显示customerID未定义时。

我能够理解它。IF语句之后的返回是400状态代码错误的原因。customerID显示在控制台中,我将body stats代码中的返回值更改为字符串“success”


你能把
标题['stripe-signature']
的内容记录下来,并把这个细节添加到你的问题中吗?@JustinMichael很抱歉回复太晚了。我按照您的建议做了,在终端中,我现在可以将条带客户ID视为标题的一部分,但是我如何将客户ID作为customerID变量进行隔离呢?看起来您的代码正在工作。。。您显示的输出指示
console.log(customerID)
记录客户ID,然后
console.log(标题)正在记录headers对象。如果删除
console.log(标题)你刚刚拿到客户ID了吗?@JustinMichael我想我找到了。customerID的if语句中的console.log有效。我得到400错误的原因是因为customerID主体的返回。最后我用一个“success”字符串替换了它,现在我可以在控制台中看到customerID,还可以得到一个200状态码。谢谢你的帮助。
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

exports.handler = async ({ body, headers }) => {
  try {
    const stripeEvent = stripe.webhooks.constructEvent(
      body,
      headers['stripe-signature'],
      process.env.STRIPE_WEBHOOK_SECRET
    );

    if (stripeEvent.type === 'checkout.session.completed') {
      const eventObject = stripeEvent.data.object;
      const customerID = eventObject.customer;
      console.log(customerID);
    }
    return {
      statusCode: 200,
      body: 'success',
    };
  } catch (err) {
    console.log(`Stripe webhook failed with ${err}`);
    return {
      statusCode: 400,
      body: `Webhook Error: ${err.message}`,
    };
  }
};