This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
06. Installing and configuring Cashier

Transcript

00:00
So to get all of the functionality to be able to create and manage subscriptions, we're going to install Laravel Cachier, then we're going to configure it and then we'll pretty much be ready to go. Okay, so we're just going to follow through with the documentation here. So I'm going to head
00:13
over to the installation section and the first thing that we need to do is pull this in with Composer. Let's go ahead and do that now and let's head back over and see what else we need to do. Okay, so Cachier requires that there are some changes to our database, so we need to go ahead and publish the migrations for this. That will go ahead and create some migration files which we
00:34
will then go ahead and use php artisan migrate to update in our database. Once that's done, what you'll notice is you'll have two new tables in here. We've got a subscriptions table which keeps track of the user's subscriptions and subscriptions items as well. Now most of the time we don't really need both of these, but depending on the complexity of the subscriptions you're
00:56
creating, perhaps a little bit later on, both of these become really important. If we head over to the users table, you'll also notice that we have a stripe id added, a pm type, pm last four and a trial ends at. This pm is payment method. This will allow you to see which payment method type the user is currently using and the last four digits of it, so you can display it to them in the UI if you want
01:20
to. And we're also going to look at trials in this course, so we'll see what trial end looks like as well. Okay, so our database is done. Let's head back over and see if there's anything else we need to do. So we can, if we want to, publish cache's configuration file. Let's go ahead and do that and just take a really quick look at it. So let's open up the cache config file here and have a look at what
01:41
we've got. So we've got these stripe keys which we're going to add to our env file in just a moment. We have a cache path which we don't really need to worry about. We've got some webhook stuff which we probably don't need to change and we also have a currency which is probably the most likely thing that you're going to change here. So what you could do is you could either change this directly over
02:02
in here or you could add a cache currency value to your env file and override this as well. So if you wanted to change any of these values, check inside the config and then just assign them a new value. I'll go ahead and set this specifically to USD here and that should be about it. So we've also got a currency locale as well and a bunch of other options as well. So if you need to change anything
02:26
head straight over to the config file. Okay so now that we've done this we need to go ahead and make any of the entities or models in our application billable. What this will do is use a trait that will give you a bunch of methods that you can then use to create subscriptions for that thing. So in our case we've just got a user and we want users to sign up for subscriptions.
02:48
Let's go ahead and add this billable trait to our user model. So let's open up user.php under app and models and let's go ahead and pull this trait in just underneath here. So let's pull in billable and we can probably just take a look at this as well. If we open this up you'll see that this is split out into multiple traits as well. Let's just open up the manage subscriptions section
03:10
here. Let's just take a look at some of the methods that this gives us. So you can see here that we've got a new subscription method so we can use this to create a new subscription. We've got the subscriptions relationship so we can access all of the user's subscriptions. Most of this we won't need to do unless you have any specific requirements but feel free to go through and look inside of the
03:31
code here to see what you can do with this package. Okay let's go back over and see if there's anything else we need to do. We can use a custom model if we want to which we're not doing here and a really important part of this are the Stripe API keys which we've already seen within the config when we dived into it. So we're going to need to go ahead and grab all of our API keys from the developer
03:52
section over on Stripe and copy these over. Okay so let's grab our publishable key and copy this and let's just remind ourselves over in the cashier config what this comes under. So let's go ahead and look for keys here and this is under Stripe key. So over in EMV we're going to say Stripe key set this here and we'll use the same thing for Stripe secret and we'll also set this as well.
04:18
So to do this we need to reveal this specifically because it is secret and let's paste this in. Now a little bit of a note on the two keys here and the differences between the two. The publishable key which we can see here is used for client-side things like if you were to create a payment form or a form to update card details directly within your application. We're not going to be doing that
04:43
because we're going to be letting Stripe handle all of this for us. You can add that a little bit later if you want to. The most important thing for us is the Stripe secret key which will allow us to make API requests directly to Stripe in the back end. So that's the difference between the two. This one can and will be public. This one always needs to be kept secret. Okay let's check if
05:04
there's anything else that we need to do here over on the documentation. Currency configuration we've already looked at. Tax configuration as well which we'll probably need to do and I think we are just about done. So now we have Laravel Cache here set up and configured. Let's now look at redirecting the user here over to checkout.
26 episodes2 hrs 38 mins

Overview

Learn to start accepting subscriptions with Laravel and build a solid foundation for your next application. In fact, it’s exactly how we do things here on Codecourse!

Even if you’re completely new to Laravel, by the end of this course you’ll have a platform where customers can purchase subscriptions and gain access to restricted areas of your application.

We’ll also cover managing subscriptions, cancellations, invoices, giving customers trial periods — and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!