nextjs-subscription-payments icon indicating copy to clipboard operation
nextjs-subscription-payments copied to clipboard

Stripe webhooks failing (status 400)

Open aleskozelsky opened this issue 1 year ago • 8 comments

Hi,

I am using this template, followed the guide and added a Stripe webhook with all events being sent to /api/webhooks on my install.

When users on the site take certain actions that are NOT on the list of relevantEvents they trigger the /api/webhooks endpoint. All of the NON relevantEvents return status 400 (error) because they are "Unsupported event type".

Events that I have seen that return errors: customer.created billing_portal.session.created balance.available setup_intent.canceled payment_intent.payment_failed checkout.session.expired invoice.finalized invoice.payment_failed charge.failed payment_intent.created checkout.session.expired

Code: File: route.ts

// api/webhooks/route.ts

// relevant events are handled nicely, they return 200 response code
const relevantEvents = new Set([
  'product.created',
  'product.updated',
  'product.deleted',
  'price.created',
  'price.updated',
  'price.deleted',
  'checkout.session.completed',
  'customer.subscription.created',
  'customer.subscription.updated',
  'customer.subscription.deleted'
]);

...

if (relevantEvents.has(event.type)) {
...
} else {
    return new Response(`Unsupported event type: ${event.type}`, {
      status: 400
    });
}

Why are the non relevant events returned as an error? Those events are retried multiple times by Stripe. Should not they return a 200 response instead?

aleskozelsky avatar Jul 08 '24 09:07 aleskozelsky

I'm seeing the same thing

kylecampbell avatar Jul 12 '24 23:07 kylecampbell

FYI I only found this to be an issue if you haven't set up your customer portal in Stripe before attempting to open customer portal. Once I did that, then subscribed, then open customer portal, no event errors.

kylecampbell avatar Jul 13 '24 00:07 kylecampbell

Yeah, there are some server actions that handle the customer portal instead. Also the customer portal is not in the relevant events. Mainly I am asking is because i feel it does not make any sense to throw errors for those event. It is very hard to spot any real errors when ~90% of the non relevant events throws errors for no reason.

aleskozelsky avatar Jul 13 '24 06:07 aleskozelsky

I'm also having this issue, please let me know if anyone figures it out

codyeisenbach avatar Jul 22 '24 23:07 codyeisenbach

Same thing here.

Customer events aren't handled in the example

Updating email or name with the /account forms causes a de-sync between supabase and stripe

cgar420 avatar Aug 05 '24 00:08 cgar420

Also facing this exact issue

kelsiesmurphy avatar Aug 18 '24 13:08 kelsiesmurphy

Same, for someone who is unfamiliar with Stripe I'm not sure what the correct approach is here.

NathanBWaters avatar Sep 07 '24 19:09 NathanBWaters

At the end I have done this:

  } else {
    return new Response(`Unsupported event type: ${event.type}`, {
      status: 269 // was 400 and was giving errors
    });
  }

I have changed the status code to 2## to avoid it falling into the "error" category and chose a random 69 number to identify the hooks in the future.

aleskozelsky avatar Sep 08 '24 08:09 aleskozelsky

I just had this, I think this is an issue when you haven't clicked save on the customer portal via Stripe. That's what worked for me anyway. Hope this helps.

RheSou avatar Nov 18 '24 15:11 RheSou

@RheSou Can you clarify what you mean by 'haven't clicked save on the customer portal via Stripe'?

treyg avatar Dec 11 '24 01:12 treyg

@treyg https://dashboard.stripe.com/settings/billing/portal

RheSou avatar Dec 11 '24 18:12 RheSou