تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Testing Without Test Mode0:29

Now for dev purposes, Paddle does not have a test mode like Stripe does. So the recommended way to test is to use free products or coupons with a 100% discount. And one more thing to take note of is user identification. So Paddle users are unique across the entire Paddle. So for example, if you order something from someone that used Paddle before, your information might already be on their servers. So let's go ahead and do what we did for Stripe and take a look at subscriptions and single charges. So let's install Paddle first. So installation, and it's composer require it.

Installing Paddle Cashier1:00

So let's install Paddle first. So installation, and it's composed to require it. And as always, I already have an application here using Jetstream, and I already have these routes set up for the things that we are going to do, like in the Stripe videos that I did. Okay, so let's install it. Okay, that's done. What's next? And it also says to make sure you have Cashier's webhook handling set up, and we'll take a look at that. When we get there, go ahead and migrate the database.

Configuring User and Keys1:30

look at that. When we get there, go ahead and migrate the database. Okay, and I believe we have to set up our keys, set up our billable model first. So let's use billable in our User. So let's go ahead and go into our code, and let's go to our User and use billable. Okay, next is the keys. So we have to grab this from our Paddle dashboard, which we'll do. Just add this first down here. Okay, so let's grab our vendor ID. And let me just delete these.

And the auth code is the secret key. And you can find that here. But obviously, I'm not going to show you that. Okay, so I added the auth code. Believe that's it. So now we have to add the JavaScript. So we can just add this Paddle.js before the closing </head> tag. So I'm just going to do that in my layout. So app.blade.php, let's put it right here. Okay.

Building Checkout Subscriptions3:09

So app.blade.php, let's put it right here. Okay. And I believe that's it. You can do this other stuff as well to set your default currency, but I'll just leave it to USD. Okay. So the most common way you'll see Paddle being used is the checkout widget. So let's take a look at how we can do that. So as you can see, this example makes a new subscription. And actually, we have to create subscription plans within the dashboard.

So as you can see, this example makes a new subscription. And actually, we have to create subscription plans within the dashboard. And I've already done that when I was playing around with Paddle. So go to checkout. Or is it in catalog? Yeah, it's in catalog subscription plans. And I have four set up here. I have one for standard, one for premium, and two for free. So just go ahead and set this up. The most important thing here is the plan ID, which we need to pass into this method.

So just go ahead and set this up. The most important thing here is the planId, which we need to pass into this subscribe method here. So this new subscription method. So let's grab this and go to our routes file. And for subscription. So this one here, or subscribe, let's paste that code in. And this is going to be the subscribe view. Okay. And I can get rid of this.

Okay. And I can get rid of this. For the User, let's just change this to the loggedInUser. So I'll do auth()->user(). Okay. And the method signature is the same for the one in Stripe. So this is the name of the subscription. You can name it whatever you want. And since we only have one, it's just default. And this is the plan ID.

And since we only have one, it's just default. And this is the planId. So let's do it for the free one. So let's grab this ID. Since like I said, there's no test mode. So I would say free, let's paste that ID in. And this is what happens after it's successful. So let's just go back to the dashboard. Okay. And now we have to create that button within our view.

Okay. And now we have to create that button within our view. So let's go to our subscribe view. And let's grab the next thing, which is the paddle button. So as you can see, it's passing in the pay link that we generated from the route, and we can style it accordingly. So let's grab that and let's see how it looks by default. So just put that button in there. And this should render a button in here. Okay.

merchant. So before I enter my credit card, obviously, I'm not going to record that part. But we also want to populate the subscriptions table. So here's my database. And after this is successful, we want a new entry in our subscriptions table here. So for Cashier and Stripe, this is all done automatically using their API. But for Paddle, it makes use of webhooks to do this. Now this is a bit more difficult to set up because you have to have your app actually up on the Internet somewhere. But we'll make use of valet or actually, I'm going to make use of expose to do that.

So let me just close this. And if you were to have multiple plans, then you have to pass in the different planId from the routes file or your controller. So for example, this is the free plan. So let's just say each three free plan. But if I wanted an option for a paid plan, we just have to add that as well. So let's do that. Actually, let's duplicate this. And let me just put a marginTop on this. And let's do the standard plan here.

