Using Stripe webhooks with Rails
Asked Answered
D

4

6

I have successfully built out a rails app as per Ryan Bates' railscast (http://railscasts.com/episodes/288-billing-with-stripe) - However, in the railscast he doesn't cover webhooks. I was wondering if there are any good examples in the public domain of implementing and using webhooks to manage a subscription billing that has been created.

Any thoughts / direction would be super helpful?

Thanks!

Diaspora answered 21/2, 2012 at 3:5 Comment(2)
webhooks are "you call me when something happens" vs the usual "I'll call (poll) you over and over again to find out if something has happened." Can you clarify your q: what do you want to happen?Grindlay
To provide some more context, what I'm trying to solve for is: 1) If a user's subscription gets successfully charged, then I need to know so that I can apply additional credit to their account 2) If a user's credit card details don't process properly, I need to know so I can communicate with them and ask them to check / update those details Those are the two main use cases. I believe webhooks are the right way to do this - i.e. have Stripe contact my app if / when there is a relevant change... as opposed to using some sort of automated task that would ping Stripe once a day or something.Diaspora
E
10

There's a nice gem for this: https://github.com/integrallis/stripe_event.

It looks to be well written and maintained.

In your gemfile you can simply add the line -

gem 'stripe_event'

There's clear doc in the readme on github.

Emotional answered 11/8, 2012 at 3:41 Comment(0)
G
3

Most all payment gateways, including stripe, have some way of telling the client (your webapp) whether the charge went through or not.

For stripe, their docs show how to receive a webhook notification from them.

The flow is:

  1. Create a controller and method in your app to receive the webhook calls from stripe. Note that the calls will come in as POSTs, not GETs
  2. Register the url with stripe using their dashboard. This is a manual step that you do once.
  3. When your method is called from stripe, use the event info to update your models which track the status of your users' subscriptions.

Any questions?

Grindlay answered 21/2, 2012 at 17:32 Comment(1)
See also the blog post on webhooks: stripe.com/blog/webhooks and the sinatra example: github.com/boucher/stripe-webhook-mailerTrinia
M
0

I'd like to use this gem for testing stripe webhooks 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
Matthia answered 21/1, 2021 at 12:25 Comment(0)
T
0

You can setup stripe-cli in you machine and capture webhook events in your server endpoint.

they have a very doog documentation on how to process webhook events in your endpoint here.

Terminator answered 19/2 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.