Locally test event handler for Stripe webhooks in Rails

Viewed 1939

I'm using the stripe_event gem to set up my event handler for Stripe webhooks. I would like to send an email that notifies the customer if their subscription wasn't renewed. One such setup is as follows:

# config/initializers/stripe.rb

StripeEvent.setup do

  subscribe 'customer.subscription.updated' do |event|
    subscription = event.data.object
    customer_id = subscription.customer
    user = User.find_by_customer_id(customer_id)

    UserMailer.delay.past_due_email(user) if subscription.status == 'past_due'
  end

end

Now my goal is to test the event handler above locally (without relying on ngrok or ultrahook and such).

In my spec, I built a fake Stripe webhook event of type "customer.subscription.updated". It also contains a POST request to localhost:3000/stripe:

RestClient.post('localhost:3000/stripe', fake_webhook_event)

I start the rails server locally and run the spec. The server is receiving the POST request and the event handler is being run, but the subscription object contained in the request has the status of active rather than past_due. The other attributes, such as the event id, are the same as the fake webhook I've written. Very strange.

I have a feeling I shouldn't be sending the request to the development server, but perhaps somewhere else? Any advice? Thank you in advance!

1 Answers
Related