Hosted /api/stripe/webhook returns error after being deployed to cloudflare
I am using a webhook to handle successful payments through Stripe in my project. While this would work locally, I get an error after I deploy with nuxthub and change the setting in Stripe to target my hosted endpoint.
The full error I get is:
POST 500 /api/stripe/webhook
[nuxt] [request error] [unhandled] [500],Class extends value #<Object> is not a constructor or null
at chunks/routes/api/stripe/webhook.mjs:1:188650
According to docs, some librairies may not be compatible with the edge runtime yet. Even though I didn't locate the exact library causing this, this could be the case as it matches the 500 error returned. I have 3 libraries being used in my webhook code that could be causing this. One is Stripe of course, second is supabase which I use to get final products and lastly nodemailer, used to send mail to the customer.
Steps to reproduce Steps to reproduce the behavior:
- Create a basic webhook endpoint
- Deploy to cloudflare with nuxthub and target with stripe
- Trigger endpoint with a test payment
- See error
Expected behavior Hosted webhook endpoint should work the same as when tested locally.
Hi @GKaza
Could you try with this module to validate the hooks? It is made to work on edge runtimes. https://github.com/Yizack/nuxt-webhook-validators
But I am quite sure the problem may come from nodemailer, you may want to use external services like Resend.
Hi @atinux ,
Thanks for the reply and suggestions, I indeed removed the code related to nodemailer and the status code changed from the 500 that I was receiving to 400, it's still not working as it should and I'm trying to set up wrangler to see more logs as it's hard to debbug this at the moment. Ideally I would like to make it work with nodemailer, as Resend has limitations on the free tier but I will keep looking into this. Thanks!
You can try https://github.com/zou-yu/worker-mailer to replace nodemailer (never tried though)
Great suggestion, on step 1 it is suggesting to add:
compatibility_flags = ["nodejs_compat"] //or compatibility_flags = ["nodejs_compat_v2"]
to wrangler.toml
I see this file is automatically generated, does nuxthub allow me to configure this file?
This is also related to another error I'm getting:
Error: [unenv] fs.readFileSync is not implemented yet!
more can can be read in this post but the above step is also mentioned in this case.
Another issue I had to solve, that other people trying to setup a Stripe webhook with cloudflare might encounter, can be found here. Basically 'await constructEventAsync()' should be used instead of 'constructEvent()' when working with the stripe event.