Skip to main content
Payments with Stripe:

Affiliate & Referrals Programs with Stripe & Rewardful

Episode 257 · August 9, 2018

Easily add an affiliate or referral program to your Rails application using Stripe and Rewardful



Earn a free month

What’s up guys? This episode episode we’re going to be diving into adding an affiliate or referral program to your rails application. So we’re going to be using a tool called Rewardful for this and it’s gonna give you links like where you can have a via and some ID in the URL and Rewardful is going to track that ID and when the customer signs up with Stripe you will include that ID into their Stripe customer or their Stripe subscription and then Rewardful is going to be able to keep track of that and figure out what customers have signed up and which ones have actually paid you and then you can pay out their commissions after 30 days. So this is one of the easiest ways to do that and it works with Stripe really really well. So that’s we’re gonna dive into today because a lot of you guys have asked about how do I add an affiliate program to my rails apps and this is definitely the way to do that. So on first you’re going to need to sign up for an account, then you’re going to need to connect your stripe account and then we’re going to need to create a campaign and integrate a couple lines of code into our Stripe implementation.
So once you’ve connected your stripe account, created your campaign, then you’re going to need to implement the code for Reward which is really really straightforward.

We need to add a little script tag to the bottom of the page right before the closing tag of the body.

<script async src='' data-rewardful='abc123'></script>

Copy and paste into our application layout and that is what we need to have right there. You can put this if you have other layouts, maybe you have a special layout for the checkout pages and put that on that one it really needs to be on the page that your checkout is added. So the important part here is that it’s going to use this code to add the Rewardful stuff into your stripe form that when you send it over you can add it to your customer. So we’ll save the modified layout and we’ll need to open our stripe form. So what I done is gone through the stripe episodes more or less like I have in the past. So we’ve got a app/views/subscriptions/new.html.erb and this is the Stripe elements form and what we need to do here is we want to add a data attribute called “Rewardful” and an empty string (like the Rewardful example):

<%= form_with url: subscription_path, id: "payment-form", data: { Rewardful: "" } do %

The string we added: data: { Rewardful: "" }.
So we go to our form and inspect it and we’ll se data-rewardful attribute (which is empty) . You can set empty string to true but it should work just fine, but empty string will be what you want.
Don’t be tempted to delete that even thought it’s empty. So from there we need to handle things server-side.
Now what this is going to do is the Rewardful javascript will look for data-rewardful on the page and then it will add the referral parameter into that form so that is sent over to the server-side. So just like how Stripe works where when you submit it takes all the credit card informations, it sends to their server, gives you a token back and then inserts the token as a hidden field. Rewardful is going to do the same thing except it’s going to work with a referral parameter instead.

Now you won’t be able to follow along with this step exactly until
you have some affiliates set up, but I want to show how this work. So basically when we go to any URL that is linked on the site before we set up stripe customer. If you put a via and affiliates name or ID in the URL when the come to your form with the data-rewardful stuff going on this is actually going to insert a hidden field into your form. So as long this marked as data-rewardful, it’s going to add that referral field as a hiddel value. So then you’ll submit this over to stripe and then Rewardful will look for that later on. So as you’re fiddling with this at the beginning you won’t have a referral ID because you won’t have any affiliates yet. So you won’t have a great wake to test that, but you just want to make sure if you pass over referral as a parameter that you send that over to your stripe customer and so that’t how that’s going to work. So then from here you can create or update your stripe customer, if you already have a stripe customer and you want to update them, go ahead and do that but normally we’re going to do this because they were signing up for the first time and you want to add that referral parameter in there. So just like normal we’ll going to our subscriptions controller and in the create action where we want to create the costumer let’s assign the code from Rewardful (but modified) to a variabile.

class SubscriptionsController < ApplicationController
def create
    customer = Stripe::Customer.create(
    email:, #User, which signed up separately, email 
        metadata:{ referral: params[:referral] }
#Charge credit card for subscription

The referral metadata is what Rewardful going to look when there is a new charge or a new subscription. So when there is a new subscriptions or whenever someone pays, it’s going to check the customer, see if they were a referral and find the affiliate associated to them and give them credit for that and then they get whatever percentage and then after 30 days it’s going to let you know that you can pay that person. It gives 30 days so you have time to give refunds and you’ll able to see all of that in the Rewardful dashboard which is awesome. It tells you know the unpaid commissions that are due, your paid commissions, total commissions and it’s really cool and it gives you the ability to see who’s doing the most referrals and all of that good stuff. So that’s really all there is to it. There’s really nothing that you have to do. That’s significant to change your application if you’ve already setup stripe elements and subscriptions and charges on the backend. So all you need to do is pass in an extra item into your form and then save it onto your stripe customer and you’re done, which is awesome! So well we will talk about this sort of from scratch in a future episode because I want to show you guys how to build something similar to this, but inside your own application. So if you building something like dropbox, where they want to give credit with different size storage or something like that but not actually pay money, we can go and build something very similar. We’ll use cookie to keep track of who referred and all of that we’ll use similar set style of url to keep track of the affiliates because that via parameter is what Rewardful uses and we can take that and kind of do the same thing but custom in our own app, but as you might expect there’s a lot of work to do that if you want to do it with stripe or payments. So it’s nice to be able to use a service like this than having to build it from scratch which is why I put it off for so long. So thanks to them for building this and talk to you guys in the next episode.


Subscribe to the newsletter

Join 31,353+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.