featured

Code Fellows Student Project: Fun With Stripe by Jeffrey Horn

Jeffrey, a current Code Fellow, built an awesome app to handle all Code Fellows student payments

Here’s his blog post via JeffreyHorn.com:

I recently got to play around with Stripe in my first big class project for Code Fellows. I have to say I was a little scared that I might be in over my head, but after working through a couple sample apps using Stripe and following the awesome docs on the Stripe site I felt a little more at ease.

To help anyone else looking to get started with Stripe I thought I would share how I used it in my app to create customers with subscriptions.

First off, head on over to the Stripe site and create an account. Your going to need your test API keys and you are going to need to make a couple plans for your customers to subscribe to.

Next you’re going to need to add Stripe to your Gemfile.

After updating our Gemfile we will create a file in our rails config/initializers folder we will call it stripe.rb. Here we will set up Stripe to use our API keys.

Here we set our :publishable_key and our :secret_key using environment variables (checkout dotenv) and then set our Stripe.api_key.

For the Subscription resources we will use a scaffold generator

`rails g scaffold subscription email:string plan_id:integer stripe_token:string

and then we’ll create a home controller for our forms to live rails g controller home index.

In our subscriptions controller we will put the code to save a subscription to our database and then the code to create a new customer and assign them to a plan with Stripe.

At the top of the create method we get the params from our submitted form. Under that we create a new Subscription and pass in the params. Then we create a new customer with Stripe and associate them with the corresponding plan. Finally we handle any card errors that Stripe might return.

Here we use the form_tag instead of the normal form_for because we are using Stripe checkout to handle our credit card data and submit the form. We also use a hidden_field_tag to hold the id for our different plans. The plan ids correspond to the ids we set up while creating plans from the Stripe dashboard.

Thats it! You can now sign users up for subscriptions using Stripe.

Next PostPrevious Post

About the Author