# Coupons

A coupon is a code a buyer types to pay less: a percent or a fixed amount off, with the
limits you set (how many times, until when, from what order size, for which products or
customers). Create and manage coupons from your server; buyers redeem them wherever they
can enter a code with us.

```
POST   https://api.worthypayments.com/v1/coupons
GET    https://api.worthypayments.com/v1/coupons
GET    https://api.worthypayments.com/v1/coupons/{id}
POST   https://api.worthypayments.com/v1/coupons/{id}
DELETE https://api.worthypayments.com/v1/coupons/{id}
```

| Scope | Grants |
|-------|--------|
| `coupons:read` | List and fetch coupons, look a code up |
| `coupons:write` | Create, update and delete coupons |

Both scopes are server-only: a publishable key is refused, so a coupon cannot be minted,
widened or listed from a browser. The coupons you create here are the same coupons as the
dashboard's **Products → Coupons** page, in the key's mode (a test key sees test coupons).

**Where a buyer redeems a coupon**

| Surface | How the coupon gets there |
|---------|---------------------------|
| Hosted checkout, payment links, store carts | The buyer types the code. |
| Subscriptions | Your server passes the coupon `id` as `coupon` on `POST /v1/subscriptions`. See [Subscriptions](./18b-subscriptions.md). |
| Your own checkout on the [Payment Element](./08b-payment-element.md) | Your server applies it to the payment intent (`discounts`), or lets the buyer enter a code in the Element (`allow_promotion_codes`). See [On a payment intent](#on-a-payment-intent). |

Related: [Products](./05-products.md), [Idempotency](./12-idempotency.md),
[Pagination](./13-pagination.md).

---

## The coupon object

```json
{
  "id": "3b9d6c1e-7a52-4f0e-8c3d-1e2f4a5b6c7d",
  "object": "coupon",
  "code": "PARTNER-ANA",
  "name": "Partner Ana",
  "description": null,
  "percent_off": 15,
  "amount_off": null,
  "currency": "usd",
  "duration": "once",
  "duration_in_months": null,
  "max_redemptions": 500,
  "max_redemptions_per_customer": 1,
  "times_redeemed": 42,
  "minimum_amount": null,
  "maximum_discount_amount": null,
  "applies_to": [],
  "customers": [],
  "first_time_order_only": false,
  "starts_at": null,
  "expires_at": 1798761599,
  "active": true,
  "valid": true,
  "livemode": false,
  "created": 1796385600,
  "updated": 1796385600
}
```

| Field | Notes |
|-------|-------|
| `id` | The coupon id. Pass it as `coupon` on `POST /v1/subscriptions`, or in `discounts` on a payment intent. |
| `code` | What the buyer types. Stored upper-cased and matched case-insensitively: `partner-ana` and `PARTNER-ANA` are one code. Unique among your live coupons in this mode. |
| `percent_off` / `amount_off` | One of the two is set; it is the coupon's kind. `percent_off` is a percent (`15` takes 15 % off). `amount_off` is integer minor units of `currency`, and never takes off more than the order. |
| `currency` | The currency of every amount on the coupon, lowercase. Fixed at create. |
| `duration`, `duration_in_months` | For subscriptions: `once` discounts the first invoice, `repeating` the first `duration_in_months` months, `forever` every invoice. A one-off payment uses the coupon once whatever this says. |
| `max_redemptions` | Total redemptions allowed. `null`: no limit. |
| `max_redemptions_per_customer` | Redemptions allowed per customer. Counted only where the buyer is a known customer; a guest checkout is not counted against it. `null`: no limit. |
| `times_redeemed` | Redemptions so far. |
| `minimum_amount` | The smallest order the coupon applies to, in minor units. `null`: no minimum. |
| `maximum_discount_amount` | A cap on what a `percent_off` coupon takes off. Always `null` on an `amount_off` coupon. |
| `applies_to` | Product or product category ids. When set, only those lines are discounted and an order with none of them is refused. `[]`: the whole order. |
| `customers` | Customer ids (`cus_...`). When set, only these customers may redeem it. |
| `first_time_order_only` | Only for a customer with no earlier successful order. A buyer we cannot identify as a customer is refused. |
| `starts_at`, `expires_at` | Unix seconds, or `null`. Not redeemable before `starts_at` or after `expires_at`. |
| `active` | The on/off switch you control. |
| `valid` | Whether a buyer could redeem it right now, cart aside: active, inside its window, and under `max_redemptions`. The cart's own rules (minimum, products, customers, first order, per-customer limit) are checked when the code is applied. |

---

## Create

`POST /v1/coupons`. `Idempotency-Key` is **required**: a retried create would otherwise try
to make a second coupon.

```bash
curl https://api.worthypayments.com/v1/coupons \
  -H "x-api-key: sk_test_xxx" \
  -H "Idempotency-Key: affiliate-8812-coupon" \
  -H "content-type: application/json" \
  -d '{
    "code": "PARTNER-ANA",
    "name": "Partner Ana",
    "percent_off": 15,
    "max_redemptions": 500,
    "max_redemptions_per_customer": 1,
    "expires_at": 1798761599
  }'
```

| Field | Required | Notes |
|-------|:---:|-------|
| `code` | Yes | 2 to 64 letters, digits, dashes or underscores. |
| `percent_off` | One of the two | Above 0 and at most 100, at most two decimals. |
| `amount_off` | One of the two | Integer minor units, at least 1. |
| `currency` | No | Three-letter ISO code. Defaults to `usd`. |
| `name` | No | Defaults to the code. At most 200 characters. |
| `description` | No | At most 500 characters. |
| `duration` | No | `once` (default), `repeating` or `forever`. |
| `duration_in_months` | With `repeating` | Refused with any other `duration`. |
| `max_redemptions`, `max_redemptions_per_customer` | No | Integers, at least 1. |
| `minimum_amount` | No | Integer minor units. `0` is no minimum. |
| `maximum_discount_amount` | No | Only on a `percent_off` coupon. |
| `applies_to`, `customers` | No | Up to 100 ids each. |
| `first_time_order_only` | No | Boolean. |
| `starts_at`, `expires_at` | No | Unix seconds. `expires_at` must be in the future and after `starts_at`. |

A new coupon is active. A field the route does not know (`metadata`, `active` on create) is
refused with `parameter_unknown`, never ignored.

### One code per affiliate

When a partner signs up in your affiliate program, create their code from your backend and
keep the returned `id` beside the partner:

1. Pick the code (for example from the partner's handle) and check it is free:
   `GET /v1/coupons?code=PARTNER-ANA` returns an empty `data` list when no live coupon has it.
2. `POST /v1/coupons` with an `Idempotency-Key` tied to the partner (such as
   `affiliate-<partner id>-coupon`), so a retry after a timeout returns the same coupon
   instead of a `409`.
3. Store the coupon `id`. A `409` `already_exists` means another coupon took the code
   first: pick another code.

When the partnership ends, set `active: false` (the code stays reserved and the history
stays readable) or delete the coupon (the code becomes free for someone else).

---

## List and look up

`GET /v1/coupons`, newest first. Deleted coupons are never listed.

| Query | Notes |
|-------|-------|
| `limit` | Page size. Defaults to 10, capped at 100. |
| `starting_after` | The `next_cursor` of the previous page. |
| `active` | `true` or `false`. Omit it to list both. This is the switch, not `valid`: an active coupon may still be expired or used up. |
| `code` | An exact code, case-insensitive. Returns that coupon alone, or an empty list. |

```bash
curl "https://api.worthypayments.com/v1/coupons?code=partner-ana" \
  -H "x-api-key: sk_test_xxx"
```

`GET /v1/coupons/{id}` returns one coupon. Another account's coupon, the other mode's, or a
deleted one is `404` `resource_missing`.

---

## Update

`POST /v1/coupons/{id}` changes only the fields you send. It takes the create fields
except `code` and `currency` (both fixed: sending either is refused), plus `active`.
`Idempotency-Key` is optional.

```bash
curl https://api.worthypayments.com/v1/coupons/3b9d6c1e-7a52-4f0e-8c3d-1e2f4a5b6c7d \
  -H "x-api-key: sk_test_xxx" \
  -H "content-type: application/json" \
  -d '{ "max_redemptions": 1000, "expires_at": 1801439999 }'
```

The body is checked against the stored coupon: `duration: repeating` needs
`duration_in_months` unless the coupon already has it, `maximum_discount_amount` is refused
on an `amount_off` coupon, and `expires_at` must stay after `starts_at`. Sending
`percent_off` to an `amount_off` coupon (or the reverse) changes its kind.

**Removing a limit**

| To remove | Send |
|-----------|------|
| `max_redemptions`, `max_redemptions_per_customer`, `minimum_amount` | `null` |
| `maximum_discount_amount` | `null` |
| `starts_at`, `expires_at` | `null` |
| `applies_to`, `customers` | `[]` |
| `description` | `null` or `""` |

A removed `starts_at` no longer holds `expires_at` back, so `{ "starts_at": null,
"expires_at": ... }` may set an end before the old start. To stop a coupon now, send
`active: false`.

A change applies to every redemption from now on. Payments already completed keep the
discount they were given.

---

## Pause or delete

| | `active: false` | `DELETE /v1/coupons/{id}` |
|---|---|---|
| Buyers can redeem it | No | No |
| Listed and retrievable | Yes | No (`404`) |
| The code | Stays reserved | Free for a new coupon |
| Undo | `active: true` | No |

Delete keeps the record behind the scenes (payments that used the coupon stay explained);
it answers `{ "id": "...", "object": "coupon", "deleted": true }`.

---

## On a payment intent

A [payment intent](./08b-payment-element.md) takes a coupon in one of two ways. Pick one per
intent; sending both is refused.

**Your server applies it.** Name the coupon by `id` or by `code` when you create the intent,
or later with `PATCH`:

```bash
curl https://api.worthypayments.com/v1/payment_intents \
  -H "x-api-key: sk_test_xxx" \
  -H "Idempotency-Key: order-1234" \
  -H "content-type: application/json" \
  -d '{ "amount": 10000, "currency": "usd", "customer": "cus_...",
        "discounts": [{ "promotion_code": "PARTNER-ANA" }] }'
```

```json
{
  "id": "pi_...",
  "amount": 8500,
  "amount_subtotal": 10000,
  "amount_details": { "discount_amount": 1500 },
  "allow_promotion_codes": false,
  "discounts": [{ "coupon": "3b9d6c1e-7a52-4f0e-8c3d-1e2f4a5b6c7d", "applied_by": "merchant" }],
  "client_secret": "pi_..._secret_..."
}
```

`amount` is what the buyer is charged. The amount you send is the subtotal the coupon comes
off. A `PATCH` with a new `amount` keeps the coupon and takes it off the new subtotal; a
`PATCH` with `discounts: []` removes it.

**The buyer enters a code.** Create the intent with `allow_promotion_codes: true`. The Payment
Element then draws an "Add promotion code" field and shows the discount once a code applies;
an intent without it draws no field. Or keep your own input, mount with
`fields: { promotionCode: 'never' }`, and call `element.applyPromotionCode(code)` /
`element.removePromotionCode()`. The Element reads the
new total from the intent; your page never sends an amount. Without
`allow_promotion_codes: true` on the intent nothing a buyer's page sends can add a code to it
or remove the one your server applied.

**What is checked.** The coupon must be yours, in the key's mode, and redeemable now
(active, inside its window, under `max_redemptions`). Then, against the payment:

| Rule | Why |
|------|-----|
| An `amount_off` coupon, or one with `minimum_amount` / `maximum_discount_amount`, must be in the intent's `currency`. A plain `percent_off` works in any currency. | Its amounts mean nothing in another currency. |
| A coupon with `applies_to` set is refused. | An intent has one amount, no products to match. |
| `minimum_amount`, `customers`, `first_time_order_only` and `max_redemptions_per_customer` are checked against the intent's `customer`. | Give the intent a `customer` to use a coupon limited to customers. |
| The discounted amount must stay at least 50 minor units. | The same minimum as any charge. |

Your server's refusal says why (`coupon_invalid` with the reason, or `resource_missing`). A
buyer's code that does not apply always reads `promotion_code_invalid`, whatever the reason,
and the Element allows 10 tries per intent every 10 minutes: the answer never tells a
guesser which codes exist.

**When it counts.** The coupon is redeemed once, when the payment succeeds (`times_redeemed`
goes up then, not when the code is applied). The charge's webhooks carry it as
`detail.discount` ([Webhooks](./16-webhooks.md#the-detail-block)); the charge's `amount` is
already the discounted total.

---

## Errors

| HTTP | `code` | When |
|------|--------|------|
| `400` | `idempotency_key_required` | Create without an `Idempotency-Key` |
| `400` | `parameter_invalid` | A field fails the rules above. `param` names it |
| `400` | `parameter_unknown` | A field this route does not take |
| `403` | `permission_denied` | The key lacks `coupons:read` / `coupons:write`, or is publishable |
| `404` | `resource_missing` | No such coupon for this key (`param: id`) |
| `409` | `already_exists` | Another live coupon of yours in this mode has the code (`param: code`) |
| `409` | `idempotency_in_progress` | The same `Idempotency-Key` is still running |

See [Errors](./10-errors.md) for the envelope.
