SaaS Stripe Integration in 3 Easy Steps

Collecting and managing payments is an integral part of any SaaS app. Unfortunately, building a payment infrastructure from the scratch is a pain. At SupportBee, we use Stripe to collect payments. Stripe has a neat API which makes life really easy for developers. This post will not go into the details of Stripe's capabilities. Instead, we will show you how to integrate Stripe in your app with minimal code.

For this post we'll use SupportBee as an example. From a billing standpoint, in SupportBee, a customer is a Company that has setup our helpdesk. We charge a Company based on their monthly ticket volume.

The Basic Setup

To start collecting a monthly subscription fees, you need to store only three fields in the database. Everything else will be securely stored on Stripe.

The Subscription Plan

Our plans and associated ticket limits etc are stored in a yml file. This helps us add or remove plans without touching any code. The plan also have to be configured on Stripe (more info on plans).

So the first field we store is the Plan that a Company is subscribed to.

Stripe Customer ID

We need to have an overview of how Stripe works to understand this one.

One of the major headaches when dealing with payments is the sensitive information like credit card numbers. To store these, you need to encrypt these and also filter your server logs etc. Thankfully, you can completely avoid this using Stripe's Javascript API.

This way, when your customer submits the credit card information, it is directly sent to Stripe using javascript and a token is returned on success. We can use this token to create a Customer on Stripe. A Stripe Customer ID is the id of this record on stripe. From now on, you can use this ID to subscribe the customer to a plan, create a new charge (for add-ons etc), change the plan or cancel a subscriptions without ever storing any credit card info.

Expiry

The final field helps your application stay in sync with Stripe (new payment, payment failed, credit card expired etc). Each Company in SupportBee is considered active based on the Expiry and monthly ticket volume. Expiry is a timestamp field in the database. Post Expiry time, the company is inactive.

To make all this work, once a company subscribes to a plan, we set the Expiry to 5 weeks from that time. Even though we charge monthly, the extra week gives our customers a buffer if there is a delay in processing payments (cards maxed out etc). Every time the Company pays we increase the Expiry by a month. Our code is similar to:

The exceeds_ticket_volume? can be replaced by your billing logic. In case this method is expensive to compute, you can use memcache to cache it. This technique allows you to be very flexible.

Do you want to increase the Trial period? Extend Expiry and you are done.

Want one of your customers to use your product free for life? Set expiry to 2099! :D

Doing more with Stripe

Make the most of Webhooks

Stripe make it easy automatically update the Expiry using Webhooks. We extend the Expiry when an invoice.payment_succeeded is fired by Stripe for the Company. The following code can be used to receive Stripe webhooks.

You may have noticed notify_campfire method in the above code. On every new payment we get a notification from Stripe on our campfire using the tinder gem

Campfire

We also use the webhooks for sending a simple invoice after every payment.

So by storing only three simple fields you can integrate Stripe in your application. Let us know if you have a more interesting approach.



blog comments powered by Disqus