How To Setup Recurring Payments With Stripe Checkout

2019-03-12

Just a couple weeks ago I wrote a step-by-step guide showing you how to setup Stripe Checkout using Express.js and Heroku. This guide only showed you how to setup one-time payments, but what if you want to setup recurring payments so you can sell subscriptions?

You’re in luck, I’ll show you how to set this up today in this post!

First, if you haven’t yet, go ahead and go through this guide and setup Stripe Checkout with one-time payments. If you already have that setup, it’s only a small change to setup recurring payments instead of the one-time payment.

After that’s setup, we need to create a product and pricing plan inside our Stripe Dashboard!

After creating your product and plan, you’ll want to snag the ID for your plan. It’ll look like this ‘plan_DguBnFdeQ27pja’:

Now the fun part! We need to change some javascript code in the app.js file.

Currently, after following this guide, you’ll have this bit of code here on lines 22-37 in the app.js file.

myapp/app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19


app.post("/charge", (req, res) => {
let amount = 500;

stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken
})
.then(customer =>
stripe.charges.create({
amount,
description: "Sample Charge",
currency: "usd",
customer: customer.id
}))
.then(charge => res.render("charge.pug"));
});

This code obviously makes only a one-time payment, so we need to change it to the following:

myapp/app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14

app.post("/charge", (req, res) => {

stripe.customers.create({
email: req.body.stripeEmail,
source: req.body.stripeToken
}).then(customer =>
stripe.subscriptions.create({
customer: customer.id,
items: [{plan: "plan_yourstripeplanID"}],
})).then(charge => res.render("charge.pug"));

});

You’d replace ‘plan_yourstripeplanID’ with your Stripe plan ID you got from earlier.

That’s it! Go ahead and test out your Stripe Checkout form to make sure it’s now making recurring payments by using a test card.

If it’s all working correctly, go ahead toggle out of ‘Test View’ mode inside your Stripe Dashboard to go into live mode. Then you’re going to have to setup your Product and Plan again add your live mode plan ID to your code like shown above.

It’s kind of confusing how you have to jump between test mode and live mode but that’s just how Stripe works. You are in test mode when you’re developing your checkout form and testing it, then you have to switch to live mode when you’re ready to real payments from customers. You can read more about it here.

What are you waiting for!? Go setup recurring payments and start selling some subscriptions! :)