# Paddle Subscription Checkout

## Objective

Sell subscriptions through Paddle with entitlements granted only by verified events.

A Paddle-backed subscription purchase and lifecycle flow where local access follows verified server events and prices stay under server control.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Hold the catalogue mapping on the server: which local plan corresponds to which provider offering, at which price and currency. The client passes a plan identifier and nothing about money.
2. Carry an account or workspace reference into the checkout so the subscription that comes back can be attached to the right tenant without matching on email address.
3. Grant access only from verified server-side events, and store the event identifier so a retried delivery changes nothing a second time.
4. Project the subscription's status onto a single local access record that every permission check in the app reads, so entitlement is answered from one place rather than recomputed per screen.
5. This brief owns the whole lifecycle for this provider: purchase, renewal, change, and revocation. If a Stripe or Lemon Squeezy brief is also applied, they must write into the same local access record rather than each keeping their own.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- The checkout's own completion callback in the browser proves only that the browser saw a success screen. Never grant an entitlement from it; show a pending state and let the verified server event do the granting.
- A completed checkout must map to exactly one account. If the reference is missing or points at an account that no longer exists, quarantine the event for an operator rather than matching on a name or an email that may belong to someone else.
- Provider events are retried until acknowledged and can arrive out of order. Verify them, deduplicate on their identifier, and refuse to apply one older than the state already recorded.
- Trials, pauses, cancellations effective at period end, refunds, and past-due states each imply different access. A cancellation scheduled for the end of the term must not revoke access today, and a past-due account must not keep access forever.
- Prices, currencies, and tax-inclusive totals must come from trusted server configuration. Tax-inclusive display means the amount shown differs by region, and letting the client choose the price makes that impossible to reconcile.
- Invoices and receipts must reflect the tax treatment actually applied, and the app should link to the provider's own document rather than reproducing figures that may not match.
- When the provider is unreachable, keep existing subscribers working on the last known access record. An inability to check is not evidence of non-payment.
- A user who upgrades or downgrades mid-term will see a proration the app did not calculate. Show the provider's figure rather than an estimate that will disagree with the invoice.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Plan, price, and currency come from server configuration and are never accepted from the client.
- [ ] Access is granted only by verified server events, never by a client-side checkout callback.
- [ ] Each subscription resolves to exactly one account, and unmatched events are quarantined for review.
- [ ] Duplicate and out-of-order events leave local state correct.
- [ ] Trials, pauses, cancellations, refunds, and past-due each produce a defined access outcome.
- [ ] All permission checks read one local access record.
- [ ] A provider outage preserves existing subscriber access.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
