Install and Run Migrations1:23
So let's go ahead and do that. Okay, that's done. And let's go ahead and php artisan install. And this should publish migrations and the config. And let's go ahead and add the invoice template. Okay. And now it says to add these to our create_users_table migration. So let's do that. CreateUsersTable. Okay.
Create users table. Okay. And let's add that here. Actually, let's put it after profile. Okay. And what else? Let's run the migrations. So I'm going to php artisan migrate --fresh --seed. Okay, that's done. What's next?
Configure Cashier Plans2:11
Okay, that's done. What's next? Next is to we already have the key. Next is to add the plans within the cashier plans config. So everything is coded driven. So in Stripe or Paddle, we had to add the plans within the backend dashboard. But for Mollie, we can do it within the code. So let's go ahead and look for that should be in config. We have the normal cashier config plans and coupons. So let's go ahead and match these plans with our form.
We have the normal cashier config plans and coupons. So let's go ahead and match these plans with our form. So here's the plans array. Let's name this one standard like we have in our form. And it's $10. That is my form $10. Yeah, it's 10 and 20. And everything here is fine as default. I'm actually going to change this in a second, but I'll show you why when I get there. So let's duplicate the standard plan.
I'm actually going to change this in a second, but I'll show you why when I get there. So let's duplicate the standard plan. And let's make one for premium as well. Okay. And let's duplicate this. Let's call it premium. Let's make it $20. And that's fine for now. Okay, what's next? Next is to add the Billable trait for our User model.
Enable Billable User3:28
Okay, what's next? Next is to add the Billable trait for our User model. So let's do that. User uses Billable here. And I'm going to press tab and that should import it as well. Okay. And you can override this if you want, but it's fine as default. And we also have to implement this providesInvoiceInformation. So let's go ahead and implement this in our User model. So it implements this and let me just see if my intelligence comes up.
So let's go ahead and implement this in our User model. So it implements this and let me just see if my intelligence comes up. It's invoice information. I'll press tab and that should import it. Okay. And now there's a squiggly because we have to implement those methods. So we can just grab this and paste it anywhere in our User model. And I'm going to put it down here. Okay. Squiggly should be gone.
Set Up Webhooks4:29
Okay. Squiggly should be gone. And it is. And it also says to schedule a periodic job to execute php artisan schedule:run. So I'm just going to run this manually, but if you have this on production, you should have it on a schedule and I'll show you what this does in a second. Okay. So one more config option. If we go into the normal config file, there's an option for webhooks. So whenever a transaction occurs, we want Mollie to ping a webhook on our server and
If we go into the normal configure, there's an option for webhooks. So whenever a transaction occurs, we want Molly to ping a webhook on our server and that would synchronize the data accordingly. So if we were to do php artisan route:list and look for Molly, you'll see those endpoints. So we actually have two here. We have one for default payment and one for first payment, and you can set up the webhook URLs here. So I'm just going to use expose like we did for the paddle and stripe videos and expose just makes a tunnel between your local project and the internet. It's similar to ngrok or if you have valet installed, you can even do valet share.
just makes a tunnel between your local project and the internet. It's similar to ngrok or if you have valet installed, you can even do valet share. And that will do the same thing making use of ngrok, but I'm just going to use expose here. So let's run that command. And now our project is available over the internet and this is the endpoint and we can replace this URL with that or just add a absolute URL, which is what I'm going to do here. And I believe there's another option down here for another webhook. So let's do it here too for first payment. Okay.
Create Subscription Endpoint6:00
So let's do it here too for first payment. Okay. So now everything should be set up and now we can create a subscription. So the API is the same as in stripe and paddle. We can make use of this new subscription method on the User model. And if you scroll down here, there is an example which I will make use of. So let me grab all of this. Let's make a new endpoint for when we submit this form. So let's go back to that form. subscribe.blade.php.
So let's go back to that form. subscribe.blade.php. Let's make a new endpoint for the form. I'm going to call it subscribe.post. Let's go to our routes file. Let's make that endpoint. So I'll just duplicate this. It is a POST request and it's called subscribe.post. Okay. And it's pasted in that code.
Okay. And it's pasted in that code. Okay. Let's change a few things here. I'm going to make use of the global helper here off the User. And for the name, I'm just going to name it main because we only have one plan in our app and this gets passed into this new subscription method. And this is just what gets stored in the database. And plan is going to come from the request. So we also have to accept the request here.
And plan is going to come from the request. So we also have to accept the request here. So request, request. And let's make sure to import that. Okay. And let's just say plan is $request->plan. Okay. And let's import this redirect to checkout response. And this is the case that happens when a user is making a first time payment within MOLLE. So let's import this.
Okay. And let me just go into that dashboard and I'll just put a message in here. So I'm just going to paste it in. Just checking for a session variable and displaying it if there is one. Okay. So now if I did everything correctly, this should redirect us to a test payment. So let me make sure I have CSRF token and I do. And let's just dd to make sure it's going in here before we actually do it. Okay. So I'm going to submit this form.
Okay. So I'm going to submit this form. Actually, let me dd request() all instead. And let's make sure we are passing the plan through. So let me just refresh this. I have to make a new User because I remigrated my database, Andre. Okay. And now let's check if we are hitting our route. And we are.
And we are. Okay. As you can see, we're passing through the standard plan because that's what we selected. And that will get passed through right here to our plan variable. Okay. So let me remove the dump and I'm going to go back to my cashier plans config and I'm going to change the time for the subscription or the interval to one minute. And that's just so I can show you how the cashier run command works. And I'll do it for both. Again, this is just to show you the cashier run command and why you need to put it on.
And we also have when it was processed. So there is another table in here called order_items. And this one has two entries. So if you look at the order_id, this one is associated with that order that I just showed you. So this one's complete. But this one up here, the order_id is no. So this is the upcoming order that it's going to create. If the current time is greater than the process_time. And that's why I made it one minute intervals.
Run Cashier Billing Cycle12:33
If the current time is greater than the process time. And that's why I made it one minute intervals. So if I run php cashier run or php artisan cashier run, it's going to process this one and create a new order in here. So let's go ahead and do that. Let's go back to the docs actually, so I can show you that again. php run right here. And again, you should put this on a schedule, but I'm just going to run it manually here. So php artisan cashier run. And since more than one minute has passed, this should process that new order.
So php artisan cashier:run. And since more than one minute has passed, this should process that new order. And it does. It says created one order. And if you look at our database and refresh the orders page, we now have two. And if you look at the order items, there should be three in here now, because it has information for the next billing cycle. And it does. Cool. So yeah, that's a very basic example of how to use subscriptions with Mollie and Cashier.
Cool. So yeah, that's a very basic example of how to use subscriptions with Mollie and Cashier. There's obviously other methods in here too, like checking the subscription status, which is exactly the same as it is in Stripe or Paddle. So you would just check if they're subscribed and you can even make a middleware like we did in the Stripe videos. So you can protect your routes and the User can have access to some sort of members only profile. Let's quickly take a look at coupons as well. So if I scroll up here, you'll see that we can redeem a coupon here and I'll just grab
Redeem Coupons Route14:07
Let's quickly take a look at coupons as well. So if I scroll up here, you'll see that we can redeem a coupon here and I'll just grab this and let's make a new route for this. Okay. Back to our code. Let's make a new route for redeeming the coupon. So I'm just going to duplicate this one. Let's say coupon. And let me just paste that code in. It's going to be the logged in user.
And let me just paste that code in. It's going to be the logged in user. So we'll do auth user redeem coupon. The coupon code is named welcome and we want to redeem it for the main plan, which is what we named it. Let's name this route coupon and it's redirect to the dashboard as well. So let me just grab this and let me just paste this in here. Let's say coupon redeemed, coupon redeemed. Okay. And let's take a look at that coupon config.
Okay. And let's take a look at that coupon config. So cashier coupons. Like I said, the default one is called welcome and the discount is $5. So if I did everything correctly, this should redeem the coupon. So let's hit this coupon route /coupon. Okay. We get coupon redeemed and we have to run php artisan cashier run again. And this coupon will be applied to the next billing cycle. So I'm going to run this again.
