#Ema.webhook-environments

1 messages ยท Page 1 of 1 (latest)

rough herald
#

Hey! By 2 webhooks do you mean 2 separate handlers (code) or 2 different webhooks configured with Stripe?

valid vault
#

Oh hi ynnoj! Talking to you has become a daily thing ๐Ÿ˜†

valid vault
#
router.route("/webhook").post( async (req, res) => {

  const sig = req.headers['stripe-signature']
  let event;
  let user;
  try {
    event = stripe.webhooks.constructEvent(req.rawBody, sig, keys.STRIPE_WH_SECRET)
  } catch(e) {
    console.log("Webhook error: " + e)
    res.status(400).json(e)
  }
  switch(event.type){
    //events
  }
  res.sendStatus(200);
}  
#

this is what my code looks like right now

rough herald
#

So both webhooks are pointing to the same endpoint?

valid vault
#

no

#

one is pointing to my prod server, one is pointing to staging

#

and they, of course, have two separate secrets

rough herald
#

Hmm, I'm struggling to understand what the issue is. Can you re-clarify? (sorry probably me pre-coffee)

valid vault
#

Right now, both webhooks get executed regardless of the server I'm running this on

rough herald
#

Hmm, the webhook should only fire for events pertinent to the environment it's created in. For example, if you have a payment_intent.succeeded event in live then only your live webhook should receive those events

#

Can you share your account ID? (acct_xxx)

valid vault
#

where do I get it from?

#

sorry!

#

found it, nvm :p

#

acct_1Jw1oLK4b2fM2OBh

rough herald
#

Perfect thanks! I can't see any live environment webhooks configured for the account

valid vault
#

They're both testing webhooks because we aren't ready to go live yet

#

but they're based on different servers right now

#
  try {
    event = stripe.webhooks.constructEvent(req.rawBody, sig, keys.STRIPE_WH_SECRET)
  } catch(e) {
    console.log("Webhook error: " + e)
    res.status(400).json(e)
  }
rough herald
#

Ah, now I see! Like I said, pre-coffee ๐Ÿ˜†

valid vault
#

shouldn't this code snippet kill the webhook if the stripe_wh_secret isn't what the webhook expects?

rough herald
#

Seems so yeah. It should return a 400 HTTP response plus the error

#

I'll have a look at your events, one second

valid vault
#

I am rebuilding my prod server just in case I had some old code leftover XD

rough herald
#

Is there a particular event you're testing? Just so I can filter them

valid vault
#

these events are the ones the webhook tests

#

first one is prod server, last one is staging. I was testing staging and it still executed the prod webhook

rough herald
#

Your 'production' endpoint returned a 400 error (seemingly the try/catch around the event construct)

valid vault
#

so the ones ~11 mins afte rthe one you linked

rough herald
#

Can you share a specific ID of one?

#

From your Dashboard

rough herald
#

Ok so delivery to your staging webhook timed out and production succeeded

valid vault
rough herald
#

By the way you can see this on that page you just linked (not sure if you knew that)

#

Not sure why it's timing out? Is it deployed?

valid vault
#

I see the clock and checkmark but no info about what they returned

rough herald
#

But if you were expecting it the other way round then I guess you have you webhook signing secrets mixed up

rough herald
valid vault
#

I am going to try once more and see what happens because maybe the builds weren't deployed yet and that's the reason for timeout

#

sorry bout the hassle :(

rough herald
#

Np!

valid vault
#

Okay, 12:06 PM, tried again after deployment, now staging webhook gives 4 green checks while I am still experiencing the same situation on prod webhook

#

Staging (to be expected)

#

Prod (should not fire at all)

rough herald
#

Why should it not fire?

valid vault
#

Because I am currently working on the staging server, so the request should only go to the staging webhook

#

that is what I am trying to achieve

#

sorry if it wasn't clear earlier :p

rough herald
#

Then just disable the webhook in your Dashboard. This is working as intended as you have configured 2 webhooks to listen for the same events in your Stripe test environment

valid vault
#

Oh so there is no way in code to make it so that only the webhook with the corresponding secret goes through?

#

Once we'll actually ship and move to a live environment this issue will, clearly, not exist anymore but I was wondering if there was a temporary "patch" for that

rough herald
#

Well yeah, that's what you have. But right now your production webhook is timing out

#

So it's not even getting to that point in the code

valid vault
#

It's not timing out!

#

oh wait let me check

#

huh it did time out

rough herald
#

Yeah unresponsive for some reason. Generally we timeout within ~20 seconds (we recommend returning a 2xx response immediately)

valid vault
#

I think I know what causes the timeout

#

but that means it's getting past the check of webhook secret for some reason

#

while what I'd want is getting a 400 back because I am trying to "access" the wrong webhook

rough herald
#

That's the purpose of the signing secret โ€“ so that unsigned/incorrectly signed payloads aren't handled

#

Why is it timing out?

valid vault
# rough herald Why is it timing out?

It is timing out because in the part where I handle the customer.subscription.* events I am awaiting an User.findOne() from my DB that doesn't work because it cannot find the user

#

but that is after the code snippet I sent you, so logically the event creation with the WH secret should fail and it should never reach that code and return 400

#

I don't know if I'm getting this wrong.

rough herald
#

I think the issue here is that the value of your keys.STRIPE_WH_SECRET variable in each environment is correct. You have 2 unique webhooks, 2 unique secrets and 2 environments. In order for it to behave how you need it to, you'd have to use the incorrect secret

valid vault
#

Shouldn't the secret be compared to the signature or am I missing something?

rough herald
rough herald
valid vault
#

If I send an event from the prod server shouldn't the signature contain the prod server Secret, so when it gets checked against the keys.STRIPE_WH_SECRET in the dev environment it fails?

valid vault
#

I assume I got any of this wrong

rough herald
#

Yes, exactly. But right now we can't confirm the issue because your one of the webhooks is timing out so you should address that (as stated above) then we can confirm the behaviour

valid vault
#

I assume a solution to the issue I'm experiencing and to stop from getting timed out/400 because I can't find the user would be having a test stripe account for developers and a prod company stripe account with separate webhooks and dashboards

#

but I assume that is against your EULA :p

rough herald
#

We're expecting a 4xx response with a Webhook error message

rough herald
#

But as I said, you should be responding with a 2xx immediately regardless of the custom logic - Stripe doesn't care about that

#

We just need to know that the event was received, signed correctly and is being handled

valid vault
#

so do res.sendStatus(200) before the event handling logic

#

Okay! I surrounded the custom logic with try-catches so that it should always reach the 200 status.

#

It is currently deploying so I'll keep you posted c: Thank you very much for your patience these past few convos!

rough herald
#

Once we fix the timeout issue we should be able to figure out the problem!

valid vault
#

Okay, the timeout issue is fixed in staging. So if I send a webhook request to prod, I should get a 400 with a Webhook error, right

#

It returned 200 OK on the staging webhook instead of giving me the signature error

#

The best way to handle this would be just having two separate accounts with one webhook each, right?

rough herald
#

Can you share that event ID please

valid vault
#

evt_1JwmYtK4b2fM2OBh553q0SbI

#

something like this

rough herald
#

Ok, so that 1 event is sent once to 2 separate webhook endpoints with 2 unique signatures

Now assuming you have the correct signing secrets configured in each deployment (yes?) then it is expected that both deliver successfully as we've seen there

valid vault
#

Right now, then, I guess, the solution to my issue would be two separate account with testing webhooks until we go to prod

rough herald
#

It doesn't matter which URL the event happened on, as it happened with your test Stripe keys in this case and you have 2 webhooks in your test environment

#

Does that make sense?

valid vault
#

So, the solution is deleting the prod webhook from the account and creating it in another account, and use that account and webhook secret in the prod environment and the current account and webhook for staging and development.

rough herald
#

I guess if you need to test in separate environments without yet going 'live' then yes

valid vault
rough herald
#

No

valid vault
#

then two accounts it is hehe

#

thank you very much again for your help

#

this dev community is amazing, I'm very thankful to Stripe for setting up such a nice way to get help

#

is there anywhere I can email to tell people how great of a job you're doing?

rough herald
#

I'll relay your kind words ๐Ÿ˜„

valid vault
#

Thank you very much c: