Affiliate System Configuration
Configure referrals, signup bonuses, commission records, and withdrawal approval
The affiliate system lets users invite new users through referral links and earn cash commissions from each successful payment made by referred users. It also supports two-way signup rewards, so both the referrer and referred user can receive credits.
Prerequisites: The affiliate system depends on the payment and credits systems. Complete Payment Configuration and Credits Configuration first.
Related Pages
| Page | Path | Description |
|---|---|---|
| User Dashboard - Affiliate | /dashboard (Affiliate tab) | View referral link, commission balance, and referral records |
| User Dashboard - Withdrawal | /dashboard (Withdrawal tab) | Request withdrawals and view withdrawal history |
| Admin - Commissions | /admin/commissions | Admin view of all commission records |
| Admin - Withdrawals | /admin/withdrawals | Admin approval for withdrawal requests |
System Overview
| Feature | Description |
|---|---|
| Commission mode | Percentage commission (default 20%) or fixed amount |
| Commission trigger | Every successful payment by a referred user |
| Signup rewards | Referrer and referred user each receive credits (default 10 credits each) |
| Withdrawal flow | User request → manual admin approval |
| Supported payments | Stripe, WeChat Pay, Alipay, Creem, PayPal |
| Feature toggle | Disabled by default; set AFFILIATE_ENABLED=true to enable |
Quick Enable
The affiliate system is disabled by default. After configuring payment, risk control, withdrawal, and support workflows, set AFFILIATE_ENABLED=true to enable it.
After enabling it, the user dashboard automatically shows Affiliate and Withdrawal tabs, and the admin sidebar shows Commissions and Withdrawals navigation items.
Minimal Configuration
You must explicitly enable the feature; all other values can use their defaults:
# Required: disabled by default
AFFILIATE_ENABLED=true
# Optional: all other values have reasonable defaults
# AFFILIATE_COMMISSION_RATE=0.20
# AFFILIATE_REFERRER_SIGNUP_BONUS=10
# AFFILIATE_REFEREE_SIGNUP_BONUS=10
# AFFILIATE_MIN_WITHDRAWAL=100
# AFFILIATE_COOKIE_EXPIRY_DAYS=30Disable the Affiliate System
AFFILIATE_ENABLED=falseAfter disabling:
- The user dashboard hides Affiliate / Withdrawal tabs
- The admin dashboard hides commission / withdrawal navigation
- APIs return
enabled: false - Payment Webhooks skip commission processing
Environment Variables
| Variable | Default | Description |
|---|---|---|
AFFILIATE_ENABLED | false | Global feature toggle; set to true to enable |
AFFILIATE_COMMISSION_RATE | 0.20 | Commission rate (0.20 = 20%) |
AFFILIATE_FIXED_COMMISSION_AMOUNT | 0 | Fixed commission amount; overrides percentage mode when > 0 |
AFFILIATE_CURRENCY | USD | Commission settlement currency (ISO 4217); see currency notes below |
AFFILIATE_COOKIE_EXPIRY_DAYS | 30 | Referral cookie lifetime in days |
AFFILIATE_MIN_WITHDRAWAL | 100 | Minimum withdrawal amount, using AFFILIATE_CURRENCY |
AFFILIATE_REFERRER_SIGNUP_BONUS | 10 | Signup credit reward for the referrer |
AFFILIATE_REFEREE_SIGNUP_BONUS | 10 | Signup credit reward for the referred user |
Currency Limitation
The current affiliate system supports only one currency. The commission balance field, commissionBalance, is a simple accumulated number and does not separate balances by currency.
If your config/payment.ts includes plans in multiple currencies, such as USD and CNY, make sure all plans participating in affiliate commissions use the same currency. Mixing currencies makes the balance number meaningless.
Recommended setup:
- Set
AFFILIATE_CURRENCYto your main billing currency (defaultUSD) - Ensure all commission-enabled plans in
config/payment.tsuse the samecurrencyasAFFILIATE_CURRENCY - If you have both CNY and USD plans, consider enabling commissions for only one currency by adding filtering logic in your Webhook handler
Future versions may support currency-bucketed commission balances. For now, keep the currency strictly consistent.
Configuration Examples
# High-commission mode for high-ticket products
AFFILIATE_COMMISSION_RATE=0.30
AFFILIATE_MIN_WITHDRAWAL=50
# Fixed commission mode: 5 per order
AFFILIATE_FIXED_COMMISSION_AMOUNT=5
# Larger signup rewards to encourage referrals
AFFILIATE_REFERRER_SIGNUP_BONUS=50
AFFILIATE_REFEREE_SIGNUP_BONUS=20Workflows
Referral Signup Flow
1. User A gets a referral link from the dashboard: https://yourapp.com?ref=NjMDryrv
2. User B opens the link
3. Middleware stores the ref parameter in a cookie (valid for 30 days)
4. User B signs up and logs in
5. The first dashboard visit triggers claim automatically
6. The system records the referral relationship in the database
7. Both users receive credit rewards if the configured reward amounts are > 0Commission Flow
1. User B, the referred user, purchases any paid plan or credits package
2. Payment succeeds → payment Webhook is triggered
3. After order processing, the Webhook calls processReferralCommission(orderId)
4. The system checks referral information in the order metadata
5. The commission amount is calculated from the configured rate
6. A commission record is created and added to the referrer's commissionBalanceWithdrawal Flow
1. User A requests a withdrawal from the dashboard Withdrawal tab
2. User enters amount, payment method, and receiving account
3. The system validates balance ≥ withdrawal amount ≥ minimum withdrawal amount
4. Balance is deducted immediately to prevent duplicate withdrawals
5. Admin reviews the request:
- Approve (completed): admin transfers funds manually
- Reject (rejected): balance is returned automaticallyAdmin Operations
View Commission Records
Open /admin/commissions to:
- View commission records for all users
- Search by email
- View commission amount, order amount, commission rate, and status
Approve Withdrawals
Open /admin/withdrawals to:
- View all withdrawal requests
- Search by email
- Click Approve to approve a withdrawal (manual transfer required)
- Click Reject to reject a withdrawal (balance is returned automatically)
FAQ
User Cannot See the Affiliate Tab?
Check that AFFILIATE_ENABLED=true is configured. Restart the development server for the configuration to take effect.
Commission Was Not Generated?
- Confirm the referred user's order metadata includes
referralCodeandreferrerId - Check payment Webhook logs for
[Affiliate][Commission] - Confirm
processReferralCommission(orderId)is called in the relevant payment Webhook handler
Referral Link Does Not Work?
- Confirm the link includes the
?ref=CODEparameter - Check whether the browser has a
referral_codecookie - The user must sign up and log in before claiming
- Each user can only be referred once
SQLite Database Compatibility?
The system supports both PostgreSQL and SQLite. SQL uses standard CAST() syntax and has passed full E2E tests on both databases.
Related Docs
- Credits System Configuration - Signup rewards are issued through the credits system
- Payment Testing Guide - Test payment Webhooks that trigger commissions
- Database Configuration - PostgreSQL / SQLite switching