hello everyone how is your day going I I hope you're having a fantastic day welcome to everyone in the chat and thank you for joining me on this surprise Sunday stream Sunday morning stream um I don't know tell me what's your experience about this but I'm just curious when I was doing research for this video I looked everywhere for tutorials about LEL cashier and strip because I need this for another project I couldn't for the life of me found find any tutorial that was one year or or less I mean every tutorial that that I found is either one year old or older going back to Lille 9 and not even using um cashier instead using the stripe API directly which is fine but we have L we have LEL 11 now we have cashier thank you everyone for the love in the chat but we have so much so many so many more tools now but there is a lack of information about them other than the documentation of course which is great so if you can find one make your own and that's what we that's why we are here I'm going to show you how to use l cashier with stripe and how to manage user subscriptions in uh L 11 in 2024 so this is the agenda for today first of all we are going to install cashier which is very simple we're going to configure cashier and we are going to make some modifications to the user model we are going to also configure stripe web hooks which are important so that we can um keep our database or the application database in sync with stripe uh also we are going to create a few products actually one product in stripe and we are going to add several prices aen to what you see in uh sub subscription websites where you can pay monthly yearly or even just make one payment and you get lifetime access and then at the end we are going to take the user to a stripe hosted checkout page so that they can finalize their subscription in that page and then we're going to make sure we check that user subscription in our web page all of the tutorials that I found use another method where they uh use like an input field which is uh hosted in an if frame and the only way that they can the user can pay is just by uh putting the credit card but what if you want to use another payment method what if you want to use Google pay or Apple pay um that's why I think the uh stripe hosted checkout page is the better option so yeah let's uh jump in and get started what I have here right now is just let me get out of the way first I have a blank application I created this using um the breeze template as you can see we have a login and register here and that's it I don't have anything else so the first thing we need to do is of course we need to install L cash here let me do this in a terminal and let me reference the documentation as always so that I don't forget anything going all the way back in installation so all you have to do is run this composer command compos the require Lal cach here let's run that here let make this a little wider there you go all right we have cash here and um and that's it it's installed the next thing that you need to do is you need to publish the migrations because like for cash year LEL is going to make modifications to the users table add a few more columns and also create a couple more tables for subscriptions specifically hello Felipe thank you for joining me I'm doing great thanks and uh so happy that you can join me today so yeah let's for first we need to publish the migrations with this command PHP Artisan vendor publish tag cashier migrations let's copy that and paste it here and now that we have the migrations published and those are in the database migrations folder we need to run the migrations so that's going to again modify the users table add a few columns and create two new tables so PHP Artisan migrate there you go create subscriptions table create Subs descriptions items table and create customer columns all right cool uh next thing we need to do is make some configurations to our application namely we need to add a few lines to the EMV file so that we can grab the API key so that we can have the API keys from stripe in our application uh hi there I'm finally able to join this Beautiful live course thank you so much for joining me madas madas m do Hardware I don't know sorry if I say if I don't say it right and the Jet of course for a single purchase we don't need Lille cashier right we well you could you can actually in fact the one of the pricing items that we are going to have in this course is you can have a monthly subscription you can have a year subscription or you can make a oneof payment so that's an option you can use cashier for that as well uh let's see so we have this now we need to make some configurations it is recommended that you also publish the configuration file so let's do that as well let's copy this and run that now we have a new file here config cashier. PHP and in in in case you don't know the names of the variables that you need to add to the EMV file you can get them there so let's open this I already have this open in the vs code so let's go and check that file out so we have the migrations here and the new config file so let's start with these two stripe key and stripe secret if I'm not mistaken I already have the those lines in mymv file at the end so stripe key stripe secret and there is also a web hook secret key that we are going to need later uh but first let's start with this two so the way that you find those uh make sure you create your own stripe account you can do that for free and uh you will be put in test mode so that you can make tests in your application and when you add access the stripe panel this is what you see you can manage your payments balances customers Billing Products and subscriptions from here but what we need right now is go to the developers Tab and in the API Keys you're going to need these two so first let's grab this one just click to copy this will be the API key make sure you put it in double quotes just in case and then we need the secret click to copy and then paste now again make sure you you use your own key because this key um it's restricted and it's not going to work for you and I'm I'm also going to roll that that key when the video is over so either way it's not going to work so make sure make sure you use your own key for this step all right now that we have the keys the next thing we need to do is make sure that our user is able to interact with stripe so the only thing that you need to do is open the user model and in the traits just add the bable tra simple right that is going to give the user the ability to check for subscriptions and um just interact with the strip uh package how are you Arturo everything okay I'm doing great thank you very much for asking and you pronounce it correctly good to know all right so we have cashier installed we have cashier configured uh what we need to do now is let me check my notes so we install cashier we configure cashier and the user model uh now let's move on to configure the web hooks why are the web hooks important because every time we do something in stripe we need to keep that information in sync with our database now we have a table a couple of tables in our database and that table needs information from from stripe for example if we create a new subscription or if we modify the subscription tier we move on from monthly to yearly for example that's that's a different price if we cancel the subscription all of those events need to be in sync with our application and the way we do that is with web hooks basically we are going to tell stripe every time something happens in the subscription or with the with the customer I want you to call this URL which is going to be a URL in our application make a post request and send the the information about what happened that way we can take that information and update our database so we need to create those URLs or create those web hooks in stripe thankfully because uh the lateral deps are very generous the process of creating those web hooks is very simple if we go to the documentation and we scroll down to handling stripe web hooks the first we need to do the first thing we need to do in this case is use the stripe CLI to make sure that when we are working locally we can listen for those events because if we configure the web hooks but we don't use the stripe CLI then those web hooks stripe is going to try stripe is going to try and call the web hooks URLs to the application that is defined in the app URL um variable of our configuration in this this case this is a stripe. test because I'm using her so stripe is going to try and make a post call to http stripe. test stripe web hook but that that is going to try to do that on in on the internet and obviously that's not going to work because that's just for local development so what we need to do is use the stripe C to redirect those calls to our local environment and then and therefore we we can listen for those so the stripe CLI is very simple to use you can read more in the in their documentation here but basically what we what you need to do is first install it and there are steps to install it using Homebrew Mac OS if you want to download the executable or the binary and on Windows as well you can download the exex file and make sure you put that in your environment in your path environment variable I already did this with Homebrew because I'm using Mac so I run this command and now I have the strike command ready so let's go to the terminal and run this stripe login command so it's saying that I need to go to this URL with this pairing code so if you just click this link here uh command click actually it's going to take you to stripe and it's going to create a it's going to verify verify your identity and create a 90day token so that you can you are basically logged in you with the CLI so here the it's actually already has the pairing code so all you have to do is allow access and now you may close this window and return to the CLI okay closing here and let's go back here actually the command that I need is not here right now but I'll tell you what it is so I'm going to go back to the documentation real quick and hello a aeq thank you for joining me so based on the documentation these are all of the events that we are going to listen to Or that LEL listens to uh with web hooks all of the request all of the requests or the post requests are going to the app URL SL stripe SL web hook and these are all events subscription created updated deleted uh C customer deleted deleted updated payment method automatically updated invoice payment action required or payment succeeded and LEL is going to make sure to keep the database in sync uh when he receives any of these events and as I said for convenience we have a BHP Artisan web hook or PHP Artisan command to automatically config our web hook if we check the stripe um console and we check the web hooks tab this should be empty for you this I have this here because I I already had some tests before and it shows disconnected but it should be connected now so for you when you visit this tab for the first time this all of this should be empty so the way we configure all of our web hooks is we go to the terminal and we do PHP Artisan stripe web hook not that okay well done let me get the actual command cashier web H sorry let's copy this paste it here PHP artisan cashier web hook so the stripe web Hook was created successfully and we can retrieve the web hook Secret in we can retrieve the web hook Secret in your stripe dashboard and Define it as an environment environment environment variable sorry so we're going to get this key in just a second keep in mind that to do that to use the cash your web hook command you need to have the API and the the key and the secret first defined in your Doan via okay and then you can look for the web hook secret so now that we have our web hooks created and let's see how that looks here if we refresh there it is now we have a URL that stripes is going to call every time something happens in the customer's account and it's defined as as HTTP stripe. test SL stripe forward slash SL webbook okay now let's listen for these web hooks locally the way that you do that is you go back to the command line and then do stripe listen and then you forward the um events from stripe all the all of the web hooks you forward those to this local machine at stripe. test stripe web hook so now we are listening right and this is the secret key so we are going to copy this key and notice how it changed in the in and the stripe portal as well is showing now that it's listening so we're going to copy this key go back to the EMV file and put that in the stripe web hook secret okay and now we're ready to listen for web hooks so with the web hooks um created let me just move to my next section in my notes I think with that we are ready to move on to the design of our page we're ready to create a pricing page to give the user the opportunity to select a product but before that actually let's let's create those products or create that product in pricings in stripe we can do either or actually let's do the first one so that you see it and then we create the products because I don't remember the pricings that I used for this test so it'll be useful to have that so let's move on to this I'm going to register here first okay so we're we're logged in we are going to create a new pricing pricing page here so to do that let's use I'm going to close this to Windows for now now I'm going to open a terminal here and let's use PHP artisan make view let's call it pricing okay we have a new view and let's see how that looks pricing BL PHP and I'm going to use the dashboard view to um modify this one blade sorry dashboard I just want to use this app. layout and also the header I'm going to copy let's actually copy all of this and paste it here and I'm going to get rid of this section here because I already prepared the design for this uh page you can find the link to this GitHub gist that I created it's in the description of this video and I'm going to copy let's see this in raw I'm going to copy all of this and just place it in here and of course fix the indentation okay so um just so you know the way that I designed this I didn't design it actually I used an example in this website called prel line.com you can go to examples pricing and I used basically this design and just trimmed everything that I didn't need and slightly changed the colors if you want to know the the source of this design all right so if you if we save this now let's add a let's add a um navigation link so I'm going to open the navigation. blade. PHP Livewire component because when I created this I use Breeze with Livewire um in VA class as I always do even though I'm not going to use use uh any liveware components um that's why this navigation layout is a liveware component so up here we have a link to the dashboard let's create another one didn't need this this is going to go to another route let's call that route pricing and down here we have the the responsive version here so let's duplicate this as well this will go to the pricing route and the label is pricing now we need the route of course so I'm going to open the web.php file located in routes in the routes folder and I'm going to Let's duplicate the dashboard route because I need the I need the same middleware and it's also going to be just a view route no controllers so so this is going to be called pricing pricing is the name of the view the URL is pricing and the route name is pricing as well okay see if that works go back here refresh now we have a pricing link and when you click on it the route checkout is not defined okay so in this design I already I I also have a route for for the checkout page so let's define that and just leave it empty for now just so that it doesn't fail and I'm going to change this in a second so it's going to be checkout and let's just do the call back function that basically does nothing and we need middleware and the name is checken the design looks awful but that's because I haven't I need to rebuild my local assets so for that I'm going to open a new tab here and just run V and there it is this is our pricing page we have three options one for $4.99 monthly $34.99 yearly and then one single payment of $174.99 for Lifetime access now we need to make the we need to make it so that when the user clicks on any of these he or she is taken to the checkout page which is strip hosted and then they can finalize the payment there and they will be then redirected to our application where where we can show the content that they want obviously checking that they are subscribed so let's go back to stripe and let's create that product so we can go to the product catalog and then add a add a product here let's call it premium you can add images if you want to and then you can have pricings let's start with the the the first one that you add is going to be the default one and I want that to be the yearly subscription so that's $34.99 I think that's it right oh we can have more pricing options so let's add another recurring um price this one's going to be $4.99 and it's going to be monthly and then next it said next am I missing something no okay oh it said it as $4.99 I changed the price sorry let's uh modify that let's modify this price because that's the default one and I said I want that to be $34.99 the yearly subscription okay next and then for this same product you can have more prices so let's add one more price this one's going to be $4.99 monthly and then one more price what was it $174.99 for one uh for uh lifetime access single payment so instead of saying that this is recurring this this is a one of payment of $174.99 and then next so there you go we have one product called premium with three pricing options and then we click add product and there it is now for this product when you click on it you can see that that the product has a product ID we're going to need that in a moment and all of the pricings all of the pricing options have an ID as well and we do need that so for example I'm going to take this price ID and you can you can oop sorry go back I'm I'm going to copy this monthly pricing the ID of it and then go back to the pricing page and head down to the first card for the monthly payment and the route that is going to take me to check out is going to carry on the plan ID so I need to pass this to the route so that I can use that information to subscribe or uh send the user to the checkout page for the correct pricing hello surf how you doing so I'm going to paste that here in the first link and then move on to the second card and this is the yearly plan so back to here I'm going to copy the ID for the yearly plan and paste it here and finally the lifetime access copy the ID oh I copied the wrong one didn't I okay so this is the livetime one and the yearly one is this one there you go and save that now what we need is a checkout endpoint a checkout route so for this I'm going to build I'm going to create a controller so PHP artisan make controller let's call it checkout controller and since it's going to do only one thing and it it only needs one function let's make it invocable okay we have the checkout controller it's created and for the contents of the controller let's actually refer to the documentation real quick in the selling subscriptions and this is what the checkout looks like you need to be log you need a login user for this so keep that in mind and on that user we just call the new subscription method the first parameter is the product ID they use this default and that's kind of confusing basically that is the product ID that has the pricings that we just created so we need to use the product ID from stripe in this first parameter and then the pricing ID that we are trying to buy we are passing that as a parameter and we are going to add that in a moment you can also add trial days if you want to so that the user can have a trial period And once the trial period it's is over then they will be charged you can also allow Pro allow promotion if you need to and then on the checkout method you provide two routes one is the success URL where the user will be redirected to if they complete the uh subscription or the payment successfully and the cancel URL if anything else happens if they decide they they cancel the the process they will be taken to this other route Ser how long have you been working with Lille it's going to be 8 years now I started working with Lille when it was in version 4 point 4.2 I think so yeah it's been a while I'm going to take the contents of this function copy it and then just use what I need but I'm going to put that in my new controller so let's open that controller check out controller inside the invoke method I'm going to paste that code here we already have a request do we need to import anything else no so let's replace this default with the with the product ID head back to stripe and up here copy this product ID and put it here now we are receiving a plan as a parameter so let's receive that this going to be string and it's going to be optional or I decided to make it optional because if you don't provide any plan for some reason you're going to be subscribing to the default one which which is the monthly the yearly subscription so real quick let's just copy the yearly ID copy that and paste it here so that's going to be the default if you don't provide any plan in the URL then we provide that plan or that pricing ID to the new subscription method if you can write it correctly land all right I'm not going to use trials I'm not going to I'm not going to use promotional codes just the checkout now I need two more routes one for Success one for cancel for cancel let's just take the user back to the dashboard and for Success let's create another route called success ESS let's go back to the routes file let's create Let's uh duplicate the dashboard again copy that and I'm going to put it down here this is going to be a another another view route so success is going to be the URL the view is going to be called success as well we need middleware and the route name success now let's create that view real quick PHP artisan make view success and I'm going to use the content of the dashboard and put it in this new success View and let's just say you are subscribed to premium we're going to make another modification on this view so that we can check the subscription directly from the user but we'll do that once we once we check out so this is saved go back to the controller make sure we have everything we need and I think we do the last thing we need to do is to modify the route to this controller because right now it's just a function so instead of that first off the route needs a plan parameter and that's optional as well for the second parameter let's just use the checkout controller class because this is an invocable controller we don't need to provide we don't need to provide the uh method name just the class so we have the route the plans view let me close a few things this is getting crowded success you can go away dashboard 2 navigation that's fine that's the controller okay the pricing page we have the route to check out we're passing the right parameters and everything should work hi musio yeah we started a while back sorry we couldn't wait so let's give this a try shall we all right so we have the web hooks running here and it's already listening for a lot of bun a bunch of stuff like products have been created here so if we go to the web page let's refresh for good measure and let's say we want to sign up to the monthly subscription we click the sign up button ah check out controller not found did I not import the class probably didn't import the class I did import the class other view oh because I did copy paste here this is actually a get route my bad all right let's try that again oh right away there you go it reloaded for me because I'm running be in the background so he just reloaded that uh route and now we are here in the hosted checkout page which is secure and you can use different payment methods like apple like Google pay you can pay with link when this is in when this is in in production I think they have even more payment methods but for now you can use Google pay and pay with link or just credit card so strip already provides a testing credit card for you to use just use 42 42 42 42 42 42 42 42 just 42s all the way you can use any date in the future let's say January 27 for example and any verification code let's put the full name on the card that's me country region etc etc you can see what pricing we are subscribing to that's a product right there premium membership and the price $4.99 a month when we click subscribe no thanks it's successful and we are back on our application and we are subscribed to premium Now how do we know that that was success first of all uh we have a payment succeeded here invoice paid invoice whatever so that web Hook was called we have a payment succeeded so we have a new subscription how do we check that well one quick way to do it is using Artisan thinker me clear this so let's do PHP Artisan thinker and let's grab the user and there's the user now notice that we have a new column here stripe ID that was uh updated automatically using the web hooks because now we are customer in stripe and also stripe has um added a relationship on this model user called subscription so if we do user subscriptions we have an entry in the subscriptions table notice the stripe ID and the price that we selected for this subscription when was it created has it ended how many trial days and when does it end what was the quantity Etc and in that subscriptions stable we also have another relationship for the subscription items with the stripe ID which is the customer name customer ID the product and the price so yeah that is all thanks to uh stripe web hooks so whenever something happens in stripe as I said those web hook web hooks are going to take care of keeping this information in our database up to date and in sync so let's do the last thing I want to do is make sure that we can check that subscription in the view for example in the success view we can use let's say for example you are subscribed to premium and we can use if we can do o you user we can use a method called subscribed wow can't type today maybe it's my keyboard that is yeah we can use the Subscribe method and we provide a product ID and L uh will check if that product if the user has that product or has purchased that product but we can also check subscribe to price I think let me check yeah subscribe to price and we can pass in the um where is it checking subscription Cent subscrib subscribed now for the subscribed we need to pass the price ID and as a second parameter the product ID so let's check for the monthly one so if the user is subscribed to price let's grab that from stripe copy this paste it here and and let's grab the product ID as well copy that paste it year so that's true let's add a p paragraph here it's saying that $4.99 per month save this go back to the web page and there you have it you are subscribed to pre to premium and we also can check what kind of subscription the user has and using the the price ID and we know those price IDs we can refer to them in the database if we want to and we can check another video for another day but for now thank you everyone for showing today and if again thank you everyone in the chat if you found any of this information helpful you know what to do hit the like button subscribe to the channel and I'll see you in the next one