Written by Chris Oliver
Adding subscriptions and one-time purchases to your Rails app can be a bit tricky, especially with Strong Customer Authentication (SCA). If you aren’t familiar, SCA is a European regularly requirement to reduce fraud and make online payments more secure. For more details, I highly recommend reading the Stripe guide on Strong Customer Authentication.
For example, Braintree doesn’t allow you to swap subscriptions between monthly and yearly plans. You actually have to cancel the subscription, calculate your proration amount and create a new subscription with the proration discount.
Pay makes this simpler by standardizing (as best as we can) subscriptions and one-time payments for:
One place this comes in handy is if you want to primarily use Stripe but still want PayPal, you can use both with very little code changes.
We won’t get into every single detail, but I want to highlight how Pay works at a high level.
First off, we’ll run the Pay migrations to add models for
Pay::Charge. These keep track of subscriptions and payments for our
Billable objects. A
Billable is a model that makes payments in your app and you can have multiple of them (for example User and Team).
class User < ActiveRecord::Base include Pay::Billable end
This module adds methods to the User model so we can add payment methods and charge the user.
First, and most importantly you’ll have to set the payment processor for the user:
user.processor = "stripe"
So let’s add a payment method:
And subscribe the user to a plan:
That’s it! Pay will create a Customer object and set the default payment method before charging the subscription.
Webhooks are also required for most payment integrations these days as well. For example, you need webhooks to store subscription charges each month and to email receipts to your users.
Pay comes with all the required webhooks out of the box so you don’t have to configure anything.
Pay also supports SCA out of the box. If a payment requires authentication, Pay will raise an
Pay::ActionRequired error that you can capture and send the user to confirm their payment. Banks may require you to authenticate the payment, so we make that workflow nice and easy for you with a pre-built confirmation page.