Stripe | Financial Infrastructure to Grow Your Revenue

Stripe | Financial Infrastructure to Grow Your Revenue

4466 articles

Custom coupons


Private preview

Custom coupons Private preview

Implement custom logic to compute discount amounts for subscriptions and invoices.

Stripe Billing lets you configure a coupon with custom logic to compute the discount amount for subscriptions and invoices, beyond fixed percentages or amounts off. Here are some examples of what you can do with a single coupon:

  • Provide 10% off for annual subscribers, but no discount for monthly subscribers.
  • Provide 20% off, but up to a maximum of 1000 USD. Only provide this for your high-value customers (tagged using metadata).
  • Provide tiered discounts (for example, 100 USD off if more than 10 seats are purchased, otherwise 30 USD off).

Get started

To understand how custom logic integrates with the billing workflow, you can explore the Stripe-authored scripts available to Billing users in our script SDK.

You can use these scripts as a foundation to build your own logic using our scripting language. We recommend starting in a sandbox.

Use Stripe-authored custom logic

You can create a coupon with Stripe-authored custom logic by navigating to the Create a Coupon page and selecting Percentage off up to maximum type. This gives you an idea of how custom logic scripts are applied to a coupon before authoring your own script.

Create a coupon

We recommend starting this process in a sandbox.

Create a coupon with no code

You can create a coupon with Stripe-authored custom logic by navigating to the create a coupon page, and using the Percentage off up to maximum coupon type, which uses a Stripe-authored script.

Create a coupon using the API

To create script coupons with the API, refer to the table below for the script specifications.

NameIDScript definition
Percentage off up to maximumbfsimpl_61S9T2y7zlKu9YlG116S8pZoN2SQpuwOX348dhXM0R9cSee script definition
Configuration fields { max_discount_amount: { amount: number; currency: string; }, discount_percent: number; } max_discount_amount.amount: value in the currency’s minor unit max_discount_amount.currency: The three-letter ISO code for the currency discount_percent: value as percentage

For example, configuring a 20% off up to 100 USD coupon using Percentage off up to maximum looks like this:

Command Line

Select a language

cURL

Stripe CLI

Ruby

Python

PHP

Java

Node.js

Go

.NET

No results

Apply a coupon

After creating the coupon, apply it to subscriptions or invoices following the same process as for a standard coupon. See coupons for detailed instructions.

Script coupon limitations

Script coupons have the following limitations:

  • You can’t use script coupons on line item coupons.
  • You can’t stack script coupons with other script coupons.
  • You can’t use script coupons on Quote objects.
  • You can’t use script coupons on Customer objects.

Author a script

To meet specific business needs, you can create your own custom logic using our scripting language.

Verify your script

You’re responsible for verifying that your script reflects your desired functionality. Make sure not to enter any proprietary, confidential information (for example, PII), or malicious code.

Discount calculator interface

All coupon scripts must abide by the discount calculator interface defined by Stripe.

Discount calculator interface

export type DiscountCalculationFunction<C> = (
 run_context: RunContext,
 configuration: C,
 discountable_item: DiscountableItem,
) => DiscountResult;

Configuration

You can define configuration values that users must provide when they’re creating a script type coupon. The configuration values set for the coupon are passed into the discount function each time you invoke it.

Sample script configuration

export type DiscountCalculatorConfiguration = {
 max_discount_amount: {
 amount: number;
 currency: string;
 },
 discount_percent: number;
};

DiscountableItem

We pass the DiscountableItem into the discount calculator and make this data available for you to use within your script. As an example, the discountable item might look like the following. For the latest definition of the parameter, refer to our SDK. You’ll see familiar Stripe objects like Customer passed into the script. However, these objects will only contain a subset of the data that is available in the API.

DiscountableItem definition

export interface DiscountableLineItem {
 subtotal: MonetaryAmount;
 quantity?: number | null;
 period: TimeRange;
 price?: Price | null;
}

export interface DiscountableItem {
 line_items: Array<DiscountableLineItem>;
 gross_amount: MonetaryAmount;
 customer?: Customer | null;
 billing_reason?: BillingReason | null;
 subscription?: Subscription | null;
}

DiscountResult

Your script must return a discount result.

Discountable result

export interface DiscountResult {
 discount: Discount;
}

Example of returning a discount result:

Sample Discountable result (truncated)

return {
 discount: {
 amount: {
 amount: discountAmount,
 currency: item.gross_amount.currency,
 },
 },
 };

Sample Scripts

If you have access to the private preview, you can submit your custom discounting logic to the Stripe Billing team through email. The below sample scripts allow you to get a sense of the custom logic you can create, and gives you a template to start working with.

Percentage-off discount with a maximum amount

Percent-up-to-max script (truncated)

Test your script

We recommend that you test Stripe-authored logic to get familiarized with the workflow. If you have access to the private preview, we load your custom logic into a sandbox, where you can go through the necessary testing to verify that it’s working as expected. After you validate it in your testing environment, Stripe uploads your script to your production account.

Submit your script for review and testing

You can access our public SDK with tools for authoring, testing, and packaging scripts. You can submit your packaged scripts to Stripe for review.

After we approve your script, we’ll import it into a sandbox you choose. Once you verify that it works as expected, we’ll apply it in live mode.

Data validation requirements

Your script must meet these requirements to execute successfully. Violations result in a validation error.

  • Return a DiscountResult with discount. amount containing both amount (number) and currency (string) on every invocation.
  • amount must be non-negative.
  • currency must match the invoice currency.

Best practices

  • Perform a currency check before computing the discount. If currencies don’t match, return a zero discount.
  • Use Stripe-provided SDK types like PositiveMonetaryAmount and Percent in your configuration for built-in validation.
  • Test invoices with multiple line items to confirm the discount applies correctly against the gross amount.
Last verified 2026-09-24

Is this helpful?