> ## Documentation Index
> Fetch the complete documentation index at: https://docs.stash.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# Value Codes

> How a value code that a player applies at the Stash checkout reaches your ConfirmPayment, what to grant for it, how Stash rounds the extra, and when the checkout refuses a code.

A value code gives the player more of a bonus at the full price, for example +20% coins on any offer. It never changes the price. The player enters the code at the Stash checkout, pays, and Stash sends the code and the extra it grants in the optional `valueCode` block of [`ConfirmPayment`](https://docs.stash.gg/api/egress/purchase/confirmpayment). Your backend grants the extra on top of `bonusItems`.

A value code is not a promo code. A [promo code](https://docs.stash.gg/guides/stash-webshop/how-tos/create-promo-codes.md) discounts the price; a value code adds to the bonus and leaves the price as it is.

## Before you start

* **A Real-time Catalog integration.** The code reaches your backend only in `ConfirmPayment`. The purchase webhooks and the payment webhooks carry no code, so a shop that grants from [`PURCHASE_SUCCEEDED`](https://docs.stash.gg/guides/stash-webshop/integration#granting-from-the-webhook) cannot grant the extra.
* **The bonus in `bonusItems`.** A code adds to an in-game currency bonus that your catalog lists in `bonusItems` (see [Web Store Bonuses](https://docs.stash.gg/guides/stash-webshop/catalog/real-time-catalog/items-and-prices.md#web-store-bonuses)). A percent code applies to the bonus's quantity in the `ConfirmPayment` `bonusItems`, which includes web store bonus rows with a `guid`. Any other amount in `contents` gets no extra.
* **Value codes on for your shop.** Stash turns value codes on for your shop and agrees the rounding rule with you (see [Rounding](#rounding)).

## How a purchase with a code works

```mermaid theme={null}
sequenceDiagram
    actor Player
    participant Stash as Stash checkout
    participant Game as Your backend
    Player->>Stash: Enters the code
    Stash-->>Player: Code applied, for example +20% Coins
    Player->>Stash: Pays the full price
    Note over Stash: Freezes the code and the rounding rule into the payment
    Stash->>Game: ConfirmPayment with bonusItems and valueCode
    Game-->>Stash: 200 OK with results
    Stash->>Game: PURCHASE_SUCCEEDED, with no code
```

The code and its rewards are frozen into the payment when it is authorized. An edit to the code, or to your shop's rounding rule, after that point does not change what `ConfirmPayment` sends for that payment.

## The `valueCode` block

`valueCode` is present only when the player applied a code to the purchase. Without one, the request has no `valueCode` field.

```json title="ConfirmPayment request (excerpt)" theme={null}
{
  "transactionId": "txn_789",
  "items": [{ "productId": "coins_pack_10", "quantity": 1, "cents": 999 }],
  "bonusItems": [{ "id": "coins", "quantity": 100000 }],
  "valueCode": {
    "code": "WELCOME20",
    "rewards": [
      {
        "bonusId": "coins",
        "kind": "VALUE_CODE_REWARD_KIND_PERCENT",
        "multiplierPermille": 1200,
        "extraQuantity": 20000
      }
    ]
  }
}
```

| Field | Sent | Meaning |
| :- | :- | :- |
| `valueCode.code` | Always | The code as it is configured for your shop, not as the player typed it. Codes match without regard to case. |
| `valueCode.rewards` | Always, at least 1 | One entry per bonus that the code adds to. |
| `rewards[].bonusId` | Always | The bonus that receives the extra: an `id` from your catalog's bonus items. |
| `rewards[].kind` | Always | `VALUE_CODE_REWARD_KIND_PERCENT` or `VALUE_CODE_REWARD_KIND_AMOUNT`. |
| `rewards[].multiplierPermille` | Percent rewards | The multiplier in thousandths, not rounded: `1200` is 1.2 times, so +20%. |
| `rewards[].extraQuantity` | Percent rewards | The extra units to grant, computed and rounded by Stash. |
| `rewards[].amount` | Amount rewards | The extra units to grant. Always more than 0. Not a money amount. |

The block has no total field. The total for a bonus is its `bonusItems` quantity plus the extra in `valueCode`.

## Grant the extra

**1. Grant bonusItems as usual**

The `bonusItems` quantities are the offer's own bonus. The extra is never inside them, so nothing counts twice.

**2. Grant each reward on top**

For each entry in `valueCode.rewards`, grant more units of the bonus in `bonusId`:

| `kind` | Grant |
| :- | :- |
| `VALUE_CODE_REWARD_KIND_PERCENT` | `extraQuantity`, exactly as sent. It can be `0`, for example when the purchase holds none of this bonus; grant nothing extra then. |
| `VALUE_CODE_REWARD_KIND_AMOUNT` | `amount`, also when the purchase holds none of this bonus. |
| Any other value | Skip the entry and grant the rest of the purchase. |

`extraQuantity` is absent only when the extra does not fit in 32 bits. In that case, compute the extra from `multiplierPermille` and the `bonusItems` quantity, under the rule agreed for your shop. Otherwise grant `extraQuantity` as sent. Under the default rule, an extra that you compute from `multiplierPermille` and the `bonusItems` quantity matches it, so you can log a difference. Under another rule, for example one that rounds the new total, it can differ, and the request does not say which rule applied.

**3. Grant once per transactionId**

A retry of the same payment carries the same `valueCode` block. If you already granted this `transactionId`, return the same `results` and grant nothing again.

| Step | Example |
| :- | :- |
| `bonusItems` quantity of `coins` | 100,000 |
| Code +20%: `multiplierPermille` `1200`, `extraQuantity` | 20,000 |
| You grant | 120,000 coins |

> [!WARNING]
>
> **Do not decline a purchase because of the `valueCode` block.** Stash has already checked the code before the charge. A decline or an error from `ConfirmPayment` means Stash does not capture the payment, so the player is not charged and receives nothing. See [Payment Flow](https://docs.stash.gg/guides/stash-webshop/catalog/real-time-catalog/payment-flow.md).

## Rounding

A percent reward rarely lands on a whole number: 20% of 333 coins is 66.6. Stash rounds it under the rule agreed for your shop and sends the result as `extraQuantity`, so your backend does no rounding.

| Part of the rule | Options | Default |
| :- | :- | :- |
| What is rounded | The extra, or the new total (the `bonusItems` quantity plus the extra). Rounding the new total needs a fixed step or a table. | The extra |
| Increment | A number of significant digits, a fixed step, or a table that sets the step by amount | 2 significant digits, never finer than 1 unit |
| Direction | Up, down, or to the nearest increment (an exact half rounds up) | Up |

With the default, 20% of 333 is 67 and 20% of 33 is 7. When your rule rounds the new total, `extraQuantity` is the rounded total minus the `bonusItems` quantity, or `0` when the rounded total is not above that quantity.

## Code settings

Each code is configured for your shop with:

| Setting | What it does |
| :- | :- |
| Code | The text the player enters: the letters A to Z, the digits 0 to 9, `-` and `_`. Case does not matter. |
| Rewards | 1 to 6 rewards, each a percent or a flat amount of an in-game currency bonus. |
| Offers | Every offer, or a list of offers. A code with a list applies only when every item of the order is on it, matched exactly against the item's `productId`. |
| Start and end | An optional window in which the code applies. |
| Total limit | The most purchases that can use the code, across all players. |
| Per-player limit | The most purchases that one player can make with the code. |
| Status | Draft, active, paused, or stopped for good. A paused code can be turned on again; a stopped code cannot. |

## At checkout

* **Signed-in players only.** A code belongs to the player's account, so the code field shows only to a signed-in player.
* **One code at a time.** The account holds one code per shop. A new code replaces the one held, and the player can remove it.
* **The code stays on the account.** An applied code also applies to the player's next checkouts, until it ends, is paused or stopped, the player's limit is used, or the player applies another code. Stash can set a shorter hold for your shop.
* **Attempts are limited.** A player can try 10 codes a minute in a shop. Each try counts, also one that applies a code.

The checkout refuses a code it cannot apply and tells the player why. The player can always pay without the code. Your backend never receives a refused code: the purchase either has no `valueCode` block, or stops before the player is charged and you receive no `ConfirmPayment`.

| Situation | At checkout |
| :- | :- |
| Unknown code, a draft, or value codes off for the shop | One generic message, so the field does not reveal which codes exist |
| Not started yet, ended, paused, or stopped for good | Its own message for each |
| The player's limit, or the total limit, is used up | Its own message for each |
| A code limited to other offers | Entered on this order: refused. Already on the account: this order pays without it, and the code stays for an order it covers |
| Too many attempts | The player waits, then tries again |
| The code was edited, paused or stopped after the player applied it | The payment is refused before the charge. The checkout shows the code as it is now, or drops it, and the next try pays accordingly |

## Limits

* A use counts when Stash asks your `ConfirmPayment` to grant the purchase. If your `ConfirmPayment` declines with an HTTP 4xx status and no `results`, the use does not count. After a 5xx or a timeout it stays counted, because the items may have been granted.
* The total limit can go slightly over. A player who applied the code in the current checkout keeps it at payment, even if other players used the last uses in the meantime. A player who applies it after the limit is reached is refused.
* A payment already authorized with a code completes with it, even if the code is paused, stopped or edited after that.

## Where else the code shows

* **Receipt.** The player's email receipt lists the code and its rewards.
* **Webhooks.** `PURCHASE_SUCCEEDED`, `PURCHASE_FAILED` and the payment webhooks carry no code. Store what you need from `ConfirmPayment`, keyed on `transactionId`.

## Next Steps

> [card] [ConfirmPayment API Reference](https://docs.stash.gg/api/egress/purchase/confirmpayment.md)
>
> The full request schema, with the valueCode block.

> [card] [Payment Flow](https://docs.stash.gg/guides/stash-webshop/catalog/real-time-catalog/payment-flow.md)
>
> Where the money is at each step, and what a decline does.

> [card] [Catalog Items and Prices](https://docs.stash.gg/guides/stash-webshop/catalog/real-time-catalog/items-and-prices.md)
>
> Bonus items and web store bonuses in your catalog.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.