Metadata-Version: 2.4
Name: stapel-promotions
Version: 0.1.0
Summary: Promo codes and automatic discounts over opaque purchasables for the Stapel framework
License: MIT
Project-URL: Homepage, https://github.com/usestapel/stapel-promotions
Project-URL: Repository, https://github.com/usestapel/stapel-promotions
Project-URL: Documentation, https://github.com/usestapel/stapel-promotions#readme
Project-URL: Changelog, https://github.com/usestapel/stapel-promotions/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/usestapel/stapel-promotions/issues
Keywords: django,stapel,promotions,discounts,promo-codes,coupons,ecommerce
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: stapel-core<1.0,>=0.99.3
Provides-Extra: all
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: pytest-django; extra == "test"
Requires-Dist: pytest-cov; extra == "test"
Requires-Dist: stapel-tools<1,>=0.84.0; extra == "test"
Requires-Dist: jsonschema>=4; extra == "test"
Requires-Dist: hypothesis>=6; extra == "test"
Dynamic: license-file

<!-- Generated by stapel-readme from docs/readme.md + docs/*.json. Do not edit this file; edit docs/readme.md and re-run `make readme`. -->

# stapel-promotions

[![CI](https://img.shields.io/github/actions/workflow/status/usestapel/stapel-promotions/ci.yml?branch=main&logo=github&label=CI)](https://github.com/usestapel/stapel-promotions/actions/workflows/ci.yml?query=branch%3Amain)
[![coverage](https://img.shields.io/codecov/c/github/usestapel/stapel-promotions?branch=main&logo=codecov&label=coverage)](https://app.codecov.io/gh/usestapel/stapel-promotions)
[![pypi](https://img.shields.io/pypi/v/stapel-promotions?logo=pypi&logoColor=white&label=pypi)](https://pypi.org/project/stapel-promotions/)
[![downloads](https://static.pepy.tech/badge/stapel-promotions/month)](https://pepy.tech/project/stapel-promotions)
[![python](https://img.shields.io/pypi/pyversions/stapel-promotions?logo=python&logoColor=white)](https://pypi.org/project/stapel-promotions/)
[![license](https://img.shields.io/github/license/usestapel/stapel-promotions)](https://github.com/usestapel/stapel-promotions/blob/main/LICENSE)
[![llms.txt](https://img.shields.io/badge/llms.txt-blue)](https://github.com/usestapel/stapel-promotions/blob/main/docs/llms.txt)

> Promo codes and automatic discounts for a Stapel shop. Staff define promotions (percent off, a fixed amount off, or free shipping), scoped to the whole cart, one purchasable type or a list of purchasable keys, with a time window, a minimum subtotal, a stackable flag and usage caps in total and per shopper; codes are many per promotion, case-insensitive, single-use or shared, optionally bound to one shopper, and generated in bulk. Automatic promotions apply without a code. The brick plugs into stapel-orders by Function name only: promotions.quote is the discount adjuster (deterministic, no writes, one adjustment per promotion, free shipping as a marker the shipping adjuster honours) and promotions.reserve/commit/release is the checkout hook that ledgers each use under the promotion's row lock, so two shoppers racing for the last use end as one order and one promo_code_exhausted. Money is Decimal end to end; a discount never exceeds what it applies to.

Part of the [Stapel framework](https://github.com/usestapel) — composable Django apps that deploy as a monolith or as microservices without changing module code.

## Install

```bash
pip install stapel-promotions
```

## At a glance

| Fact | Value |
|---|---|
| Version | `0.1.0` |
| Python | `>=3.11` (3.11, 3.12, 3.13, 3.14) |
| HTTP operations | 14 |
| Config axes | 1 |
| Extension points | 7 |
| Error codes | 67 |
| Documented flows | 2 |
| Fleet dependencies | [`stapel-auth`](https://github.com/usestapel/stapel-auth) (optional) · [`stapel-core`](https://github.com/usestapel/stapel-core) · [`stapel-orders`](https://github.com/usestapel/stapel-orders) (optional) |

## Documentation

[OpenAPI](https://github.com/usestapel/stapel-promotions/blob/main/docs/schema.json) · [capabilities.json](https://github.com/usestapel/stapel-promotions/blob/main/docs/capabilities.json) · [llms.txt (for agents)](https://github.com/usestapel/stapel-promotions/blob/main/docs/llms.txt)

## What this is

Promo codes and automatic discounts for a Stapel shop: "a promo code", "a
sale", "free shipping over N". Staff define **promotions**; shoppers unlock
them with **codes** (or get them automatically); the orders checkout asks
this brick for the discount and records every use on a **ledger**, so
usage caps hold even when two shoppers race for the last use.

The brick never imports stapel-orders: it answers the orders checkout
contract by comm Function name (`promotions.quote` is the discount
adjuster, `promotions.reserve/commit/release` the checkout hook).

## Quick start

```python
INSTALLED_APPS = [
    # ...
    "stapel_promotions",
]

# urls.py
path("promotions/api/", include("stapel_promotions.urls"))   # -> /promotions/api/v1/...

# settings.py — expire lapsed holds, purge old attempts (Celery optional)
from stapel_promotions.beat import get_promotions_beat_schedule
CELERY_BEAT_SCHEDULE = {**get_promotions_beat_schedule()}
```

Wire it into the orders checkout. A scaffolded project already calls the
merge helper in `settings.py`: add this brick's preset to that list (the
helper merges two levels deep, so other presets and your own keys stay):

<!-- orders-wiring:scaffold -->
```python
_stapel_merge_preset_defaults(globals(), [
    # ... the presets already listed (stapel_shop.preset, other companions),
    "stapel_promotions.preset",
])
```

A hand-wired project merges per key instead — **never assign**
`STAPEL_ORDERS = {...}`: pasted after another fragment it would drop that
fragment and the host's `PURCHASABLE_TYPES`:

<!-- orders-wiring:hand -->
```python
from stapel_promotions.preset import SETTINGS_DEFAULTS as _PROMOTIONS

STAPEL_ORDERS = globals().get("STAPEL_ORDERS") or {}
STAPEL_ORDERS = {
    **STAPEL_ORDERS,
    "ADJUSTERS": {**_PROMOTIONS["STAPEL_ORDERS"]["ADJUSTERS"], **STAPEL_ORDERS.get("ADJUSTERS", {})},
    "CHECKOUT_HOOKS": {**_PROMOTIONS["STAPEL_ORDERS"]["CHECKOUT_HOOKS"], **STAPEL_ORDERS.get("CHECKOUT_HOOKS", {})},
}
```

The shopper's codes travel in the orders checkout `context`:
`{"codes": ["SUMMER10"]}` on quote and on checkout.

## Concepts

- **Promotion** — `percent` (capped by `max_discount`), `fixed` (in one
  currency) or `free_shipping`; scope `cart`, one `purchasable_type`, or a
  list of `keys`; a `[starts_at, ends_at)` window; `min_subtotal`;
  `stackable`; `priority`; `usage_limit_total` and `usage_limit_per_user`;
  `automatic` = applies with no code.
- **Promo code** — many per promotion, stored upper-cased (case-insensitive
  by construction), optional per-code `usage_limit` (1 = single-use) and
  `assigned_user_id` (only that shopper). Bulk generation:
  `manage.py promotions_generate_codes <key> <count>` or the staff API.
- **Ledger** — one `Redemption` per promotion per checkout attempt:
  `reserved` → `committed` → (`released` on cancel); a hold that lapses is
  `expired`. Caps count committed uses plus live holds, under the
  promotion's row lock.
- **Stacking** — all stackable promotions together, or one non-stackable
  alone, whichever saves the shopper more. Free shipping breaks ties.
- **Free shipping** — a zero-amount `discount` adjustment with
  `meta.free_shipping: true`; the shipping adjuster (order 200, after
  discounts) sees it in `adjustments_so_far` and quotes its line at 0.
- **Money** — Decimal end to end, strings on the wire. A percent rounds
  half-up to the cent; shares across lines round down with the leftover
  cents to the largest remainders. A discount never exceeds the line totals
  it applies to.

## API

| Method | Path | Who |
|---|---|---|
| GET | `active/` | anyone — live automatic promotions for a banner |
| POST | `codes/validate/` | anyone (throttled) — would this code apply |
| GET/POST | `staff/promotions/` | staff |
| GET/PATCH/DELETE | `staff/promotions/{id}/` | staff |
| GET/POST | `staff/promotions/{id}/codes/` | staff |
| POST | `staff/promotions/{id}/codes/generate/` | staff |
| GET | `staff/promotions/{id}/redemptions/` | staff |
| PATCH/DELETE | `staff/codes/{id}/` | staff |

Functions: `promotions.quote`, `promotions.reserve`, `promotions.commit`,
`promotions.release`, `promotions.codes.issue`, `promotions.codes.revoke`. Facts:
`promotions.redemption.committed`, `promotions.redemption.released`,
`promotions.promotion.exhausted`. Settings: `CONFIG.MD`. Design:
`docs/SPEC.md`. Extension points: `MODULE.md`.

## License

MIT — see [LICENSE](https://github.com/usestapel/stapel-promotions/blob/main/LICENSE).

---

<sub>This page is assembled by `stapel-readme` from `docs/readme.md` plus the contract artifacts in `docs/`. Edit the prose in `docs/readme.md`; the badges, facts and links above and below it are generated — do not hand-edit `README.md`.</sub>
