Dashboard

Add Discount Codes to an AI-Built App Safely

Add discount codes to an AI-built app without losing margin: coupon vs promotion code, five limits to set, and a stacking trap shown with real numbers.

Steve Jefferson
Steve Jefferson
Developer Advocate
11 October 20261 min read

To add discount codes to an AI-built app without losing margin, decide five limits before you ask the agent to build anything: who can use a code, how many times, for how long, for which products, and whether it stacks with other offers. Left unspecified, an AI will happily build a box where anyone types SAVE50 forever, on anything, as often as they like, and that is how a launch promotion becomes a permanent price cut.

If your payments run through Stripe, most of the machinery already exists, and the job is configuring it correctly rather than coding discount logic by hand.

Two different things called a discount

Stripe's coupons and promotion codes documentation draws a distinction that saves a lot of confusion. A coupon defines the discount logic and is backend-driven: your system decides who gets it and when. A promotion code is a customer-facing code that wraps around a coupon, so the customer decides when to apply it. Many promotion codes can point to one coupon, and promotion codes inherit the product restrictions of their parent coupon.

That gives you a clean split for the agent:

  • A negotiated deal for one client: apply a coupon directly from your backend. The customer never types anything.

  • A marketing campaign: create a promotion code, such as a launch code, that customers enter at checkout.

Tell the agent which one you mean. Asking for "discount codes" without saying so is how you end up with a hand-built system that duplicates what the payment provider offers.

How to add discount codes to an AI-built app: the five limits

  1. Who. Promotion codes can be limited to a specific customer, or to first-time orders only. Per the docs, the first-time restriction blocks customers who already started a payment or subscribed to a trial, even if it was later canceled.

  2. How many times. max_redemptions caps total uses across all customers. A code shared on a coupon site with no cap is the classic margin leak.

  3. Until when. A promotion code can have an expiry, and it cannot be later than its coupon's. Set one on every public code.

  4. Which products. Restrict the coupon to the products it is meant for. If a subscription has no eligible products, no discount applies.

  5. For how long. A coupon's duration is once, repeating for a set number of months, or forever. The default is once, which covers only the first invoice. Choose deliberately: "forever" is rarely what a launch promotion should mean.

Two traps in the docs are worth knowing. A coupon set to last forever, combined with an expiry date, still gives the discount forever to anyone who received it before the date; the expiry only stops new people applying it. And a once coupon is removed from the subscription once the invoice finalizes, so a subscription can look discount-free even though the discount was applied. If your admin screen reads "no discount" for a customer who got one, that may be why.

The stacking trap

Stripe supports multiple discounts on one subscription, up to 20, and the order matters when you mix percentage and fixed amounts. On a plan priced at 100:

Order

Calculation

Customer pays

20% off, then 5 off

100 - 20 = 80, then 80 - 5 = 75

75

5 off, then 20% off

100 - 5 = 95, then 95 x 0.8 = 76

76

One unit apart is small, but across thousands of customers and several stacked offers it adds up, and your price page and support replies should agree on the order. The simplest policy is no stacking: one code per order. The documentation lists restrictions too, such as that you cannot pass a coupon and a promotion code created from the same coupon together.

A brief for the agent

Add discount code support to checkout using Stripe promotion codes.
Do not build custom discount math. Use allow_promotion_codes on the
Checkout Session. Create the codes in the Stripe dashboard, not in code.
Each public code must have: an expiry date, max redemptions, product
restriction, and first-time-customer-only where I say so.
No stacking: one promotion code per checkout.
Show the discount on the order summary and receipt.
Write a test that an expired code and an over-limit code are both rejected.

Then test with the payment provider's test mode: redeem a code twice past its limit, use an expired one, and try it on a product it should not cover. Do not accept "it works" without seeing the rejections.

Six tests worth running

Discount bugs are cheap to find in test mode and expensive in production. Run these before launch.

Test

Expected result

A valid code on an eligible product

Discount shown at checkout and on the receipt

The same code after its expiry date

Rejected with a clear message

The code after the redemption limit is reached

Rejected

A first-time-only code used by an existing customer

Rejected

A code on a product it does not cover

No discount applied

Two codes in one checkout

Whatever your no-stacking rule says, consistently

Also check what the customer sees on rejection. A bare "invalid code" is a support ticket waiting to happen; "This code has expired" or "This code is for new customers only" is not. Pass the wording to the agent as part of the brief, and see writing error messages with AI for how to keep those messages clear.

Beyond the code box

Think about the business side too. A discount trains customers to wait for the next one, and a deep first-month discount can attract people who leave when the price rises; reducing churn on an AI subscription product discusses the retention side. If the discount is really a trial, a free trial that expires may be the cleaner mechanism. When a client asks for a discount because you used AI, that is a pricing conversation, covered in when a client wants a discount because you used AI. The underlying payment setup is in adding payments to an AI-built app, and the general build path is in the guide to building an app with AI.

Frequently asked questions

What is the difference between a coupon and a promotion code in Stripe?

A coupon defines the discount and is applied by your backend. A promotion code is the customer-facing code that maps to a coupon and lets customers apply it themselves at checkout.

How do I stop a discount code being shared everywhere?

Set a maximum number of redemptions and an expiry date, and consider restricting it to first-time customers or a specific customer.

Should discount codes stack?

Usually not. Stacking makes the final price depend on order, complicates support and can cut margin more than intended. One code per order is simplest.

Can an AI coding agent build discount logic itself?

It can, but hand-built discount math is easy to get wrong. If you use a payment provider, configuring its coupon and promotion code features is safer than custom code.

How did this land?

About the author

Steve Jefferson
Steve Jefferson

Developer Advocate

Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.