#drewzoviek_webhooks

1 messages ยท Page 1 of 1 (latest)

fickle radishBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1420168194534146199

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

fading marsh
#

Hi there,
can you give me a bit more context? What exactly are you doing, what are you expecting to happen and what happens instead? Especially when you say "nothing is happening", what exactly do you mean?

supple wharf
#

So my react project website. Users can create their listings and have feature credits to use. Everytime it comes to the billing end the subscription should repeat, keeping the user's listings and resetting the feature credits again. When a user cancels their subscription, I have it set to the end of the cycle. When cancellation is final, the user's listings should be archived and feature credits not resetted but trashed.

I tried getting a test subscription. It links to the user's account but when i cancel via the stripe 'Subscriptions' page, nothing is happening to the user's listings or feature credits

#

Basically, I think Stripe and my Project is not connected properly when it comes to cancellations because there's no update in my backend when subscriptions are cancelled, hence nothing else is happening as expected

fickle radishBOT
supple wharf
#

Hi solanum! Been a while. Was really grateful for your help last time

wanton lantern
#

oh hello!!

supple wharf
#

I tried, and I'm not sure why it's not working. When cancellations take place, nothings updating on my end. If that's the guide I'm missing, I'll try it out again.

wanton lantern
supple wharf
#

I want it to cancel the user's subscription at the end of the billing, no refunds. Are those links the right place?

wanton lantern
#

this will show you how to set up webhooks to handle events. then you just need to handle the event customer.subscription.deleted

supple wharf
#

I'll try that. Thank you Solanum

wanton lantern
#

yep of course! let me know if you need any more help as you work through it

supple wharf
#

I'll keep you updated. Should be less than half an hour

glossy owl
#

Bro so my stripe account just got closed and stripe is saying they'll withhold the money. Isn't that illegal under us law

wanton lantern
#

@glossy owl please read the rules in #help , the server is for developer questions only. for questions like this please chat with our support team

glossy owl
supple wharf
#

Ok, so I got some stuff moving. I made a button in my application which supposedly cancels subscription. It's pushing properly and actually saying in Stripe subscription that it will cancel 'Oct 23'. But then my project still says 'active' when i push cancel via Stripe

#

bro get out of my stuff

wanton lantern
supple wharf
#

go get your own thread

#

Solanum, im trying to explain it to you as clear as possible. If you need any more details please let me know

wanton lantern
supple wharf
#

I do have a listener called 'customer.subscription.deleted' in my backend system

wanton lantern
#

i would set up logging in your webhook handler to just log something every time the endpoint is called, and then you can set up additional logging around how you're actually handling the specific event type

supple wharf
#

It is running locally

wanton lantern
#

ok cool, have you set up the stripe CLI yet and gone through that section i linked to?

supple wharf
#

on it

supple wharf
#

I don't understand solanum. It's listening properly but my project isnt updating

wanton lantern
#

is your webhook being called and have you set up logging to verify that?

#

are you using a node.js backend?

supple wharf
#

correct

#

runnning on port 5000

wanton lantern
#

the example on our docs looks like this, does yours look similiar?

const express = require('express');
const app = express();

// Match the raw body to content type application/json
// If you are using Express v4 - v4.16 you need to use body-parser, not express, to retrieve the request body

// Replace this endpoint secret with your unique endpoint secret key
// If you're testing with the CLI, run 'stripe listen' to find the secret key
// # If you defined your endpoint using the API or the Dashboard, check your webhook settings for your endpoint secret: https://dashboard.stripe.com/webhooks
const endpointSecret = 'whsec_...';

