Metadata-Version: 2.4
Name: stapel-referrals
Version: 0.1.0
Summary: Referral codes, attribution and rewards for the Stapel framework
License: MIT
Project-URL: Homepage, https://github.com/usestapel/stapel-referrals
Project-URL: Repository, https://github.com/usestapel/stapel-referrals
Project-URL: Documentation, https://github.com/usestapel/stapel-referrals#readme
Project-URL: Changelog, https://github.com/usestapel/stapel-referrals/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/usestapel/stapel-referrals/issues
Keywords: django,stapel,referrals,invite,rewards,attribution
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"
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-referrals

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

> Referral codes, attribution and rewards. Every member gets one personal code (minted lazily, collision-proof; vanity codes by policy) and a share link; staff issue campaign codes with a window and an attribution cap. A visitor's client stores the code on the first visit and claims it right after sign-up: the account is attributed once (unique constraint, first touch wins), self-referral, old accounts, stale captures and closed or used-up campaign codes are refused, and opt-in guards (same device, duplicate contact, custom) can refuse a claim with a recorded reason. Reward rules fire on facts — sign-up (user.signed_up), the first qualifying order or the Nth order (orders.order.placed) — for the referrer, the referee or both, with caps per referrer per rolling period and budgets counted under a lock on the rule row. The reward ledger is idempotent per (rule, attribution, trigger, role), holds order rewards for a period, rejects them when the order is cancelled during the hold and reverses them after it. Rewards are paid by a strategy that calls the paying brick by comm Function (billing_credit, promo_code) or just records them (noop); the module never imports auth, orders, billing or promotions.

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-referrals
```

## At a glance

| Fact | Value |
|---|---|
| Version | `0.1.0` |
| Python | `>=3.11` (3.11, 3.12, 3.13, 3.14) |
| HTTP operations | 17 |
| Config axes | 4 |
| Usage surface | 6 |
| Extension points | 7 |
| Error codes | 64 |
| Documented flows | 3 |
| Fleet dependencies | [`stapel-auth`](https://github.com/usestapel/stapel-auth) (optional) · [`stapel-billing`](https://github.com/usestapel/stapel-billing) (optional) · [`stapel-core`](https://github.com/usestapel/stapel-core) · [`stapel-notifications`](https://github.com/usestapel/stapel-notifications) (optional) · [`stapel-orders`](https://github.com/usestapel/stapel-orders) (optional) · [`stapel-promotions`](https://github.com/usestapel/stapel-promotions) (optional) |

## Documentation

**Errors:** [English](https://github.com/usestapel/stapel-referrals/blob/main/docs/errors.en.md) · [Español](https://github.com/usestapel/stapel-referrals/blob/main/docs/errors.es.md) · [Русский](https://github.com/usestapel/stapel-referrals/blob/main/docs/errors.ru.md) · [OpenAPI](https://github.com/usestapel/stapel-referrals/blob/main/docs/schema.json) · [capabilities.json](https://github.com/usestapel/stapel-referrals/blob/main/docs/capabilities.json) · [llms.txt (for agents)](https://github.com/usestapel/stapel-referrals/blob/main/docs/llms.txt)

## What this is

Referral codes, attribution and rewards for a Stapel product — "invite a
friend, both get a bonus" and partner links. Every member gets one personal
code (minted on first use, vanity codes by policy) and a share link; staff
issue campaign codes with a window and a cap. A visitor's client stores the
code on the first visit and claims it right after sign-up; the account is
attributed once, first touch wins. Reward rules fire on facts — sign-up, the
first qualifying order, the Nth order — and pay through a strategy that asks
the paying brick by comm Function (`billing_credit`, `promo_code`) or just
records the reward (`noop`). The module never imports auth, orders, billing
or promotions.

## Quick start

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

STAPEL_REFERRALS = {
    "SHARE_URL_TEMPLATE": "https://shop.example/r/{code}",
}

# urls.py
path("referrals/api/", include("stapel_referrals.urls"))   # -> /referrals/api/v1/...

# settings.py — grants after the hold, reversal retries, purge (Celery optional)
from stapel_referrals.beat import get_referrals_beat_schedule
CELERY_BEAT_SCHEDULE = {**get_referrals_beat_schedule()}
```

Then a staff member creates a rule (`POST /referrals/api/v1/staff/rules/`):

```json
{"name": "Friend bonus", "trigger": "first_order", "min_order_total": "30.00",
 "currency": "USD", "beneficiary": "both", "strategy": "billing_credit",
 "referrer_params": {"credits": 100}, "referee_params": {"credits": 50},
 "cap_per_referrer": 10, "cap_period_days": 30}
```

The client side is two calls: `GET me/` (my code and link) for the referrer,
and `POST claim/ {code, captured_at}` right after sign-up for the referee.

## Concepts

- **Code** — personal (one primary per member; a vanity change retires the
  old code, which stays claimable so shared links keep working) or campaign
  (staff-owned, `starts_at`/`ends_at`, `max_attributions`). 31-symbol
  alphabet without look-alikes; case-insensitive.
- **Attribution** — one per account, enforced by a unique constraint. Self
  referral, old accounts (`CLAIM_MAX_ACCOUNT_AGE_HOURS`), stale captures
  (`ATTRIBUTION_WINDOW_DAYS`) and closed or used-up campaign codes are
  refused. Opt-in guards (`same_device`, `duplicate_contact`, or your own)
  record a refused attribution that keeps the account's slot.
- **Reward rule** — trigger, beneficiary (referrer / referee / both),
  strategy and params per beneficiary, caps per referrer per rolling period,
  budgets in triggers and in value, a hold for order rewards.
- **Reward** — the ledger: `pending → granted | rejected`, `granted →
  reversed`. Idempotent on `(rule, attribution, trigger, role)`. Caps are
  counted under a lock on the rule row. A provider failure keeps a reward
  pending and the beat retries; it is never recorded as a refusal. An order
  cancelled during the hold rejects its rewards; after the grant (within
  `REVERSAL_WINDOW_DAYS`) it reverses them.
- **GDPR** — erasure pseudonymises the person on codes, attributions and
  rewards and replaces their code text; the ledger stays.

## License

MIT — see [LICENSE](https://github.com/usestapel/stapel-referrals/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>
