honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
18.33k stars 515 forks source link

Stripe webhooks express middleware implementation in hono #1561

Open shahreazebd opened 11 months ago

shahreazebd commented 11 months ago

I'm working on a stripe payment backend in hono. Stripe hooks needs a middleware to work properly. This code is for express.


  app.use((req, res, next) => {
    if (req.originalUrl === "/api/v1/webhook") {
      next();
    } else {
      express.json()(req, res, next);
    }
  });

How I implement similar middleware in hono?

yusukebe commented 11 months ago

cc: @hideokamoto

hideokamoto-stripe commented 11 months ago

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.

shahreazebd commented 10 months ago

I just want to know about how to verify the Stripe Webhook API request on the Hono?

hideokamoto-stripe commented 10 months ago

@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.

dickeyy commented 7 months ago

@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.

image

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.

hideokamoto-stripe commented 7 months ago

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.

dickeyy commented 7 months ago

@hideokamoto-stripe

hey, I'm just running Hono with Bun runtime locally. It's not hosted on CF.

riderx commented 6 months ago

@hideokamoto-stripe same problem with Wrangler on my end

riderx commented 6 months ago

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.

riderx commented 6 months ago

Ok forgot what i said i was using the prod key with a test event, it works

hideokamoto-stripe commented 6 months ago

@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.