April 25th, 2022 × #Coupons#Ecommerce#Payments
Building a Coupon Engine
Scott and Wes discuss strategies and challenges around building custom coupon logic and engines for ecommerce sites and course platforms. They share tips on implementing constraints, conditional rules, tracking, expiration, and more.
- Talking about building coupon systems for ecommerce
- Using Prismic as a headless CMS to build content with Slice components
- Using LogRocket to debug errors and monitor user experiences in web apps
- Both built custom coupon engines, challenges and strategies
- Layering custom coupon logic on top of payment provider functionality
- Supporting multiple payment methods requires custom coupon logic
- Coupon constraints like dollar or percentage discounts
- Constraints around allowing coupons on products or subscriptions
- Restricting coupons to specific products with unique codes
- Limiting number of coupon redemptions
- Incrementing coupon usage count in database
- Applying discounts to cart total vs individual items
- Setting coupon expiration dates
- Deactivating coupons to stop abuse
- Creating single-use coupons
- Allowing only one coupon per order
- Stacking student discounts with coupons
- Restricting coupons to certain countries
- Restricting country-specific coupons to those countries
- New vs existing customer coupons
- Connecting affiliate coupons
- Using referral URLs to apply automatic coupons
- Chaining conditional coupon rules
- Complex conditional logic is difficult to manage
- Can't prevent coupons from being shared
- Focus time on business not anti-piracy
- Unique coupon codes to track attribution
- Inferring metadata from coupon code format
- Applying coupons automatically via URL
- Trim whitespace and normalize capitalization
- Simplify coupon format for usability
- Helpful error messages for invalid coupons
- Make coupon input easy and clean
- Don't frustrate users during purchase
- Summary of coupon engine tips
- Example of coupon logic exploit
Transcript
Announcer
Monday. Monday. Monday.
Announcer
Open wide dev fans, get ready to stuff your face with JavaScript, CSS, node modules, barbecue tips, get workflows, breakdancing, soft skill, web development, the hastiest, the craziest, the tastiest web development treats coming again hot. Here is Wes, Barracuda,
Talking about building coupon systems for ecommerce
Scott Tolinski
Boss, and Scott CSD. Welcome to Syntax.
Scott Tolinski
On this Monday, hasty treat. We're We're gonna be talking about coupon, coupons, building a coupon engine. It's something that if you work in ecommerce or something in general, if you're working on the web now, coupons it. Are a whole thing. Now we're gonna be talking about some strategies for building a coupon engine, some ideas behind it, some do's and don'ts, it. And just some general thoughts. My name is Scott Talinski. I'm a developer from Denver, Colorado. And with me, as always, is Wes Bos.
Wes Bos
Hey.
Scott Tolinski
Coupon engines. Complicated. Lots of if statements. Lots of ifs. If coupon equals this coupon. Give discount.
Wes Bos
We are sponsored by 3 awesome companies or 2 awesome companies today, Prismic and LogRocket. Let me tell you about Prismic. Prismic is A headless CMS where you just sign up and start creating your content types. It's awesome. So they focus their CMS around this thing called Slices, which I think is really cool. So, what you do is you sign up For Prismic, you log in. You can start creating your your data types.
Using Prismic as a headless CMS to build content with Slice components
Wes Bos
But instead of creating just like pages, You can create slices. So maybe you could create a a person slice, and a person might have a name and a job title and a photo, and maybe some other information that is associated with that slice. And then once you've got these different types of slices, you can bring I'm in, and sort of it's just a page builder at that point.
Wes Bos
What I really like is that a slice on Prismic It's just a component in your application that whatever it is that you're using to to consume the API and build it out, it Works really good. I used it myself on a website about 6 months ago, and I really, really like that approach. So check it out it for yourself. Prismic.
Wes Bos
I0forward/syntax.
Wes Bos
They got a whole bunch of starters. You can check it out with any of the, Any of the starters that you wanna use, whether using React or Vue or Svelte or whatever. Thank you, Prismic, for sponsoring.
Scott Tolinski
Cool. This episode is also sponsored by LogRocket, l o g r o c k e t. Love spelling things on syntax here. LogRocket, it. Is the perfect place to see all of your errors and exceptions happen in your site with a scrubbable video replay, but not only that, understand user behavior and it. Figure out what exactly the good experiences and what the bad experiences are on your site and how you can go ahead and and fix those with us. Like I said, like, actual visibility.
Using LogRocket to debug errors and monitor user experiences in web apps
Scott Tolinski
It. It's really, really super neat. It's the modern front end tool for dealing with errors, exceptions, performance.
Scott Tolinski
Stop guessing about your digital experience, which is really I mean, that that really labels it. We we send off these these These services, these apps, we just push them out to the public. People use them and they try them. And maybe 90% of the time, you'll never Understand if a user hit a problem or why they hit a problem or where they hit slowness. But now you can understand all of that and more with LogRocket's a scrubbable video replay, one of the coolest services that we've talked about here on syntax. And you can go ahead and get 14 days for free if you sign up at logrocket.comforward/ syntax.
Scott Tolinski
Check it out. Works with all the tools you know and love, so you don't have to worry about does this play nicely with, you know, Spaghetti framework and, that'd be a good name for a framework, actually.
Scott Tolinski
Spaghetti framework and meatballs dot CSS. That'd be great. Okay. Cool. So thanks so much for LogRocket for sponsoring. Alright. Wes, coupon engines. Let's get in to coupon engines.
Scott Tolinski
I I take it you have built a coupon engine as I've also built a coupon engine. So I'm sure between the 2 of us, we have a lot of, do's and don'ts here. Yeah. Coupons are a funny thing, and I I'll I'll start off by saying is if you are building a full blown ecommerce
Both built custom coupon engines, challenges and strategies
Wes Bos
solution, you probably wanna use Either a service or the one that's built into whatever.
Wes Bos
But, like, for my course platform, I built my own coupon engine, and there's a lot that surprisingly goes into it. And I thought we would just kinda go through The ideas behind building a coupon engine and the ones that we've built ourselves.
Scott Tolinski
Yeah. I'm I'm super into that.
Scott Tolinski
And I I do actually I'll I'll kick it off by asking you. You you were talking about Stripe. Right? You use Stripe for your payment processing? Yeah. But I don't use Stripe for any of my coupon stuff. Do you? So we use Braintree, and Braintree has essentially an add ons and a discounts feature baked in, And it is fairly limited.
Scott Tolinski
So ours is kind of like a layer on top of Braintree's.
Layering custom coupon logic on top of payment provider functionality
Scott Tolinski
So we have to create a coupon in Braintree's dashboard and then create the same coupon in our database so that our site can, you know, control it just a little bit more than what Braintree's offering in terms of just like a straight up discount percentage or discount So, we we are ours is kind of it's like a layer in front of Braintree.
Wes Bos
Oh, yeah. Yeah. I so I've looked at that many times over, but the the kicker for me is that I need PayPal.
Supporting multiple payment methods requires custom coupon logic
Wes Bos
And, you're lucky with Braintree where it does pay PayPal and
Scott Tolinski
Exactly.
Wes Bos
But for myself, Stripe if I if I move any logic into Stripe, I am then limiting myself from other things like PayPal or crypto or any other payment methods that I wanna use.
Wes Bos
And at the time, when I had rolled out parity purchasing power, that was not something that Stripe offered at all. So it was just like, okay. You gotta you gotta build this thing yourself. So, I don't do any of the coupon stuff instead of Stripe. I do it all in my own course platform. Nice. So let's talk about coupons first.
Wes Bos
When you create a coupon, in your your coupon engine, you you make something. And for the longest time, my coupons were simply just a An object, in JavaScript where the the coupon itself was was the the key. And then in the object, it had all the information that we're gonna be talking about. Since it's been moved to a database, because the biggest pain in the butt with that was I had to restart my app for a coupon to Oh, yeah. Right? You change it Oh, man. To kick in. So it's it's much nicer to have them actually in the in the database yourself. But once You have a coupon.
Wes Bos
That coupon will have a bunch of metadata and what I'm gonna call constraints Mhmm.
Wes Bos
In terms of, like, what they're good for. So, obviously, the big one is how much is the coupon good for, and is that a percentage, Or is it a flat dollar amount? So you have a coupon, coupon code syntax.
Coupon constraints like dollar or percentage discounts
Wes Bos
It's good for $10 off, but you I could also make another one called Syntax 50, and it's a Syntax is a percentage based coupon where it's good for 50% off. Do you do both of those types as well? I know it's a bit different with you because you have
Scott Tolinski
recurring Yeah. Payments.
Scott Tolinski
We yeah. We have we have those constraints, And ours is just a checkbox. Like, the checkboxes is percent discount. So if you type in 10, it by default is a $10 off coupon. But then if you it. Check the is percentage discount. It'll be 10% off. So, yeah, those are our constraints. We also have constraints on whether or not this can be used on a product or a subscription.
Constraints around allowing coupons on products or subscriptions
Scott Tolinski
And furthermore, if it's used on a subscription, can it be used on even, like, sub type of subscriptions? Can be used on an individual or a yearly an annual subscription. Can it only be used on an annual subscription? So, like, for for instance, the syntax, coupon that we have for our our subscriptions can only be used on the annual subscription.
Scott Tolinski
So that's a a constraint that we have there. But giving us that full control over, you can use this only on this specific skew, only on this specific subscription or products in general or subscriptions in general.
Wes Bos
Yeah. It's it. This is where it starts to get a little ego a little bit bug eye because you're like, Oh, man. That's logicky. Yeah. There's a lot into it. So my constraints that I have on mine are, they can be Constrained to a specific course or product. So the way that I do courses and products in mine is a course is React for beginners, and a product is Rec for Beginners starter package, Rec for Beginners master package, which is all the videos, and then there is a team licenses, which which goes up from there. So I'm able to restrict it. And the way I do it is I do like, Every single course I have has a code, r f b, req for beginners.
Wes Bos
And then every single product has r f b 1, r f b 2, r f b 3, etcetera, etcetera. And there's all kinds of different, products within there. So I built mine where you can restrict it to either an entire course Or a specific product.
Restricting coupons to specific products with unique codes
Wes Bos
What else do I have on there? Number of redeems. So Sometimes I will email out and I'll say, okay, the next 100 people can get $10 off with this code, And you need to be able to limit the number of redeemed. So it's pretty simple.
Wes Bos
Every single time someone redeems a coupon, it just increments the number of redeems
Limiting number of coupon redemptions
Scott Tolinski
on that coupon by 1, you include that data on the coupon itself in the in the database table? Yeah. Yeah. It's right on. You could also
Wes Bos
Just call that info up from the database. Mhmm.
Incrementing coupon usage count in database
Wes Bos
If you needed it so you could query all coupons. But then I find it much easier just to set it. And and that way, you can also just blow the coupon out of the water and and start back at 0 if you wanna Or or if you need to increment it. One time, I didn't do enough coupons.
Wes Bos
So, like, people, 15 minutes after getting the email, it's already Use that if you get mad at that, you know, you know, as frustrating. So I'll increment it and say, oh, sorry about that. You, this is one thing I don't have to necessarily do because I don't have a concept of a cart just yet. I will be rewriting my checkout, but, if you wanna buy multiples of my product, you just have to check out Multiple times, but it would be nice to people could buy 3 products at once, 3 courses at once. Right? So you when you're building Coupon IG, you need to know, like, does this Is this coupon restricted to a specific product, or can it be used on the entire cart? Is it 20% off a product or 20% everything that you put inside of the cart oh yeah yeah those are all tricky things because we we would just do
Scott Tolinski
we would do a percentage off Yeah. That's actually tricky because what we don't do is we don't do a percentage off the the whole cart. I would guess for individual course purchases, we're only doing Dollar amounts because it does take it just straight out of the cart for us because we do have a cart, a whole shopping cart experience. So it. It's not the discount for us, I don't think, unless it's like a product sale, is ever being applied individually to one thing, but only being applied to the total cart. And, therefore, I'm pretty sure we're only doing percentage off of subscriptions, but I'm not positive about that. That's really like you said, there's so many permeations of all of this stuff, And it's not necessarily
Wes Bos
a big deal Right.
Applying discounts to cart total vs individual items
Wes Bos
For that. But if you have a, 20% off of a $40,000
Scott Tolinski
car. Yeah. You don't wanna be able to get someone use a coupon code military or something like that. I had a I had a coupon, though. Don't I get, like, you know, 5 grand off or whatever? Yeah. Yeah.
Setting coupon expiration dates
Wes Bos
Another thing I do is valid dates.
Wes Bos
So if something should be valid until, Easter is over or until the New Year, right, you can you can disable those. I have a Valid or deactivated Boolean on there as well, so you can you can just turn coupons off. That's a big one for me because what happens is you give someone a coupon, And before you know it, that thing has made its way onto all the coupon websites or it's rippled through a Discord room or whatever.
Deactivating coupons to stop abuse
Wes Bos
And and sometimes that's fine. Sometimes it's like, no. That coupon was just meant for you. I made that up I made that up real quick just for you.
Wes Bos
So in that case, I used to not have the concept of redeems as well. So in in this case, I can do, like, a single use coupon, which is really nice.
Creating single-use coupons
Wes Bos
Nice. Can they be stacked? Can you apply multiple coupons? I do nothing in that regard. One coupon per order.
Allowing only one coupon per order
Wes Bos
It is a little bit annoying sometimes when somebody wants to use a coupon, but they are also a student, and they want to Stack those together. I currently don't have an option for that because the student discount is very generous. So in most cases, I say just use whichever one is better. For us, a student discount is actually like a different space than a coupon.
Stacking student discounts with coupons
Scott Tolinski
So, like, the student discount for us is its own subscription key. So the the student subscription is its own key in its own thing, that exists, and we just don't have our coupons able to be applied to that. But, yeah, likewise, we only have 1 coupon per order, but, I guess that that sales and discounts is a little different for us. Yeah, totally.
Restricting coupons to certain countries
Wes Bos
I also do countries valid So I can I don't do that? Offer up a coupon and restrict it to certain countries. And that was important, specifically because in Iran, I often get people wanting to use the Iranian discount, but not use it within, Iran. Mhmm.
Restricting country-specific coupons to those countries
Wes Bos
So, in most cases, it's here's the coupon for the country. It can only be used in this country, And it's just 1 to 1, 1 to 1, 1 to 1. But a lot of there's a couple of countries where people have to use a VPN or they have to have a friend buy it Who's who's in the UK, but they wanna they're back in the they have to use their friend's credit card. You know? Like, there's some, like, messiness around that. So I have a little bit of logic around being able making sure that that works and is not kind of annoying.
New vs existing customer coupons
Wes Bos
Is it good for a new customer or an existing customer? That's not this is not something I've integrated into mine because I don't necessarily care, but sometimes there's, I don't know. Half off, but only new customers. You know, a lot of times people wanna do that inside of their products. So that's something to think about as well. Yeah. We don't do any of that either.
Connecting affiliate coupons
Wes Bos
What else do we have? Affiliates?
Scott Tolinski
Yeah. Sometimes the coupons Connected directly to an affiliate code. And I I think about that because we don't do that that way, but there's you know, that that tonal exercise thing I have. The way that their referral system works Is that you create a referral code, and then that referral code is essentially a coupon code for whoever buys it to get a discount. So In that regard, the coupon is acting double duty as it's probably attaching the referral to that user account at the same time it's giving a discount.
Scott Tolinski
But it's a great way to keep track of and give out.
Scott Tolinski
You know, what we do is we just end up having referral accounts, and then we give it. Our affiliates coupons to give their audience, but maybe just having them click use this link, and you'll get it for x, y, and z price is maybe a it. Pretty sweet system. Actually, I think we could, like, really easily integrate that. So maybe that's something to look into. I think I should be doing that as well, especially because if you are giving it to somebody like, let's Say,
Wes Bos
someone has a podcast and you want them to be an affiliate.
Wes Bos
Telling them to Use the link doesn't necessarily always work, but telling them to use the coupon code syntax.
Using referral URLs to apply automatic coupons
Wes Bos
And that way, you can still give the person who referred credit, and then it's it's also done via word-of-mouth. You know who does that? Is Like, a 1000 podcast sponsors.
Wes Bos
Yeah. Yeah. So that's good. Let's talk about chaining the rules together.
Wes Bos
For mine, I thankfully don't have to do this. I don't either. Yeah. But for a lot of times You need to like we talked about the restrictions.
Chaining conditional coupon rules
Wes Bos
You can chain them together. People have built entire UIs, Where you can chain ifs and ors together or ifs, ands, and ors. So you can say if the customer Is, new and or, like, if the customer's email address ends in dotharvard.edu And, they're buying the course React for Beginners or the customer's email address ends in yale.edu and, like, something else. You know, like, you could do you could chain them together, and that gets a really, really I I deal with this in my email software a lot 2 when I'm making complex rules, because you just get it. You have to keep it all in your head in terms of how it's all working and whatnot.
Complex conditional logic is difficult to manage
Wes Bos
Oh, another thing with the affiliate is, I tell my affiliates not to put the coupons on their websites, because what I find is that before somebody buys something, the the first thing they do is they just Google West Boss coupon code. Yep. Yep. And then They'll go to the website and, like, it's it's kind of like a ingenuous like, if I have somebody on the website ready to buy, Then and then they just go and look for a coupon code, and it makes you click the link, and then it and then it tags that sale as their affiliate. It's not really their affiliate. All they did was just, like, SEO game, the fact that it was an affiliate. So I tell my affiliate, you can't just, like, Like, you can you can give it in your email or whatever, but don't put them on, like like, retail me not or something like that or, like, your own personal blog because then
Scott Tolinski
You're getting affiliates for, like, nothing really. Yeah. I I I don't say this to be cool, but I don't really care.
Can't prevent coupons from being shared
Scott Tolinski
Like, I'm not, like, trying to be, like, oh, blah. But I don't I guess I don't care if they get posted to that stuff because I don't know how much you can stop that, you know, there's there's just like something in there, like, piracy of our courses. You know, people email me all the time. I found your courses on this This, this website, actually, I got a really funny DM the other day. Somebody's like, hey. I, just so you know, you're exposing this paid font in a Repo, and I didn't realize I was exposing a paid font in a repo.
Scott Tolinski
And they're like, your your your repo has been, like, posted on some it. Something like hackers for like, I really wanna get this font. Where can I get it? Oh, check out Scott Kalinski's repo. And I pulled it down, obviously, like, immediately, and I had no idea. But it was like, I don't know. You just how much that stuff you can stop. If if it if it's out there, people are gonna people are gonna post it. You can have
Wes Bos
Safeguards in place, but you cannot ruin your day trying to chase around and and enforce that because at the end of the day, your your time is better spent making content In in furthering your business rather than trying to just protect it. You know? It's it's it's
Focus time on business not anti-piracy
Scott Tolinski
you're not gonna win that game ever. Yeah. You're going to end up like Metallica. Totally.
Wes Bos
Another thing is, you can do, it. Like tracking coupons, which is kinda interesting where, like, let's say you wanted to You wanted to give a $10 off coupon to 30 people. Conference attendants.
Wes Bos
Yeah. Conference attendants. And every single person gets their own unique 1, you can just make, like, let's call it Reactathon. You could say, like, Reactathon 10 dash a, b, c 123. Mhmm. And that that dash, you can you can infer the details of the coupon from the first bit of that coupon, And then you can use the additional stuff after the dash as a unique identifier if you need to be able to track, like, okay, this one This 1 talk brought in 6 sales, whereas the other talk brought in none.
Unique coupon codes to track attribution
Wes Bos
And you could just make multiple coupons, but if you need to make 100 of them, It's kind of nice to be able to, to do it in that regard where it just starts with the coupon and then the rest of it is metadata. Yeah. Very clever. It. Very special match, actually. Yeah. Automatically apply coupons. This is, like, I don't do. I'm curious if you do it. So if you wanted to sorry. I was just thinking about, like, the affiliate link stuff that we are talking about. This would be a good use case for that if they had an affiliate link and they used a
Applying coupons automatically via URL
Scott Tolinski
a query parameter being passed into the URL, you Drop it in as a cookie, and maybe that cookie could be used to apply some sort of automatic discount.
Wes Bos
We don't do it, but seems cool. It's something I probably will do as part of my checkout because then you don't the person doesn't have to go and find it or copy and paste it.
Wes Bos
Or like the thing a problem I had many years ago is people would copy paste it, but they have a space in the front of it, And then it wouldn't get applied. And it's like it's a simple just trim it.
Wes Bos
But there's just little things like that. And It's actually really funny. I should have run some stats as I can see what coupons people try unsuccessfully, but still go through. So, like, I've run stats just like free ship is the big one or, like, West Boss or love you or something like that. Like, what people type into the you know, I do it all the time. I whenever I'm checking out, I'm like, alright. Let's try a free ship or let's try, I've never done that That's very funny. Oh, no. Oh, man. I do it all the time. It never works. But that's funny. That's very funny.
Trim whitespace and normalize capitalization
Scott Tolinski
Okay. Let's talk about some other little tips here. I have some Some quick tips, especially if you're building this thing yourself. Normalize to upper or lowercase, because users will type so do it 2 ways. Normalize the input. Ain't nobody wanna have to worry about if the first letter is capitalized by their phone's keyboard on the coupon thing. Right? So in the UI, have that be normalized to all caps or all lowercase, whatever. So they can't even type in something that's not. But, also, before you check to see if the coupon exists server side, it. Just run that thing to all lowercase or all uppercase. Make sure your ones in the database are the same. Make sure they're all the same. People will goof that up left and right. And, likewise, don't try to get fancy with 2 word stuff, hyphens, underscores.
Simplify coupon format for usability
Scott Tolinski
No. Thank you. If it's syntax 30, do just syntax Thirty. Don't do syntax hyphen 30. That ain't gonna work out either. So,
Helpful error messages for invalid coupons
Wes Bos
just make it as easy as possible for the user. Yeah. I've I've run into that many times. Just make it as nice and clean as you possibly can.
Make coupon input easy and clean
Wes Bos
Otherwise, people will goof it up. And even then, I get people, do spelling mistakes.
Wes Bos
What I found helpful there is good error messages if something is expired.
Wes Bos
Like like, you put in a thing and it's just this invalid coupon. Mhmm. And you're just like, what the hell? This this email says it's good. Right? But if you can say, that coupon is no longer valid or it's been deactivated after these dates, that's really helpful because the last thing you want to do is make your
Scott Tolinski
users mad when they're trying to buy your product, right? The last thing you want to do is make your users Matt. Definitely.
Don't frustrate users during purchase
Wes Bos
Other than that, I think I've that's all the tips I have. You have anything else to add? No. I think we nailed it. Coupons.
Summary of coupon engine tips
Wes Bos
It's a it's a whole thing. Yeah. It it is a whole thing. That's a that's a good way. And I would love to to talk to somebody one day who has Built an entire coupon for, like, like, a online car or something like that. You know? Or, like, a coupon that is is good for 10% off up to a maximum of $30. You know, that that's that's both. Right? It's percentage based, but it's also, Like, a flat dollar amount. That's something you could do as well. There's just it's endless, the coupon logic. And every now and then you hear about, like, a couple of months ago, there was an Abercrombie coupon where somebody on TikTok figured out that you can get 90% off Everything at Abercrombie.
Wes Bos
And there is just, like, all kinds of people on TikTok buying 1,000 of dollars worth of code Oh, that's funny. Of of clothes. And then, like, Abercrombie, they've they pushed they shipped the orders, but, like, you know, somewhere there's a freaking poor developer Being like like you just lost the company $400,000 in in sales, you know, like, oh, that that kills me as there's so many edge cases and People will try to exploit that and figure out the the sneaks that sneaks around it. Yeah. That was a whole plot of, a movie, not digital coupons.
Example of coupon logic exploit
Scott Tolinski
What, Punch Drunk Glove with Adam Sandler is like an, you know, serious film that he did. But that was, like, the whole thing was that there was, like, some, Essentially, like a flaw in their coupon logic. And it wasn't a computer based thing, but it was just like a actual coupons. And then he used enough of them to be able to, like, Take a flight in a vacation somewhere for free, essentially, on this company, and it was like, he they they didn't wanna honor it. But this is like, I mean, that's on the you you have the rules right here. I'm following the rules.
Wes Bos
So yeah. Yeah.
Wes Bos
Yeah. Oh, that's great. Coupons. Alright. Tweet us your thoughts on coupon engines. We'd love to hear it at syntaxfm.
Wes Bos
Other than that, we'll catch you on Wednesday. Peace. Peace.
Scott Tolinski
Head on over to syntax.fm for a full archive of all of our shows, And don't forget to subscribe in your podcast player or drop a review if you like this show.