Affiliates Architecture & Specs
Built on an auditable commission ledger with native database bindings to Commerce and Billing.
Performance Metrics
Tracking Latency
<5ms (in-transaction)
Refund Clawback Sync
Instant (Native Hook)
Ledger Integrity
Append-only Immutable
1. Native Tracking Integration
Unlike third-party systems that rely on asynchronous webhooks or fragile browser cookies, Aeion Affiliates is evaluated directly within the same checkout transaction as the order itself. Because Affiliates and Commerce share the same database, there is no webhook to miss, no sync window to drift, and no second system to reconcile. - Deterministic Promo Codes: The system reads the affiliate code directly from the validated order — the primary, most reliable attribution path, immune to cookie loss or ad-blockers. - First-Party Cookie Fallback: For organic traffic that arrives without a code, click attribution runs server-side via signed referral tokens. The token sets a first-party cookie and writes a server-side attribution record at click time, with a configurable attribution window. Conversion-side matching pairs customer email, transaction timestamp, and token reference — first-party data only, no third-party cookies. - Multi-Touch Attribution: When a conversion has multiple qualifying touchpoints across channels, attribution resolves which partner earns credit rather than silently awarding the last click. - Schema Validation: Strict, strongly-typed validation ensures all tracking data is well-formed before it's written to the ledger.
typescript
// Example: Reading an affiliate's ledger after a refund clawbackconst entries = await aeion.api.get( "/v1/affiliate_ledger?where[affiliateId][equals]=aff_123");
// entries.data includes both the original commission and the// automatic reversal entry created when the order was refunded:// [// { type: "commission", amount: 25.00, orderId: "ord_789" },// { type: "refund_reversal", amount: -25.00, orderId: "ord_789" }// ]2. The Auditable Ledger
Every affiliate's commission history is an append-oriented application ledger: commissions and refund reversals are added as separate entries, while payout rows transition from pending to cleared or failed during reconciliation. This is not a double-entry accounting subsystem, and authorized admins retain collection maintenance controls. - Transaction Types: Commission, payout, and refund-reversal entries, each individually auditable. - Fraud Review: Commission entries post as pending and can be scored by an AI compliance agent (fraud risk 0–100) that recommends clear, manual review, payout hold, or affiliate suspension — a human keeps final say, this doesn't auto-lock the balance. - Serialized Balance Updates: Per-affiliate mutation locks serialize balance changes. Ledger/balance pairs use compensating rollback when the second write fails; they are not claimed as one database transaction.
3. Event-Driven Payout Surface
The whole lifecycle is event-driven, so each stage is observable and wireable without touching the ledger internals. Subscribe to any of these to extend the flow:
- affiliates.link.clicked — records the click and sets the first-party attribution cookie.
- affiliates.conversion.completed — calculates commission and appends the ledger entry.
- affiliates.order.refunded — clawback: appends the automatic reversal entry.
- affiliates.payout.requested — fires when AffiliateService.processPayout creates a pending ledger hold; the built-in durable subscriber immediately attempts the affiliate's configured method through the provider registry.
The registry and reconciliation service are built in, but no production Stripe Connect, PayPal, or ACH adapter is registered by this module. Without one, the honest fallback returns transmittedToProvider: false, the ledger stays pending, and no money movement is fabricated. Payout thresholds do not initiate this lifecycle automatically.
`AffiliateService.processPayout` serializes balance access, validates available funds and any enabled tax-form gate, deducts the amount as a hold, appends a pending payout entry, and emits `affiliates.payout.requested`. The durable subscriber calls `PayoutDisbursementService` with the ledger ID as the provider idempotency key. A registered adapter can return paid (ledger clears), failed (ledger fails and the balance is refunded), or pending. With no adapter, the request stays pending and `transmittedToProvider` remains false.
The Portal's payout-request route is an approval request: it creates a pending entry without deducting balance or emitting the disbursement event. `POST /payouts/:ledgerId/disburse` is admin-only; for a Portal-origin row it rechecks and deducts the current balance under the affiliate mutation lock, marks the hold, and calls the same provider reconciliation path while still holding the lock. Competing approvals therefore cannot consume the same balance twice. Legacy pending rows without an explicit request source remain blocked rather than risking a second deduction. The dashboard's manual-payout action separately records an external payment as cleared and deducts the balance; it does not contact a provider.
Each program configures its own commission type — percentage or fixed amount — applied per order. The logic executes server-side, preventing client-side manipulation of bounty amounts.
Frequently Asked Questions
Affiliates and Commerce share the same database, so commission logic reads the affiliate code directly off the validated order in the same transaction the order is written. There's no outbound webhook to fire, retry, or drop — the attribution is committed atomically with the sale.
A `Refund Event` appends a matching negative `refund_reversal` entry while leaving the original commission in place. The affiliate balance update is serialized with a per-affiliate lock and uses compensating rollback if its paired write fails. No manual clawback entry is normally needed.
Commissions, payouts, and reversals remain individually auditable while the denormalized balance supports fast reads. Reconciliation updates payout status as provider outcomes arrive, so this is append-oriented history rather than a strictly immutable or double-entry ledger.
Only when a deployment has registered a real adapter for the affiliate's payout method. The built-in durable subscriber attempts service-initiated payouts, and an admin can approve a Portal request through the disbursement endpoint, which reserves its balance before the same provider attempt. The standard module ships no Stripe Connect, PayPal, or ACH adapter. Without one, the payout remains pending with `transmittedToProvider: false`; thresholds alone do not create or send payouts.
Each program's commission type — percentage or fixed amount — is applied per order server-side, so bounty amounts can't be manipulated from the client. Pending commissions can additionally be scored by an AI compliance agent (fraud risk 0–100) that recommends clear, review, hold, or suspend — but a human always keeps final say.
See the Ledger in Action
Native checkout tracking, atomic refund clawbacks, and event-driven payouts — no webhooks, no sync errors.