app.post('/webhook', express.json({type: 'application/json'}), (request, response) => {
  const event = request.body;
  if (endpointSecret) {
    // Get the signature sent by Stripe
    const signature = request.headers['stripe-signature'];
    try {
      event = stripe.webhooks.constructEvent(
        request.body,
        signature,
        endpointSecret
      );
    } catch (err) {
      console.log(`โš ๏ธ  Webhook signature verification failed.`, err.message);
      return response.sendStatus(400);
    }

  // Handle the event
  switch (event.type) {
    case 'customer.subscription.deleted':
      const paymentIntent = event.data.object;
      // Then define and call a method to handle the successful payment intent.
      // handlePaymentIntentSucceeded(paymentIntent);
      break;
    default:
      console.log(`Unhandled event type ${event.type}`);
  }

  // Return a response to acknowledge receipt of the event
  response.json({received: true});
});

app.listen(4242, () => console.log('Running on port 4242'));```
#

if so can you add a simple console.log("webhook called!") type line on the next line after the app.post line?

supple wharf
#

comparing now

#

Your docs use express.json() which parses the body into a JavaScript object
My implementation uses express.raw() which keeps the body as a raw Buffer

#

does that make a difference

wanton lantern
#

for our purposes that should be fine! that might have implications if you're running into signature verification issues, but i'm not even sure if your webhook is being called

#

so can you add the log line i suggested above and try again, then tell me if you're getting any logs?

#

just create and delete a subscription

supple wharf
#

I'll add the log right after their app.post definitions

#

Will do it now

#

adding the consolelog

wanton lantern
#

you can also try curl -X POST localhost:5000/webhook real quick to see if anything happens

#

i would expect a signature verification error

supple wharf
#

So far i dont see any console logs creating and deleting the subscription, let me try that too

wanton lantern
#

obviously if your URL is different you'll need to change it

supple wharf
#

is this the culprit HTTP/1.1 400 Bad Request

wanton lantern
#

that could be fine, again that might indicate that you're having signature verification issues. did you see the "webhook called!" i asked you to add?

supple wharf
#

Negative, i checked the web console

wanton lantern
#

can you share your webhook handler code real quick?

supple wharf
#

ummm

#

[0] [Webhook] BYPASSING signature verification for development
[0] [Webhook] Parsed event from raw body: customer.subscription.deleted
[0] [Webhook] Received event: customer.subscription.deleted

wanton lantern
#

ok cool! was that from a subscription deletion?

#

or from you calling the endpoint with cURL

supple wharf
#

endpoint crul ๐Ÿ™

#

let me try again

#

1 min

#

nope

wanton lantern
#

ok, that at least means that your webhook endpoint is doing something, so the issue is probably with how your CLI forwarding is set up. i would run through the steps in this doc again very carefully and make sure you're doing everything properly
https://docs.stripe.com/webhooks#test-webhook

#

when you run the CLI command does it look kinda like this?
stripe listen --forward-to localhost:5000/webhook

supple wharf
wanton lantern
#

ok, and you're forwarding to those using the CLI?

#

can you share your webhook handler code where you added the logging?

supple wharf
#

is this it?

#

// Handle Stripe webhooks
async function handleStripeWebhook(req, res) {
const sig = req.headers['stripe-signature'];
let event;

// Temporary bypass for debugging - remove in production
if (process.env.NODE_ENV === 'development' && process.env.STRIPE_WEBHOOK_SECRET === 'whsec_test_webhook_secret_here') {
console.log('[Webhook] BYPASSING signature verification for development');
try {
const rawBody = Buffer.isBuffer(req.body)
? req.body.toString('utf8')
: (typeof req.body === 'string' ? req.body : JSON.stringify(req.body));
event = JSON.parse(rawBody);
console.log('[Webhook] Parsed event from raw body:', event.type);
} catch (err) {
console.error('[Webhook] Failed to parse webhook body:', err.message);
return res.status(400).send(Webhook Error: ${err.message});
}
} else {
try {
event = stripe.webhooks.constructEvent(req.body, sig, process.env.STRIPE_WEBHOOK_SECRET);
} catch (err) {
console.error('[Webhook] Stripe signature verification failed:', err.message);
return res.status(400).send(Webhook Error: ${err.message});
}
}

console.log('[Webhook] Received event:', event.type);
if (event.type.startsWith('customer.subscription')) {
console.log('[Webhook] Subscription event:', JSON.stringify(event, null, 2));
}

// ...existing switch(event.type) with cancellation handling...
}

#

sorry

#

app.post('/api/webhook', express.raw({ type: 'application/json' }), (req, res, next) => {
console.log('webhook called!');
console.log('[WEBHOOK DEBUG] Received webhook request');
console.log('[WEBHOOK DEBUG] Headers:', JSON.stringify(req.headers, null, 2));
console.log('[WEBHOOK DEBUG] Body length:', req.body ? req.body.length : 'no body');
next();
}, handleStripeWebhook);

// Also support '/stripe/webhook' for compatibility
app.post('/stripe/webhook', express.raw({ type: 'application/json' }), (req, res, next) => {
console.log('webhook called!');
next();
}, handleStripeWebhook);

wanton lantern
#

FYI it makes code much easier to read if you wrap it in triple `

#

so i'm assuming you've tried stripe listen --forward-to localhost:5000/api/webhook?

supple wharf
#

it's not working

#

Let me check something

#

Solanum

#

I just realized Stripe CLI wasn't installed

fickle radishBOT
supple wharf
#

but how was I able to connect stripe subscriptions and payments to my react project?

glacial moth
#

hello! taking over from solanum! can you share what you mean by "connect stripe subscriptions and payments to your react project"?

supple wharf
#

Hi Alex, glad to have you here

#

So I have stripe hooked up so that when payment goes through my react project grants the user data subscription and coins

#

When I cancel a user's test subscription via stripe, it's not updating my user's backend information

glacial moth
#

alright, and I believe you're getting your webhook endpoint set up to listen for the relevant events to process it? Have you gotten your CLI up and running yet and is your localhost successfully receiving the webhook events?

supple wharf
#

I just activated the CLI, didn't know about that until now

#

Testing to see if the webhook listens

glacial moth
#

okay, let me know if you're running into issues as you work through it

supple wharf
#

roger

#

I can't seem to find the backend log

#

I'm expecting a 'webhook called!' but theres none

glacial moth
#

can you share the event id which you expected to be received / sent to your webhook endpoint? it'll have the prefix evt_

supple wharf
#

getting it

#

I can't find it alex

#

does this help?

#

A newer version of the Stripe CLI is available, please update to: v1.30.0
Setting up fixture for: customer
Running fixture for: customer
Setting up fixture for: product
Running fixture for: product
Setting up fixture for: price
Running fixture for: price
Setting up fixture for: subscription
Running fixture for: subscription
Setting up fixture for: subscription_deleted
Running fixture for: subscription_deleted
Trigger succeeded! Check dashboard for event details.

#

I trigger customer.subscription.deleted

glacial moth
#

did you run stripe listen --forward-to commands which we mentioned previously to your endpoints on a separate terminal window?

supple wharf
#

ran it in a separate terminal and itโ€™s currently forwarding to my webhook.

glacial moth
#

okay, in that window it should list the event ids when you trigger events from another window?

supple wharf
#

I'm not too familiar with that

glacial moth
#

what do you see in the window where you ran stripe listen --forward-to can you share a screenshot?

supple wharf
#

I'm sorry if im not being helpful, its my first ever project so im still learning

#

Let me get that for you

#

my laptops overheating just a moment

#

i get the signing secret ket?

#

A newer version of the Stripe CLI is available, please update to: v1.30.0

Ready! You are using Stripe API Version [2025-01-27.acacia]. Your webhook signing secret is

glacial moth
#

ah no, redact (don't show) the signing key. What I wanted to mention is that if you trigger a webhook in terminal window A, then you should see in terminal window B that events are being forwarded to your webhook endpoint, assuming it's all working as expected. Gimme a second to give you a screenshot of how it looks like in mine

#

this is an example, ignore the error cause I didn't actually set up my endpoint here that's why it's unable to post the webhook event

supple wharf
#

Hold on Alex, that looks familiar.

#

Isn't that what it looks like whe you start up the stripe front end and backend?

#

I have a vivid memory from a tutorial I watched on youtube about a year ago

#

Unfortunately I don't have that

glacial moth
#

do you at least see the corresponding events being sent on the terminal window, when you trigger the events on another terminal window?

supple wharf
#

I'm on a react project, I'm not sure how id do that

#

but yes

#

I did see both of those messages in the same termianl

#

cd backend/stripe-cli; .\stripe.exe listen --forward-to http://localhost:3001/webhook/stripe
A newer version of the Stripe CLI is available, please update to: v1.30.0

Ready! You are using Stripe API Version [2025-01-27.acacia]. Your webhook signing secret is

glacial moth
#

can you maybe share a screenshot of how you're running both commands? remember to redact (cover) the webhook secret before sharing your screenshot

supple wharf
#

I have seen the trigger succeeded as well

#

getting that now

#

here

#

is that what you're looking for?

#

@glacial moth @wanton lantern ?

glacial moth
#

yep, that's what I'm looking for!

#

Now leave the left terminal alone, it's ready and listening now. And if you now trigger a webhook on the right terminal again, does anything happen on the left terminal?

supple wharf
#

getting errors

#

2025-09-23 20:57:49 --> charge.succeeded [evt_3SAh1iRvfEABTXBO0QotBeE2]
2025-09-23 20:57:49 --> payment_intent.succeeded [evt_3SAh1iRvfEABTXBO0sOSzQ5W]
2025-09-23 20:57:50 --> payment_intent.created [evt_3SAh1iRvfEABTXBO0Wt5gHjm]
2025-09-23 20:57:52 [ERROR] Failed to POST: Post "http://localhost:3001/webhook/stripe": dial tcp [::1]:3001: connectex: No connection could be made because the target machine actively refused it.

2025-09-23 20:57:52 [ERROR] Failed to POST: Post "http://localhost:3001/webhook/stripe": dial tcp [::1]:3001: connectex: No connection could be made because the target machine actively refused it.

2025-09-23 20:57:52 --> charge.updated [evt_3SAh1iRvfEABTXBO0cezocqN]
2025-09-23 20:57:52 [ERROR] Failed to POST: Post "http://localhost:3001/webhook/stripe": dial tcp [::1]:3001: connectex: No connection could be made because the target machine actively refused it.

2025-09-23 20:57:54 [ERROR] Fai

glacial moth
#

okay so yep, ^ is what i was expecting to see in that the CLI should be attempting to deliver events to the defined endpoint

#

about the error, are you sure that your webhook endpoint is listening on 3001?

supple wharf
#

its on 3000

glacial moth
#

then it shouldn't be http://localhost:3001/webhook/stripe and should probably be http://localhost:3000/webhook/stripe

supple wharf
#

Let me get that restarted

glacial moth
#

Your endpoint is returning 200 so it looks good now

#

is it working as expected for you now?

supple wharf
#

[0] [WEBHOOK DEBUG] Body length: 2330
[0] [Webhook] BYPASSING signature verification for development
[0] [Webhook] Parsed event from raw body: payment_intent.succeeded
[0] [Webhook] Received event: payment_intent.succeeded
[0] [AUDIT] stripe_webhook_received - system:evt_3SAh8URvfEABTXBO0tlxtwlD by user:null
[0] Unhandled event type payment_intent.succeeded
[0] [AUDIT] stripe_webhook_received - system:evt_3SAh8URvfEABTXBO0tlxtwlD by user:null
[0] Unhandled event type payment_intent.succeeded

#

testing now

#

ALEX

#

ALEX ITS WORKING

#

Thanks to you and Solanum!

#

So I guess

  1. I didn't have Stripe CLI downloaded
#
  1. It was posting to the wrong port
#

I hope you both have a good night, thank you for staying up late to help me

glacial moth
#

happy to have been able to help! ๐ŸŽ‰

supple wharf
#

really thankful guys