This is the third 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 (this tutorial)
- Stripe Payments in Rails, Part 4 – Plan Discounts with Coupons
In this post, we will work on the same code base for this tutorial series. We will implement a course subscription service to demonstrate recurring payment handling by Stripe.
We will work on the recurring branch:
1 2 3 4 5 6 7 8
Make sure you have followed all the steps in Stripe Payments in Rails, Part 2 – Use Custom Forms with Stripe.js before continuing with the steps below.
Step 1. Modify our model to offer a subscription plan
Stripe’s recurring payment processing is implemented with the Plan concept. So first of all you need to create your plan in Stripe console > Plans:
Once we have our “all_courses” plan, we will add this plan info to our course model
rails g migration add_plan_to_courses plan:string:
1 2 3 4 5
And running rails console
rails c, create a course:
Step 2. Sign up for the recurring payment plan
In registration model, add plan id when customer is created:
1 2 3 4 5 6 7 8 9 10 11
Step 3. Create webhook to handle the monthly payment events
Every month Stripe will make an attemp to charge the card that the customer provided and notify us the result with a web hook (HTTP POST). On our side, we need to be able to update the user’s status accordingly. For our application, we will use an “end_date” column to capture when the user’s subscription is valid until, and increment that by 1 month every time the subscription payment goes through.
Add end date to Registration model
Run migrations generator
rails g migration add_columns_to_registrations end_date:date customer_id:string:
1 2 3 4 5 6
Annotate Customer Id when Registration is Created
Add to ‘models/registration.rb’, at the end of ‘process_payment’ method:
Create hook resource
Add to your ‘config/routes.rb’ file:
Process the payment event
Add the following method to RegistrationsController
1 2 3 4 5 6 7 8 9 10 11
When the monthly payment is succeeded, just renew the registration.
Here, the “#renew” method is adding one month to the registration end date. Let’s add the following method to
1 2 3
Show registration attributes
Add in ‘views/registrations/show.html.haml’:
1 2 3 4 5 6 7
Step 6. Testing the implementation
Expose our service to be accessed by Stripe
When we are running with the development mode, our webhook can not be accessed from Stripe to be tested directly because it is not exposed in the public internet, it is on our localhost.
We will use Ngrok to tunnel our local application to a Ngrok subdomain. We can then allow Stripe to send a HTTP POST to our localhost and test our payment process locally.
You can download Ngrok and install it for your system.
Now you can publish your application just running the command:
ngrok 3000, since the rails port is 3000.
It will respond with a URL,
http://1a74746e.ngrok.com in my case. You have to create the webhook into Stripe settings:
- Log in to your dashboard.
- Select your account name > Account Settings > Webhooks.
- Click Add URL and enter your webhook URL (http://1a74746e.ngrok.com/registrations/hook in my case). Indicate whether this is for live or test transactions.
- Click Create webhook URL.
Test this feature
Run the application, choose our new “ALL COURSES PLAN (monthly payment)”, fill the form (using dummy card number for test environment: 4242424242424242) and submit:
Now you have your registration successfully created with your ticket:
Give some time to let Stripe call our webhook, and refresh the ticket page. You have to see the Plan end date (just one month from today):
If you go to the Stripe console, you can see all events created with the subscription and the first payment:
You can also see the customer creation event details:
In this article we have showed you a basic example of setting up a recurring payment plan subscription. There are more options available if you have more sophiscated use cases. Check out the following Stripe documentation on recurring payments:
In the next tutorial – Stripe Payments in Rails, Part 4 – Plan Discounts with Coupons, we’ll look at how to add coupons to the payment plan.