#Thorkell

1 messages ยท Page 1 of 1 (latest)

past prismBOT
gentle ocean
#

Hi there, 50x means server error, a certificate error should be 4xx.

river marsh
#

Ahhhh ok cool. Should I wait for Render to get back with me then? I assume 500 means it's specific to their hosting correct?

#

Also I love your pfp

gentle ocean
#

What is Render in this context?

river marsh
#

Render is where our API is hosted, and we have a stripe_controller.rb for context that is setting up webhooks at https://api.getdistilled.io/stripe-webhooks

I'm trying to figure out why 85% of my webhooks have been failing ๐Ÿ˜ฆ

Events:

https://dashboard.stripe.com/events/evt_1MZmi3FCaczULzzDSr4Cxo1c

https://dashboard.stripe.com/events/evt_1MZmi7FCaczULzzDWzRL3yDG

Stripe Controller (part):

    when 'checkout.session.completed'
      workspace = Workspace.find_by(id: event.data.object.client_reference_id)
      if workspace
        StripeCustomer.find_or_create_by!(
          stripe_customer_id: event.data.object.customer, workspace: workspace
        )
        Rails.logger.info "Stripe checkout session completed - Workspace with id: #{workspace.id} was found and associated with stripe customer id: #{event.data.object.customer}"
      else
        Rails.logger.error "Stripe checkout session completed - Workspace with id: #{event.data.object.client_reference_id} was NOT found"
      end
    when 'customer.subscription.created'
      workspace = StripeCustomer.find_by(stripe_customer_id: event.data.object.customer).workspace
      if workspace
        workspace.update!(stripe_product: event.data.object.plan.product)
        Workspaces::OnboardingSteps.new(workspace).pass_onboarding_step('subscription')
        Rails.logger.info "Stripe customer subscription created - Workspace with id: #{workspace.id} was found and updated with product: #{event.data.object.plan.product}"
      else
        Rails.logger.error "Stripe customer subscription created - Stripe customer with id: #{event.data.object.customer} was NOT found"
      end
gentle ocean
#

Hmm, it looks like the same webhook endpoint was registered in both live and test mode

#

Is it intentional to use the same endpoint for both live and test mode events?

#

And your server is sometimes returning 500 or 422 errors

river marsh
gentle ocean
#

Since Stripe can still receive response from your server, I don't think the issue is related to certificate problem.

river marsh
#

So it is receiving a response? This is wild to me because afaik what I have set up should work

gentle ocean
river marsh
#

Any suggestions on how to best troubleshoot?

gentle ocean
#

I'd suggest you to check your server log and see why it returns 500 instead of 200.

river marsh
#

Lol probably a good start ๐Ÿ’€

#

I noticed this:

Do you think that running sentry might have made me run into rate limits?

gentle ocean
#

Are you sending lots of requests to Stripe?

river marsh
#

I didn't think we were? But if this is from our server wouldn't it be Render's rate limiting quota?

gentle ocean
#

Is this 429 returned from Stripe or other server?

river marsh
#

It's returned on the Render server

gentle ocean
river marsh
#

Alright, so let me rewind a bit. My main issue is that this part of the stripe_controller.rb isn't creating a customer:

Alright, just looking at the API, I think I've narrowed it down to being an issue with the stripe_controller.rb,

    when 'checkout.session.completed'
      workspace = Workspace.find_by(id: event.data.object.client_reference_id)
      if workspace
        StripeCustomer.find_or_create_by!(
          stripe_customer_id: event.data.object.customer, workspace: workspace
        )
        Rails.logger.info "Stripe checkout session completed - Workspace with id: #{workspace.id} was found and associated with stripe customer id: #{event.data.object.customer}"
      else
        Rails.logger.error "Stripe checkout session completed - Workspace with id: #{event.data.object.client_reference_id} was NOT found"
      end
    when 'customer.subscription.created'
      workspace = StripeCustomer.find_by(stripe_customer_id: event.data.object.customer).workspace
      if workspace
        workspace.update!(stripe_product: event.data.object.plan.product)
        Workspaces::OnboardingSteps.new(workspace).pass_onboarding_step('subscription')
        Rails.logger.info "Stripe customer subscription created - Workspace with id: #{workspace.id} was found and updated with product: #{event.data.object.plan.product}"
      else
        Rails.logger.error "Stripe customer subscription created - Stripe customer with id: #{event.data.object.customer} was NOT found"
      end

