This episode is for members only

Sign up to access "Build a Referral System with Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
12. Setting up Cashier

Transcript

00:00
All right, so let's go through all the steps
00:01
that we need to get subscriptions working within our application. Because when we build our referral functionality, we want this to tie into the subscriptions that
00:09
have been created. Laravel Cache works really nicely for this. You might be using something else, but for the rest of the course, as we are receiving payments
00:17
in and attaching them up and calculating stuff, you can pretty much use any solution. OK, so Laravel Cache is basically just a package that hooks up and creates subscriptions for you,
00:27
allows you to create checkout links. All of that good stuff, we'll go through it in a second. Let's get it installed and just get this working. So we'll obviously go ahead and pull this in with Composer.
00:37
And let's take a look at the database migrations, because these are really important for what we're doing. So we'll go ahead and run phps and migrate. And you can see that's created a Customers column,
00:48
or added Customers columns to the Users table. It's created a Subscriptions table and a Subscriptions Items table. Now, the thing that we're most interested in
00:57
is this Subscriptions table. So what's basically going to happen is when someone creates a subscription, this is going to fill in with all of the details from Stripe.
01:06
Now, what we want to do when a subscription gets created is we want to hook up the subscription that just got created to the referral code. That means that on our dashboard,
01:17
based on the referral code we've got, we know which subscriptions are responsible or have been created from our referral code. So that's really important.
01:27
So we're not going to worry too much about subscription items. That's useful for Laravel Cachier, but we're really interested in this Subscriptions table. OK, let's go through and see if we need to do anything else.
01:38
We'll go ahead and publish all of the assets for this, which is going to be our migrations, which we've already done. And we don't want to ignore the migrations.
01:47
OK, so the next thing is the billable model. We need the user to be a billable entity. So let's go straight over to the user model. And up here, we will go ahead and use the billable trait.
01:57
This contains everything that we need to create subscriptions, generate links, all of that kind of stuff. So let's go down here. The next most important thing are the API keys from Stripe,
02:08
or this just isn't going to work. So we'll head over to our EMV file. We'll paste these at the bottom, and then we'll just copy these over.
02:15
We're not going to worry about the webhook secret just yet. We'll add that in later when we get to receiving webhooks. But we at least want our Stripe key and our Stripe secret. So we're going to go over to the Developers section over in
02:26
Stripe, head over to API keys. And we just need these two keys. So we're going to grab the publishable key, which is for the client side.
02:34
And we're going to grab the test key, which is for the back end. So let's go ahead and enter our password and just wait for that to finish so we can reveal this key.
02:42
And there it is. Great. So let's go ahead and pop that directly into there. And we're pretty much done.
02:48
Just check if we need to set anything else up on here. Currency configuration if you need to. Tax configuration. I think we're just about done.
02:55
So that is Stripe now set up. And what we can do from this is when the user clicks on here, we can go through to another controller. And we can generate out a checkout link
03:04
for that specific plan or price. Because we have that price stored in our database now, we can forward the user over properly. So let's head over and do that next.

Episode summary

In this episode, we dive into setting up Laravel Cashier so we can handle subscriptions in our app. This is an important foundation, especially since we're going to connect subscription events with our referral system later on.

We kick things off by installing the Cashier package with Composer. After that, we run the migrations that Cashier provides. This adds things like the subscriptions and subscription_items tables to our database, and even tweaks the users table so it's ready for subscriptions. We talk about how the subscriptions table is the key piece we'll use later when tying subscriptions to referral codes.

Next, we make sure our User model is "billable" by adding the appropriate trait—that's what Cashier needs to handle subscription logic.

After that, we grab our Stripe API keys and update the .env file with them. We walk through exactly where to find those keys in the Stripe dashboard, and set them up in our project. For now, we're skipping webhooks (that's for later), and just focusing on the basics: integrating Cashier and Stripe so we can create subscriptions and link the right user data.

By the end of this video, we'll be ready to actually generate Stripe checkout links for specific plans or prices, which we'll tackle in the next episode. If you've followed along, your app is now set up to start processing subscriptions!

Episode discussion

No comments, yet. Be the first!