Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ protected signed annotated `vX.Y.Z` tag already on `origin/main`, followed by

## Where things stand

Phase 3: `@pegma/billing-core` (ledger collection, `(eventAt, eventId)`
Phase 4: `@pegma/billing-core` (ledger collection, `(eventAt, eventId)`
watermark, effective-watermark guard, lifecycle-rank tie-break, declared
`sticky` / `firstWins` combinators, checkout reservation, snapshot
reconciliation). Nothing is published. `@pegma/billing-stripe` is Phase 4
— do not create that package here yet.
reconciliation) and `@pegma/billing-stripe` (Stripe event and snapshot
translation). Nothing is published. Phase 5 is the first-consumer
`repo.js` swap — do not implement that here.

Siblings: [spine](https://github.com/pegma-dev/spine),
[storage-core](https://github.com/pegma-dev/storage-core),
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ Thank you for helping improve Billing Core.
violate (arrival-order application, payment processing, entitlements,
receipt dedup, invoicing/tax/metering, or storing card data / payloads /
amounts).
- If a proposal needs this package to process payments or to create a
provider adapter before a consumer exists, say so explicitly — those are
the changes the design cannot absorb.
- If a proposal needs this package to process payments, verify webhook
signatures, resolve entitlements, or swap a host `repo.js`, say so
explicitly — those are the changes the design cannot absorb.

## Local development

Expand Down
23 changes: 11 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ hosts: the durable record of what a customer's subscription **is**,
maintained correctly under out-of-order webhook delivery.

> [!IMPORTANT]
> Pegma is in early `0.x` development. `@pegma/billing-core` Phase 3 is
> in-tree and unpublished. No public API is stable.
> Pegma is in early `0.x` development. `@pegma/billing-core` and
> `@pegma/billing-stripe` Phase 4 are in-tree and unpublished. No public
> API is stable.

## The part everyone gets wrong

Expand All @@ -29,13 +30,13 @@ makes that impossible:
- periodic **snapshot reconciliation** that repairs field drift without
disturbing the dedup identity of the events themselves.

Phase 3 ships the watermark, the effective-watermark guard,
Phase 4 ships the watermark, the effective-watermark guard,
lifecycle-rank arbitration, declared ledger invariants (`sticky`,
`firstWins`), the checkout reservation, and snapshot reconciliation.
The Stripe adapter is a later phase. The core knows lifecycle states, not
Stripe's vocabulary. And the data boundary is absolute: the ledger stores
provider identifiers and derived state — never a card number, a raw
payload, or an amount.
`firstWins`), the checkout reservation, snapshot reconciliation, and
Stripe event/snapshot translation. The core knows lifecycle states, not
Stripe's vocabulary — `@pegma/billing-stripe` is the translation. And
the data boundary is absolute: the ledger stores provider identifiers
and derived state — never a card number, a raw payload, or an amount.

Not here, on purpose: payment processing and checkout flows, entitlement
resolution ([Authorization Core](https://github.com/pegma-dev/authorization-core)'s
Expand All @@ -56,10 +57,8 @@ application.

| Package | Role | Phase |
| ----------------------- | ------------------------------------------------------- | ----- |
| `@pegma/billing-core` | Ledger, watermark, rank, invariants, reservation, sweep | 3 |
| `@pegma/billing-stripe` | Event and snapshot translation | later |

The Stripe adapter package is not created until Phase 4.
| `@pegma/billing-core` | Ledger, watermark, rank, invariants, reservation, sweep | 4 |
| `@pegma/billing-stripe` | Event and snapshot translation | 4 |

## Constraint that shapes everything

Expand Down
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

This project is in early `0.x` development and is not published yet.
Security fixes apply to the latest reviewed source until the first
advertised release of `@pegma/billing-core`.
advertised release of `@pegma/billing-core` and `@pegma/billing-stripe`.

## Reporting a vulnerability

Expand Down
20 changes: 12 additions & 8 deletions docs/PROJECT_PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Status

**Stage:** Phase 3 — snapshot reconciliation in-tree. Nothing is
**Stage:** Phase 4 — Stripe adapter in-tree. Nothing is
published. (`0.1.0`, unpublished.)

**Extraction source:** the RetireGolden account API's subscription ledger —
Expand Down Expand Up @@ -176,10 +176,15 @@ reservation with its read-winner-back semantics and TTL expiry.
The sweep, the domain CAS token, the always-write freshness rule — with
the delayed-intermediate-webhook scenario pinned as a named test.

### Phase 4 — the Stripe adapter
### Phase 4 — the Stripe adapter (this tree)

Translation from Stripe events and subscription snapshots, against the
reference application's real event corpus.
Translation from Stripe events and subscription snapshots into
`LedgerEvent` / snapshot fetches. The core stays provider-agnostic.
Signature verification stays with the host; the vendor client stays
behind the adapter. Named consumers are RetireGolden.org (Stripe
subscriptions) and Exsimplify (Stripe Checkout today; the adapter still
translates subscription and event shapes). Their private internals stay
out of this tree.

### Phase 5 — first consumer

Expand All @@ -191,10 +196,9 @@ acceptance bar.

## Timing

Phase 3 is implemented in-tree. Later phases remain gated: the Stripe
adapter (Phase 4) waits until a named consumer needs it. Phase 5 (first
consumer cutover) stays gated on that application's operational calendar
— never swap code under a soak or mid-migration.
Phase 4 is implemented in-tree. Phase 5 (first consumer cutover) stays
gated on that application's operational calendar — never swap code under
a soak or mid-migration.

## Open questions

Expand Down
16 changes: 9 additions & 7 deletions docs/RELEASING.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Release operations

`@pegma/billing-core` publishes only from a stable GitHub release. Merging a
pull request never publishes, and the workflow has no manual-dispatch or
npm-token fallback.
`@pegma/billing-core` and `@pegma/billing-stripe` publish only from a
stable GitHub release. Merging a pull request never publishes, and the
workflow has no manual-dispatch or npm-token fallback.

## Required external configuration

Before the first release through this workflow:

- configure `@pegma/billing-core` on npm with the GitHub Actions trusted
publisher `pegma-dev/billing-core`, workflow `publish.yml`, environment
`npm-publish`, and allowed action `npm publish`;
- configure `@pegma/billing-core` and `@pegma/billing-stripe` on npm with
the GitHub Actions trusted publisher `pegma-dev/billing-core`, workflow
`publish.yml`, environment `npm-publish`, and allowed action
`npm publish`;
- create the GitHub `npm-publish` environment. A second reviewer is not
required under Pegma's single-maintainer policy;
- create the repository Actions variable `RELEASE_ALLOWED_SIGNERS` containing
Expand All @@ -25,7 +26,8 @@ remaining traditional npm publish tokens.

## Release procedure

The version in `packages/billing-core/package.json` is the release version.
The versions in `packages/billing-core/package.json` and
`packages/billing-stripe/package.json` are the release versions.
Change it through an ordinary reviewed pull request and run the complete gate
on Node 22 and 24.

Expand Down
5 changes: 2 additions & 3 deletions packages/billing-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,5 @@ clobber fresher fields. `snapshotAt` never jumps into a later Unix second
than the sampled bound, so same-second webhooks still get lifecycle-rank
arbitration. The watermark identity is never touched.

Phase 3 is the arbitration core, combinators, reservation, and snapshot
reconciliation. The Stripe adapter is a later phase — do not import it
from this package.
Phase 4 keeps this package provider-agnostic. Import Stripe translation
from `@pegma/billing-stripe`, not from here.
21 changes: 21 additions & 0 deletions packages/billing-stripe/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 RetireGolden, LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
90 changes: 90 additions & 0 deletions packages/billing-stripe/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# `@pegma/billing-stripe`

Stripe event and snapshot translation for
[`@pegma/billing-core`](https://github.com/pegma-dev/billing-core). The
ledger stays provider-agnostic. This adapter maps Stripe event types and
subscription objects onto `LedgerEvent` / snapshot fetches.

> [!IMPORTANT]
> Pegma is in early `0.x` development. This package's public API is unstable.

```ts
import { createBillingLedger } from "@pegma/billing-core";
import { createStripeBillingAdapter } from "@pegma/billing-stripe";
import { createMemoryStore } from "@pegma/storage-core";

const ledger = createBillingLedger({
store: createMemoryStore(),
clock: { now: () => "2026-08-15T16:00:00.000Z" },
});

const stripe = createStripeBillingAdapter({
client: {
retrieveSubscription: (id) => hostStripe.subscriptions.retrieve(id),
},
});

// Host verifies the webhook signature, then:
const event = stripe.translateEvent(verifiedStripeEvent);
if (event !== null) {
await ledger.apply(accountId, event);
}

await ledger.reconcile(accountId, stripe.fetchSnapshot);
```

The host verifies Stripe signatures and binds a customer to an account
id. This package never calls `constructEvent`, never processes a
payment, and never resolves entitlements. The vendor client stays behind
`retrieveSubscription`.

Translation copies provider identifiers and derived state only — never
card data, raw payloads, line items, or amounts, even when those fields
are present on the Stripe object.

## Lifecycle mapping

| Stripe `status` | Ledger `LifecycleStatus` |
| -------------------- | ------------------------ |
| `incomplete` | `incomplete` |
| `incomplete_expired` | `canceled` |
| `trialing` | `trialing` |
| `active` | `active` |
| `past_due` | `past_due` |
| `canceled` | `canceled` |
| `unpaid` | `unpaid` |
| `paused` | `unpaid` |

`incomplete_expired` is a finished first-invoice failure. `paused` is
non-granting and still live; it maps to `unpaid` so a same-second
`active` cannot resurrect it. Unknown Stripe statuses are dropped, not
coerced into a grant.

`event.id` and `event.created` become the ledger watermark
`(eventId, eventAt)`. Arrival order is not an input. A same-second
Stripe `canceled` and `active` are ranked by the core: canceled wins in
either delivery order.

Checkout Session and Invoice objects translate only when they carry an
**expanded** Subscription. A bare `sub_…` id is not enough to write
derived state — fetch the subscription through the snapshot port.

`plan` defaults to the Price `lookup_key`. Pass `planFromPrice` to map a
Price id to a host plan name. Do not put customer-specific price tables
in this package.

## What this is not

- Payment processing, Checkout UI, or Customer Portal orchestration
- Webhook signature verification (host) or receipt dedup
([`@pegma/webhooks`](https://github.com/pegma-dev/webhooks))
- Entitlement resolution
([`@pegma/authorization-stripe`](https://github.com/pegma-dev/authorization-core))
- A host `repo.js` swap (Phase 5)

Named consumers of this translation are RetireGolden.org (Stripe
subscriptions) and Exsimplify (Stripe Checkout today; the adapter still
translates subscription and event shapes for the ledger). Their private
account internals stay out of this tree.

Phase 4 is in-tree and unpublished.
43 changes: 43 additions & 0 deletions packages/billing-stripe/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "@pegma/billing-stripe",
"version": "0.1.0",
"description": "Stripe event and snapshot translation for the Pegma billing ledger.",
"license": "MIT",
"type": "module",
"sideEffects": false,
"publishConfig": {
"access": "public"
},
"files": [
"dist/**/*.d.ts",
"dist/**/*.d.ts.map",
"dist/**/*.js",
"dist/**/*.js.map"
],
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}
},
"engines": {
"node": ">=22"
},
"repository": {
"type": "git",
"url": "git+https://github.com/pegma-dev/billing-core.git",
"directory": "packages/billing-stripe"
},
"dependencies": {
"@pegma/billing-core": "0.1.0",
"@pegma/spine": "0.1.2"
},
"devDependencies": {
"@pegma/storage-core": "0.4.0"
},
"scripts": {
"build": "tsc -p tsconfig.json",
"check": "tsc -p tsconfig.json --noEmit",
"prepack": "pnpm run build"
}
}
Loading
Loading