← Back to projects
Summary

Lustr's growth problem is one every local service business faces: you can't buy Google Ads into profitability on a finite crew calendar. The affiliate system is the answer — a network of partner businesses (gyms, car dealerships, body shops) each holding a branded NFC card that, when tapped or scanned, drops a first-party attribution cookie and routes the customer to self-book. The same infrastructure extends to social media influencers: a unique /r/<code> link works in an Instagram bio just as well as on a physical card. I designed and built the entire system from the DB schema to the partner portal UI.

Stack

What it's built with.

Attribution Infrastructure

  • Vercel Edge Middleware
  • First-Party Cookie Design
  • Stripe Webhook Attribution
  • Defense-in-Depth Validation

Database & Security

  • PostgreSQL / Supabase
  • Row-Level Security
  • RPC + Grant Design
  • Privilege Escalation Guards

Frontend & Portal

  • React (Vite)
  • QR Code Generation (qrcode)
  • NFC / Physical Card Distribution

Commission & Payout

  • Tiered Commission Engine
  • Anti-Farming Guardrails
  • Idempotent Attribution
  • Month-Close Payout Reports

Testing

  • Vitest (Unit)
  • Playwright (E2E)
Skills exercised

The non-code parts.

Channel StrategyPartnership DevelopmentPhysical Product ThinkingSecurity-First DesignCommission Economics
Details

How it works.

The problem and the physical-first insight

Lustr's best acquisition channel isn't paid search — it's warm referrals from businesses whose clients already need automotive detailing. A gym whose members drive to every workout, a car dealership's service lane, a body shop waiting room. The overlap is perfect. The challenge was turning that warm overlap into a measurable, low-friction channel that doesn't require the partner to do anything more than put a card on their front desk.

The insight was to make the referral hardware-simple. A physical NFC card — designed, manufactured, and distributed by Lustr — gets programmed to a unique lustrservices.ca/r/<code> URL. The partner sets it on their counter. Their customer taps their phone, lands on the Lustr site, and can book at their own pace. No app, no partner login, no training, no concierge flow. When the booking is paid, the partner earns their commission. The card does the only thing that matters: create the attributable touchpoint.

Why first-party cookies and edge middleware

Attribution via a redirect sounds simple until you think about where the cookie has to live. The customer's booking happens on lustrservices.ca. If the redirect goes through a Supabase function URL (*.supabase.co), any cookie it sets is third-party from the browser's perspective — blocked by default in Safari, increasingly restricted everywhere else. The attribution would evaporate exactly for the customers least likely to be using a Chrome-based browser.

The fix is Vercel edge middleware: a thin function that runs on lustrservices.ca itself, resolves the referral code anon-side over fetch (edge-runtime-safe, no Node SDK), sets the attribution cookie as first-party, logs the click with sha256-hashed IP, and redirects to the homepage. The cookie is HttpOnly to prevent client-side tampering, plus a second readable copy that the checkout fetch (going to *.supabase.co) can include in the request body. Server-side re-validation at checkout and again at the Stripe webhook means the readable cookie is verified, not trusted — a forged value resolves to nothing.

Influencer channel — same infrastructure, different medium

The same /r/<code> middleware and webhook attribution path work for social media with zero additional engineering. An influencer's link in an Instagram bio, a YouTube description, or a TikTok profile routes through the identical attribution hop. The commission engine, earnings dashboard, and payout reports are all channel-agnostic.

Partner affiliate and influencer affiliate are the same software but different operational models: a physical business partner gets an NFC card and earns a recurring commission on client bookings; a social media influencer gets a referral link and earns a flat fee per serviced booking. Volume and risk profile differ, but the tracking and payout machinery doesn't. The distinction is kept at the commission tier and lifecycle level, not at the code level.

Future proposals doc notes the remaining considerations for opening the influencer channel: capacity constraints (a viral post can overwhelm a finite crew calendar in a way a gym's trickle of referrals can't), commission math verification against the cheapest qualifying services, and member-booking edge cases (active members pay no deposit, which removes the self-funding mechanism for the commission). The infrastructure is already there when the operational prerequisites are met.

