Tip:
Highlight text to annotate it
X
Hi, my name is Bret McGowen and I'm a software developer here at Rackspace working on the
RackConnect project. Today we're going to be talking about using Stripe in your Rails
application to accept payments. If you haven't seen the first video on getting started with
Stripe, you can click the Rackspace logo in this video to learn more. In the last video,
my friend Carrie was getting started selling cookies on the Internet. Today, she's going
to take her website to the next level, by creating an interactive Rails application
and using stripe to accept payments. Let's take a look at how easy that is to get started.
So Carrie is about to set up her store as a Rails application, so she'll do just the
normal rails new, we'll call it Carrie's Cookies. And now that that's set up, we can install
the Stripe gem. So we'll go into our Gemfile in the project and we'll add this line "gem
stripe" with a git reference and do a bundle install. Now that that's finished installing,
we'll create a new controller called charges.
And this is going to set up some template views and controllers for our payment form.
So now that we've set up our gem file and created the controller for charges, let's
create some new files for our application. If you're not familiar with MVC, you can learn
more about it on the Ruby on Rails website, but basically we'll have to create a controller,
a view, a layout and some routes to get this all wired up.
The first thing that we'll create is the charges controller, and just so you don't have to
type in all this code as we go along, click the Rackspace logo here on the video. That
will take you to my blog post, and I'll link to where you can get this sample code to copy
and paste into your application. In this controller we've got some information, for example, the
charge amount is 2500 cents, the customer and so forth.
We're going to go to the routes and here we'll add resources:charges so that it'll wire up
our charges routes. We're going to add stripe.rb and that's going to be in the initializers
under config, we're going to create this. And we're going to set up the publishable
key and the secret key.
So you may be wondering why we don't just paste the API keys directly in the source
code. So for security reasons, when you check this code in source control or you email it
to your friends to look at, you don't want to actually include those API keys in there,
because once they have those, they can log directly into your account and get all your
Stripe information. So instead, we're going to set those up in the environment of the
local machine, and in a second I'm going to show you how to do that.
We're also going to create a charges layout and this is going to go in the views, layouts
folder. We're going to create the new.html under the charges folder in the views folder.
Now this is where the actual checkout form is going to live. So this is going to be where
Carrie's customers come in and take a look and fill out their credit card information.
The create.html file this just going to be the thank you page, so when the order is successfully
submitted, they are going to come to this page so she can give them a message, "Thanks,
you've paid for some great cookies."
So now let's take a look at this application actually in action. To do that, you're going
to run from the command line PUBLISHABLE_KEY equals this value, SECRET_KEY equals that
value, and that you can get from the Stripe website under your account, space rails s
for server. So let me start that, and that will actually start up the web server so we
can take a look at our page.
So if we browse to localhost:3000/charges/new you'll see this form, Carrie's cookies $25
for one dozen, and this will look pretty familiar from the other video. When I click "Pay With
Card" it will have a form we can fill out, so let's put in some dummy information. This
4242, this is a value that Stripe uses that you can use for testing, so that can be any
expiration date that's valid, and any CVC code.
I'll hit pay $25 and here we get a nice message from Carrie, "Thank you for buying some great
cookies!" So once again if we go to the terminal window, we'll see the form values that posted
back, we can see that there was a Stripe token here, tok 2M and so forth. You'll notice that
there is actually no credit card information, there's no name, there's no CVC, so we don't
have to worry about purging any of that from the logs or securing any of that data.
So that's how you can install Stripe in your Rails application and accept payments. It's
pretty easy and there's a lot of examples out there as you can learn more. Click here
for a step-by-step guide about how to install Ruby on Rails on your Mac, follow us by subscribing
to our YouTube channel, or check out some of the videos below.