# Discount calculation

Configure Stripe-authored scripts to customize discount calculation behavior.

### Interested in getting early access to customize coupons with scripts?

Enter your email to request access.

```bash
curl https://docs.stripe.com/preview/register \
  -X POST \
  -H "Content-Type: application/json" \
  -H "Referer: https://docs.stripe.com/billing/scripts/stripe-authored/discount-calculation" \
  -d '{"email": "EMAIL", "preview": "scripts_preview"}'
```

Use the following scripts to customize how discounts are calculated for custom coupons. You configure these scripts when you create a coupon in the Dashboard, without writing code.

You can review the implementations in the [Stripe Coupon Extensions repository :external: link-to-coupon-extensions](https://github.com/stripe/coupon-extensions).

> You’re responsible for making sure your configuration is correct. Stripe doesn’t take responsibility for any issues that arise as a result of your configuration.
> 
> In preview, if customization fails for any reason or causes harm, Stripe reserves the right to default to standard functionality and/or pause subscription processing. To understand the default behavior, see the descriptions of each [available script](https://docs.stripe.com/billing/scripts/stripe-authored.md#use-cases).

## Percent off up to maximum 

This script applies a percentage discount to the invoice total and caps the discount at a maximum monetary amount. The maximum amount must use the invoice currency. If the currencies don’t match, Stripe applies no discount.

Use this script to offer a percentage discount while limiting the maximum discount exposure on an invoice.

### Configuration options 

In the Dashboard, go to the [**Create a coupon**](https://dashboard.stripe.com/coupons/create) page.

1. Select **Percent off up to maximum**.
2. Set the **Percentage discount**, which is the percentage of the invoice total to discount.
3. Set the **Maximum discount**, which is the largest monetary amount the coupon can discount.
4. Select **Save**.

### Example 

Configure the script for 20% off, up to a maximum of 50 USD.

**Discount below the maximum**

- Invoice total: 200 USD
- Percentage discount: 40 USD
- Discount applied: 40 USD
- Customer pays: 160 USD

**Discount reaches the maximum**

- Invoice total: 400 USD
- Percentage discount: 80 USD
- Discount applied: 50 USD
- Customer pays: 350 USD

## BOGO and quantity promos 

This script supports buy one, get one free (BOGO) and other quantity promotions by letting you configure the number of units a customer must purchase and the number of additional units they can receive for free.

It selects products using an exact metadata key and value, combines quantities when the same product appears on multiple invoice lines, and discounts the cheapest eligible units first. Allows targeted quantity discounts for specific products with no purchase requirement when **Purchase quantity** is set to 0.

Use this script for quantity-based promotions on selected products.

### Configuration options 

Before creating the coupon, add the metadata key and value to each eligible product.

In the Dashboard, go to the [**Create a coupon**](https://dashboard.stripe.com/coupons/create) page.

1. Select **BOGO and quantity promos**.
2. Set the **Purchase quantity**, which is the number of units the customer must purchase before free units are available. Can be 0.
3. Set the **Free quantity**, which is the maximum number of additional units that can be free.
4. Set the **Product metadata key** and **Product metadata value** that identify eligible products. Metadata matching is exact and case-sensitive.
5. Select **Save**.

### Example 

An eligible product costs 20 USD per unit. Set the **Buy quantity** to 3 and the **Free quantity** to 2.

- Buying 3 units applies no discount.
- Buying 4 units discounts 1 unit, for a 20 USD discount.
- Buying 5 or more units discounts 2 units, for a maximum 40 USD discount.

The buy quantity and free quantity apply independently to each eligible product.

## Price-targeted amount off 

This script applies a fixed monetary discount once to each invoice line item whose price has an exact metadata key and value. Stripe caps each line’s discount at its subtotal and caps the combined discount at the invoice total. The configured amount must use the invoice currency.

Use this script to take a fixed amount off selected prices without discounting unrelated invoice items.

### Configuration options 

Before creating the coupon, add the metadata key and value to each eligible price.

In the Dashboard, go to the [**Create a coupon**](https://dashboard.stripe.com/coupons/create) page.

1. Select **Price-targeted amount off**.
2. Set the **Fixed amount off**, which is the amount to discount from each matching line item.
3. Set the **Price metadata key** and **Price metadata value** that identify eligible prices. Metadata matching is exact and case-sensitive.
4. Select **Save**.

### Example 

Configure a 10 USD discount for prices with matching promotion metadata. An invoice contains matching line items with subtotals of 50 USD and 8 USD, plus a nonmatching line item of 30 USD.

- First matching line discount: 10 USD
- Second matching line discount: 8 USD, capped at its subtotal
- Nonmatching line discount: 0 USD
- Total discount: 18 USD

## Price-targeted percent off 

This script applies a percentage discount to each invoice line item whose price has an exact metadata key and value. Stripe adds the discounts from all matching lines and applies the combined amount to the invoice.

Use this script to apply a percentage promotion to selected prices without discounting unrelated invoice items.

### Configuration options 

Before creating the coupon, add the metadata key and value to each eligible price.

In the Dashboard, go to the [**Create a coupon**](https://dashboard.stripe.com/coupons/create) page.

1. Select **Price-targeted percent off**.
2. Set the **Percentage off**, which is the percentage to discount from each matching line item’s subtotal.
3. Set the **Price metadata key** and **Price metadata value** that identify eligible prices. Metadata matching is exact and case-sensitive.
4. Select **Save**.

### Example 

Configure a 25% discount for prices with matching promotion metadata. An invoice contains matching line items with subtotals of 80 USD and 20 USD, plus a nonmatching line item of 40 USD.

- First matching line discount: 20 USD
- Second matching line discount: 5 USD
- Nonmatching line discount: 0 USD
- Total discount: 25 USD

## See also

- [Stripe-authored scripts](https://docs.stripe.com/billing/scripts/stripe-authored.md)
- [Customize discount calculations](https://docs.stripe.com/billing/scripts/discount-calculation.md)
- [Stripe Coupon Extensions repository :external: link-to-coupon-extensions](https://github.com/stripe/coupon-extensions)
