Designing the data model for our course platform (with Stripe)
This is a long form video describing how I built out the complex data model for our course and e-commerce platform.
Our platform powers: - highperformancesqlite.com - masteringpostgres.com - highleveragerails.com - screencasting.com
Everything I said
All right, let me set the stage for you a little bit. This video is gonna be a little bit longer, and this is going to be an exploration into defining a data model for a learning platform/e-commerce platform. So here are some of the, uh, explorations that I've done. You can see orders, order lines, products, access, and then all of the different types of things that can be bought down here.
Courses, digital products, tangible products, coupons. Oh, that's interesting. Courses have modules that have videos. Well, that's interesting. Um, if you're not familiar with who I am or what we do, uh, me and my friend Steve run an education company, for want of a better word. Uh, we've got High Performance SQLite, where you can learn all about SQLite. If you want to learn about Rails and SQLite, we've got High Leverage Rails.
We've also got Mastering Postgres for Postgres. And then if you wanna learn how to make videos, we've got Screencasting.com, um, so you can go learn how to make videos there. Now, the problem that we're having, the problem that we're having is it used to be that we just sold courses, and that was pretty easy, and it used to be that each individual site only had one course.
So every site had a course, and then every course had many modules, and then modules had many videos, but now that is no longer the case. Now, we have digital downloads. We've got tangible goods. We're selling physical things. Of course, we have coupons, and then not represented in this one is we also have bundles somewhere, so you could buy, uh, more than one course at a time.
All right, so what I'm gonna do in this video is we're gonna draw out this data model. We're gonna use this TL Draw, which is a great product. We're gonna draw out the data model, and I'll walk you through some of the decisions I made and why I made them. This... It's a little bit of a gnarly... It's a little bit of a gnarly problem. Moving from, uh, one payment provider to Stripe is going to give us a lot more control over our actual checkout and everything, but we also have to do a lot more work, and the data model is the fundamental part of
designing this entire system. So let's walk through the data model. Okay, whenever I start with any kind of, like, system design or data model or anything like that, I like to say what we actually need to handle. Like, what, what is the goal here? So in this system, we need to handle videos that are free, but you must log in to view, videos that are totally free, um, when you buy access to a full course, when you buy access to part of a course, so you can imagine having, like, a basics and a complete
tier. So we need to handle, um, being able to access some of the modules, but not all of them. And then physical stuff like stickers, digital downloads. Of course, we do need coupons, team purchases, bundles of products. This is a big one. Um, this is a big one that we're, that we're trying out, basically. So if we look at, um, if we look at High Leverage Rails right now, and you go down to Buy Now, you'll see we have High Performance SQLite bundle.
So you get High Performance SQLite and the Rails course, and this bundle has been doing pretty well, and so this is something that we absolutely, um, need to carry over to Stripe. We're moving from Lemon Squeezy to Stripe, and we need to carry this over to Stripe, but we also need to make it a lot easier on ourselves to make bundles. So bundles and then upgrades from basic to full.
So I think right now if you go to Screencasting.com, and you go down to Pricing, you'll see what I'm talking about. Basic is two forty-nine, and then Complete is two ninety-nine. Um, and this record, this is six modules, and then this is the other two modules, uh, down here. So we need to handle, we need to handle, uh, upgrades from basic to full as well.
So those are the things that we need to handle. Let us, um... Let's just come up here. We're gonna copy that and come up here, and we're gonna start with, um, kind of how the models are laid out, and then we will go into, uh, we'll go into how we're gonna kinda mirror that or mimic that in Stripe because we do have, like, our local setup, but then we also need to have Stripe products and Stripe prices and checkouts and all of that.
So figuring out how we're gonna link those two things together is pretty important. So, um, let's, uh, zoom in to a reasonable amount. If you've never used TL Draw, it's totally awesome. Um, not sponsored, but I just love these guys. So here we've got a site. Now, a site is basically a, a domain. So HighLeverageRails.com is a site.
Screencasting.com is a site. This entire thing is one database. We're basically running multi-tenancy, but for ourselves. Um, so every site, um, has... It used to just be every site had a course, and so you could do something, you could do something like this, um, and say a site has a course.
I think for clarity, we're gonna change this to that little handle, um, if I remember to do that, because a site can have many courses, and that's as close to the has many thing as I could get. So a site has many courses, and then, um, a course has many modules, and a module is just what we're calling, uh, this right here.
So Module 3: Building a Rails Application, Module 2: Powering Your App, Module 1: Introduction. That's just kind of a way that we organize it. Doesn't super matter. And then modules have videos. Uh, modules have videos, and so we're gonna say, yep, that's right, and then that's right. So a site has many courses.
Courses have many modules. Modules have many videos. This is mostly right. Um, how do we do arrange, distribute? Option+Shift+V. There we go. So this is mostly right. Uh, it is simplified just a little bit, but only by leaving stuff out.
There's nothing here that is, uh, fundamentally incorrect. So then we have users, right? So we've got, uh, users over here, and a user may have access to one or more courses across one or more sites, frankly, but that part's irrelevant. Um, so if we do, if we do it like this, we can make it, uh, we can make it look a little bit nicer.
So a user has access to one or more courses. That is mostly correct. Um, there's a little nuance here that a user may actually have access to one or more modules. Um, and so that's something that we're gonna have to figure out as we keep going, because, for example, they may not have access to some of these upper level, um, complete package if they only bought the basic tier.
So that's something we gotta figure out, and we will. Now, the question is, um... Actually, let's go ahead and draw out, let's draw out one more thing. So we're gonna move this over here, and then we're gonna say, um, that we've got, let's say, uh, digital downloads. And so this is gonna play, this is gonna play a bigger role on screencasting.com because we could have something like, um, Final Cut Pro plugin.
That could be one digital download for twenty-nine. Hey, let's make it more expensive. We gotta make money. Forty-nine dollars. Then we could have a DaVinci plugin for, you know, forty-nine dollars as well. So these, um, these different sites might have digital downloads, but not all of them will, but some of them definitely will.
And you can imagine we want to bundle, um, like the Final Cut Pro course with the Final Cut Pro plugin for a discount. But we also need to sell the course discreetly, and we also need to sell the plugin discreetly. And so you can already see we're starting to, we're starting to, like, have kind of a messy model, um, when it comes to how do we represent all of this stuff in Stripe.
So let's keep going. You'll see down here on one of the old, uh, one of the old models that I drew that we don't actually end up using, I have orders at the top and then order lines, so that would be line items in an order, and those are linked to products, which is linked to access, which then links to the underlying actual asset down here.
This is not the model we end up going with. I don't think this is a very good model. That's, um, that's, that's not very good at all. Um, and then this is kind of trying to do the same thing, but not really getting anywhere. So let's come back up to this clean one that we're working on, and let's throw something in the middle here. So what we're gonna do is we are going to throw, um, we'll call this, uh, an enrollment.
This is basically a pivot table, okay? So you could, um, I think out of the box in most frameworks, this would be called something like user courses, which is fine. That's a fine name for it. Um, I do like the idea of giving, um, of giving these pivot tables actual names because it does make it a little more clear.
And when a user is taking a course, what do we say? Well, we maybe say that the user is enrolled in the course. So we'll call this an enrollment. So this is an enrollment. And, um, I think importantly, none of this has to do with payments yet, right? We have done, we've done nothing, uh, as it relates to checkouts or billing or anything like that, and that's gonna be something that we actually maintain.
So in fact, we are going to draw kind of like a strict firewall between, um, the payments side, and so we'll put, uh, we'll put, uh, models over here. That's not totally right. Uh, we'll put, uh, these are kind of our assets. No, I feel like models is maybe better. Um, and then over on this side, we're gonna have billing.
But those are still gonna be models, but you get the idea. And we're actually gonna not... We're not gonna try to cross this barrier very much. And the reason is, let's make this XL. We'll make this XL too. The reason is we wanna be able to enroll users in courses without having to go through, uh, go through Stripe.
So imagine that somebody pays via ACH, right? So somebody pays, um, for like a team plan with a check or a pay, uh, a purchase order or something like that. We don't need to go through Stripe for that. So I don't want the access to the underlying assets linked to something in Stripe.
What I do want is something in Stripe to generate those underlying enrollments, but I don't want them to rely on each other. We'll see more why in a second. Um, okay, so if we take, uh, let's take all of this and, um, we're just gonna kill site for now 'cause that's gonna make this a lot easier.
So we'll move that guy up there, and we'll say that a user has many enrollments, potentially, if they are enrolled in multiple courses. But a user also has, um, we'll say digital, uh, we'll say user downloads. That seems reasonable enough.
If you can think of a better name, please let me know. That needs to be... What is this guy? That is a medium arrow. Okay, so let's draw another medium arrow, and we'll say that a user has many digital downloads. And, um, yeah, so then we'll draw this guy down here, and this is actually gonna just be one.
So I think that's how you do one. Um, so every user download record is going to be linked to one digital download. So this is how we will tell, these are, these are like the-- this is kind of the access layer. So this is how we will tell if someone is allowed, uh, is allowed to access these underlying assets here.
So these are the underlying assets. We'll make those underlying assets blue, and then we'll say, um, these are the pivot tables that represent whether or not they have access to those underlying records. So you can also imagine, uh, we don't have it yet, but you can imagine, um, let's come over here, and we'll say, um, we'll say a physical product.
And so if they buy a sticker or something, we would need a corresponding, um, user, I don't know, uh, you know, physical whatever. Um, and then we could draw out this way as well, and we'll say that that is, that's fine, and have many physical access records, and then each one of those has one, is linked to one physical product.
Okay. We have solved, um, or at least we have started to solve the, um, how does a user access the things that they purchased, right? So when a user goes to a digital download, we can check this user downloads table, and if there's a record in there, you're good to go. You're allowed to buy that. Same with a course.
You go to a course, if you are enrolled in that course, whether manually or by purchasing it, you can access it. Physical product, the same thing, and in fact, on this one, we would probably have something like, um, we'd probably have a Boolean that's like fulfilled, and if it is not fulfilled, then a cron job could come along, pick up this, this access record and say, "Great, we gotta fulfill it." Like, somebody has allowed access to this physical product.
Now we gotta ship it. And so th-that is where we will store this, um, fulfillment information. And yeah, I think that's probably good on the access. So right now, I really do feel like that should be called access. So right now we're solving the access problem, and you know what? I think we have. That feels pretty good.
Now the trick is how do we actually how do we represent that in Stripe? Um, this is, this is where it gets a little bit gnarly. So if we come over here to, this is my Stripe sandbox, um, the product catalog. So, so one thing you have to know about Stripe is, um, um, it's great.
We love Stripe. We wouldn't be able to do what we do without Stripe. It used to be a lot simpler. I will say that. It used to be a lot simpler. Um, so what we have now is, uh, we've got this product catalog. So we've got all of these products in here, right? So we've got Mastering Postgres, High Performance, uh, team, team tiers.
You know, this looks like a bundle here. Then we've got all of these screencasting courses, and then that looks like a bundle there as well. Inside of every product, so let's click into one that has two prices. Inside of every product, which is this guy here, we have prices, and each price has its own discrete, uh, UID or a universally unique ID, and each price is where you actually store how much the thing costs.
So when you go to a checkout on Stripe, um, or when you create a checkout on Stripe, you have to deliver to Stripe the price ID, not the product ID. 'Cause if you deliver the product ID, um, there are some instances where that's okay, but you need to tell Stripe what price to charge. And so coming back, uh, let's come back over here.
We'll say that this is, um, we can just call this Stripe. That's fine. So we'll say that this is the product, and in Stripe, in Stripe, every product has one or more prices. And so we gotta change this to the multi guy. Uh, a product has one or more price.
Now, okay. We can't, um, we can't just say that a product is, you know, a product is a course. We can't just say course ID and then, you know, have, you know, ninety-nine dollars down here.
This isn't gonna work. And the reason that this isn't going to work, um, the reasons are many, in fact, because how do you model, how do you model this to support courses but also, um, you know, physical ID, but also, I guess I could have just done one text box here.
Uh, so you've got your physical product, you've got your digital product, but maybe it's also a bundle that in fact references a couple of courses and digital. So do you see like on a, um, on a database, uh, table basis, how do you actually model that? Not like this.
That's not how we're gonna do it. We're not gonna do it that way at all. Um, so instead of, instead of trying to, um, have some sort of like intermediate table here, um, that says, "Hey, this gives, you know, product," product what? I guess product asset and then have a polymorphic, have a polymorphic, uh, relationship down to these guys.
Um, that's kind of what we're gonna do, but not like this. This, this is, this is not right to me. So, um, what we're gonna do instead, and the reason, the reason I don't like that, um- The reason I don't like that is, let's come out here.
Um, that may be... Let's look around to see if I wrote it down anywhere. Yeah. Asset ID, asset type. So I tried this. This is, this is kind of where I started. Many records that indicate what assets the product enables access to.
This is close to where we end up, but this is not where we end up. Um, so if we were to do it this way, it would have to be polymorphic. So we would have to say that the ID equals one, and, you know, the type equals course, or something like that. Um, polymorphic doesn't move me very often.
I don't super love it. Sometimes it's exactly what you want, but sometimes it is, uh, more trouble than it's worth. Um, you can see here, I tried to mimic products and products within Platform and Stripe, and didn't get very far on that. And then this is, this is where we land, entitlements.
Okay, let's talk about this word entitlement. I feel pretty proud of that word. Um, uh, you know, maybe 40, 50% of data modeling is coming up with great words. I like the word entitlement because, um, when you buy a product, it entitles you to certain things, and those things may be a course, part of a course, an entire course, a digital download.
Maybe I'll ship you a sticker in the mail. Maybe I'll come to your house and shake your hand. I don't know what the entitlement is, but when you buy a thing, you are entitled to receive some other thing. So now that we've landed on that word, let's see, let's see how that fits in. Okay, coming back up to the clean one that we are working on, which is way up here, we're gonna remove, um...
Actually, let's do this. We're not gonna remove the firewall, but we are going to, uh, we are going to have these models locally as well. So we've got the firewall, um, and then we'll say that this is our database because that is what we are working with here. And we're going to-- we're just gonna have a one-to-one, um, mirroring of our products to Stripe's products and our prices to Stripe's prices.
But what we're also going to do is we're going to add a little bit more information in our local database. So this is truly synced, this is truly synced from Stripe. This is actually what I do. In fact, um, if I have, uh, Stripe prices, and I don't have any there, but you know what?
We can just load 'em. Why not? We're just doing it all. Let's, um, let me change into the platform and then put this over here. So if I do, let's do, um, Artisan Stripe sync. So this is gonna pull down all of the, uh, prices and products from the sandbox that I'm working on.
So if you see here, we are now in the Stripe Products table, and I have, like, the honest-to-goodness Stripe ID. I've got the name. We've got some descriptions here, whether or not it's active, and then look, I've got entitlements over here. So some of these things are default Stripe, like, um, Stripe ID, name, description, uh, active.
Default Stripe price ID links to all of my prices over here. And some of these things are default Stripe, like Stripe ID. That is like how I, uh, reference it in Stripe. But then I've got, you know, MSRP unit amount. And so that's if we put something on sale, we can say that, like, the default bundle costs $199, but we're gonna show it on our front end as $179 because it is in early access.
So that's what I'm talking about when I say we are gonna mirror Stripe. Like, um, I could go to Stripe right now and find these prices, but some of that data we're not going to sync 'cause there's no place to put it in Stripe, so we'll keep some of that data locally. Okay, let me show you, um, let me show you back over here in Stripe. We are in the products.
We're in the products table, and we're looking at, um, how to screencast. Now, each, uh, each product could have many prices, right? We've got an early access price and a standard price. But one decision that we made, one decision that we made is that the entitlements live at the product level.
So when you buy a product, regardless of what price it is, you get the entitlements. So, um, you'll n- you'll see in the way that we've set up, like, um, High Performance SQLite has five seats. So this is a team product right here, and then High Performance SQLite is, uh, down here. This is the solo product.
And the reason that we didn't make, um, the reason that we didn't make teams just a price under a product is so that we could do those entitlements separately. So if we come back here, and we just, um, let's just take this guy up here and explore with it for just a second, and we'll make a, we'll make a price as well.
So if we call this product, um, if we call this High Performance SQLite, that's a lot to type, so let's just call this the SQLite course, and we've got early access, and we've got, uh, we'll put regular at the top, regular. So we've got, uh, two prices for this particular course, and that's the way to do it.
So, um, the nice thing about this setup right here is we can change that, and we can change that, and we can change that. Yep. Oh, I thought I changed that. Um, oh, opacity. The nice thing about this setup here is I don't have to go into my, uh, my back end, which I'll show you in a second. I don't have to go in here and say, "Hey, when they buy early access, they get access to, um, they get access to SQLite.
And if they bought regular, they get access to SQLite." That's fine, but that can get really, really annoying if then you've got, you know, 10, 10 different prices, which, you know, we could have for, let's say, Black Friday, purchasing power parity, um, anything else, right?
So we could have a bunch of prices. So what we decided to do is move this entitlement up to the top. So this entitles you to, this entitles you to SQLite, the course. So then, of course, you could see SQLite, um, five seats.
This entitles you to five SQLite seats. And then you could similarly have, you could similarly have a bunch of, uh, a bunch of different prices here, and you don't have to, for every single price, go in and say, "Well, for this price, they get five seats. For this price, they get five seats." Like, come on. I, I only have so much time in the day.
So that is, uh, one decision we made, is that the entitlement lives at the product level. So we'll come down here, and we'll say entitlements are attached to products. Now, I'm not gonna make a table out of them. In fact, I'm not. We're gonna store that in a JSON blob. Um, and I know some people are like, "Yes, JSON," and some people are like, "No, JSON." That's fine.
We'll figure it out. All right, so here at, uh, the product level in our database, we're gonna store entitlements. And those entitlements, as you've already seen, look like this. So we have the entitlements. Let me go to the structure. Entitlements is a JSON, uh, blob, and by default, it is just a blank JSON array.
So by default, there are no entitlements. That's fine. We update them all anyway. Then you'll see, uh, this JSON blob in here. I wonder if this is an easier way to show it. There you go. The type is course, and then there are some fields. And so this is an interesting thing about the entitlements, is every different type of thing that you buy, um, there's different, uh, like there's different levels, or there are different things that are required, right?
And so for a course, I need to know how many seats are they entitled to, what course is it? Are they entitled to, um, the basic tier, the complete tier, the whatever tier? So every entitlement has a bunch of parameters that need to be, uh, input, which is one of the reasons I like JSON for this, because it is in fact schemaless, right?
If, uh, if I was just selling courses, I would probably have a different data model, but because I'm selling many discrete and varied things, the data model has to be a little bit flexible in this regard, because I need to say they're entitled to two stickers, um, and the stickers are, you know, the red one and the blue one.
They're entitled to this course, but only, you know, tier one of this course, not tier two of this course. And those are fundamentally discrete fields. They got a large T-shirt. Like, I don't wanna make a column for every potential thing, for every potential product, right? JSON to the rescue. Okay, now we have seen that in the product table, um, which is plural, it should be called product, we have an entitlements column that is, uh, that is a JSON field.
So that's products, prices, and the entitlements live at the products level. Now, I know that this is, uh, this is a data modeling video, but I do wanna show you a little bit of code, because I do think it's important, um, how we handle this stuff, right? Because when, uh, when the user comes back from Stripe, that's when we need to look at the entitlements and process them to create all of these access records right here, right?
So the entitlements will generate these access records, and so we can, um, yeah, we can just draw this arrow, and we'll make it this guy, and we'll say that, uh, generates, entitlements generate, and then we'll put, uh, we'll put a green guy.
So we'll say, rip, you go there, and we'll send you to the back and turn your opacity down, and there we go. So these are all of our, um, these are all of, like, our access records, and the entitlements generate access records, right? So let's look at a little bit of code here.
You'll see, um, we're in the platform directory. Like I said, this is a single repo that, that powers many different sites, and we have a, a full-on entitlements directory here. And so this is where we define what the types of entitlements are. So if we open that, you'll see what we've been talking about, a course, and then we see a download, and just for giggles, I've started on a sticker entitlement.
So if we click into the download entitlement, you'll see a couple of different things. Um, I like to make these self-contained. I like to make these, um, discrete entities that can handle the creation but also the fulfillment. So if you look here, we've got a fields, uh, method, and so this defines, um, what the user, in this case me, what, what, what does the admin need to enter when they're attaching
an entitlement to a product? In this case- We're just gonna make a select box, name it Download, store it as download_id. Uh, it is an integer. It is required. And then the options are all of the digital downloads. So in the admin interface, you can pick any digital download, and that goes into the entitlement.
Do you wanna make one? I wanna make one. Let's make one. Um, we'll go to highperformancesqlite.test/nova. Um, please don't email me. I think that password is just password. Okay, so we are in Nova now, and if we come down to Stripe Products, which is what we've been talking about, um, we can create a new product.
And the, the cool thing is this is actually going, going to create a new product on Stripe. Um, so that's kinda cool. So let's do Sample for YouTube. Hey, everybody. This is visible to the customer. Default price, there's no price yet. Um, as always, we'll put a to-do there. And now look at this. We have entitlements.
These are the things that the user should get. So what should the user get? Um, let's give them a download like we've been talking about. Look, there is our download. Remember I showed you over here, uh, the only field for this entitlement is Download, which is what we're looking at, Download. It is required, so we get that little asterisk.
And then we have some options. And the only options we're going to use are from the digital download model. And so we can click here and say they are entitled to the Test Download. So we can create Stripe product. It's gonna go out to Stripe. And you see that this entitlement is set to Download, and its name is Test Download.
And if we come out to the product catalog, Sample for YouTube, no prices yet. Uh, but look, one thing we do actually sync are the entitlements. We sync that as metadata, um, just because when we're, uh, when we're developing, we're constantly blowing away the database and rebuilding it. And so storing that on Stripe when we sync down the Stripe products will, uh, recreate those entitlements for us so that while I'm testing, I don't have to do that.
So that's kinda cool. Now, we could, um, of course, edit this and say, you know what? They're entitled to... Let's call this Sample for YouTube. Actually, we wanna call this a bundle. Yay. Now they're entitled to a course. And look, a course has more, has more options that the admin, me, has to enter.
So they're entitled to, let's say, High Performance SQLite, one seat, tier one, and they're entitled to High Leverage Rails, one seat, tier one. Update. So now whenever the person, whenever this person buys this product, they are entitled to these three things. And if we come back to the database and look at Stripe Products and refresh, discard the changes, that's fine.
You'll see... And then we'll open this guy over here. You'll see there are the three. The type is Download. We have the fields, which are the download_id. The type is Course. We've got three fields. And then the type is Course. We've got three fields. Um, importantly, and this is a pet peeve of mine, we're not storing any fully qualified class names in the database, right?
So I'm not saying app/entitlements/download 'cause I want the freedom to refactor those later. So we've set up a little bit of a map. So when it goes into the database, the course entitlement in the database is stored as course. The download entitlement in the database is stored as download.
I would recommend always, always, always having a map. Never put class names in your database. Never, ever, ever do that. It's just going to cause you pain. Keys go in the database, so they should never, ever be changed. Rename the classes all you want. Thanks, Aaron. I will do that. I appreciate that. Now, going back to the download entitlement, this is, uh, this is how we create those entitlements, right?
And then once they're in the database, they're just like a JSON blob. Now, how do we fulfill them? Well, we've got a handle method. This is pretty easy. So, um, the nice part about this is I know what fields, this data, I know what fields are going to come in because I just defined them. I can just look up a few lines and be like, "Hey, what's, uh, what's going on up there?" Uh, you stored it as download_id?
Great. I'll reference it as download_id. And the first thing I do is, um, I cast all the data. You can see that this is supposed to be an integer, um, but it might come in as a string, so I cast it all. And then for the user, I find their digital downloads, and I sync without detaching this new download, which this is all just Laravel stuff.
This just says, "Hey, add this record in the pivot table, but don't, don't delete any. Just make sure that this one is there." Um, if we look at the course entitlement, course entitlement, you'll see something funny down here. To-do, handle team licenses. I'm not there yet, okay? So this is still a work in progress. But we cast the data. If there are more, there's more than one seat, we'll just pretend that it worked.
We'll figure that out later. Otherwise, look, we're creating an enrollment, right? So this is going back to that, um, that named pivot table that we talked about. We are creating the enrollment, um, for this user and for the course and the tier, um, that the entitlement entitles them to.
We also have a checkoutLineId, which I'll show you in a second. And I add a little message that says, "You've successfully enrolled in the course!" We want people to be excited that they're enrolled in the course, right? So that is the entitlements. Um, this is both how we define them and then how we fulfill them. And it's important to me to keep those things close together So I like this model, I'm pretty proud of this model.
Um, it all comes from a base class that has a bunch of, uh, a bunch of different stuff, like instantiate by key. That's how we go from course to the actual, um, course entitlement class. And then we've got some, you know, some other stuff, some other nonsense stuff down here that doesn't super matter. Back to the data model.
Okay, so now you fully... Well, now I've explained it. I don't know if I've done it well, but now I have explained, uh, how the entitlements generate access records and how they are being stored on the product model itself. Now, we need to go up one level and say, um, we have a, a checkout, right?
So we have, you know, this could be called, this could be called a cart, um, but because on Stripe's side, this is called a checkout, we're gonna call ours a checkout as well. So on, um, in our database, we have a checkout. Uh, every checkout has checkout lines.
So we've got checkout lines over here, and I'll just do it all over here. We've got checkout lines, and then checkout lines, actually, you know, that's not right. Um, checkout lines are actually linked to prices.
So when you, um... That's kind of goofy, isn't it? When you do a checkout, so if we come over here and we look at, um... Yeah, payment links are fine. Um, payment links, does it show? There you go. So the product is High Leverage Rails, but it's a specific price, so you have to tell Stripe what price you are adding to your checkout.
In this case, we're using payment links to get to that checkout. Um, but you have to tell Stripe what price you are going to use. So, um, what we do is we just, we just pass the price ID. I think if your product has a default price, you can pass the product. But we, you know, we have everything and we're managing everything, so we just put the, um, the price ID on there.
So we'll move this firewall over just a little, and that is exactly how it is modeled in our local database as well. So coming back to our local database, uh, we will check for checkouts, and there are no checkouts. Checkout lines, and there are no checkouts. But you can see, um, if you look here, the Stripe price ID, um, and then quantity, and these are all, you know, just different, uh, different things that Stripe
will actually fill in, the discount and the total and everything. Processed is how we know if we've processed, if we've processed this particular, uh, set of entitlements yet. So, um, let's talk about this flow just a little bit. So a checkout has many, that actually, uh... Yep, that should be different. A checkout has many checkout lines, and then, um, checkout lines link to one particular, so we'll do it that way, link to one particular price.
Every checkout line has a price on it. Now, you'll remember that the entitlements are stored at the product level, so when the user comes back with a, um, completed checkout, what we have to do is find what price they bought, what product that's linked to, and then what the entitlements for that product is, are.
Um, so it gets a little, it gets a little bit hairy. Um, but this is, I think, the cleanest way, the cleanest way to do it. Um, so what is going to happen on, uh, on the platform side is a user is going to be browsing the website, and they're going to add a price.
It's gonna look like a product to them, but they're gonna add a price to their checkout. So we can check the code here, and we can go to, uh, let's go to common.php and find checkout. Here we go. Um, here we go. Checkout. So when they post, when they issue a post to checkout/lines, this is basically saying, "Hey, create a new checkout
line." And when they delete it, that's deleting a checkout line. This is basically a add to cart. That's add to cart, and this is remove from cart. We're just sticking to the HTTP verbs because, you know, I like, I like that crud style. But if you come into the store, you'll see it's very, it's very, very simple.
Um, so look here. We are saying, um, when we store, we're validating their request, and we're actually doing it by product ID, which is not what I told you. Let's keep going. We're saying, uh, you must, the front end, you must give me a product ID, and that product ID is required, and it must exist in the Stripes, the Stripe products table, and it must be active.
So if we have a product that we have since discontinued or disabled or whatever, we don't want somebody sneaking in and saying, "I'm gonna buy it at the old price," or whatever, the product that doesn't exist anymore. Um, and then we look up the Stripe product, and then we, um, find the checkout based on their session, and we add the product line. Now, if we go to the product line, look.
Default price. So what we're doing on our side is we're saying, "You can give me a product, but what I'm gonna do is I'm gonna find the default price, and I am going to add the price to the cart," 'cause that's what we have to send to Stripe. So we have, um, we have a few affordances here. You can add a product. That's totally fine. What that's gonna do is that's gonna defer to adding a price, which is Stripe price, and then that's where we will say this lines.
So for this checkout, create a new line, um, and here's the price ID, and look, that is where we copy the entitlements onto the checkout line. Right? So we know, we know that the product carries the entitlements, but we are going to duplicate that onto the, um, checkout line, such that when they come back, we can process all of those checkout lines discreetly.
So if we look... In fact, um, let's just add something to... Let's see if I can do this. If I do, uh, high-performance SQLite.test, I am logged in, so I should be able to do checkout instant nine, and that... Look at that. That's kinda cool. Honestly, that's kinda cool, right? Um, so that just did a couple of things, but you'll see it took me to Stripe with high leverage rails in my cart, right?
So now I'm ready to check out this product. This particular price, in fact, is there. My email has been pre-filled. This is smooth. I, I mean, I, I made it. Um, I didn't make Stripe, but I made the system, so I'm kinda biased, but I feel like this is really smooth. So, um, we're now on Stripe. We're ready to pay.
Let's bail on that cart and come look at... Now we can look at the checkouts. So if we look at the checkouts, we can see a couple of things. We see a session ID, which everyone, upon visiting the website, uh, gets their own unique session ID, and then we've got the Stripe ID. So we have created, on Stripe, we have created a payment link on Stripe, and now we can reference it on Stripe, um, by that payment link ID, and
this is the checkout, uh, URL. So when you create a payment link on Stripe, uh, they give you a unique URL that you can send your user to, and they can pay over there, and you'll see it is in draft mode. Client reference ID is a way that you can link them up between Stripe and yourself, which is kinda nice, and that's it. So this is the checkout.
Now, if we were to look at the corresponding checkout lines for this guy, you'll see here, look, we've got, um, we've got the checkout ID as one. That has not... The Stripe ID has not yet been filled, um, because we haven't actually gotten all of that information back from Stripe yet. This will, this will be a unique ID.
It'll be something like, uh, I think it's, like, plink item or something like that, and then a bunch of, and then a bunch of text, but that'll, that'll be filled out later. This is the Stripe price ID, which is a reference to our price, which is that Stripe product ID number nine, um, but that is the $129 version of, uh, high leverage rails.
And then you'll see over here, the entitlements were copied to this checkout line, and this is another great thing about using JSON, is you can just pass this blob over, and now this represents a point in time. When they bought this product, this is what they were entitled to. If we change it in the future, we, uh, the entitlements in the future, we don't have to worry about, oh, is this gonna kick off some sort of back propagation event that goes through and, like, grants access to all of these things?
No. No. No. No. It's just a single standalone blob that holds all of the entitlements, and we can see this has not yet been processed, which is nice. So we can go line by line for everyone's checkouts, and we can process all of their entitlements. Are we having fun yet or what? I don't know, I don't know how long we're into the video, but it's gotta be a long way into the video, and we haven't even...
I don't, I don't... Maybe we are close. Who knows? You can see. You can see how long the video is and see if we're close. Okay, so we've got, uh, I feel like we've got a pretty good model here. We actually, in fact, kinda have a firewall here as well. Um, this is still, you know, all our database, but then we've got, uh, Stripe over here. We can turn Stripe blue 'cause they, their brand color is blue.
So our database is over here. We'll make this firewall just a little bit cleaner. Um, so yeah, what we've done is we've separated kind of, like, the checkout from the actual fulfillment, and so you can see, even if nobody checks out or, like, goes through the payment flow, we can still generate enrollments manually, you know, if somebody writes in or whatever.
So I'm feeling pretty good about this, and I think this is, um, I think this is the correct data model. We've talked about how checkout lines end up here as well, um, because when they add a price to the cart, we copy those entitlements up there so that when they get back, we can go ahead and fulfill those entitlements.
I think the last thing, the last thing I want to show you is, like, the, um, I guess the round trip experience for coming to our site and then going to Stripe. We looked at that just, um... This is a live site. We don't need that. We looked at that just a little bit when we did checkout instant nine.
Now, we, we do have, like, a full-on, uh, cart where you can add items to the cart, but we also have some that's just like, "Hey, I just wanna... But, you know, there's only one, there's only one thing for sale on this website. Don't make me go to a cart." And the checkout instant/product ID, um, that is what accomplishes that, but we do have a cart that you can add stuff to.
One thing, um, about this, so if we go back to, what was it? Highleverage.test checkout instant. Um, one thing about this is- I'm remembering now that this is in fact the Stripe product ID, because if I exposed the Stripe price ID, so let's say that, you know, the Stripe price ID was seven eight nine, and this is some, some sort of like early access pricing, then a clever person could tell
all their friends, "Hey, the early access or the PPP price is seven eight nine." Right? And then when they come to the site, they could say, "Well, it's not on sale anymore, but I'm just gonna punch in seven eight nine, 'cause I know that's the price ID." When we do it like that, and we just give them the product ID, then we internally get to look up what is the currently active price, and we add that to the cart.
So that's why I like doing it like that. Now, let's go find, um... Well, first of all, look how pretty this is. I feel super good about this. Um, you know, we've got sites up here somewhere. Um, users, users can log in to any site with their, um, credentials 'cause it's all, you know, centrally managed.
Um, and so maybe the only one that is like super dependent is, you know, maybe courses. Um, yeah, a site has many courses. But anyway, um, I think this is, I think this is a pretty good, a pretty good model. I'm actually pretty pleased with this, and we went through, you know, we went through many, many iterations to get here.
Um, but this feels, this feels right to me. Now, what is the flow? Like what, what, what's the user flow for handling all of this? I think I have that written down. Um, this was my first attempt at it. Um, so we'll just zoom in so you can't, uh, read ahead.
This was my first attempt at it. User clicks buy on High Leverage Rails. They're directed to /buy. Our platform determines upsells and cross-sells. This is not something we've talked about yet. I need to tell you about upsells and cross-sells. That is a big part of what we're doing here. Um, so when they're directed to buy, we need to look at what's in their cart and see if there's anything else that we can offer them, right?
So maybe you're buying SQLite, and we say, "Hey, we've got a, a Rails course that is based on, uh, or uses SQLite quite heavily." Or maybe it's the other way around. You're buying the Rails course, and we say, "Hey, we've got a fundamental course on SQLite. Do you wanna buy those things together?" So we need to figure out upsells and cross-sells, which I'll show you in a second. Then, as we've seen, a Stripe checkout link is generated via API.
This is more correctly called a payment link. And so if we go back to the sandbox, um, not that one. If we go back, you'll see payment links right here. I've saved that as one of my shortcuts. And there are all of the payment links that I have been testing. And so payment links are cool because they are, um, basically, they're basically pre-configured checkout, uh, sessions.
And we set them to only be usable one time. So after a person completes a, a checkout through a payment link, it becomes deactivated. Zero payments left. This, these all have one payment left. And that's just, that's just so, um, if we generate a payment link that has some sort of, you know, heavy discount for one reason or another, only one person can use it.
So you're welcome to share that with your friends, but they're gonna get the discount, not you. So every payment link is basically a one-use cart kind of. So that is correct. A Stripe payment link is generated. They are in fact redirected to Stripe. They do in fact check out on Stripe, and they are redirected back to the platform. We process entitlements into enrollments.
So anything they're entitled to, we enroll them in the courses. Anything they're entitled to download, we create an access record. This is correct. Um, this is not exactly the fullness of it. So if we come over here, you'll see we've got another platform Stripe delineation.
And, um, this is, this is spiritually similar, but it's a little more detailed. They add items to the checkout model. The interstitial is basically like if we don't know who they are, we need to get their information before they head off to Stripe, such that when they get back, we can associate all the important stuff with them, right? So this may or may not happen.
Um, you can see here, ask for email if they're not logged in. So this is kinda, this is kind of an open case. Um, then they click pay, and the API creates the payment link, and then we update the local model with the payment link ID and the user ID, and then we redirect to Stripe with the client reference.
They enter their payment details on Stripe, and they're redirected back to the platform with the client reference. So Stripe, um, allows you to attach a client reference ID, and the whole purpose of that is so that you can reassociate stuff on your side from Stripe. Now, what we don't do is listen for the webhook.
I'm sorry. We don't listen for the webhook. The webhooks annoy me. Um, it's annoying in, it's annoying in local development to get them running, but we're gonna be redirected back with the client reference, right? And so on the off case that while they're being redirected back from Stripe, so while this is happening right here, while this part is happening, this redirect, redirect back to platform with client reference, this is the, um,
in-flight, uh, redirect. So if our server goes down, they close the browser during the redirect, this part will never, ever happen. That's fine. We have a job in the background that checks these payment links to see if, um, they have been paid.
So w- we have a background process that picks it up. We actually don't need to listen to the webhook. Maybe someday we'll do webhook, receive webhook, update status of local checkout. We're, we're just not gonna do that now. We're gonna rely on the redirect. So we look up the Stripe checkout by the... This should say ref, reference, by the reference, by the client reference, and then we update the status of the local checkout.
So we update the status from Stripe. We actually request Stripe again. We say, "Hey, I just got back. I heard that this checkout's complete. Can you verify that for me?" So even if somebody hits it, you know, hits the success URL, you can't fool me. Fool me once. Um, we'll update the status, and then we just throw away their old cart and generate a new cart, and then we fulfill the entitlements.
And so this is, this is the actual flow, um, this being, uh, this guy being optional and then these being API, going out to Stripe, coming home, and then we, uh, fulfill those entitlements. Now, what actually happens here is, um... Let's see if we can just do it.
Let's try, um, let's try high_performance_sqlite_test_checkout_instant9. Let's see what happens here. Um, let's make sure... Yeah, we are running. Yep, okay. So we're just gonna try this. Um, so we'll do the 42 dance, 424242, 4230.
Yeah, that's fine. We're gonna do all this. Aaron, that's fine. One, two, three, four, five. That's fine. All right. So we are about to pay. Now, before we do, look up here. You see Client Reference ID. This is, um, something that you can, uh, when you're creating a payment link via API, or a- actually, when you're redirecting, you can append it as this query parameter here.
And so this starts with 42 and ends with AC. So if we come over here to... What table are we in? We need to go to Checkouts, and if we scroll all the way over here, starts with 42, ends with AC. So that is, that's the one that we're dealing with right now.
So that's good so far. We're gonna go ahead and pay. Yeah, I don't need to say that 'cause it's not real. Now look, confirming with our payment gateway. So please, please ignore, uh, you know, please ignore this design. Confirming with our payment gateway. That is where we are reaching back out to Stripe to see, "Hey, did they really pay this?" Setting up your account.
The order's associated with Aaron Francis. You're already logged in. Granting access to products. Whoops, my bad, dog. Um, so it didn't actually work, but hey, this is what it should look like, and you'll see this is kind of how I mapped it out here. We check Stripe for the payment completion, then we associate with the user, or we create an account, because all we got at that point is their email.
So if they're not a user, now we create an account for them and send them a password to that email address, and then we unlock the products by processing all of the entitlements, and then we're just gonna give them a big button that says, like, "Hey, you did it. Way to go." So Steve still needs to design this, um, but this is what the success page is going to look at, look like, and you'll see success reference is 42, ends in AC.
So we can do this again, and it should just, it should just be totally idempotent. Um, it's gonna error out again. Who knows why it errored out? Um, but those, uh, entitlements were not processed. So the final thing that I want to show you is... Where were we? Um, our platform determines upsells and cross-sells.
So one of the things that we are absolutely trying to accomplish here is, um, when they add a product to their cart, we wanna say, "Do you also wanna buy this? Because we would love if you spent more money." You know? That's actually what we're after here. So one way that we are going to do that, in fact, the only way that we're going to do, do that, is we have, um, we have a new model.
Let's come up here. We have a new model called an offer. So this is, um, this is a model that stores, um... Let's see. What does it store? It stores the trigger. So when somebody adds an item or a product to their cart, what is the trigger, and then what is the target?
What is the thing that we are trying to get them to buy? So the trigger is, the trigger is a product, and the target is a Stripe price. So when they have this Stripe product in their cart at any price, then offer them this other Stripe price.
A little confusing. Let's come over here, and we'll come to... This, this part actually isn't done yet, so it'll be interesting for me to see, um, it'll be interesting for me to see what parts of this are done. Come on, wake up. Yep, there we go. Okay, so we come down here, Offers. Now, um, we don't have any offers in the database.
We can look at the schema really quickly. Uh, offers, pretty simple. Triggers, targets, replaces. We can do it this way. Um, name and description and public message, and then we have triggers and targets, which are both JSON, and whether or not the, uh, target replaces the trigger. Stay with me. Stay with me.
All right. If I go to create a new offer, I can say that this is, uh, we'll call this a SQLite upsell. Um, SQLite... Let's call this a Rails- To SQLite upsell. Rails to SQLite upsell. Um, yada, yada, yada. We'll skip replace for now.
Now, the help text that I wrote, which is very helpful to me, this offer will be triggered if any of these Stripe products are in the user's cart. It doesn't matter what price. Okay. So if they are buying Rails, they're buying High Leverage Rails, which is the name of the course. They're buying High Leverage Rails, we want to offer them, uh, let's say, uh, Rails plus SQLite standard bundle.
And in this case, if they are checking out with High Leverage Rails in their cart, we wanna say, "Whoa, whoa, whoa, whoa. Dear friend, my friend, friend, friend, come here. Come closer. Do you wanna buy this instead? Because this is a good deal. This is the standard bundle pricing. This is, in fact, less than you would pay otherwise." And if they say yes, well, they don't need the High Leverage Rails 'cause they're buying Rails plus SQLite.
So in that case, the target does replace the trigger, right? So we're saying, um, "You're buying half of a bundle. Why don't you just, why don't you just go full Monty? Buy the whole thing. Let's buy the whole bundle." And when they do, it should remove that from their cart. So we can create that offer, and then we can... I mean, we can create one where it doesn't, it doesn't replace, uh, the actual target, or I'm sorry, it doesn't replace the trigger.
So we can say SQLite plus Postgres. Let's say that you're just a database freak, and you're like, "I want it all, man," which is great. Please, go buy it all. Um, in this case, we'll say, if you are, uh, buying High Performance SQLite, then you should be offered Mastering Postgres standard pricing.
I might create a new price that's like, you know, slightly discounted price. Um, but you can imagine if you buy, if you buy, uh, let's say, How to Screencast, you may want to buy, um, any of, let's say, uh, ScreenFlow, uh, upsell.
Look at that. I already created it. Upsell. And then you might wanna buy Final Cut Pro. If you don't wanna buy ScreenFlow, you might wanna buy Final Cut Pro. And if you don't wanna buy that, you might wanna buy Premiere. So I need to add discount prices for that too. But look, now anytime a user adds How to Screencast to their cart, they're gonna get offered, uh, maybe all three of these, maybe one of these three.
Maybe if they decline one, they'll get shown another, whatever, whatever, whatever. That's fine. Um, but now this is an offer, and you can see it doesn't replace the trigger. Whenever they get, uh, How to Screencast, they'll be offered any of these, uh, other items here. Boy, oh boy. Have we done it or what? I think we've done it.
I think that's probably enough. Let me, um, let's just look at the database real fast. Is there anything here, um, is there anything here? Videos, users, Stripes, zip, zip, zip, zip, nah, we don't need any of this. Yeah, we don't need any of that. So we have, uh, we're finished.
We've finished. Um, so some of this is deployed, some of this is not, right? So we're not yet to the point where screencasting.com has, um, the Final Cut, the Premiere. By the time you're watching this, it might, because we're releasing that pretty soon. But now we have this robust, uh, we have this robust way of buying multiple different things, putting them all in the same cart, upselling, cross-selling, bundling, fulfilling products, um, selling teams, uh, multiple seats at a discounted
price. We can do it all. I think spending a lot of time modeling your data up front is necessary. I know that vibe coding is, is, like, super fun and can be really, really fast. I would still recommend, even in the vibe age, that you spend a lot of time getting your data model right.
Because if you get your data model wrong, it becomes very, very difficult to reason about everything else going forward, um, and you're gonna find yourself in, in a world of hurt. So I think data modeling is incredibly, incredibly important. I hope you enjoyed this long dive into how we're data modeling our course and e-com platform.
Um, if you have any questions, leave them below. And if you wanna see more kinda, like, um, super in-depth, long videos like this, please let me know. I know that on YouTube a lot of people like, you know, the 5 to 10-minute videos, but I feel like this is valuable. So if you like it, please let me know. I'll try to do more in the future. And if you have any questions, feel free to reach out.
I hope you enjoyed this. See ya.