This episode is for members only

Sign up to access "Laravel Subscriptions" right now.

Get started
Already a member? Sign in to continue
Playing
09. Allowing promo codes

Transcript

00:00
So in this episode I'm going to show you how to enable promotion codes within checkout, but I'm going to show you the process of me digging through the code to find out how this
00:09
actually works and how it gets sent over to Stripe. So let's start with the easy thing, to allow promotional codes we just use the allow promotion codes method before we go ahead and hit the checkout and that will go ahead and enable this for us. Let's take a look at just what this looks like and then we'll go ahead and dig into the code. So let's click on any of the subscriptions
00:31
here and you can see that this is enabled the promotion code section and you can set them up directly within your Stripe account under your product catalog. So you can go over to coupons, you can add a coupon, set the name and then you can give that to the users to use with your checkout. Okay so ignoring that then for now let's look at the process of digging into how this actually
00:54
works so we can find out what else we can do with cash here without really looking at the docs. Okay so a little bit earlier we dived into the billable trait and we dived into the manager's subscriptions trait as part of this as well. We know that we're using this new subscription method to go ahead and create a subscription, this has returned to us a subscription builder.
01:16
So if we look inside of this subscription builder you'll see that we have that allow promotion codes method just here. What this is doing is it's setting a property within this builder to true. So the allow promotion codes builder is setting this to true. Now if we look for this inside of this file you can see that it doesn't actually exist. It exists within this
01:38
checkout builder. Now where does checkout builder come from? Well it comes from when we call the checkout method. So let's go back over to our subscription builder and let's look at that checkout method that we used earlier. So this does a couple of checks, checks about trials which we're going to look at later and then builds up a payload which gets sent over to Stripe. So all of this data
02:01
gets sent over to Stripe and this returns a checkout customer which returns to us if we just have a look in here a checkout builder. So you just want to follow the code through to see what's going on behind the scenes and if you have a very specific requirement something you need to send over to Stripe this will help you out as well. So if we open up the checkout builder and we look
02:22
inside of here you'll find allow promotional codes which if we just have a look here within this payload to create this goes ahead and passes this through. It's going to send all of this data over to the Stripe API so that will be handled for you. Now this checkout method also we have a bunch of session options and customer options as well. So as an example what you could do is you could
02:48
pass through any of the details about your customer in this second option here. So for example we could pass in the email address of the customer so we can access that via the user here and we could grab their email address. Let's look at the difference that's made because that's probably the most important part about this. So if we do this you can see that my email here has been pre-filled
03:11
you can pre-fill a name and address any of the information that you need to send over. So I'll leave that in there as a reference remember these are the customer options and these are the session options. So now you know how to allow promotion codes specifically but also dig into the code to find out what else you can do really without looking at the dots.

Episode summary

In this episode, we're diving into how to enable promotion codes during checkout, but we're not just blindly following documentation—instead, I'll walk you through my process of exploring the codebase to see how things actually work. We'll start with the easy part: enabling promotion codes by using the allowPromotionCodes() method before starting the checkout process. This adds a new section in the checkout where users can enter their promo codes, and of course, you'll set up your promo codes and coupons directly in your Stripe dashboard.

But we're not stopping there! I'm also showing you how I dig deeper into the code to figure out where this functionality is set and how it's wired up. We'll poke around different traits and builders like the subscription and checkout builders. You'll get to see how the allow promotion codes setting actually gets passed through the checkout payload sent to Stripe.

Along the way, we look at customer options—like how you can pre-fill the checkout form with your user's email, name, and address. By the end, you'll not only know how to enable promo codes but also get a feel for how to read through and experiment with the code to find out all the other cool things you can tweak for your specific needs—all without needing to pore over the official docs every time.

Episode discussion

No comments, yet. Be the first!