After the customer enters their payment details and clicks the Subscribe button, Stripe will handle the payment processing. Whether the payment goes through or not, Stripe will send another request back to our SaaS app telling us what is happening. This request is know as an event in Stripe.
And then in the background, we'll make changes to our databases based on the type of event.
For example, if the event type is customer.subscription.created
, that means the customer has successfully initiated a new subscription, and we should update their role from free user
to paid user
.
To implement this, we need to first tell Stripe where to send the request. Go to your terminal and make sure your development server is already running. Open a new tab and execute the following command:
1stripe listen --forward-to localhost:3001/api/stripe/webhook
If you plan to design the webhook API differently, make sure you update the URL accordingly. After a few seconds of preparation, you should see the following output:
1> Ready! You are using Stripe API Version [2023-10-16]. Your webhook signing secret is whsec_xxx (^C to quit)
Copy the webhook signing secret into your .env
. We are going to need this secret key to ensure the message is indeed sent from Stripe.
.env