My Code Fellows Project: Fun With Stripe
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. You’re going to need your test API keys and then make a couple plans for your customers to subscribe to.
Next, add Stripe to your Gemfile.
gem 'stripe', :git => 'https://github.com/stripe/stripe-ruby'
After updating the Gemfile, we will create a file in our rails
config/initializers folder. Let’s call it
stripe.rb. Here, we will set up our app to use our Stripe API keys.
1 2 3 4 5 6
In our newly created file, we set the
:publishable_key and the
:secret_key using environment variables (checkout dotenv). Then we configure Stripe to use our secret key whenever we call out to their API.
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 contoller for our forms to live
rails g contoller 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
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.
Now we create the forms to handle our credit card data in our
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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.
The source for this sample app can be found on GitHub and below is a video of me demoing the app I made for Code Fellows.