81 lines
4.5 KiB
Markdown
81 lines
4.5 KiB
Markdown
# Referral codes and commission wallets
|
|
|
|
## Rollout
|
|
|
|
Apply `db/migrations/000002_referrals.up.sql` with the existing migration runner before deploying the API.
|
|
Deploy the accompanying RezinaLMS-Admin changes. Administrators can then open **Settings ? Referrals**
|
|
and save a commission percentage between 0 and 100, with at most two decimal places.
|
|
The initial rate is **0%**; this disables new commission credits until configured.
|
|
The migration has a matching down migration; rolling it back deletes referral and wallet history.
|
|
|
|
## Attribution
|
|
|
|
Active STUDENT and OPEN_LEARNER accounts can create a stable, cryptographically random share code.
|
|
A new account can supply `referral_code` in its existing registration request. User creation and
|
|
attribution commit together; an invalid code rolls both back. Codes ignore case and surrounding whitespace.
|
|
Authenticated learners registering through other providers can instead call the apply endpoint within
|
|
seven days of account creation and before any successful positive payment.
|
|
|
|
Each account has at most one referrer. Self referral, replacement of the original attribution,
|
|
unknown codes, inactive referrers, and attribution after payment are rejected. No referred learner
|
|
names, email addresses, or phone numbers appear in learner referral lists.
|
|
|
|
## Commission rules
|
|
|
|
Only the **first successful positive subscription payment** after attribution qualifies.
|
|
The payment must have both a plan and subscription linked; late subscription linkage is supported.
|
|
Free plans, pending or failed payments, and renewals do not earn commissions.
|
|
Database triggers record conversion and wallet credit in the same transaction as the payment.
|
|
Duplicate callbacks cannot add another credit. Conversion is recorded even at 0%, so increasing the
|
|
rate or replaying a callback does not retroactively reward an earlier payment.
|
|
|
|
The rate is read at conversion and saved on the credit. Changing settings affects future conversions.
|
|
Credits use PostgreSQL numeric arithmetic and round to two decimal places. Each currency has its own
|
|
balance; amounts and percentages are returned as decimal strings. A payment leaving SUCCESS reverses
|
|
its credit; subsequent callbacks cannot restore that credit automatically.
|
|
|
|
The wallet currently records accrued commissions. It does not send money, process withdrawal requests,
|
|
convert currencies, or spend wallet credits at checkout. External payouts need a separate settlement flow.
|
|
|
|
## APIs
|
|
|
|
All paths below are relative to `/api/v1`, require authentication, and return the normal `data` envelope.
|
|
Learner endpoints are available without an active subscription.
|
|
|
|
| Method | Path | Purpose |
|
|
| --- | --- | --- |
|
|
| POST / GET | `/referrals/code` | Create or return own share code: `{ "code": "ABCDEF0123456789" }` |
|
|
| POST | `/referrals/apply` | Apply `{ "code": "ABCDEF0123456789" }`; invalid or ineligible codes return 400 |
|
|
| GET | `/referrals/me` | Own code, current commission percentage, total referrals, conversions, and wallet balances |
|
|
| GET | `/referrals?limit=20&offset=0` | Own referral history with conversion flags |
|
|
| GET | `/referrals/wallet?limit=20&offset=0` | Own balances and commission ledger, including reversals |
|
|
| GET | `/admin/referrals/settings` | Read saved commission percentage (ADMIN / SUPER_ADMIN) |
|
|
| PUT | `/admin/referrals/settings` | Save `{ "commission_percent": 10.25 }` (ADMIN / SUPER_ADMIN); audited |
|
|
|
|
Pagination accepts limit 1?100 and non-negative offset. Commission changes do not alter subscription
|
|
prices, access duration, or existing wallet entries. Admin settings changes require no subscription.
|
|
|
|
Example registration addition:
|
|
|
|
```json
|
|
{
|
|
"email": "learner@example.com",
|
|
"password": "a-secure-password",
|
|
"referral_code": "ABCDEF0123456789"
|
|
}
|
|
```
|
|
|
|
Clients can attach the returned code to their signup URL and pass it to registration. The API returns
|
|
codes rather than guessing a learner-app URL.
|
|
|
|
## Verification
|
|
|
|
Run `go test ./internal/services/referrals ./internal/repository ./internal/web_server/... ./internal/services/user ./internal/domain`.
|
|
Run `npm run build` in the sibling `RezinaLMS-Admin` repository.
|
|
|
|
The SQL regression script uses an isolated in-memory PostgreSQL engine (PGlite), not application data.
|
|
Install `@electric-sql/pglite` in a scratch directory and set `REFERRAL_TEST_PACKAGE_JSON` to that
|
|
directory's `package.json`, then run `node db/tests/referrals.test.cjs`. It verifies the actual migration
|
|
and application attribution query, numeric rounding, duplicate callbacks, renewals, rate changes,
|
|
0% conversions, reversals, currencies, late subscription linkage, eligibility, and migration rollback.
|