#nikivi

1 messages · Page 1 of 1 (latest)

tight tideBOT
digital zinc
#

Hello! Can you clarify - which event are you expecting?

knotty crater
#

this is my router currently

#

i create a checkout session here

#

on test account, i go through the payment with 424242

#

in local I run this

#

i expect the stripe to send events to my endpoint

#

but i get nothing

#

after completing this

digital zinc
#

Can you give me a checkout session ID I can look at?

digital zinc
#

When you ran the stripe listen command, did you make sure to run it before the payment was successful?

knotty crater
#

yes

#

as i was testing another product before

#

but its all under my name

digital zinc
#

Possibly - are you sure you're logged into the same account in the CLI that's doing the payments?

knotty crater
#

there is only one account

#

so yes

#

just ran stripe login again

#

maybe localhost:8787/wiki-bought

#

should be full ip address

#

it doesn't like localhost

#

anything i can do to debug

#

never runs

digital zinc
#

let's pause for a second

#

just do stripe listen (don't include the forward-to)

#

and then try the payment again to see if you get the event

knotty crater
#

i do

#

stripe listen --forward-to http://127.0.0.1:8787/wiki-bought

#

going to try with this

#

ok that works

#

i do get this though

#
app.post("/wiki-bought", async (c: Context) => {
  let event = c.req.body
  const stripe = new Stripe(c.env.STRIPE_WIKI_SECRET_KEY!, {
    apiVersion: "2022-11-15",
    typescript: true,
  })
  const endpointSecret = c.env.STRIPE_WIKI_WEBHOOK_SECRET!
  if (endpointSecret) {
    // Get the signature sent by Stripe
    const signature = c.req.header("stripe-signature")
    try {
      event = stripe.webhooks.constructEvent(c.body, signature, endpointSecret)
    } catch (err) {
      console.log(`⚠️  Webhook signature verification failed.`, err.message)
      c.status(400)
      return c.json({ err: "failed" })
    }
  }

  // Handle the event
  switch (event.type) {
    case "checkout.session.completed":
      const checkoutSessionCompleted = event.data.object
      console.log(checkoutSessionCompleted)
      break
    default:
      // Unexpected event type
      console.log(`Unhandled event type ${event.type}.`)
  }
  return c.json({})
})
#

is my code

#

its not express

#

its using https://hono.dev

Hono is a small, simple, and ultrafast web framework for the Edges. It works on Cloudflare Workers, Fastly Compute@Edge, Deno, Bun, Vercel, Lagon, AWS Lambda, Node.js, and others. Fast, but not only fast.

#

but that signature check should work

#

as thats unrelated to hono

#

i think

#

what could cause this

digital zinc
#

give me a few minutes to look

knotty crater
#

im running it in context of cloudflare workers

digital zinc
#

Still looking - sorry for the wait

knotty crater
#

yea ofc

digital zinc
#

Instead of constructEvent try using constructEventAsync

knotty crater
#

ok @digital zinc

#

that fixed that error

#

but now

#
app.post("/wiki-bought", async (c: Context) => {
  let event = c.req.body
  const stripe = new Stripe(c.env.STRIPE_WIKI_SECRET_KEY!, {
    apiVersion: "2022-11-15",
    typescript: true,
  })
  const endpointSecret = c.env.STRIPE_WIKI_WEBHOOK_SECRET!
  if (endpointSecret) {
    // Get the signature sent by Stripe
    const signature = c.req.header("stripe-signature")
    try {
      event = stripe.webhooks.constructEventAsync(
        c.body,
        signature,
        endpointSecret
      )
    } catch (err) {
      console.log(`⚠️  Webhook signature verification failed.`, err.message)
      c.status(400)
      return c.json({ err: "failed" })
    }
  }
  console.log(event, "event")

  // Handle the event
  switch (event.type) {
    case "checkout.session.completed":
      const checkoutSessionCompleted = event.data.object
      console.log(checkoutSessionCompleted)
      break
    default:
      // Unexpected event type
      console.log(`Unhandled event type ${event.type}.`)
  }
  return c.json({})
})
#

on this code

#

it never reaches checkout.session.completed

#

i console logged event

#

these are logs i see

#

its taken from stripe example code

#

are logs in listen

digital zinc
#

Sorry I should've been more specific - constructEventAsync is an async function now, so you have to wait for it resolve before you get the event

knotty crater
#

it should in theory reach the case

#

oh

digital zinc
#

event = await stripe.webhooks.constructEventAsync

knotty crater
#

so event = await stripe.webhooks.constructEventAsync(

#

ok trying

digital zinc
#

yup

knotty crater
#

getting this

#
    try {
      event = await stripe.webhooks.constructEventAsync(
        c.body,
#

i think its this c.body

digital zinc
#

Yeah, I don't know the specifics of how to do this with cloudflare workers, but agreed that c.body is likely the issue. You need to find out how to get the raw body (as a strong or buffer)

knotty crater
#

would array buffer work

digital zinc
#

I'm not sure - but you should just try it out and see

#

(I don't have a hono integration handy, so it'll be way faster for you to try this out on your end)

knotty crater
#

where is stripe.webhooks.constructEventAsync in stripe docs

#

can't find it

#

someone in hono discord asked

#

@digital zinc

digital zinc
tight tideBOT
knotty crater
#

trying to debug now

#

if anyone knows a solution, would be lovely

#

but even the stream printed there should at least show some bytes, iirc
at least Node does, not sure if the CF runtime does too

#

perhaps this

thorn jolt
#

Hello! I'm taking over and catching up...

knotty crater
#

thank you ❤️

thorn jolt
#

Try passing it c.req.text().

knotty crater
#

Hono is a small, simple, and ultrafast web framework for the Edges. It works on Cloudflare Workers, Fastly Compute@Edge, Deno, Bun, Vercel, Lagon, AWS Lambda, Node.js, and others. Fast, but not only fast.

#

its using this

#

@thorn jolt just so you know

thorn jolt
#

Yep.

knotty crater
#

thats with this code

#

full code here

#

from hono discord

thorn jolt
#

Sorry, I wasn't clear. c.req.text() returns a Promise that resolves with the text content of the body. You need to do something like const textBody = await c.req.text() and then pass in textBody.

knotty crater
#

so this

#

do you by chance know what those errors are about

#

signature!,

#

i can silence signature one like this i guess

#

but that would still be doing this

#

ok i get my events thank god

thorn jolt
#

Those are type errors that are beyond the scope of what I can help with.

knotty crater
#

thank you kindly @thorn jolt

knotty crater
#

not sure if i should open new help thread

#

but basically

#

events don't get sent for me

thorn jolt
#

What do you mean?

#

I thought you got the Events above just now?

thorn jolt
#

If you do just stripe listen does it work?

knotty crater
#

it does not

#

is it because i have to login individually

#

to each of those things

#

or something

#

i thought if stripe listen

#

because those checkouts

#

are on KusKus project

thorn jolt
#

Oh, yeah, if it's happening on a different account then you need to use stripe login to log in to the different account.