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: