Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stripe webhooks express middleware implementation in hono #1561

Open
shahreazebd opened this issue Oct 10, 2023 · 11 comments
Open

Stripe webhooks express middleware implementation in hono #1561

shahreazebd opened this issue Oct 10, 2023 · 11 comments
Labels
enhancement New feature or request.

Comments

@shahreazebd
Copy link

shahreazebd commented Oct 10, 2023

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?

@shahreazebd shahreazebd added the enhancement New feature or request. label Oct 10, 2023
@yusukebe
Copy link
Member

cc: @hideokamoto

@hideokamoto-stripe
Copy link

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
Copy link
Author

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

@hideokamoto-stripe
Copy link

@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
Copy link

dickeyy commented Feb 6, 2024

@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
Copy link

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
Copy link

dickeyy commented Feb 7, 2024

@hideokamoto-stripe

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

@riderx
Copy link

riderx commented Feb 18, 2024

@hideokamoto-stripe same problem with Wrangler on my end

@riderx
Copy link

riderx commented Feb 18, 2024

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
Copy link

riderx commented Feb 18, 2024

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

@hideokamoto-stripe
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

5 participants