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.
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.
So the first field we store is the Plan that a Company is subscribed to.
We need to have an overview of how Stripe works to understand this one.
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:
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
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
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.