Description This Rails application is designed to handle subscription lifecycle events from Stripe. It automatically updates local subscription records based on Stripe webhook events such as creation, payment, and cancellation.
- Subscription Creation: Automatically creates a subscription record in the local database with an initial status of 'unpaid' when a new subscription is created in Stripe.
- Invoice Payment: Updates the local subscription record status to 'paid' when the first invoice is paid.
- Subscription Cancellation: Changes the local subscription record status to 'canceled' when a subscription is canceled in Stripe, but only if the subscription was previously 'paid'.
- Ruby 3.2.2: Ensure that Ruby version 3.2.2 is installed on your system.
- PostgreSQL: Verify that PostgreSQL is installed and operational.
- Stripe Account: Create a free Stripe account if you don't already have one.
- Stripe CLI: Install the Stripe CLI for local webhook testing.
- Redis: Install Redis for background job
git clone https://github.com/Haseeb717/stripe-webhook-app.git
cd stripe-webhook-app
bundle install
rails db:create
rails db:migrate
bundle exec sidekiq
Login to stripe-cli
stripe login
Ensure these events are forwarded to your local server using Stripe CLI:
stripe listen --events customer.subscription.created,invoice.payment_succeeded,customer.subscription.deleted --forward-to localhost:3000/stripe_webhook
You can add other events also in above command as per requirement.
Copy the example environment variables file and edit it with your Stripe credentials:
cp .env_example .env
Edit .env with your Stripe keys:
STRIPE_SECRET_KEY: Your Stripe secret key. You can get it from api-keys on Stripe dashboard
STRIPE_WEBHOOK_KEY: Obtain this by running sh stripe listen --forward-to localhost:3000/stripe_webhook
. For production or staging we can get this key from Stripe dashboard as per endpoint registered.
Execute the test suite:
bundle exec rspec
Run the rails server on console with port 3000:
rails s -p 3000
- Trigger Stripe subscription creation event using it:
stripe trigger customer.subscription.created --override subscription:payment_behavior=default_incomplete --add customer:[email protected]
- Mark then invoice paid of that subscription from stripe dashboard or trigger Invoice Payment succeed event manually overriding with that subscription id
stripe trigger invoice.payment_succeeded
- Cancel the subscription from UI or trigger Subscription delete event
stripe trigger customer.subscription.deleted