And let me just put a margin top on this. And let's do the standard plan here. And let me just say, do standard. And let's say that let's get rid of this data-theme on here. And now we just have to pass in the payLink for the appropriate plan. So I'm going to name it payLinkStandard. And we'll pass it in from the routes file. So let's just duplicate this. Let's say payLinkStandard. And this will be payLinkStandard.

Let's say payLinkStandard. And this will be payLinkStandard. And we'll pass it in here to right here. And these two will be payLinkStandard. Okay, so let me just grab the planId for the standard plan. So it should be this space down in here. And now we should have the option for a free plan or a standard plan. payLinkStandard. Yeah, I named it lowercase here. And there it is.

Yeah, I named it lowercase here. And there it is. It is just being blocked here. It should be inline-block, I believe. Okay, so now if I hit standard, this should actually be $10. Sorry about that. The spelling is different here too. So that should work now. Let's try again. Okay, so now it's going to ask for however much this plan costs.

Setting Up Webhooks9:20

Let's try again. Okay, so now it's going to ask for however much this plan costs. And there you go. $10 a month. Okay, now let's take a look at the webhooks like I was mentioning. If you go to the setup webhooks section. So cache here adds this paddle/webhook endpoint, which handles all of the webhooks that paddle provides. So to take a look at that, we can do php artisan route:list. And let's look for paddle.

So to take a look at that, we can do php artisan route:list. And let's look for paddle. Okay, and there it is. And you can see it's pointing to this WebhookController. So let's take a look at that. Okay, so here's the WebhookController, and it's within the vendor directory. And you can see if I scroll down, it has this invoke method, which grabs the payload that's coming back from paddle. And it generates this method that it will call depending on the payload alertName. So that just means depending on the webhook that gets called.

And it generates this method that it will call depending on the payload alert name. So that just means depending on the webhook that gets called. So one of these webhooks here. So whether it's a created subscription, a subscription that's updated, and so on and so forth. And then it just calls that method here. So for example, let's take a look at the one we're doing now. So subscription with a handleSubscriptionCreated this one. So this is going to create a new entry in our database table for the subscription that is created.

So this is going to create a new entry in our database table for the Subscription that is created. So I think there's one more thing we have to set up to make sure the webhooks work. Yeah, so it's this CSRF protection. So we have to add this in our verifyCsrfToken in the $except variable here, or the $except array. So it bypasses the CSRF protection. So let's grab this. It's going to verify CSRF token. There it is.

I believe it's in alerts, and you'll see that I already have it set up for the project I did before I started recording. And as you see, it's the same endpoint /paddle/webhook. So that's what we want to do. But we want to do it for our new project here. So let's grab the URL here. So it's this. Okay. Let's paste that in. And I am using HTTPS here.

Let's paste that in. And I am using HTTPS here. And you can also set it up for email notifications, but I'll just leave it and make sure you have webhook checked for the ones you want the webhook to happen on. So I have it for all of subscriptions and all of one off purchases. So let's go ahead and save this. And it says it can't resolve it because it's not available on the Internet yet. Oops. Sorry. No, I meant to use the expose endpoint here.

So in our case, it's LC cashier paddle example. So it automatically detects if you're using valet and it will make this endpoint available. So there it is. And that's what we have set up in our let me just copy this to make sure it's going to paste it to make sure. Okay. And let's go ahead and save that. Let's see if it works now. Okay. So now it's been saved.

Protecting Routes via Middleware15:37

And it looks like a real checkout. Okay, let's take a look at checking subscription status like we did in the Stripe videos. And I want this section to be behind some sort of middleware. So only people that have paid are able to access it. So it's the same thing. So I'm just going to do it quickly. Let's make a new middleware called PayingCustomer and we'll add this handle method. So should I stop this? No, let me just open a new one here. So let's do php artisan make:middleware PayingCustomer.

No, let me just open a new one here. So let's do php artisan make:middleware, let's call it PayingCustomer. And let's go ahead and paste that information in. So PayingCustomer, let me just replace this with this. And the plan is called default, so that's fine. It's called default. All right. Let's double check. Yeah, it's called default. And we want to be direct to the subscribe page if they're not a PayingCustomer.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