Painless Stripe Subscriptions with Laravel Cashier

Introduction

Most applications have some sort of premium subscription, or products you can buy or some other thing to purchase. Odds are, you’ll probably want to bill your users more than once. Probably on a regular basis. Stripe is a service that helps you accomplish that by providing very easy, yet incredibly robust, platform for accepting payments in your applications.

In this post, I’ll walk you through the steps of getting a subscription billing plan setup and working with your Laravel project. This post assumes you already have a Laravel project and an active Stripe account. I’ll also assume you already have a table named users. If you don’t have Stripe, it’s free. Go register.

Create a new Stripe Subscription

The first thing you should do is create a new subscription in your Stripe dashboard. After you login, you should see you current sales and some stats. From there, just click on the Plans link to get started.

Once the subscriptions window opens, you’ll see a list of all subscription plans associated with your Stripe account. If you already have one, you can of course use it. For this post, I’;ll assume you don’t.

Click the new button in the top righthand corner of the screen. This will open the New Plan creation window.

On this screen, fill out all of the relevant details for your Stripe subscription plan. Make sure you remember the ID, because this is how we will identify the plan when we initiate the recurring billing.

You’ll be required to enter your password again (usually) after clicking the Create plan button.

Install and Configure Cashier

A: Now that your subscription is setup, we need to get your project setup to bill. Add the following line to your composer.json

  "laravel/cashier": "~2.0"

B: Register the Cashier provider in your app configuration file by adding this line to /config/app.php

  Laravel\Cashier\CashierServiceProvider::class

C: Laravel Cashier requires some database columns in order to work properly, so we need to use artisan to create add them. We will use a table called users. Whatever table you use, you should already have.

  php artisan cashier:table users

Once the migration has been created, just run the migrate command.

Setup the Model

In order to associate the billing with your user (the record in your user table), we need to add the BillableTrait and date mutators to our user model. Your user model should look something like this:

use Laravel\Cashier\BillableTrait;
use Laravel\Cashier\BillableInterface;

class User extends Eloquent implements BillableInterface {

    use BillableTrait;

    protected $dates = ['trial_ends_at', 'subscription_ends_at'];

}

You will also need to set your stripe key in one of your bootstrap file like so:

  User::setStripeKey('stripe-key');

Bill the user!

Now that we’ve got everything setup and configured, it’s time to make some money! Subscribing a user to your subscription plan is incredibly easy, and can be accomplished with just a couple lines of code.

  $user = User::find(1);
  $user->subscription('monthly')->create($creditCardToken);

The subscription method takes care of the subscription, and updating the user table in your database. If the plan you created in your Stripe dashboard allows a trial period, the trial dates will also be added to your user table.

Conclusion

That’s it! Stripe was made for easy integration by brilliant people, so integrating your application is painless. There are millions of ways you could integrate. With the fully robust APIs, you can even manage your subscriptions, plans, users, coupons, trials, etc, etc. The list goes on.

My goal was to create a clear, easy post to follow. If I accomplished that, let me know! If I did not accomplish that, also let me know. Be sure to share the link!

Your email address will not be published. Required fields are marked *

*