Refer a Friend using Stripe and Rails in 15 Lines of Code

A referral program on a SaaS product can often be the difference between success and failure. A famous example of this is Dropbox, which aquired four million users in just fifteen months; their referral program likely driving the companies success (which is just about to IPO).

For a one person shop, the trick is to completely automate the program – requiring no work to maintain. That’s what this article covers! In a few short lines of code, we’ll show you how the ProjectPiglet.com referral program was desgined / written, utilizing Ruby on Rails and Stripe.

Our Referral Program

First, it’s important to note there are many ways to design a referral program, ours is as follows:

Get 90 days free subscription ($150 value) in 2 minutes or less!

Here’s how it works:

  1. Refer someone you know providing the following link:
    <link on refer page for ProjectPiglet.com>
  2. They click on the link
  3. They sign up, with their credit card and activate their account.
  4. You immediately receive 90 days free on your subscription (in form of “trial”)

That’s it! Round trip, it should take less than two minutes.

Pretty straight forward and we make it worth the users effort! Now, lets look at implementation.

Rails App Design

Every web application is different, however Ruby on Rails forces a particular way to develop, which in this case is a good thing!

For instance, I can assume you have a “user scaffold”, which also comes with a user model & controller, with that model & contoller we can also make the assumption there is a “create function” in said controller. From there, all you need is stripe setup! For the sake of this post, we have a service wrapper around the stripe on ProjectPiglet.com – so basically we can treat stripe as a model. It’s really nice, and you can see that in our github gist.

That’s it! That’s all the prerequisites we assume – which is essentially: you’re using Rails and using Stripe!

User Registration Workflow

When a new users comes to register you typically want a few pieces of information. For ProjectPiglet.com, we try to keep that to a simple four pieces of information: Name, Email, Password; then you enter your credit card (which we validate). For us that looks like the following:

The question then becomes, how would you want to handle referrals? In our case, it was decided a link will be used for friend referrals and a coupon will be tied back for our “influencer referrals” (i.e. users who get enough people to register, they a cut of the subscription profits).The influencer referral program is not covered in this post.

The link we are looking for should be something like the following:

https://projectpiglet.com/signup?referred_by=<identifier>

Users then simply have to share that link to get the referral(s), the new potential users will be sent to the signup page and the “referred_by” parameter will then be used by rails to link bake to the referrer. In our case, it’ll just be the user id.

Refer a Friend Implementation (Rails & Stripe)

With the design being in place, it’s really as simple as adding a little bit of code to the create function in user_controller.rb class (14 lines of functional code). I personally add it after the credit card and email validation checks:

Note, our payment system service is in this github gist – it’s merely a wrapper around Stripe.

Then, for some Rails magic, we add the foreign key reference to your user.rb model.

This lets your use it as a reference to link back to a particular user (1 line of functional code):

With both of those in place, you’re done! We utilize the stripe “trial” feature to set the date to 90 days further out. This makes it completely automated dead simple to maintain. The only difficult part (if you’d call it that), was ensuring that the trial times are added to the current trial end date (basically the trial time stacks). Without that, the end date would always be just 90 days from the date of code execution (which would upset customers).

Finally, if you’d like to display who referred who, you can use the following partial:

Again, dead simple – in part because we created that referred_by foreign key reference.

Unfortunately, there are some caveats, although they are minimal. For instance, ProjectPiglet.com has users add their credit card at signup. That means, I know with a high probability it’s not a fake user singing up (as we do card validation). This makes it much easier for me to manage any potential users trying to game the system, if your web application doesn’t have that, additional validations should occur.

There’s also the case of shutting down the new account. If a user shuts down account, do you take away the free trial? That’s a question for you, personally I’ve gone both ways.

Regardless, of the caveats above, I hope this helps!

If you’re interested in using ProjectPiglet.com, use the coupon code: pigletblog2018

It’s 25% off for 6 months! Plus, clearly we have a refer a friend program!

Leave a Reply

Your email address will not be published. Required fields are marked *