0

I am trying to console log the Stripe customerID once the checkout.session.completed event is complete. but I am getting this 400 status code error "Unable to extract timestamp and signatures from header". I can't seem to figure out a solution to get the Stripe customer ID to console log.

Any help or pointers are appreciated. Here is my code:

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}`,
    };
  }
};

When I console log the headers as suggested from a comment in get this in the terminal: console.log(headers)

I can now see the customer ID (cus_JOqbW95ulF3zx0) in the terminal, but what do I need to log it as customerID? When the terminal is saying customerID is not defined.

SteveKim72
  • 31
  • 6
  • 1
    Can you log the content of `headers['stripe-signature']` and add that detail to your question? – Justin Michael Apr 26 '21 at 21:18
  • @JustinMichael Sorry for the late response. I did as you suggested and in the terminal i can now see the Stripe customer ID as part of the headers, but how would i go about isolating just the customer id as a customerID variable? – SteveKim72 Apr 30 '21 at 20:42
  • It looks like your code is working... the output you're showing indicates `console.log(customerID);` is logging the Customer ID, then `console.log(headers);` is logging the headers object. If you remove `console.log(headers);` do you just get the Customer ID? – Justin Michael Apr 30 '21 at 23:13
  • @JustinMichael I think I got it figured out. the console.log in the if statement for customerID works. the reason I was getting the 400 error was because of the return for the body of customerID. I just ended up replacing it with a 'success' string and now I am able to see the customerID in the console and also get a 200 status code. Thanks for your help. – SteveKim72 May 04 '21 at 17:00

1 Answers1

0

I was able to figure it out. My return after the IF statement was the reason for the 400 status code error. the customerID shows up in the console and I changed the return in the body stats code to a string "success".

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}`,
    };
  }
};
SteveKim72
  • 31
  • 6