Integrating Stripe Webhooks with Rails

Kunal Kumbhar

What is Stripe?

Many of you probably already know what Stripe is and that is the reason you have stumbled upon this article, but for the people who do not know what Stripe is here is a quick introduction to Stripe: Stripe is a well-known Payment Processing Software that can be used to transfer money, using different methods, from one bank account to other.

In this article, we will be discussing Stripe Webhooks and how to use them.

What is a webhook?

A webhook is just an HTTP callback that is triggered when a certain event occurs. You can consider them like APIs that you build in your web applications, but the only difference is that you don’t need to have an HTTP request to trigger a webhook. Webhooks get automatically triggered when an event occurs. Here’s a diagram to explain how a webhook works:

Stripe Webhooks

According to Stripe Documentation,

“Stripe uses webhooks to notify your application when an event happens in your account. Webhooks are particularly useful for asynchronous events like when a customer’s bank confirms a payment, a customer disputes a charge, a recurring payment succeeds, or when collecting subscription payments.”

So, to further simplify the above statement, whenever a type of event(Eg. Transaction is made, Subscription is created, etc.) occurs we use Stripe Webhooks to get information(Eg. Status of payment, Transaction ID, etc.) related to that event. The information, related to a transaction, which you are able to see in your Stripe Dashboard can be accessed into your application by using these Webhooks.

How to Use Stripe Webhooks

Let’s consider a situation where we need to send an email to the person who has successfully paid an invoice.

The flow will be as follows:

For this, we need to have a mailer to help in sending mails and StripeEvent gem which will help us in triggering the mail whenever an invoice payment is successful. You can read more about mailers and how to create them here. After making some required changes this is how the mailer will look like:

We will also create a simple view for our order_successful_email method.

Now, as we are done with the mailer part, let’s implement the part which will trigger the emails using webhooks.

To implement this part you will have to create an endpoint for your webhook through Stripe Dashboard. You will need an endpoint URL and the event that the webhook will listen to. Now if you are using localhost, you will have to create a public URL to use as your endpoint URL in the stripe dashboard. You can use Ngrok to create a public URL. Here is how you can create a Public URL using Ngrok:

  1. Create an Account on Ngrok.
  2. Download and unzip your Ngrok files.
  3. Connect your auth token to Ngrok.
  4. Finally run the command: ./ngrok http 3000

You will get a public URL similar to this:

We can use this URL to create an endpoint for our webhook. Let’s keep our endpoint URL as

and we need to listen to “invoice.paid” event. This should look like this 👇

We also need to add this route to our routes file. To add this endpoint to our routes add the below-given line to your routes.rb file.

Now we need to trigger the mailer whenever an event occurs. This can be done by using the following code:

We use signing secrets to verify that the webhook request is genuine and it’s not coming from a server acting like Stripe.


  • Webhooks are easy to set up and do not take up a lot of resources.
  • They are fast and provide real-time information/updates.
  • You do not have to check whether an event has occurred or not, Webhooks automatically send the data as soon as an event occurs.

At Scalereal We believe in Sharing and Open Source.

So, If you found this helpful please give some claps 👏 and share it with everyone.

Sharing is Caring!

Thank you ;)

Here I have used Rails credentials to store all credentials related to Stripe. To know more about how to use Rails credentials you can check out this article down below.

To know more about Stripe Webhooks and signatures here are the official docs:

Managing Encrypted Secrets(Credentials) 🔐 with Rails6

To know more about Stripe Webhooks and signatures here are the official docs:

Use incoming webhooks to get real-time updates

Check the webhook signatures