Getting Started with Stripe Payments

October 25, 2012

Here at 320ny we often work with clients who need to accept online payments. There are a variety of options in this field, but we tend to recommend Stripe.

For clients who are just starting out Stripe is a great way to quickly integrate a flexible payment system without upfront or fixed monthly costs. As developers we like Stripe because of their extensive documentation and powerful API.

Stripe makes it easy to get started but we noticed a small snag with the basic form.

Using Stripe Errors

If you peruse Stripe's 'Building a Payment Form' page you can see the standard Stripe payment form has an intermediate step prior to submitting to your server where Stripe will handle credit card authorization.

The code is fairly straightforward:

  1. Intercept the payment form submit event
  2. Use jQuery to collect the credit card details
  3. Use Stripe's createToken function to submit them to Stripe for authorization
  4. Call the stripeResponseHandler function

Stripe will have returned an object with data for you to work with. Either we have an error, which we'll insert into some kind of alert class. Or we have our token, which we'll use to create a Stripe payment from our server.

Let's talk about the first case: Stripe gives us a nice error object to work with, but when we first implemented our form we noticed that we weren't getting a invalid_expiry_month or invalid_expiry_year error, instead we were getting undefined. Let's take another look at our Stripe.createToken call.

Pretty simple, but what if your exp_month, exp_year values are empty? To get Stripe to return an invalid_expiry_year or invalid_expiry_month error you need to actually submit a value, the field cannot be blank. This can be solved either with a conditional assignment

Or you can specify the default values in the html for the form inputs.

That's it for now, in the future we'll talk in more detail about Stripe in your models and controllers, as well as testing your Stripe implementation with rspec.