#Xcoder-webhook

1 messages · Page 1 of 1 (latest)

nimble karma
#

Hi, what's the question/issue?

hexed vigil
#

I have created a webhook.js

#

In api/

#

And i wanted to save my stripe data in firebase database

#

I have also added permission.js

nimble karma
#

Ok, is there a specific issue or error?

hexed vigil
#

Its not working

#

I am not getting any error

#

But it's not working

#

I want to link my strip with firebase

#

So i can store my data in it

nimble karma
#

You'll need to share some more context. The code would be a good start

hexed vigil
#

Okay

#

I'll share you my github repo

nimble karma
#

Can you share the specific file?

hexed vigil
nimble karma
#

Ok, and there's no Firestore document or errors?

hexed vigil
#

No

#

I am following a youtube video

nimble karma
#

You'll need to add debugging/logs in your code to figure out at what point its failing and step through it

hexed vigil
#

I'm not getting this type of text

#

payment_intent etc

#

When I run stripe listen --forward-to localhost:3000/api/webhook

#

I just get this text

#

Ready! You are using Stripe API Version [2020-08-27]. Your webhook signing secret is

#

..................

hexed vigil
nimble karma
#

So looking at your code you're using the checkout.session.completed event. Have you actually completed a Checkout Session to fire that event on your account?

hexed vigil
#

yeah

nimble karma
#

Or you can simulate it with the CLI: stripe trigger checkout.session.completed

hexed vigil
#

my checkout session is working

#

I'm getting data in my stripe dashboard

#

I have installed stripe cli

nimble karma
#

Right, but you just said the events aren't being forwarded to your local webhook via the CLI?

hexed vigil
#

yeah

#

but it's forwarding to stripe

#

but not in firebase

nimble karma
#

Forwarding to Stripe? You don't forward webhooks back to us

#

Are you able to log something out in your webhook handler when checkout.session.completed events fire?

#

Because you just told me you don't see anything extra to suggest they're being forwarded in the CLI

hexed vigil
hexed vigil
hexed vigil
nimble karma
#

Right, but the issue is in the webhook. You need to be sure that your webhook, where you create the Firestore documents, is actually receiving those checkout.session.completed events

hexed vigil
#

Okay but how

nimble karma
#

Is it ever logged in your terminal where Next.js is running?

hexed vigil
nimble karma
#

Which means your webhook signing is failing

hexed vigil
#

I am getting this error

nimble karma
#

I can see that 🙂

hexed vigil
#

ERROR Timestamp outside the tolerance zone

#

I am getting this error

nimble karma
#

Also, no idea what buffer from that micro library does. But in my experience in working with Next.js and Stripe webhooks you should do this:

async function buffer(readable) {
  const chunks = []

  for await (const chunk of readable) {
    chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk)
  }

  return Buffer.concat(chunks)
}

async function handler(req, res) {
  if (req.method === 'POST') {
    const buf = await buffer(req)
    const sig = req.headers['stripe-signature']
    const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET
    let event

    try {
      if (!(sig || webhookSecret)) return
      event = stripe.webhooks.constructEvent(buf, sig, webhookSecret)
    } catch (err) {
      console.log(`❌ Error message: ${err.message}`)
      return res.status(400).send(`Webhook Error: ${err.message}`)
    }

    ...

    res.status(200).json({ received: true })
  } else {
    res.status(405).send()
  }
}
hexed vigil
#

Too difficult? 👉 Watch my REACT BASICS 101 class for FREE with 1 month FREE Skillshare access (First 1000 people): https://skl.sh/2Srfwuf

🚨 Join the world’s BEST developer community “Zero to Full Stack Hero” NOW: https://www.papareact.com/course

Join me on Day 4 of 5 of the 5-DAY AMAZON CHALLENGE where we build the following:

👉 Payment Proce...

▶ Play video
nimble karma
#

I'm trying to help you with the above

hexed vigil
#

I'm following this youtube video

nimble karma
#

Have you tried re-writing your code to what I shared?

hexed vigil
#

1:10:48 Explaining Webhook

hexed vigil
nimble karma
#

Is it the same error?

hexed vigil
mint violet
#

don't think we can help you with that!