@pithy-sh/payments 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/admin/coverage.d.ts +35 -0
- package/dist/admin/coverage.d.ts.map +1 -0
- package/dist/admin/coverage.js +56 -0
- package/dist/admin/read.d.ts +222 -0
- package/dist/admin/read.d.ts.map +1 -0
- package/dist/admin/read.js +159 -0
- package/dist/audit/actions.d.ts +211 -0
- package/dist/audit/actions.d.ts.map +1 -0
- package/dist/audit/actions.js +211 -0
- package/dist/capability.d.ts +72 -0
- package/dist/capability.d.ts.map +1 -0
- package/dist/capability.js +228 -0
- package/dist/client/api.d.ts +435 -0
- package/dist/client/api.d.ts.map +1 -0
- package/dist/client/api.js +415 -0
- package/dist/client/checkout.d.ts +144 -0
- package/dist/client/checkout.d.ts.map +1 -0
- package/dist/client/checkout.js +173 -0
- package/dist/client/fixtures/price-preview-de.js +89 -0
- package/dist/client/fixtures/price-preview-gb.js +89 -0
- package/dist/client/fixtures/price-preview-jp-yen.js +89 -0
- package/dist/client/fixtures/price-preview-us-country-only.js +89 -0
- package/dist/client/fixtures/price-preview-us-new-york.js +89 -0
- package/dist/client/fixtures/pricePreview.d.ts +54 -0
- package/dist/client/fixtures/pricePreview.d.ts.map +1 -0
- package/dist/client/fixtures/pricePreview.js +61 -0
- package/dist/client/hooks.d.ts +235 -0
- package/dist/client/hooks.d.ts.map +1 -0
- package/dist/client/hooks.js +428 -0
- package/dist/client/paddle.d.ts +433 -0
- package/dist/client/paddle.d.ts.map +1 -0
- package/dist/client/paddle.js +391 -0
- package/dist/client/paddleCache.d.ts +123 -0
- package/dist/client/paddleCache.d.ts.map +1 -0
- package/dist/client/paddleCache.js +159 -0
- package/dist/client/paddlePrices.d.ts +94 -0
- package/dist/client/paddlePrices.d.ts.map +1 -0
- package/dist/client/paddlePrices.js +55 -0
- package/dist/client/paddlePricesTag.d.ts +127 -0
- package/dist/client/paddlePricesTag.d.ts.map +1 -0
- package/dist/client/paddlePricesTag.js +207 -0
- package/dist/client/projection.d.ts +128 -0
- package/dist/client/projection.d.ts.map +1 -0
- package/dist/client/projection.js +0 -0
- package/dist/client/test-utils/cacheStore.d.ts +18 -0
- package/dist/client/test-utils/cacheStore.d.ts.map +1 -0
- package/dist/client/test-utils/cacheStore.js +42 -0
- package/dist/client/wholeUnits.d.ts +18 -0
- package/dist/client/wholeUnits.d.ts.map +1 -0
- package/dist/client/wholeUnits.js +120 -0
- package/dist/config/config.d.ts +238 -0
- package/dist/config/config.d.ts.map +1 -0
- package/dist/config/config.js +249 -0
- package/dist/data/discount.d.ts +127 -0
- package/dist/data/discount.d.ts.map +1 -0
- package/dist/data/discount.js +91 -0
- package/dist/data/entitlement.d.ts +43 -0
- package/dist/data/entitlement.d.ts.map +1 -0
- package/dist/data/entitlement.js +43 -0
- package/dist/data/money.d.ts +15 -0
- package/dist/data/money.d.ts.map +1 -0
- package/dist/data/money.js +76 -0
- package/dist/data/pause.d.ts +133 -0
- package/dist/data/pause.d.ts.map +1 -0
- package/dist/data/pause.js +63 -0
- package/dist/data/providerAccount.d.ts +40 -0
- package/dist/data/providerAccount.d.ts.map +1 -0
- package/dist/data/providerAccount.js +34 -0
- package/dist/data/purchase.d.ts +107 -0
- package/dist/data/purchase.d.ts.map +1 -0
- package/dist/data/purchase.js +77 -0
- package/dist/data/rail.d.ts +74 -0
- package/dist/data/rail.d.ts.map +1 -0
- package/dist/data/rail.js +74 -0
- package/dist/data/reconcileRun.d.ts +120 -0
- package/dist/data/reconcileRun.d.ts.map +1 -0
- package/dist/data/reconcileRun.js +120 -0
- package/dist/data/renderMoney.d.ts +85 -0
- package/dist/data/renderMoney.d.ts.map +1 -0
- package/dist/data/renderMoney.js +146 -0
- package/dist/data/status.d.ts +58 -0
- package/dist/data/status.d.ts.map +1 -0
- package/dist/data/status.js +68 -0
- package/dist/data/subject.d.ts +111 -0
- package/dist/data/subject.d.ts.map +1 -0
- package/dist/data/subject.js +125 -0
- package/dist/data/subscription.d.ts +624 -0
- package/dist/data/subscription.d.ts.map +1 -0
- package/dist/data/subscription.js +497 -0
- package/dist/data/syncCursor.d.ts +33 -0
- package/dist/data/syncCursor.d.ts.map +1 -0
- package/dist/data/syncCursor.js +29 -0
- package/dist/data/tables.d.ts +37 -0
- package/dist/data/tables.d.ts.map +1 -0
- package/dist/data/tables.js +44 -0
- package/dist/data/webhookEvent.d.ts +134 -0
- package/dist/data/webhookEvent.d.ts.map +1 -0
- package/dist/data/webhookEvent.js +128 -0
- package/dist/entitlement/manual.d.ts +101 -0
- package/dist/entitlement/manual.d.ts.map +1 -0
- package/dist/entitlement/manual.js +84 -0
- package/dist/entitlement/resolver.d.ts +60 -0
- package/dist/entitlement/resolver.d.ts.map +1 -0
- package/dist/entitlement/resolver.js +101 -0
- package/dist/entitlement/subjectSeam.d.ts +102 -0
- package/dist/entitlement/subjectSeam.d.ts.map +1 -0
- package/dist/entitlement/subjectSeam.js +85 -0
- package/dist/error/errors.d.ts +247 -0
- package/dist/error/errors.d.ts.map +1 -0
- package/dist/error/errors.js +326 -0
- package/dist/grants/apply.d.ts +74 -0
- package/dist/grants/apply.d.ts.map +1 -0
- package/dist/grants/apply.js +148 -0
- package/dist/grants/clawback.d.ts +43 -0
- package/dist/grants/clawback.d.ts.map +1 -0
- package/dist/grants/clawback.js +71 -0
- package/dist/grants/ledgerSeam.d.ts +89 -0
- package/dist/grants/ledgerSeam.d.ts.map +1 -0
- package/dist/grants/ledgerSeam.js +66 -0
- package/dist/http/dispatch.d.ts +35 -0
- package/dist/http/dispatch.d.ts.map +1 -0
- package/dist/http/dispatch.js +40 -0
- package/dist/http/guards.d.ts +43 -0
- package/dist/http/guards.d.ts.map +1 -0
- package/dist/http/guards.js +52 -0
- package/dist/http/manifestConfig.d.ts +38 -0
- package/dist/http/manifestConfig.d.ts.map +1 -0
- package/dist/http/manifestConfig.js +49 -0
- package/dist/http/responses.d.ts +1455 -0
- package/dist/http/responses.d.ts.map +1 -0
- package/dist/http/responses.js +728 -0
- package/dist/http/routes.d.ts +315 -0
- package/dist/http/routes.d.ts.map +1 -0
- package/dist/http/routes.js +1966 -0
- package/dist/http/schemas.d.ts +404 -0
- package/dist/http/schemas.d.ts.map +1 -0
- package/dist/http/schemas.js +359 -0
- package/dist/http/scopes.d.ts +160 -0
- package/dist/http/scopes.d.ts.map +1 -0
- package/dist/http/scopes.js +240 -0
- package/dist/http/view.d.ts +96 -0
- package/dist/http/view.d.ts.map +1 -0
- package/dist/http/view.js +170 -0
- package/dist/http/webhookGuard.d.ts +109 -0
- package/dist/http/webhookGuard.d.ts.map +1 -0
- package/dist/http/webhookGuard.js +206 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/migrations/0001_purchases.d.ts +35 -0
- package/dist/migrations/0001_purchases.d.ts.map +1 -0
- package/dist/migrations/0001_purchases.js +80 -0
- package/dist/pricing/location.d.ts +138 -0
- package/dist/pricing/location.d.ts.map +1 -0
- package/dist/pricing/location.js +81 -0
- package/dist/pricing/visitor.d.ts +56 -0
- package/dist/pricing/visitor.d.ts.map +1 -0
- package/dist/pricing/visitor.js +35 -0
- package/dist/projection/event.d.ts +66 -0
- package/dist/projection/event.d.ts.map +1 -0
- package/dist/projection/event.js +45 -0
- package/dist/projection/orphans.d.ts +114 -0
- package/dist/projection/orphans.d.ts.map +1 -0
- package/dist/projection/orphans.js +162 -0
- package/dist/projection/owner.d.ts +121 -0
- package/dist/projection/owner.d.ts.map +1 -0
- package/dist/projection/owner.js +114 -0
- package/dist/projection/resolve.d.ts +30 -0
- package/dist/projection/resolve.d.ts.map +1 -0
- package/dist/projection/resolve.js +42 -0
- package/dist/projection/writer.d.ts +103 -0
- package/dist/projection/writer.d.ts.map +1 -0
- package/dist/projection/writer.js +280 -0
- package/dist/provision/resolvePaymentsConfig.d.ts +48 -0
- package/dist/provision/resolvePaymentsConfig.d.ts.map +1 -0
- package/dist/provision/resolvePaymentsConfig.js +41 -0
- package/dist/rails/apple/certs.d.ts +7 -0
- package/dist/rails/apple/certs.d.ts.map +1 -0
- package/dist/rails/apple/certs.js +8 -0
- package/dist/rails/apple/der.d.ts +107 -0
- package/dist/rails/apple/der.d.ts.map +1 -0
- package/dist/rails/apple/der.js +217 -0
- package/dist/rails/apple/fixtures/chain.d.ts +117 -0
- package/dist/rails/apple/fixtures/chain.d.ts.map +1 -0
- package/dist/rails/apple/fixtures/chain.js +289 -0
- package/dist/rails/apple/http.d.ts +74 -0
- package/dist/rails/apple/http.d.ts.map +1 -0
- package/dist/rails/apple/http.js +32 -0
- package/dist/rails/apple/jws.d.ts +17 -0
- package/dist/rails/apple/jws.d.ts.map +1 -0
- package/dist/rails/apple/jws.js +75 -0
- package/dist/rails/apple/notification.d.ts +116 -0
- package/dist/rails/apple/notification.d.ts.map +1 -0
- package/dist/rails/apple/notification.js +199 -0
- package/dist/rails/apple/rail.d.ts +25 -0
- package/dist/rails/apple/rail.d.ts.map +1 -0
- package/dist/rails/apple/rail.js +68 -0
- package/dist/rails/apple/serverApi.d.ts +120 -0
- package/dist/rails/apple/serverApi.d.ts.map +1 -0
- package/dist/rails/apple/serverApi.js +244 -0
- package/dist/rails/apple/verify.d.ts +36 -0
- package/dist/rails/apple/verify.d.ts.map +1 -0
- package/dist/rails/apple/verify.js +46 -0
- package/dist/rails/apple/x509.d.ts +85 -0
- package/dist/rails/apple/x509.d.ts.map +1 -0
- package/dist/rails/apple/x509.js +273 -0
- package/dist/rails/contract.d.ts +871 -0
- package/dist/rails/contract.d.ts.map +1 -0
- package/dist/rails/contract.js +60 -0
- package/dist/rails/google/fixtures/push.d.ts +71 -0
- package/dist/rails/google/fixtures/push.d.ts.map +1 -0
- package/dist/rails/google/fixtures/push.js +107 -0
- package/dist/rails/google/http.d.ts +77 -0
- package/dist/rails/google/http.d.ts.map +1 -0
- package/dist/rails/google/http.js +39 -0
- package/dist/rails/google/jwt.d.ts +53 -0
- package/dist/rails/google/jwt.d.ts.map +1 -0
- package/dist/rails/google/jwt.js +94 -0
- package/dist/rails/google/oidc.d.ts +112 -0
- package/dist/rails/google/oidc.d.ts.map +1 -0
- package/dist/rails/google/oidc.js +179 -0
- package/dist/rails/google/playApi.d.ts +188 -0
- package/dist/rails/google/playApi.d.ts.map +1 -0
- package/dist/rails/google/playApi.js +350 -0
- package/dist/rails/google/rail.d.ts +57 -0
- package/dist/rails/google/rail.d.ts.map +1 -0
- package/dist/rails/google/rail.js +70 -0
- package/dist/rails/google/rtdn.d.ts +105 -0
- package/dist/rails/google/rtdn.d.ts.map +1 -0
- package/dist/rails/google/rtdn.js +179 -0
- package/dist/rails/google/verify.d.ts +20 -0
- package/dist/rails/google/verify.d.ts.map +1 -0
- package/dist/rails/google/verify.js +76 -0
- package/dist/rails/lemonSqueezy/api.d.ts +92 -0
- package/dist/rails/lemonSqueezy/api.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/api.js +121 -0
- package/dist/rails/lemonSqueezy/checkout.d.ts +52 -0
- package/dist/rails/lemonSqueezy/checkout.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/checkout.js +84 -0
- package/dist/rails/lemonSqueezy/discounts.d.ts +50 -0
- package/dist/rails/lemonSqueezy/discounts.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/discounts.js +75 -0
- package/dist/rails/lemonSqueezy/fixtures/events.d.ts +56 -0
- package/dist/rails/lemonSqueezy/fixtures/events.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/fixtures/events.js +144 -0
- package/dist/rails/lemonSqueezy/objects.d.ts +229 -0
- package/dist/rails/lemonSqueezy/objects.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/objects.js +379 -0
- package/dist/rails/lemonSqueezy/portal.d.ts +35 -0
- package/dist/rails/lemonSqueezy/portal.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/portal.js +18 -0
- package/dist/rails/lemonSqueezy/pricing.d.ts +25 -0
- package/dist/rails/lemonSqueezy/pricing.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/pricing.js +54 -0
- package/dist/rails/lemonSqueezy/rail.d.ts +41 -0
- package/dist/rails/lemonSqueezy/rail.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/rail.js +67 -0
- package/dist/rails/lemonSqueezy/read.d.ts +28 -0
- package/dist/rails/lemonSqueezy/read.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/read.js +37 -0
- package/dist/rails/lemonSqueezy/refresh.d.ts +42 -0
- package/dist/rails/lemonSqueezy/refresh.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/refresh.js +40 -0
- package/dist/rails/lemonSqueezy/signature.d.ts +56 -0
- package/dist/rails/lemonSqueezy/signature.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/signature.js +92 -0
- package/dist/rails/lemonSqueezy/verify.d.ts +33 -0
- package/dist/rails/lemonSqueezy/verify.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/verify.js +41 -0
- package/dist/rails/lemonSqueezy/webhook.d.ts +53 -0
- package/dist/rails/lemonSqueezy/webhook.d.ts.map +1 -0
- package/dist/rails/lemonSqueezy/webhook.js +179 -0
- package/dist/rails/paddle/adjustments.d.ts +60 -0
- package/dist/rails/paddle/adjustments.d.ts.map +1 -0
- package/dist/rails/paddle/adjustments.js +66 -0
- package/dist/rails/paddle/api.d.ts +138 -0
- package/dist/rails/paddle/api.d.ts.map +1 -0
- package/dist/rails/paddle/api.js +152 -0
- package/dist/rails/paddle/checkout.d.ts +69 -0
- package/dist/rails/paddle/checkout.d.ts.map +1 -0
- package/dist/rails/paddle/checkout.js +100 -0
- package/dist/rails/paddle/discounts.d.ts +67 -0
- package/dist/rails/paddle/discounts.d.ts.map +1 -0
- package/dist/rails/paddle/discounts.js +73 -0
- package/dist/rails/paddle/events.d.ts +140 -0
- package/dist/rails/paddle/events.d.ts.map +1 -0
- package/dist/rails/paddle/events.js +183 -0
- package/dist/rails/paddle/fixtures/browserForged.d.ts +42 -0
- package/dist/rails/paddle/fixtures/browserForged.d.ts.map +1 -0
- package/dist/rails/paddle/fixtures/browserForged.js +52 -0
- package/dist/rails/paddle/fixtures/pausedSubscription.d.ts +78 -0
- package/dist/rails/paddle/fixtures/pausedSubscription.d.ts.map +1 -0
- package/dist/rails/paddle/fixtures/pausedSubscription.js +76 -0
- package/dist/rails/paddle/objects.d.ts +519 -0
- package/dist/rails/paddle/objects.d.ts.map +1 -0
- package/dist/rails/paddle/objects.js +588 -0
- package/dist/rails/paddle/portal.d.ts +45 -0
- package/dist/rails/paddle/portal.d.ts.map +1 -0
- package/dist/rails/paddle/portal.js +42 -0
- package/dist/rails/paddle/rail.d.ts +55 -0
- package/dist/rails/paddle/rail.d.ts.map +1 -0
- package/dist/rails/paddle/rail.js +117 -0
- package/dist/rails/paddle/read.d.ts +53 -0
- package/dist/rails/paddle/read.d.ts.map +1 -0
- package/dist/rails/paddle/read.js +66 -0
- package/dist/rails/paddle/recorded.d.ts +49 -0
- package/dist/rails/paddle/recorded.d.ts.map +1 -0
- package/dist/rails/paddle/recorded.js +79 -0
- package/dist/rails/paddle/refresh.d.ts +54 -0
- package/dist/rails/paddle/refresh.d.ts.map +1 -0
- package/dist/rails/paddle/refresh.js +94 -0
- package/dist/rails/paddle/refund.d.ts +82 -0
- package/dist/rails/paddle/refund.d.ts.map +1 -0
- package/dist/rails/paddle/refund.js +175 -0
- package/dist/rails/paddle/signature.d.ts +83 -0
- package/dist/rails/paddle/signature.d.ts.map +1 -0
- package/dist/rails/paddle/signature.js +163 -0
- package/dist/rails/paddle/subscription.d.ts +137 -0
- package/dist/rails/paddle/subscription.d.ts.map +1 -0
- package/dist/rails/paddle/subscription.js +538 -0
- package/dist/rails/paddle/verify.d.ts +46 -0
- package/dist/rails/paddle/verify.d.ts.map +1 -0
- package/dist/rails/paddle/verify.js +43 -0
- package/dist/rails/paddle/webhook.d.ts +98 -0
- package/dist/rails/paddle/webhook.d.ts.map +1 -0
- package/dist/rails/paddle/webhook.js +181 -0
- package/dist/rails/providers.d.ts +73 -0
- package/dist/rails/providers.d.ts.map +1 -0
- package/dist/rails/providers.js +62 -0
- package/dist/rails/stripe/api.d.ts +118 -0
- package/dist/rails/stripe/api.d.ts.map +1 -0
- package/dist/rails/stripe/api.js +153 -0
- package/dist/rails/stripe/checkout.d.ts +60 -0
- package/dist/rails/stripe/checkout.d.ts.map +1 -0
- package/dist/rails/stripe/checkout.js +86 -0
- package/dist/rails/stripe/discounts.d.ts +50 -0
- package/dist/rails/stripe/discounts.d.ts.map +1 -0
- package/dist/rails/stripe/discounts.js +87 -0
- package/dist/rails/stripe/fixtures/events.d.ts +64 -0
- package/dist/rails/stripe/fixtures/events.d.ts.map +1 -0
- package/dist/rails/stripe/fixtures/events.js +67 -0
- package/dist/rails/stripe/objects.d.ts +237 -0
- package/dist/rails/stripe/objects.d.ts.map +1 -0
- package/dist/rails/stripe/objects.js +389 -0
- package/dist/rails/stripe/portal.d.ts +29 -0
- package/dist/rails/stripe/portal.d.ts.map +1 -0
- package/dist/rails/stripe/portal.js +41 -0
- package/dist/rails/stripe/pricing.d.ts +25 -0
- package/dist/rails/stripe/pricing.d.ts.map +1 -0
- package/dist/rails/stripe/pricing.js +68 -0
- package/dist/rails/stripe/rail.d.ts +28 -0
- package/dist/rails/stripe/rail.d.ts.map +1 -0
- package/dist/rails/stripe/rail.js +81 -0
- package/dist/rails/stripe/refresh.d.ts +33 -0
- package/dist/rails/stripe/refresh.d.ts.map +1 -0
- package/dist/rails/stripe/refresh.js +21 -0
- package/dist/rails/stripe/signature.d.ts +71 -0
- package/dist/rails/stripe/signature.d.ts.map +1 -0
- package/dist/rails/stripe/signature.js +92 -0
- package/dist/rails/stripe/verify.d.ts +15 -0
- package/dist/rails/stripe/verify.d.ts.map +1 -0
- package/dist/rails/stripe/verify.js +63 -0
- package/dist/rails/stripe/webhook.d.ts +26 -0
- package/dist/rails/stripe/webhook.d.ts.map +1 -0
- package/dist/rails/stripe/webhook.js +31 -0
- package/dist/secret/registry.d.ts +192 -0
- package/dist/secret/registry.d.ts.map +1 -0
- package/dist/secret/registry.js +154 -0
- package/dist/seeds/example.d.ts +3 -0
- package/dist/seeds/example.d.ts.map +1 -0
- package/dist/seeds/example.js +171 -0
- package/dist/version.generated.d.ts +5 -0
- package/dist/version.generated.d.ts.map +1 -0
- package/dist/version.generated.js +7 -0
- package/dist/workflows/paddleSweep.d.ts +175 -0
- package/dist/workflows/paddleSweep.d.ts.map +1 -0
- package/dist/workflows/paddleSweep.js +433 -0
- package/dist/workflows/railAccess.d.ts +48 -0
- package/dist/workflows/railAccess.d.ts.map +1 -0
- package/dist/workflows/railAccess.js +36 -0
- package/dist/workflows/reconcile.d.ts +161 -0
- package/dist/workflows/reconcile.d.ts.map +1 -0
- package/dist/workflows/reconcile.js +352 -0
- package/dist/workflows/report.d.ts +28 -0
- package/dist/workflows/report.d.ts.map +1 -0
- package/dist/workflows/report.js +54 -0
- package/dist/workflows/retryPolicy.d.ts +29 -0
- package/dist/workflows/retryPolicy.d.ts.map +1 -0
- package/dist/workflows/retryPolicy.js +37 -0
- package/dist/workflows/specs.d.ts +102 -0
- package/dist/workflows/specs.d.ts.map +1 -0
- package/dist/workflows/specs.js +84 -0
- package/dist/workflows/worker.d.ts +62 -0
- package/dist/workflows/worker.d.ts.map +1 -0
- package/dist/workflows/worker.js +92 -0
- package/dist/workflows/workerConfig.d.ts +46 -0
- package/dist/workflows/workerConfig.d.ts.map +1 -0
- package/dist/workflows/workerConfig.js +38 -0
- package/package.json +22 -13
- package/pithy.manifest.json +19 -13
- package/src/version.generated.ts +1 -1
- package/src/client/paddlePrices.iife.ts +0 -43
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { railCredentials } from "../secret/registry.js";
|
|
2
|
+
import { mintAppleApiToken } from "../rails/apple/serverApi.js";
|
|
3
|
+
import { mintPlayAccessToken } from "../rails/google/playApi.js";
|
|
4
|
+
import { resolveRailProvider } from "../rails/providers.js";
|
|
5
|
+
//#region src/workflows/railAccess.ts
|
|
6
|
+
/**
|
|
7
|
+
* Build one page's rail access, minting each rail's batch token on first use.
|
|
8
|
+
*
|
|
9
|
+
* Lazy per rail: a page holding only Stripe purchases never signs an Apple assertion, and a project that
|
|
10
|
+
* enabled one rail never pays for the other two. The mint happens inside `providerFor`, so a rail whose
|
|
11
|
+
* credentials are missing raises `payments/rail_not_configured` at the same place a disabled rail does — one
|
|
12
|
+
* refusal for the caller to handle rather than two.
|
|
13
|
+
*/
|
|
14
|
+
function batchedRailAccess(options) {
|
|
15
|
+
const { config, credentials, now } = options;
|
|
16
|
+
const base = options.trust ?? {};
|
|
17
|
+
const built = /* @__PURE__ */ new Map();
|
|
18
|
+
async function build(rail) {
|
|
19
|
+
const trust = { ...base };
|
|
20
|
+
if (rail === "apple" && trust.appleApiToken === void 0) trust.appleApiToken = await mintAppleApiToken(railCredentials(credentials, "apple"), { now });
|
|
21
|
+
if (rail === "google" && trust.googleAccessToken === void 0) trust.googleAccessToken = await mintPlayAccessToken(railCredentials(credentials, "google"), {
|
|
22
|
+
now,
|
|
23
|
+
transport: base.googleTransport
|
|
24
|
+
});
|
|
25
|
+
return resolveRailProvider(rail, config, credentials, trust);
|
|
26
|
+
}
|
|
27
|
+
return { providerFor(rail) {
|
|
28
|
+
const existing = built.get(rail);
|
|
29
|
+
if (existing) return existing;
|
|
30
|
+
const pending = build(rail);
|
|
31
|
+
built.set(rail, pending);
|
|
32
|
+
return pending;
|
|
33
|
+
} };
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { batchedRailAccess };
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { D1Database } from "@cloudflare/workers-types";
|
|
2
|
+
import { type AuditEmit } from "@pithy-sh/core/src/audit/recorder";
|
|
3
|
+
import type { PaymentsConfig } from "../config/config";
|
|
4
|
+
import { type PurchaseEnvironment } from "../data/purchase";
|
|
5
|
+
import type { PurchaseProjection } from "../projection/writer";
|
|
6
|
+
import type { PaddleSweepReport } from "./paddleSweep";
|
|
7
|
+
import type { ReconcileRailAccess } from "./railAccess";
|
|
8
|
+
import { type PaymentsReconcileParams } from "./specs";
|
|
9
|
+
/**
|
|
10
|
+
* The reconciliation pass: asking each store what it thinks, and repairing what disagrees.
|
|
11
|
+
*
|
|
12
|
+
* **Why this exists at all.** A webhook-only system rots silently. A deploy that drops requests, a Pub/Sub push
|
|
13
|
+
* subscription pointing at a retired URL, a rotated signing key, a store outage — each loses events with
|
|
14
|
+
* nothing surfacing anywhere. And one drift needs no incident to explain it: a subscription can simply lapse,
|
|
15
|
+
* with the store deciding not to renew and telling nobody. The read path already rechecks `expiresAt` so a
|
|
16
|
+
* lapsed row stops granting, but the row itself stays wrong, the renewal that *did* happen stays unheard, and
|
|
17
|
+
* neither is visible until somebody complains.
|
|
18
|
+
*
|
|
19
|
+
* **Why a Workflow rather than a scheduled pass.** The work is hundreds of calls to rate-limited third-party
|
|
20
|
+
* APIs that fail part way. A plain `scheduled()` handler is wall-clock bounded and restarts from nothing; a
|
|
21
|
+
* Workflow step is journalled, so a run that dies on page forty resumes at page forty rather than re-asking
|
|
22
|
+
* Apple about four thousand subscriptions. That is durable execution earning its place, not decorating a cron.
|
|
23
|
+
*
|
|
24
|
+
* ## The shape of a run
|
|
25
|
+
*
|
|
26
|
+
* Pure over injected dependencies — the step runner, the rails, the clock, and the audit emitter all arrive as
|
|
27
|
+
* parameters — so the whole of it, resuming included, is tested against real D1 with fake rails and no
|
|
28
|
+
* network. `workflows/worker.ts` is the durable shell that supplies the real ones.
|
|
29
|
+
*
|
|
30
|
+
* **Pagination is keyset, never offset**, and that is the correctness argument for the file. The selection
|
|
31
|
+
* matches subscriptions that are near expiry *or* have not been verified lately, and a reconciled row stops
|
|
32
|
+
* matching both — so the result set shrinks underneath the scan. With `LIMIT/OFFSET` that is the classic skip
|
|
33
|
+
* bug: page two starts at row 100 of a set that just lost its first 100 members, and half the catalog is never
|
|
34
|
+
* reconciled, silently. Reading `id > cursor` in id order cannot skip and cannot repeat, whatever the
|
|
35
|
+
* predicate does to the rows behind the cursor.
|
|
36
|
+
*
|
|
37
|
+
* **Each page is one `step.do`, named from a page counter.** Step names must be identical across replays or
|
|
38
|
+
* the journal is unusable, so they are derived from a counter and never from a timestamp or a row id.
|
|
39
|
+
*
|
|
40
|
+
* ## Who a repaired purchase belongs to
|
|
41
|
+
*
|
|
42
|
+
* **Off the row, always.** A pass has no request, so there is no `c.var.auth` and no adopter
|
|
43
|
+
* `resolveSubject` — `entitlement/subjectSeam.ts` resolves a subject from a Hono context, and this file
|
|
44
|
+
* never has one. A rail's `refresh` answer carries no subject either, by construction. So every subject the
|
|
45
|
+
* pass touches is the pair already stored on the purchase it read, and the narrowing a support pass takes is
|
|
46
|
+
* the pair a human typed. See `workflows/worker.ts` for why that is structural rather than an omission.
|
|
47
|
+
*
|
|
48
|
+
* ## What is asked, and what is left alone
|
|
49
|
+
*
|
|
50
|
+
* **Subscriptions only.** A consumable or a non-consumable is bought once and does not drift: the single thing
|
|
51
|
+
* that changes one is a refund, which arrives as its own notification on every rail. Asking a store about
|
|
52
|
+
* every one-time purchase forever would be the bulk of the cost and none of the value.
|
|
53
|
+
*
|
|
54
|
+
* **Not the terminal states.** `expired`, `never_paid`, `refunded`, and `revoked` are states we learned *from a
|
|
55
|
+
* store*, and re-asking cannot change them: a resubscription is a new transaction and therefore a new row on all
|
|
56
|
+
* three rails. Excluding them is also what stops a catalog's dead subscriptions being re-fetched every night
|
|
57
|
+
* forever. A row that says `active` with an expiry in the past is **not** terminal — that is precisely the row
|
|
58
|
+
* whose renewal we may have missed, and it is the one this pass exists to ask about.
|
|
59
|
+
*
|
|
60
|
+
* ## What a failure means
|
|
61
|
+
*
|
|
62
|
+
* A store that **cannot be reached** (`payments/provider_unavailable`) fails the step, so the Workflow retries
|
|
63
|
+
* it — the page is idempotent, so a retry re-asks and re-projects to the same place. Every other refusal is
|
|
64
|
+
* about one purchase or one rail and is counted rather than thrown: an unmapped store state, a SKU no longer
|
|
65
|
+
* in the catalog, a rail switched off since the row was written. A pass that aborted on the first of those
|
|
66
|
+
* would leave the rest of the catalog unreconciled to preserve a tally.
|
|
67
|
+
*/
|
|
68
|
+
/** The Workflow step runner, structurally. Injectable, so a test can run a body with no durable execution. */
|
|
69
|
+
export interface ReconcileStep {
|
|
70
|
+
/** Run a named step, or return its journalled result if this instance already completed it. */
|
|
71
|
+
do<T>(name: string, callback: () => Promise<T>): Promise<T>;
|
|
72
|
+
}
|
|
73
|
+
/** What one reconciliation run needs. Every one is a seam. */
|
|
74
|
+
export interface ReconcileDeps {
|
|
75
|
+
/** The app database the `pithy_payments_*` tables live in. */
|
|
76
|
+
d1: D1Database;
|
|
77
|
+
/** The resolved catalog — the writer's product lookup and the rails' enablement come from it. */
|
|
78
|
+
config: PaymentsConfig;
|
|
79
|
+
/** This deployment's store environment. A refreshed sandbox purchase is refused in production, as ever. */
|
|
80
|
+
environment: PurchaseEnvironment;
|
|
81
|
+
/** One page's rail access, built fresh per step so each page mints its own batch tokens. */
|
|
82
|
+
railAccess(now: Date): ReconcileRailAccess;
|
|
83
|
+
/** The clock. */
|
|
84
|
+
now(): Date;
|
|
85
|
+
/** The run id minter, injected so a test is deterministic. Defaults to `crypto.randomUUID`. */
|
|
86
|
+
newId?: () => string;
|
|
87
|
+
/**
|
|
88
|
+
* The audit seam. Drift is emitted through it, because repeated drift on one rail is the signal that the
|
|
89
|
+
* webhook path is broken and a tally in a log line is not something anybody queries. Defaults to core's
|
|
90
|
+
* no-op, which is what a host worker with no audit capability composed gets.
|
|
91
|
+
*/
|
|
92
|
+
emit?: AuditEmit;
|
|
93
|
+
/**
|
|
94
|
+
* Fulfillment for a repaired purchase — the ledger credit a `grants` product asks for.
|
|
95
|
+
*
|
|
96
|
+
* A pass that repaired a subscription renewal and did not credit its coins would leave the entitlement right
|
|
97
|
+
* and the balance wrong, and nothing else would ever fix it: the renewal the webhook lost is the same
|
|
98
|
+
* renewal this pass discovered, so whoever discovers a period is the only one who can pay for it.
|
|
99
|
+
*
|
|
100
|
+
* Injectable, and defaults to {@link fulfillPurchase}. Safe to run over an already-credited purchase — the
|
|
101
|
+
* ref is a pure function of the purchase id and the currency, and the ledger's `ref` is UNIQUE, so a credit
|
|
102
|
+
* the webhook already applied is a no-op rather than a double.
|
|
103
|
+
*/
|
|
104
|
+
fulfill?: (projection: PurchaseProjection) => Promise<unknown>;
|
|
105
|
+
/**
|
|
106
|
+
* The Paddle events sweep, or undefined to skip it.
|
|
107
|
+
*
|
|
108
|
+
* Injected rather than built here, because it needs Paddle's credentials and its own transport and this
|
|
109
|
+
* module deliberately knows nothing about any single rail — `railAccess` is the seam every other rail is
|
|
110
|
+
* reached through, and the sweep is not a rail method. The Workflow worker supplies it.
|
|
111
|
+
*
|
|
112
|
+
* Undefined on a project that does not sell through Paddle, which is why the step below is skipped
|
|
113
|
+
* entirely rather than run and reported as empty: an empty tally for a rail nobody uses reads as a rail
|
|
114
|
+
* that found nothing, which is a different statement.
|
|
115
|
+
*/
|
|
116
|
+
sweepPaddle?: () => Promise<PaddleSweepReport>;
|
|
117
|
+
}
|
|
118
|
+
/** The tally a run reports. */
|
|
119
|
+
export interface ReconcileReport {
|
|
120
|
+
/**
|
|
121
|
+
* The run's id — the row this pass wrote to `pithy_payments_reconcile_runs`, and the value every repair it
|
|
122
|
+
* audited carries as `runId`. Minted before the first page so the events can name it.
|
|
123
|
+
*/
|
|
124
|
+
runId: string;
|
|
125
|
+
/** Pages read — one durable step each. */
|
|
126
|
+
pages: number;
|
|
127
|
+
/** Purchases examined. */
|
|
128
|
+
scanned: number;
|
|
129
|
+
/** Purchases whose stored state already matched the store's. */
|
|
130
|
+
unchanged: number;
|
|
131
|
+
/** Purchases whose stored state disagreed. The number that matters: a rising one means webhooks are lost. */
|
|
132
|
+
drifted: number;
|
|
133
|
+
/**
|
|
134
|
+
* Purchases a later period of the same subscription has replaced, settled on this pass.
|
|
135
|
+
*
|
|
136
|
+
* Counted apart from `drifted` deliberately. A rail answers about the family's **current** transaction
|
|
137
|
+
* whichever row it is asked about, so an old period compared against its own successor always looks like
|
|
138
|
+
* drift — and while these were counted as drift, `drifted` measured how long the catalog had been selling
|
|
139
|
+
* rather than whether webhooks were arriving. Settling makes the old row terminal, so it leaves the scan and
|
|
140
|
+
* this number falls back to zero once a catalog's history has been walked through once.
|
|
141
|
+
*/
|
|
142
|
+
superseded: number;
|
|
143
|
+
/** Purchases no store could be asked about. */
|
|
144
|
+
skipped: number;
|
|
145
|
+
/**
|
|
146
|
+
* What the Paddle events sweep found, or undefined when it did not run.
|
|
147
|
+
*
|
|
148
|
+
* Undefined rather than a zeroed tally, and the difference is a statement: a rail nobody sells through
|
|
149
|
+
* did not sweep, where a rail that swept and found nothing is a healthy integration. Collapsing the two
|
|
150
|
+
* would make "the webhooks are fine" indistinguishable from "we never looked".
|
|
151
|
+
*/
|
|
152
|
+
swept?: PaddleSweepReport;
|
|
153
|
+
/** Purchases a store refused to answer for. */
|
|
154
|
+
failed: number;
|
|
155
|
+
/** Whether the run stopped at its page cap with more of the catalog unexamined. */
|
|
156
|
+
truncated: boolean;
|
|
157
|
+
/** Whether this run only reported. */
|
|
158
|
+
dryRun: boolean;
|
|
159
|
+
}
|
|
160
|
+
export declare function reconcilePayments(deps: ReconcileDeps, step: ReconcileStep, params?: PaymentsReconcileParams): Promise<ReconcileReport>;
|
|
161
|
+
//# sourceMappingURL=reconcile.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reconcile.d.ts","sourceRoot":"","sources":["../../src/workflows/reconcile.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAC5D,OAAO,EAAE,KAAK,SAAS,EAAY,MAAM,mCAAmC,CAAC;AAI7E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,EAAoB,KAAK,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAK9E,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAgE,KAAK,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAErH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0DG;AAEH,8GAA8G;AAC9G,MAAM,WAAW,aAAa;IAC5B,+FAA+F;IAC/F,EAAE,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;CAC7D;AAcD,8DAA8D;AAC9D,MAAM,WAAW,aAAa;IAC5B,8DAA8D;IAC9D,EAAE,EAAE,UAAU,CAAC;IACf,iGAAiG;IACjG,MAAM,EAAE,cAAc,CAAC;IACvB,2GAA2G;IAC3G,WAAW,EAAE,mBAAmB,CAAC;IACjC,4FAA4F;IAC5F,UAAU,CAAC,GAAG,EAAE,IAAI,GAAG,mBAAmB,CAAC;IAC3C,iBAAiB;IACjB,GAAG,IAAI,IAAI,CAAC;IACZ,+FAA+F;IAC/F,KAAK,CAAC,EAAE,MAAM,MAAM,CAAC;IACrB;;;;OAIG;IACH,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB;;;;;;;;;;OAUG;IACH,OAAO,CAAC,EAAE,CAAC,UAAU,EAAE,kBAAkB,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAC/D;;;;;;;;;;OAUG;IACH,WAAW,CAAC,EAAE,MAAM,OAAO,CAAC,iBAAiB,CAAC,CAAC;CAChD;AAyBD,+BAA+B;AAC/B,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;IACd,0CAA0C;IAC1C,KAAK,EAAE,MAAM,CAAC;IACd,0BAA0B;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,SAAS,EAAE,MAAM,CAAC;IAClB,6GAA6G;IAC7G,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;;;;OAQG;IACH,UAAU,EAAE,MAAM,CAAC;IACnB,+CAA+C;IAC/C,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;;OAMG;IACH,KAAK,CAAC,EAAE,iBAAiB,CAAC;IAC1B,+CAA+C;IAC/C,MAAM,EAAE,MAAM,CAAC;IACf,mFAAmF;IACnF,SAAS,EAAE,OAAO,CAAC;IACnB,sCAAsC;IACtC,MAAM,EAAE,OAAO,CAAC;CACjB;AA0PD,wBAAsB,iBAAiB,CACrC,IAAI,EAAE,aAAa,EACnB,IAAI,EAAE,aAAa,EACnB,MAAM,GAAE,uBAA4B,GACnC,OAAO,CAAC,eAAe,CAAC,CAyL1B"}
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { PaymentsPurchase } from "../data/purchase.js";
|
|
2
|
+
import { recordReconcileRun } from "../data/reconcileRun.js";
|
|
3
|
+
import { PAYMENTS_PURCHASES_TABLE, paymentsDatabase } from "../data/tables.js";
|
|
4
|
+
import { PaymentsAuditActions } from "../audit/actions.js";
|
|
5
|
+
import { fulfillPurchase } from "../grants/apply.js";
|
|
6
|
+
import { projectPurchase } from "../projection/writer.js";
|
|
7
|
+
import "./specs.js";
|
|
8
|
+
import { PithyError } from "@pithy-sh/core/src/error/pithyError";
|
|
9
|
+
import { SQLiteDate } from "@pithy-sh/core/src/data/codecs";
|
|
10
|
+
import { noopEmit } from "@pithy-sh/core/src/audit/recorder";
|
|
11
|
+
//#region src/workflows/reconcile.ts
|
|
12
|
+
/** Purchases per page when the caller names none. Bounded so one page fits inside one minted token's life. */
|
|
13
|
+
const DEFAULT_PAGE_SIZE = 100;
|
|
14
|
+
/** Pages per run when the caller names none. Ten thousand purchases is a long pass and a sane ceiling. */
|
|
15
|
+
const DEFAULT_MAX_PAGES = 100;
|
|
16
|
+
/**
|
|
17
|
+
* The statuses a pass never re-asks about. Each was reported by a store and cannot become anything else on the
|
|
18
|
+
* same transaction — see the module doc on why `active` with a past expiry is deliberately not among them.
|
|
19
|
+
*/
|
|
20
|
+
const TERMINAL_STATUSES = [
|
|
21
|
+
"expired",
|
|
22
|
+
"never_paid",
|
|
23
|
+
"refunded",
|
|
24
|
+
"revoked"
|
|
25
|
+
];
|
|
26
|
+
/** The state a drift comparison reads off a row or a refreshed event. */
|
|
27
|
+
function stateOf(value) {
|
|
28
|
+
return {
|
|
29
|
+
status: value.status,
|
|
30
|
+
expiresAt: value.expiresAt?.getTime() ?? null
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/** Whether the store's answer says something different from what is stored. */
|
|
34
|
+
function drifted(before, after) {
|
|
35
|
+
return before.status !== after.status || before.expiresAt !== after.expiresAt;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* One page of purchases to reconcile.
|
|
39
|
+
*
|
|
40
|
+
* The predicate is the pass's whole selection policy: a live subscription that is near or past its expiry, or
|
|
41
|
+
* one nothing has verified lately. `id > cursor` in id order is what makes it resumable — see the module doc.
|
|
42
|
+
*/
|
|
43
|
+
async function readPage(deps, params, cursor, cutoffs, limit) {
|
|
44
|
+
let query = paymentsDatabase(deps.d1).selectFrom(PAYMENTS_PURCHASES_TABLE).selectAll().where("type", "=", "subscription").where("status", "not in", [...TERMINAL_STATUSES]).where((eb) => eb.or([eb("expiresAt", "<", SQLiteDate.encode(cutoffs.expiring)), eb("updatedAt", "<", SQLiteDate.encode(cutoffs.stale))])).orderBy("id").limit(limit);
|
|
45
|
+
if (cursor !== null) query = query.where("id", ">", cursor);
|
|
46
|
+
/**
|
|
47
|
+
* The holder narrowing — **both halves in one guard, or no narrowing at all**.
|
|
48
|
+
*
|
|
49
|
+
* The schema already refuses half a subject, so this is the second line rather than the first. It is
|
|
50
|
+
* written as one condition because the failure it prevents is the tempting one: two independent `if`s
|
|
51
|
+
* would let `subjectId` alone reach the query, and `where subject_id = 'acme'` matches the user *and* the
|
|
52
|
+
* organization called `acme` — nothing keeps those id spaces apart. The support pass would then reconcile
|
|
53
|
+
* a stranger's purchases and report the count as the account it was asked about.
|
|
54
|
+
*
|
|
55
|
+
* Widening when only one half arrives is the safe direction: a pass over the whole catalog is slow and
|
|
56
|
+
* correct, where a pass over the wrong holder is fast and wrong.
|
|
57
|
+
*/
|
|
58
|
+
if (params.subjectType !== void 0 && params.subjectId !== void 0) query = query.where("subjectType", "=", params.subjectType).where("subjectId", "=", params.subjectId);
|
|
59
|
+
if (params.rail !== void 0) query = query.where("rail", "=", params.rail);
|
|
60
|
+
return (await query.execute()).map((row) => PaymentsPurchase.parse(row));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Reconcile one purchase: ask its rail, compare, and project any disagreement through the **same writer** the
|
|
64
|
+
* webhook and the client submission use.
|
|
65
|
+
*
|
|
66
|
+
* Projecting through the writer rather than updating the row is what keeps the whole design's central claim
|
|
67
|
+
* true — one idempotent projection, three triggers, one row. It also means a repair re-derives the entitlement
|
|
68
|
+
* rows in the same D1 transaction, so a subscription found to have lapsed stops granting in the same commit
|
|
69
|
+
* that records the lapse.
|
|
70
|
+
*/
|
|
71
|
+
async function reconcileOne(deps, runId, access, purchase, now, dryRun) {
|
|
72
|
+
let refreshed;
|
|
73
|
+
try {
|
|
74
|
+
refreshed = await (await access.providerFor(purchase.rail)).refresh(purchase, { now });
|
|
75
|
+
} catch (cause) {
|
|
76
|
+
if (cause instanceof PithyError && cause.payload.code === "payments/provider_unavailable") throw cause;
|
|
77
|
+
return "failed";
|
|
78
|
+
}
|
|
79
|
+
if (refreshed === void 0) return "skipped";
|
|
80
|
+
/**
|
|
81
|
+
* The store answered about a **different** transaction, which means this row is a period that has ended and
|
|
82
|
+
* the one that replaced it is elsewhere in the table.
|
|
83
|
+
*
|
|
84
|
+
* Every rail's `refresh` looks up the subscription family, not the row it was handed — it has to, because a
|
|
85
|
+
* renewal is a new transaction and the family is the only handle a store offers. So comparing this row
|
|
86
|
+
* against that answer compares two different purchases, and it always disagrees: the old period's expiry is
|
|
87
|
+
* in the past and the new one's is not. That disagreement is not drift, and counting it as drift is what made
|
|
88
|
+
* a year-old subscriber contribute twelve permanent "repairs" every night.
|
|
89
|
+
*
|
|
90
|
+
* Settling the row once is what ends it. Projecting it `expired` at its own expiry puts it in
|
|
91
|
+
* `TERMINAL_STATUSES`, so the next pass does not select it, does not ask the store about it, and does not
|
|
92
|
+
* report it — and the entitlement is unaffected, because the successor grants it and the derivation reads
|
|
93
|
+
* every purchase for the key.
|
|
94
|
+
*/
|
|
95
|
+
if (refreshed.providerTransactionId !== purchase.providerTransactionId) {
|
|
96
|
+
if (dryRun) return "superseded";
|
|
97
|
+
try {
|
|
98
|
+
await projectPurchase(deps.d1, {
|
|
99
|
+
...purchase,
|
|
100
|
+
status: "expired",
|
|
101
|
+
providerEventAt: now,
|
|
102
|
+
payload: { supersededBy: refreshed.providerTransactionId }
|
|
103
|
+
}, {
|
|
104
|
+
config: deps.config,
|
|
105
|
+
environment: deps.environment,
|
|
106
|
+
now
|
|
107
|
+
});
|
|
108
|
+
} catch (cause) {
|
|
109
|
+
if (cause instanceof PithyError) return "failed";
|
|
110
|
+
throw cause;
|
|
111
|
+
}
|
|
112
|
+
return "superseded";
|
|
113
|
+
}
|
|
114
|
+
const before = stateOf(purchase);
|
|
115
|
+
if (dryRun) return drifted(before, stateOf({
|
|
116
|
+
status: refreshed.status,
|
|
117
|
+
expiresAt: refreshed.expiresAt ?? null
|
|
118
|
+
})) ? "drifted" : "unchanged";
|
|
119
|
+
let projection;
|
|
120
|
+
try {
|
|
121
|
+
projection = await projectPurchase(deps.d1, {
|
|
122
|
+
...refreshed,
|
|
123
|
+
subjectType: purchase.subjectType,
|
|
124
|
+
subjectId: purchase.subjectId
|
|
125
|
+
}, {
|
|
126
|
+
config: deps.config,
|
|
127
|
+
environment: deps.environment,
|
|
128
|
+
now
|
|
129
|
+
});
|
|
130
|
+
} catch (cause) {
|
|
131
|
+
if (cause instanceof PithyError) return "failed";
|
|
132
|
+
throw cause;
|
|
133
|
+
}
|
|
134
|
+
const after = stateOf(projection.purchase);
|
|
135
|
+
if (!drifted(before, after)) return "unchanged";
|
|
136
|
+
await emitDrift(deps, runId, purchase, before, after);
|
|
137
|
+
/**
|
|
138
|
+
* Fulfillment, on drift only.
|
|
139
|
+
*
|
|
140
|
+
* Every rail dates a refresh `now` — it has to, or the monotonic write rule would discard the repair — so
|
|
141
|
+
* the writer reports `updated` for every row a pass touches. Keying fulfillment on that outcome would mean
|
|
142
|
+
* ten thousand ledger calls per pass, each one a no-op against a ref that already exists. Drift is the
|
|
143
|
+
* signal that actually distinguishes a repair from a confirmation, and a repair is the only one that can owe
|
|
144
|
+
* anybody coins.
|
|
145
|
+
*/
|
|
146
|
+
const fulfill = deps.fulfill ?? ((repaired) => fulfillPurchase(deps.d1, repaired, {
|
|
147
|
+
config: deps.config,
|
|
148
|
+
emit: deps.emit,
|
|
149
|
+
now: () => now.getTime()
|
|
150
|
+
}));
|
|
151
|
+
try {
|
|
152
|
+
await fulfill(projection);
|
|
153
|
+
} catch (cause) {
|
|
154
|
+
if (cause instanceof PithyError) return "failed";
|
|
155
|
+
throw cause;
|
|
156
|
+
}
|
|
157
|
+
return "drifted";
|
|
158
|
+
}
|
|
159
|
+
/** Record a repaired disagreement. The one audit event that is about this deployment rather than a caller. */
|
|
160
|
+
async function emitDrift(deps, runId, purchase, before, after) {
|
|
161
|
+
await (deps.emit ?? noopEmit)({
|
|
162
|
+
action: PaymentsAuditActions.purchaseReconciled,
|
|
163
|
+
outcome: "success",
|
|
164
|
+
severity: "warning",
|
|
165
|
+
actorType: "service",
|
|
166
|
+
actorId: PaymentsAuditActions.purchaseReconciled,
|
|
167
|
+
resourceType: "purchase",
|
|
168
|
+
resourceId: purchase.id,
|
|
169
|
+
metadata: {
|
|
170
|
+
runId,
|
|
171
|
+
rail: purchase.rail,
|
|
172
|
+
productId: purchase.productId,
|
|
173
|
+
from: before.status,
|
|
174
|
+
to: after.status,
|
|
175
|
+
expiryChanged: before.expiresAt !== after.expiresAt
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Run one reconciliation pass. Idempotent by construction — a second run over a reconciled catalog finds every
|
|
181
|
+
* row already matching and writes nothing — so a retried Workflow step, a manual trigger after a cron, and a
|
|
182
|
+
* nervous operator running it twice all reach the same place.
|
|
183
|
+
*/
|
|
184
|
+
/** What a sweep that could not run reports. Never written to a report — see `ReconcileReport.swept`. */
|
|
185
|
+
const EMPTY_SWEEP = {
|
|
186
|
+
read: 0,
|
|
187
|
+
projected: 0,
|
|
188
|
+
ignored: 0,
|
|
189
|
+
orphaned: [],
|
|
190
|
+
duplicate: 0,
|
|
191
|
+
failed: 0,
|
|
192
|
+
quarantined: [],
|
|
193
|
+
cursor: null,
|
|
194
|
+
gap: null
|
|
195
|
+
};
|
|
196
|
+
async function reconcilePayments(deps, step, params = {}) {
|
|
197
|
+
const limit = params.pageSize ?? DEFAULT_PAGE_SIZE;
|
|
198
|
+
const maxPages = params.maxPages ?? DEFAULT_MAX_PAGES;
|
|
199
|
+
const dryRun = params.dryRun === true;
|
|
200
|
+
/**
|
|
201
|
+
* The run's identity and its clock, minted **once, in one journalled step**, and read back from the step's
|
|
202
|
+
* return value.
|
|
203
|
+
*
|
|
204
|
+
* **Why a step.** A Workflow does not resume inside the step it died in; it re-executes this body from the
|
|
205
|
+
* top and serves every completed step from the journal. So anything the body *computes* answers differently
|
|
206
|
+
* on a resume, while everything the journal holds does not — and a run's id and its start instant are both
|
|
207
|
+
* things every page behind the interruption has already been written under.
|
|
208
|
+
*
|
|
209
|
+
* The id was moved here first (#328): a second mint made the run record name an id no repair carried, which
|
|
210
|
+
* broke the runs table's only join. The clock was left in the body one release longer, and it was the same
|
|
211
|
+
* defect one field over (#331) — `startedAt` was recomputed on resume, so a run interrupted at nine and
|
|
212
|
+
* resumed at three claimed to have begun six hours *after* the repairs it names. A pass's start instant is
|
|
213
|
+
* not "when this attempt got going"; it is when the pass got going.
|
|
214
|
+
*
|
|
215
|
+
* It is also the comparison instant every page judges against, and journalling it is right there too: the
|
|
216
|
+
* pages already run were selected and dated against it, and a resume that widened the windows underneath
|
|
217
|
+
* them would make one pass two different queries.
|
|
218
|
+
*
|
|
219
|
+
* Epoch milliseconds rather than a `Date`, because a journal round-trips JSON and a `Date` would come back
|
|
220
|
+
* a string on the resume and an object on the first pass — a difference that shows up as a type error six
|
|
221
|
+
* frames away rather than here.
|
|
222
|
+
*/
|
|
223
|
+
const context = await step.do("start-run", async () => ({
|
|
224
|
+
runId: (deps.newId ?? (() => crypto.randomUUID()))(),
|
|
225
|
+
nowMs: deps.now().getTime()
|
|
226
|
+
}));
|
|
227
|
+
const runId = context.runId;
|
|
228
|
+
const now = new Date(context.nowMs);
|
|
229
|
+
const cutoffs = {
|
|
230
|
+
expiring: new Date(now.getTime() + (params.expiringWithinSeconds ?? 172800) * 1e3),
|
|
231
|
+
stale: /* @__PURE__ */ new Date(now.getTime() - (params.staleAfterSeconds ?? 604800) * 1e3)
|
|
232
|
+
};
|
|
233
|
+
const report = {
|
|
234
|
+
runId,
|
|
235
|
+
pages: 0,
|
|
236
|
+
scanned: 0,
|
|
237
|
+
unchanged: 0,
|
|
238
|
+
drifted: 0,
|
|
239
|
+
superseded: 0,
|
|
240
|
+
skipped: 0,
|
|
241
|
+
failed: 0,
|
|
242
|
+
truncated: false,
|
|
243
|
+
dryRun
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* The Paddle events sweep, before the pages — the repair `refresh` cannot make.
|
|
247
|
+
*
|
|
248
|
+
* `refresh` re-reads rows this deployment already holds, so a purchase whose webhook was never delivered
|
|
249
|
+
* and which no client submitted is invisible to it forever. Paddle publishes an account-wide event
|
|
250
|
+
* stream, so it can be found; no other rail here can do this.
|
|
251
|
+
*
|
|
252
|
+
* **First, so what it discovers is then reconciled by the same run.** A subscription the sweep projects
|
|
253
|
+
* is a row the pages below will consider, which is one pass rather than two.
|
|
254
|
+
*
|
|
255
|
+
* Its own `step.do`, so a transient fault retries the sweep rather than losing a whole run's pages. It
|
|
256
|
+
* is skipped entirely when the pass was narrowed to another rail — `--rail stripe` means Stripe.
|
|
257
|
+
*/
|
|
258
|
+
if (deps.sweepPaddle && (params.rail === void 0 || params.rail === "paddle")) {
|
|
259
|
+
const swept = await step.do("sweep-paddle", async () => await deps.sweepPaddle?.() ?? EMPTY_SWEEP);
|
|
260
|
+
report.swept = swept;
|
|
261
|
+
if (swept.gap !== null) await (deps.emit ?? noopEmit)({
|
|
262
|
+
action: PaymentsAuditActions.purchaseReconciled,
|
|
263
|
+
outcome: "failure",
|
|
264
|
+
severity: "warning",
|
|
265
|
+
actorType: "service",
|
|
266
|
+
actorId: "paddle",
|
|
267
|
+
metadata: {
|
|
268
|
+
rail: "paddle",
|
|
269
|
+
runId,
|
|
270
|
+
reason: "sweep_gap",
|
|
271
|
+
detail: swept.gap
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
let cursor = null;
|
|
276
|
+
let page = 0;
|
|
277
|
+
for (;;) {
|
|
278
|
+
page += 1;
|
|
279
|
+
const after = cursor;
|
|
280
|
+
const result = await step.do(`page-${String(page).padStart(6, "0")}`, async () => {
|
|
281
|
+
const rows = await readPage(deps, params, after, cutoffs, limit);
|
|
282
|
+
if (rows.length === 0) return {
|
|
283
|
+
cursor: after,
|
|
284
|
+
scanned: 0,
|
|
285
|
+
unchanged: 0,
|
|
286
|
+
drifted: 0,
|
|
287
|
+
superseded: 0,
|
|
288
|
+
skipped: 0,
|
|
289
|
+
failed: 0,
|
|
290
|
+
done: true
|
|
291
|
+
};
|
|
292
|
+
const access = deps.railAccess(now);
|
|
293
|
+
const tally = {
|
|
294
|
+
unchanged: 0,
|
|
295
|
+
drifted: 0,
|
|
296
|
+
superseded: 0,
|
|
297
|
+
skipped: 0,
|
|
298
|
+
failed: 0
|
|
299
|
+
};
|
|
300
|
+
for (const purchase of rows) tally[await reconcileOne(deps, runId, access, purchase, now, dryRun)] += 1;
|
|
301
|
+
return {
|
|
302
|
+
cursor: rows[rows.length - 1].id,
|
|
303
|
+
scanned: rows.length,
|
|
304
|
+
...tally,
|
|
305
|
+
done: rows.length < limit
|
|
306
|
+
};
|
|
307
|
+
});
|
|
308
|
+
report.pages = page;
|
|
309
|
+
report.scanned += result.scanned;
|
|
310
|
+
report.unchanged += result.unchanged;
|
|
311
|
+
report.drifted += result.drifted;
|
|
312
|
+
report.superseded += result.superseded;
|
|
313
|
+
report.skipped += result.skipped;
|
|
314
|
+
report.failed += result.failed;
|
|
315
|
+
cursor = result.cursor;
|
|
316
|
+
if (result.done) break;
|
|
317
|
+
if (page >= maxPages) {
|
|
318
|
+
report.truncated = true;
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* The run, kept.
|
|
324
|
+
*
|
|
325
|
+
* **A run that repaired nothing is written too**, and that is the load-bearing half: *"it ran and found
|
|
326
|
+
* nothing"* is the answer to *"is this integration healthy"*, and a table holding only the exceptional
|
|
327
|
+
* passes makes silence ambiguous with a cron that stopped firing — which is the failure the table exists
|
|
328
|
+
* to make visible.
|
|
329
|
+
*
|
|
330
|
+
* Its own `step.do`, so a D1 hiccup retries the write rather than losing the record of a pass that did
|
|
331
|
+
* real work. The writer is idempotent on the run's id, so a replayed step lands on the row it already
|
|
332
|
+
* wrote instead of counting one pass twice. It is the last step and never the first: a row claiming a
|
|
333
|
+
* finish time before the pass had one would be a lie the health read would then repeat.
|
|
334
|
+
*
|
|
335
|
+
* `finishedAt` is read from the clock again rather than reused from `now`, because `now` is the pass's
|
|
336
|
+
* comparison instant and a run over ten thousand purchases is not instantaneous.
|
|
337
|
+
*/
|
|
338
|
+
await step.do("record-run", async () => {
|
|
339
|
+
await recordReconcileRun(deps.d1, {
|
|
340
|
+
id: runId,
|
|
341
|
+
startedAt: now,
|
|
342
|
+
finishedAt: deps.now(),
|
|
343
|
+
environment: deps.environment,
|
|
344
|
+
rail: params.rail ?? null,
|
|
345
|
+
report
|
|
346
|
+
}, { now: deps.now() });
|
|
347
|
+
return null;
|
|
348
|
+
});
|
|
349
|
+
return report;
|
|
350
|
+
}
|
|
351
|
+
//#endregion
|
|
352
|
+
export { reconcilePayments };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AuditEmit } from "@pithy-sh/core/src/audit/recorder";
|
|
2
|
+
import type { Logger } from "@pithy-sh/core/src/logger/logger";
|
|
3
|
+
import type { ReconcileReport } from "./reconcile";
|
|
4
|
+
/**
|
|
5
|
+
* The audit emitter a standalone host can honestly offer: a structured record per repaired purchase.
|
|
6
|
+
*
|
|
7
|
+
* A host worker composes no capabilities, so there is no `@pithy-sh/audit` recorder to reach and no
|
|
8
|
+
* `c.var.emit` to inherit — writing to `pithy_audit_events` from here would mean this package reaching into
|
|
9
|
+
* another capability's tables, which is exactly what the seam exists to prevent. A logged event is what the
|
|
10
|
+
* seam degrades to everywhere else it is uncomposed, and it keeps the drift visible where an operator reads
|
|
11
|
+
* this worker's output. The event shape is core's, so a recorder wired in later takes the identical input.
|
|
12
|
+
*
|
|
13
|
+
* It takes the run's logger rather than building its own, so every event carries the instance the repair
|
|
14
|
+
* happened in — without that, a trail read out of Workers Logs cannot be attributed to a pass.
|
|
15
|
+
*/
|
|
16
|
+
export declare function auditLogEmit(log: Logger): AuditEmit;
|
|
17
|
+
/**
|
|
18
|
+
* The tally, as one record. The run's only visible output: a pass whose findings are invisible is a pass
|
|
19
|
+
* nobody can tell has stopped working.
|
|
20
|
+
*
|
|
21
|
+
* Three levels, and the distinction is the point of logging it at all. `failed` counts purchases a store
|
|
22
|
+
* refused to answer for — a failure that was observed, so an operator has something to chase. `drifted`
|
|
23
|
+
* and `truncated` are the degraded-but-continuing pair: the first says webhooks are being dropped and the
|
|
24
|
+
* repair is covering for them, the second says the page cap stopped the pass with catalog left unread. A
|
|
25
|
+
* clean pass is routine, and a nightly job that reports routine at `warn` teaches an operator to ignore it.
|
|
26
|
+
*/
|
|
27
|
+
export declare function logReconcileReport(log: Logger, report: ReconcileReport): void;
|
|
28
|
+
//# sourceMappingURL=report.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"report.d.ts","sourceRoot":"","sources":["../../src/workflows/report.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAE/D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAqBnD;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAQnD;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,eAAe,GAAG,IAAI,CAG7E"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//#region src/workflows/report.ts
|
|
2
|
+
/**
|
|
3
|
+
* What a reconcile pass says about itself: its tally, and each repair it had to make.
|
|
4
|
+
*
|
|
5
|
+
* Both are pure functions of a logger and a value, and both used to live in `worker.ts` beside the
|
|
6
|
+
* Workflow class that calls them. That module imports `cloudflare:workers`, which resolves in workerd
|
|
7
|
+
* and nowhere else, so everything it exported was reachable only from inside the Workers runtime — a
|
|
8
|
+
* Node-side caller taking one of these would have taken the whole runtime module with it and failed
|
|
9
|
+
* with `Could not load pithy.config.ts`, naming the config rather than the import.
|
|
10
|
+
*
|
|
11
|
+
* That is #172 and #180, twice, and neither was noticed until somebody accepted the offer. The shape
|
|
12
|
+
* both were fixed into is this one: a sibling module with no runtime import, which the runtime module
|
|
13
|
+
* imports from. `configEntrypoints.test.ts` states the invariant and holds every runtime module to it.
|
|
14
|
+
*
|
|
15
|
+
* Nothing here needs a binding, a request, or a Workers global, which is why its tests are node tests.
|
|
16
|
+
*/
|
|
17
|
+
/** An audit event ranks itself; this is that rank as a log level. */
|
|
18
|
+
const AUDIT_LEVEL = {
|
|
19
|
+
info: "info",
|
|
20
|
+
warning: "warn",
|
|
21
|
+
critical: "error"
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* The audit emitter a standalone host can honestly offer: a structured record per repaired purchase.
|
|
25
|
+
*
|
|
26
|
+
* A host worker composes no capabilities, so there is no `@pithy-sh/audit` recorder to reach and no
|
|
27
|
+
* `c.var.emit` to inherit — writing to `pithy_audit_events` from here would mean this package reaching into
|
|
28
|
+
* another capability's tables, which is exactly what the seam exists to prevent. A logged event is what the
|
|
29
|
+
* seam degrades to everywhere else it is uncomposed, and it keeps the drift visible where an operator reads
|
|
30
|
+
* this worker's output. The event shape is core's, so a recorder wired in later takes the identical input.
|
|
31
|
+
*
|
|
32
|
+
* It takes the run's logger rather than building its own, so every event carries the instance the repair
|
|
33
|
+
* happened in — without that, a trail read out of Workers Logs cannot be attributed to a pass.
|
|
34
|
+
*/
|
|
35
|
+
function auditLogEmit(log) {
|
|
36
|
+
return async (event) => {
|
|
37
|
+
log[AUDIT_LEVEL[event.severity ?? "info"]]("audit event, no recorder composed", { ...event });
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The tally, as one record. The run's only visible output: a pass whose findings are invisible is a pass
|
|
42
|
+
* nobody can tell has stopped working.
|
|
43
|
+
*
|
|
44
|
+
* Three levels, and the distinction is the point of logging it at all. `failed` counts purchases a store
|
|
45
|
+
* refused to answer for — a failure that was observed, so an operator has something to chase. `drifted`
|
|
46
|
+
* and `truncated` are the degraded-but-continuing pair: the first says webhooks are being dropped and the
|
|
47
|
+
* repair is covering for them, the second says the page cap stopped the pass with catalog left unread. A
|
|
48
|
+
* clean pass is routine, and a nightly job that reports routine at `warn` teaches an operator to ignore it.
|
|
49
|
+
*/
|
|
50
|
+
function logReconcileReport(log, report) {
|
|
51
|
+
log[report.failed > 0 ? "error" : report.drifted > 0 || report.truncated ? "warn" : "info"]("reconcile pass complete", { ...report });
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
export { auditLogEmit, logReconcileReport };
|