Question

I'm using the Stripe payment service, with the stripe and stripe_event gems. So far so good-- they work great.

I want to use the stripe_event webhooks listener to do a bunch of actions. For example, when Stripe sends a webhook advising the application of a new subscription, I want to add that subscription to the subscriptions table, send an email to the new user, advise admins, etc.

In the (very scant) stripe_event docs on github, they say to subscribe an object with the call method, and show the example as

class CustomerCreated
  def call(event)
    # Event handling
  end
end

However they don't show where this code lives (what file it would be placed in) or how it's invoked from config/initializers/stripe.rb.

Can someone provide a clear example of how to use these?

Was it helpful?

Solution

What I would do is make a new directory app/stripe_handlers and stick that code in a file named customer_created.rb. Any code in app/ gets automagically loaded by Rails. You could also stick it somewhere in lib/ but it wouldn't get autoloaded.

You would put your handler from your question in app/stripe_handlers/customer_created.rb, then in config/initializers/stripe_event.rb you would say something like:

StripeEvent.configure do |events| events.subscribe 'customer.created', CustomerCreated.new end

When StripeEvent gets an event with type customer.created it will call the call method on the instance of CustomerCreated that you made in the initializer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top