4
votes

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
Is your goal to write an automated test, or to experiment locally with webhooks? See the testing section of the stripe_event readme for examples of writing automated tests. In irb or rails console run Stripe::Event.construct_from(fake_webhook_event) (assuming fake_webhook_event is a hash with symbol keys) to debug the active/past_due issue.dwhalen
@dwhalen Thanks for the suggestion, I solved the issue by following the spec in github.com/invisiblefunnel/test-hooks.Adler Santos

1 Answers

1
votes

Enjoy using this gem https://github.com/stripe-ruby-mock/stripe-ruby-mock

Features

  • No stripe server access required
  • Easily test against stripe errors
  • Mock and customize stripe webhooks
  • Flip a switch to run your tests against Stripe's live test servers