Security design

Every write path is defense-in-depth. The referral code resolver is anon-callable but returns only an opaque affiliate_id UUID — no PII, no business info. The validator RPC (used at checkout and webhook) is authenticated/service-role only; an explicit REVOKE EXECUTE from anon prevents Supabase's default auto-grant from accidentally making it callable by a random visitor.

Attribution is stamped exactly once and is then immutable. A database trigger (appointments_guard_attribution) reverts any non-service-role attempt to overwrite an already-stamped affiliate_id, so neither a client bug nor a malicious request can re-attribute a booking after the fact. The self-referral guard runs at webhook time against the paying customer's auth.uid, so an affiliate can't book under a different email and then collect on themselves.

Suspension and channel kill-switch both drop attribution between scan and pay — if an affiliate is suspended after a customer taps their card but before that customer pays, the webhook finds status != 'active' and skips the stamp. Commission is earned only on bookings that are fully paid and serviced, not on holds.

Physical product — manufacture and distribution

The NFC cards are a deliberate physical-product decision. A QR code alone works fine, but NFC creates a frictionless mobile tap and signals quality — it's the same mechanism as contactless payment, familiar to every smartphone user. For a premium detailing brand, a matte-finished card with the Lustr mark on the counter of a partner business does brand work in addition to attribution work.

Distribution is admin-gated: a Lustr admin provisions a partner account through the in-person provisioning flow, which creates the auth user, affiliates row with a unique referral_code, and fires a magic-link welcome email. The partner's referral_code is then the programming target for their NFC card — one card per partner, programmed once, replaced if the card is lost or the partner relationship ends. Card design uses the same brand system as Lustr's other print collateral (B2B partner cards from the design sub-project).

Highlights

The things I'm proudest of.

  • Designed the NFC card distribution model: each partner business receives a branded physical card programmed to their unique lustrservices.ca/r/<code> URL. Customers tap or scan at the partner location, self-book without any partner involvement, and the referral is attributed automatically. No training, no concierge flow, no friction for the partner.
  • Built Vercel edge middleware for the /r/<code> attribution hop: resolves the referral code via an anon-callable RPC, sets a first-party HttpOnly + readable cookie pair (48h, Secure, SameSite=Lax) on lustrservices.ca, logs the click with sha256-hashed IP (never raw), and 302s to the homepage. First-party cookies were a deliberate constraint — a Supabase function URL would be third-party and blocked by modern browsers.
  • Threaded attribution through the full payment stack: the readable cookie passes affiliate_ref into the checkout body, both checkout edge functions re-validate it server-side, and the Stripe webhook stamps source='affiliate' + affiliate_id onto the appointment after payment clears. Defense-in-depth re-validation at every layer means a forged or stale cookie resolves to nothing.
  • Built a tiered commission engine with per-affiliate tier overrides, a member flat-fee variant, anti-farming guardrails, idempotent attribution (stamped once, immutable), self-referral guard, and a suspension/channel kill-switch that drops attribution between tap and pay if an affiliate is deactivated.
  • Built an end-to-end partner portal at /affiliate: sign-in (magic-link or password), referral link card with scannable QR + 1024px print-resolution download, earnings dashboard, and a manual booking fallback for call-in cases. Admin side adds an Affiliates management page, month-close payout reports, and an in-person provisioning flow that magic-links the new partner's welcome email.
  • Extends cleanly to influencer marketing: the same /r/<code> middleware, referral_code resolver RPC, and Stripe webhook attribution path work whether a customer arrived from a physical NFC tap, a printed QR scan, or a link in an Instagram story — no separate infrastructure required.
  • Full test coverage: 16-case unit suite for the attribution flow (cookie parsing, self-referral, suspension, idempotency), E2E specs for the RPC grant model and full attribution chain, all 487 unit tests green. Click log verified against live local DB with hashed-IP and grant-level assertions.
Concept

What it looks like.

Partner display concept: tap the pad to open the booking site. QR as fallback.