Dan Pastori
December 17th, 2019
So to kick off this mini-series, we first need to Install Laravel Cashier. The documentation (https://laravel.com/docs/6.x/billing) to install Laravel Cashier is pretty straight forward and will get you where you will need to be within your application.
We will be using the newest version of the Laravel Cashier Package (10.2.1). I'll step you through the process and explain a few things that I thought stood out along the way.
This is really straight forward. Like the documentation states, you just need to run composer require laravel/cashier
and voila! You are ready to rock and roll!
For our course, we aren't going to do anything really tricky. We are simply going to allow users to subscribe to plans within our application. This will require a few extra database columns and tables to make this work.
When installing Laravel Cashier, you will get a couple database migrations along with the package. To migrate these changes simply run php artisan migrate
.
A quick note, we are using the default users
table that came with Laravel, so the columns needed will be added to that. If you are using your own authentication table or allowing a different entity like a team to subscribe to your application, you probably should read the documentation on overriding these migrations.
This migration will add the following columns to the users
table:
stripe_id
: The unique identifier for Stripe for the user card_brand
: The brand of the card they have on file card_last_four
: The last four digits of the card that's on file trial_ends
: The date when the trial period for your application ends (if you are doing a trial period)
The migration will also add a subscriptions
table which will be in relation to the user
. This table contains the following columns:
id
: Unique ID of the subscription user_id
: The ID of the user who has the subscription. References the users
table. name
: The name of the subscription (this is set in Stripe, will talk about soon!) stripe_id
: The unique ID of the subscription for the user stripe_status
: The status of the subscription such as active
stripe_plan
: The ID of the plan that the user is subscribed to (you set this up within Stripe and will discuss soon!) quantity
: The amount of the subscriptions the user has (in our case, this will be 1) trial_ends_at
: When the trial ends at ends_at
: When the subscription ends at if they cancel
The documentation to do this is found here: https://laravel.com/docs/6.x/billing#billable-model. What you will need to do is open up your User model and add the Billable
trait. This will ensure that you can call the methods used to perform billable processes on the entity.
In our example, the file is our User.php
model. By default this is located in the /app
directory.
We will need to add the Billable
trait like:
use Laravel\Passport\HasApiTokens;
use Laravel\Cashier\Billable;
class User extends Authenticatable
{
use HasApiTokens, Billable;
}
Pretty much the same as the documentation! Notice, we've already installed Laravel Passport so the user has the HasApiTokens
trait which we will be heavily relying on later when we call our API. Next, open up your environment variable file and add the following line: CASHIER_MODEL=App\User;
This just ensures that Laravel Cashier knows what model it should use to perform billing methods. Yes, this is the default, but I like it explicit especially if your application grows and you move the models to a different directory, it's a nice reminder to configure this later. If it's not in the .env I tend to forget about it.
Like I mentioned in the last step, I like to have placeholders in my .env
file even if I haven't configured the variables yet. So before we start to diverge from the official Laravel docs, add the following variables to your .env
file:
STRIPE_KEY=your-stripe-key
STRIPE_SECRET=your-stripe-secret
This is mentioned in the documentation as well, but we will be setting up this information in the next section.
So to be honest, we've essentially run through the Laravel Docs for setting up Laravel Cashier. I included what we needed for this course and we will begin to dive in deeper in the next tutorial where we set up our Stripe account. We will also be taking some of the javascript that is in the documentation and making some Vue components to work in our SPA. On to setting up Stripe!
Professional developers choose Server Side Up to ship quality applications without surrendering control. Explore our tools and resources or work directly with us.
We're a community of 3,000+ members help each other level up our development skills.
Active Discord Members
We help each other through the challenges and share our knowledge when we learn something cool.
Stars on GitHub
Our community is active and growing.
Newsletter Subscribers
We send periodic updates what we're learning and what new tools are available. No spam. No BS.
Be the first to know about our latest releases and product updates.