And I gotta ask, could stripe throw that 500 and 422 errors if the request is messed up?

gentle ocean
#

Stripe will respond 4xx error if the request is failed, or 500 if there's a server error at Stripe (rarely but it happens)

river marsh
#

Huh, so is there any clear indication of what's going on here?

gentle ocean
#

Do you have request ID of the failed customer creation request?

river marsh
#

Is this what you mean?

gentle ocean
#

I don't see any problem in this event.

#

You mentioned part of the stripe_controller.rb isn't creating a customer:. Did you create the customer by yourself? or you let Checkout Session to create one for you?

river marsh
#

That's the thing, they always fail on the first time but only succeed on the retry. And I let Checkout session create one

gentle ocean
#

they always fail on the first time -> what failed? can you be more specific?

river marsh
#

The webhook:

gentle ocean
#

It failed because your server responded 500

river marsh
#

I think I'm starting to put together pieces. But why did it succeed on the retry?

gentle ocean
#

Your server returned 200

river marsh
#

Lmao obviously

gentle ocean
#

As I mentioned earlier, your webhook endpoint is listening events generated in both live and test mode.

#

So there's a possibility that the endpoint is using incorrect secret key when making request for resources created in a different mode.

river marsh
#

I was wondering that. Need to look into that.

gentle ocean
#

For example, you receive an event generated in live mode, but the endpoint is using test mode secret key to create requests

river marsh
#

do I just need to switch the button? And check the key?

gentle ocean
#

What button?

river marsh
#

The test mode/live mode

gentle ocean
#

That's just a switch in Dashboard. I'm talking about the secret key that you use in webhook endpoint to create requests.

river marsh
#

Yeah, but I when I switch into test mode I see a totally different secret key

gentle ocean
#

Yes, that's exactly what I mean. The test mode key and live mode key are different.

river marsh
#

Are they not supposed to be?

gentle ocean
#

Here's my suggestions

  1. Don't use the same URL to listen to events generated from live and test mode, you should use one URL for live events and the other for test evetns
  2. Make sure you are using the correct secret key when making requests in your webhook endpoint.
river marsh
#

I mean I've checked my .env and the live secret key is the only one in prod so ๐Ÿคทโ€โ™‚๏ธ

#

I made a few test requests from the CLI today, but that's about it

gentle ocean
#

a few test requests from the CLI today, -> can you share with me the request ID?

river marsh
#

Here you go

#

This another one

#

Oh that old tho

gentle ocean
#

OK. So Both requested are created in test mode, which means the checkout sessions are created in test mode, not live mode.

However since your webhook endpoint is using prod mode secret key in making requests (it's trying to retrieve the customer created in this checkout session), it will fail because the resources are not available in live mode.

#

This explains why your server returned 500 errors

river marsh
#

Wait I'm having trouble understanding what you're saying. Why are those requests that I made with the CLI relevant to the convo if they are in test mode? Sorry I'm trying to wrap my head around the implications because I'm just trying to get my webhooks working on live lol

#

Wait. I thinkkkkk I'm putting it together

#

Ok

#

I feel like an idiot

gentle ocean
#

I'm saying the requests that you made with CLI were created in test mode (learn the difference between live and test mode here https://stripe.com/docs/keys#test-live-modes).

So when your code execute

          stripe_customer_id: event.data.object.customer, workspace: workspace
        )

Since you are using live mode secret key, you are asking stripe to retrieve the customer created in live mode, which will raise an error because the checkout session (and associated customer) is created in test mode.

river marsh
#

Should I delete the webhook endpoint from test mode?

gentle ocean
#

I will if I were you, and I'll set up a different endpoint for test events.

river marsh
#

You will?

#

I think I see what you're saying haha

gentle ocean
#

Cool. Let us know if you need more help.

river marsh
#

Will do, thank you all for helping me out ๐Ÿ’ช