A creator referral program sounds like a link and a percentage. Once you build one, it turns into four questions: who gets credit for a signup, which payments earn a commission, how do you avoid counting the same payment twice, and what happens when a payment is refunded after you've recorded it?
We're building MotionLab , an AI-personalized stretching app with a Next.js web app on Vercel, an Android app, and Supabase underneath. Subscriptions come from Stripe on the web and Google Play on Android, and creators who share MotionLab earn a commission on subscriptions they bring in. This post is the code behind that: a landing route that stashes the referral, a Postgres function that records it once, a commissions table keyed so each payment lands once, and the jobs that undo commissions after refunds.
Stack: Next.js App Router, @supabase/supabase-js , PL/pgSQL, the stripe SDK and googleapis . Snippets are trimmed from the real code; // ... (or -- ... in SQL) marks cuts.…