Loading...

Rails Stripe Subscriptions Part 1 - Stripe Subscriptions with Stripe Connect

I've got some exciting news to write about and it has been some time since I've updated my blog. Recently, I've finished the course at The Firehose Project, became a Firehose Ambassador, added tons of logic to the final Firehose project Firechess, and successfully added Stripe Connect with subscriptions and fees to an application I'm building in my free time.

Adding Stripe API, Stripe Connect, and subscriptions to my Qelody application was a nice learning experience but documentation for it is lacking in a lot of detail and explanation. I would like to walk through some key steps besides adding checkout.js or other basic Stripe configuration to help others build out this same feature.


Step 1: Stripe Connect allows users of your application to make Stripe accounts which can interact with your application's Stripe account. Once a user is connected, you can send payments, create customers, create plans, create subscriptions, and much more on the connected user's Stripe account.

To setup Stripe Connect there a several things that need to happen to properly retrieve a new users Stripe information and connect them to your application's Stripe account. The following code is written with the assumption that you have setup your application with Stripe Connect under your application settings in Stripe dashboard, are using devise for authentication, and also assumes that you, the reader, has knowledge of some basic Stripe configuration.

1. Add the gem 'omniauth-stripe-connect' to your gemfile and within your routes.rb add:

devise_for :users, :controllers => { :omniauth_callbacks =>"omniauth_callbacks" }

Add this to your User model.

devise :omniauthable

2. Generate a migration and add these columns to your user table to save Stripe Connect information:

t.string   "publishable_key"
t.string   "provider"
t.string   "uid"
t.string   "access_code"

3. Generate an OmniauthCallbacksController and stripe_connect method to save Stripe credentials to your user table:

class OmniauthCallbacksController < ApplicationController
  def stripe_connect
    @user = current_user
    if @user.update_attributes(provider: request.env["omniauth.auth"].provider,
                               uid: request.env["omniauth.auth"].uid,
                               access_code: request.env["omniauth.auth"].credentials.token,
                               publishable_key: request.env["omniauth.auth"].info.stripe_publishable_key)
      # anything else you need to do in response..
      sign_in_and_redirect @user, :event => :authentication
      flash[:notice] = 'Stripe Account Created And Connected' if is_navigational_format?
    else
      session["devise.stripe_connect_data"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end
end

4. You will need this in your devise.rb initializer so Stripe can write to your database:

config.omniauth :stripe_connect,
                ENV['CONNECT_CLIENT_ID'],
                ENV['SECRET_KEY'],
                :scope => 'read_write',
                :stripe_landing => 'register'

5. Add a Stripe Connect button to a view where you would like your user to create a Stripe account, save Stripe credentials to their user account, and connect their Stripe account to your application's Stripe account. The png image in this code can be downloaded off of Stripe's website here.

<%= link_to image_tag('blue-on-light.png'), user_omniauth_authorize_path(:stripe_connect) %>

6. Under your Stripe account settings within the Stripe website/dashboard, you will need to set your test URI redirect to here:

http://localhost:3000/users/auth/stripe_connect/callback

7. If you do not set the URI properly your application will not receive the new user Stripe Connect information. Also, make sure to use private and public test keys within your application.yml when testing Stripe Connect functionality and not live production keys. If you add these keys to your application.yml, make sure to either add this file to your .gitignore or preferably use a gem like figaro. Once you've added keys to application.yml, you can reference these keys safely throughout your application like this:

ENV['CONNECT_CLIENT_ID'], #CONNECT_CLIENT_ID and SECRET_KEY are variables within application.yml
ENV['SECRET_KEY']

Once you have users connecting to your application, there are many things you can do. Check out this for more information. In the next part of this series I will go over how to dynamically generate customers, subscriptions, and plans on connected stripe accounts and; go over ways on how to retrieve and manage this information efficiently.