This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
08. Validating plans on checkout

Transcript

00:00
One thing we're currently not doing here is validating the plan that we're passing through in the URL. So as an example, if I were to just go ahead and copy the link address for this,
00:10
paste it in, and then go ahead and change this over to any value, this gives us an error. So it's not necessarily a massive issue, but we do want to go ahead and validate this. So how do we do that? Well, there's a couple of ways, but the easiest way would be to just abort the request with say a 404 if the plan couldn't be found. To do this, we can completely wrap
00:31
the extraction of the plan out in an abort unless function call within Laravel. So let's take everything that we've done here. We're going to pass this through as the first argument to abort unless. Get will return to us null if it can't be found, because we're trying to access something on null in that error that we saw. And the second argument is the status code that we
00:54
want to send through. So in this case, we'll just say 404 and you can change that over if you want and do anything you need. So let's go over and just try this out. We'll go ahead and grab that link again and we'll paste this in and choose something else. And there we go. We get a 404 not found because, of course, this plan can't be found. Another way that you can do this if you
01:14
want to log something or redirect specifically, you could just put this in an if statement. So you could change this up to do something like the following. You could say if and you could assign the plan at the same time. And you could say, well, if it doesn't exist, so just make that falsy. Then you can return here, redirect the user somewhere to a specific route if you wanted to
01:37
maybe log something perhaps before you return anything you need to do in there. You can put that in an if statement. I prefer abort unless because it's highly unlikely that the user is going to choose the wrong plan unless they're specifically changing the URL. OK, great. So now we have protection against a plan, an invalid plan being chosen.
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!