Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 4 - stripe_event functions

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?

like image 369
user101289 Avatar asked Apr 24 '14 17:04

user101289


1 Answers

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.

like image 113
Pete Keen Avatar answered Nov 15 '22 16:11

Pete Keen