Open shahreazebd opened 11 months ago
cc: @hideokamoto
Hi @shahreazebd . Do you mean that you want to get to know about how to get the original url or path data on the Hono like the express one? Or, do you want to know about how to verify the Stripe Webhook API request on the Hono?
Thanks.
I just want to know about how to verify the Stripe Webhook API request on the Hono?
@shahreazebd We have a template to use Hono and Stripe API / Webhook: https://github.com/stripe-samples/stripe-node-cloudflare-worker-template
You can learn how this example verify the Stripe Webhook API request on the Hono from this file: https://github.com/stripe-samples/stripe-node-cloudflare-worker-template/blob/main/src/index.js#L44-L73
Thanks.
@hideokamoto-stripe
Not sure if the example you provided works anymore...
Here's my code
// POST /stripe/webhook
stripe.post("/webhook", async (c) => {
const body = await c.req.text();
const sig = c.req.raw.headers.get("stripe-signature");
if (!sig) {
return c.json({ error: "No signature" }, 401);
}
const event: any = await constructStripeEvent(body, sig);
if (event === null || event.error) {
c.status(400);
return c.json({ error: "Invalid signature" });
}
log.info({ event });
return c.json({ message: "ok" });
});
// construct wh event
async function constructStripeEvent(body: any, sig: string) {
try {
let event = await stripe.webhooks.constructEventAsync(
body,
sig,
config.stripe.webhookSecret,
undefined,
Stripe.createSubtleCryptoProvider()
);
return event;
} catch (error) {
log.error(error);
return { error };
}
}
And when I send a test webhook via the Stripe CLI, it is unable to find the signature from the body.
I'm using Bun + Hono for reference. LMK if I'm missing something, but as far as I know, all the logic is matching the example yet it doesn't work.
Thanks for reaching out @dickeyy . Just for confirm, is your Hono app running on Cloudflare Workers or Wrangler? Or using this for the another runtime like Node.js or AWS Lambda like that?
Thanks.
@hideokamoto-stripe
hey, I'm just running Hono with Bun runtime locally. It's not hosted on CF.
@hideokamoto-stripe same problem with Wrangler on my end
i just tried to deploy this without any change: https://github.com/stripe-samples/stripe-node-cloudflare-worker-template/blob/main/src/index.js on wrangler and signature fail.
Ok forgot what i said i was using the prod key with a test event, it works
@dickeyy Thank you for the details. The previous sample I mentioned was intended for Cloudflare Workers. Since you're utilizing Bun, it may be necessary to switch to the following sample code: https://docs.stripe.com/webhooks/quickstart?lang=node
I haven't had experience with Bun, so I'm currently investigating the phenomenon you've reported. If the Node sample code provided in the documentation doesn't work for you, I would appreciate it if you could share any error messages or additional details.
I'm working on a stripe payment backend in hono. Stripe hooks needs a middleware to work properly. This code is for express.
How I implement similar middleware in hono?