Integrating Stripe with any framework can be done with relative ease, and Stripe provides great documentation for integration and testing. In this post, I will discuss testing stripe webhooks in a development environment, as this is one aspect for which documentation is surprisingly hard to find.
Initially, testing webhook events on my local machine was a struggle!
There are different ways to test webhooks:
I will show how to test Stripe webhooks using stripe-cli with the Ruby on Rails Framework.
Rails version : Rails 5.1.7
Ruby version : ruby 2.4.2p198
I assume you have already integrated Stripe in your application. You should have a file at config/initializers/stripe.rb. This file is used to execute custom code on Stripe specific events.
Stripe.api_key = ENV['STRIPE_API_KEY']
StripeEvent.signing_secret = ENV['STRIPE_SIGNING_SECRET']
StripeEvent.configure do |events| events.subscribe 'customer.subscription.deleted' do |event| # Write your custom code you want to execute in stripe # customer subscription deleted event end end
In above example STRIPE_API_KEY is the key for Stripe integration.
STRIPE_SIGNING_SECRET is the signing secret used to communicate with Stripe either through Ultrahook endpoint or through stripe-cli.
The value of STRIPE_SIGNING_SECRET will be different for both connections.
You need to store STRIPE_API_KEY either in the .env file or, if you are testing using any testing framework, in the .env.test file.
First step is to install stripe-cli on your local machine. See https://stripe.com/docs/stripe-cli for instructions.
As per documentation, to fire Stripe events you normally need to login using Stripe credentials, but we will discuss how to test Stripe events without Stripe login credentials.
First you need to start listening to the Stripe server using stripe-cli:
stripe listen --api-key ENV[‘STRIPE_API_KEY’] > Ready! Your webhook signing secret is whsec_P3rrta1iba57AbetSBEjGeLJT27AIAx1 (^C to quit)
Copy generated signing secret code into the STRIPE_SIGNING_SECRET environment variable.
Second step is to forward hooks events to localhost:
stripe listen — forward-to localhost:3000/hooks --api-key ENV[‘STRIPE_API_KEY’]
This will listen for Stripe events and forward to localhost.
If you have define custom routes for hooks then you need to specify that path instead of /hooks.
Third step is to start your rails server:
Last step is to fire the Stripe event.
stripe trigger customer.subscription.deleted --api-key ENV[‘STRIPE_API_KEY’]
The above command will fire the Stripe customer subscription deleted event and all the callbacks will forward to “localhost:3000/hooks”
Cheers! It will execute the code written inside the stripe.rb file.
In this article, we will look at setting up Docker to build a production version of our Next.js application as a Docker image. We will also configure Docker to run this...
Cloudinary is a SaaS product that provides, true to the name, a cloud-based digital asset management system. It is intended for any size web platform that wants their...
Integrating Stripe with any framework can be done with relative ease, and Stripe provides great documentation for integration and testing. In this post, I will discuss...