Migrate an Affiliate System (legacy)
Move referral codes and reward lifecycle into BillStack.
Legacy (v1) API. This page describes the Stripe-first legacy API. It stays supported for existing integrations but is frozen: new features ship only in v2. Prefer v2 for new work: Import referrals.
Use this migration when your app has its own affiliate or ambassador tables.
This page is about projects where BillStack owns checkout. If your product keeps its own Stripe account, Checkout sessions and subscriptions (external billing mode), follow Import an Existing Referral System instead: it imports codes with their Stripe promotion codes, attributions and the credit history.
Target state
- BillStack owns referral code generation.
- BillStack applies referral codes to project customers.
- BillStack tracks the referral through its full lifecycle (see below).
- Your app only renders referral UI and calls the API.
Referral lifecycle
A referral created through /referrals/apply/by-external-id starts as pending and then moves through one of two paths, depending on how the referee pays:
pending— referral code applied to the referee, waiting for a paid entitlement.refund_period— the referee's payment activated an entitlement (a Stripe subscription goingactive, a Stripe lifetime purchase, or a Razorpaypayment.captured/subscription.activated/subscription.chargedevent). The referral now waits out the project's refund window (refundWindowDays, default 7 days) before it can be rewarded.rewarding— once the refund window has elapsed, a reward-processing pass claims the referral and prepares to credit the referrer.rewarded— the referrer has been credited (bonus months applied to their entitlement).
Every referral that reaches an active entitlement — Stripe subscription, Stripe lifetime, or Razorpay — goes through the same pending → refund_period → rewarding → rewarded path; there is no separate provider-specific shortcut in this flow. (An immediate pending → converted transition exists in the codebase, but it only applies to a legacy/non-project referral path unrelated to the by-external-id API described here.)
Migration steps
- Enable and configure referrals for the project in BillStack.
- Map existing referrers to project customers with
externalId. - Generate or import referral codes.
- Replace local code creation with
/referrals/code/by-external-id/{externalId}. - Replace local referral application with
/referrals/apply/by-external-id. - Send referral checkout users through signup/login before checkout.
- Replace local stats queries with
/referrals/stats/by-external-id/{externalId}. - Schedule periodic reward processing (see Reward processing is not automatic below) and only remove local reward-processing jobs once that schedule is live and verified.
BillStack does not automatically process due referral rewards. See below — skipping this step leaves referrals stuck in refund_period forever.
Reward processing is not automatic
processDueProjectReferralRewards — the function that claims eligible refund_period referrals, credits the referrer, and marks them rewarded — is only invoked when you call:
curl -X POST \
"https://billing.w3dev.app/api/billstack/teams/{teamId}/projects/{projectId}/referrals/rewards/process" \
-H "Authorization: Bearer bs_..." \
-H "Content-Type: application/json" \
-d '{ "limit": 100 }'Nothing in BillStack (no cron job, no Inngest function) calls this endpoint on its own. If you don't call it yourself on a recurring schedule (for example, a daily cron job in your app or infra), referrals that clear their refund window will sit in refund_period indefinitely and referrers will never be credited. Do not remove your existing local reward-processing job until this schedule is in place and verified end to end.
Anonymous referral rule
Anonymous Payment Links are useful for normal logged-out checkout. They are not the right default for referral checkout because BillStack needs the referred user identity.
If the request has a referral code and no logged-in user, redirect to signup/login. After signup, apply the referral and start logged-in checkout.