This is the fourth article in the “Process Payments with Stripe in Rails” tutorial series:
- Stripe Payments in Rails, Part 1 – Stripe Checkout
- Stripe Payments in Rails, Part 2 – Use Custom Forms with Stripe.js
- Stripe Payments in Rails, Part 3 – Recurring Payments with Stripe
- Stripe Payments in Rails, Part 4 – Plan Discounts with Coupons (this tutorial)
In this post, we will work on the same code base for this tutorial series. We will implement a coupon option that comes with the subscription service that we covered in part 3 of this series. Note that the coupon functionality runs only with subscriptions, not with simple payments.
We will work on the coupons branch:
1 2 3 4 5 6 7 8
Make sure you follow all the steps in Stripe payments in Rails, Part 3 – Recurring Payments with Stripe before continuing.
Step 1. Create coupon in Stripe dashboard
Go to Stripe dashboard > Subscriptions > Coupons:
The code that we use here will be “50DISCOUNT”.
Step 2. Add the coupon input to Registration form
Create a coupon column in the registration model
rails g migration add_coupons_to_registrations coupon:string:
1 2 3 4 5
In the form we will create the input for the coupon
1 2 3 4
This input will be shown only if the course is sold with a subscription plan.
In our registration controller, add it to the permitted params:
1 2 3
Add the coupon when we create a customer in our Registration model:
1 2 3
Step 3. Test the coupon feature
Select “ALL COURSES PLAN” and fill the form with the coupon “50DISCOUNT”:
If you go to the Events in the Stripe Dashboard you can see:
- How your coupon creation was registered
- How the new customer is created with the coupon discount
Stripe’s documentation on coupons is a great resource, where you can find more details about this feature.