@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,206 @@
|
|
|
1
|
+
import { PaymentsWebhookEvent, isWebhookEventFinished, webhookEventState } from "../data/webhookEvent.js";
|
|
2
|
+
import { PAYMENTS_WEBHOOK_EVENTS_TABLE, paymentsDatabase } from "../data/tables.js";
|
|
3
|
+
import { PaymentsWebhookUnverifiedError } from "../error/errors.js";
|
|
4
|
+
import { PaymentsAuditActions } from "../audit/actions.js";
|
|
5
|
+
import { PAYMENTS_PROVIDER_SECRET, paymentsSecretsRegistry } from "../secret/registry.js";
|
|
6
|
+
import { resolveRailProvider } from "../rails/providers.js";
|
|
7
|
+
import { InternalError, PithyError } from "@pithy-sh/core/src/error/pithyError";
|
|
8
|
+
import { withD1Retry } from "@pithy-sh/core/src/data/withD1Retry";
|
|
9
|
+
import { safeEmit } from "@pithy-sh/core/src/controlPlane/audit/actions";
|
|
10
|
+
import { sharedSecretsStore } from "@pithy-sh/secrets/src/sharedSecretsStore";
|
|
11
|
+
//#region src/http/webhookGuard.ts
|
|
12
|
+
/**
|
|
13
|
+
* The `signed-webhook` gate — the repo's first, and the shape the Google and Stripe webhooks will reuse.
|
|
14
|
+
*
|
|
15
|
+
* ## Why it reads the body itself
|
|
16
|
+
*
|
|
17
|
+
* All three rails prove authenticity over the **exact received bytes**: Apple signs them, Stripe HMACs them,
|
|
18
|
+
* Google signs a token alongside them. A parsed-and-re-serialized object is different bytes — key order alone
|
|
19
|
+
* changes it — so the gate must see what arrived, not a reconstruction. That is why the Biome plugin bans
|
|
20
|
+
* `c.req.json()` under `src/http/**` but deliberately allows `c.req.text()`, `c.req.header()`, and
|
|
21
|
+
* `c.req.raw`: reading the body for a signature check is the sanctioned use, and this is the module that has
|
|
22
|
+
* it.
|
|
23
|
+
*
|
|
24
|
+
* `c.req.text()` rather than `c.req.raw.text()`, and that distinction matters. Hono caches its own body reads,
|
|
25
|
+
* so the route's `zValidator("json", …)` afterwards is served from the same cache and sees identical bytes.
|
|
26
|
+
* Reading `c.req.raw` directly would consume the stream and leave the validator with nothing.
|
|
27
|
+
*
|
|
28
|
+
* ## Why it persists before it hands off
|
|
29
|
+
*
|
|
30
|
+
* Every provider delivers at-least-once and retries, so a redelivery is expected rather than exceptional. The
|
|
31
|
+
* row goes in first, keyed `UNIQUE (rail, providerEventId)`, and a delivery that has already been **finished
|
|
32
|
+
* with** short-circuits with 200 instead of running again. Two things follow from that. A retry storm costs
|
|
33
|
+
* one insert instead of a projection. And "why didn't this renew" becomes answerable: `receivedAt` says
|
|
34
|
+
* whether the notification arrived at all, `processedAt` whether it was finished with, `abandonedAt` whether
|
|
35
|
+
* a repair pass gave up on it, and `error` why.
|
|
36
|
+
*
|
|
37
|
+
* **Finished, not seen.** The distinction is the whole of #337 and it is worth the sentence. A delivery that
|
|
38
|
+
* arrived and failed to project must be reprocessed by the next one — that is the only repair path this
|
|
39
|
+
* package has, because a provider's retry and an operator's replay both reuse the original event id. So the
|
|
40
|
+
* short-circuit reads {@link isWebhookEventFinished}, and only a row nothing is left to do with is answered
|
|
41
|
+
* as a duplicate.
|
|
42
|
+
*
|
|
43
|
+
* A forgery is never persisted. Verification comes first, so the table holds only notifications the store
|
|
44
|
+
* actually sent — otherwise anyone could fill it.
|
|
45
|
+
*
|
|
46
|
+
* ## Why the verified payload travels in a WeakMap
|
|
47
|
+
*
|
|
48
|
+
* The gate has already decoded the notification in order to verify it, and the handler needs that result. The
|
|
49
|
+
* options were a `ContextVariableMap` augmentation, which would put a payments-only variable on the types of
|
|
50
|
+
* every Hono app in an adopter's project including the ones that never compose payments; re-exporting an
|
|
51
|
+
* unverified decoder for the handler to call, which is a function whose only safe use is the one place it is
|
|
52
|
+
* called from; or this — a request-scoped map keyed on the `Request` object, read through an accessor that
|
|
53
|
+
* throws when the gate did not run. The last is the only one with no way to reach an unverified payload and no
|
|
54
|
+
* effect outside this package.
|
|
55
|
+
*/
|
|
56
|
+
/**
|
|
57
|
+
* Our own name for what refused a delivery, for the audit row's `metadata.step`.
|
|
58
|
+
*
|
|
59
|
+
* Derived from the rail's error **code**, never from its `detail` or a raw `Error.message`: a code is a value
|
|
60
|
+
* this package defined, where a message can carry text a sender influenced, and the trail must hold nothing a
|
|
61
|
+
* forger wrote. An unrecognized throw is `unknown` rather than its message, for the same reason.
|
|
62
|
+
*
|
|
63
|
+
* `detail` on the 401 still carries the full reason for the operator reading logs — this is the queryable
|
|
64
|
+
* half, and it is deliberately coarser.
|
|
65
|
+
*/
|
|
66
|
+
function failingStep(cause) {
|
|
67
|
+
return cause instanceof PithyError ? cause.payload.code : "unknown";
|
|
68
|
+
}
|
|
69
|
+
/** The codes a rail may raise that describe our side rather than the sender's, and so keep their own status. */
|
|
70
|
+
const PASS_THROUGH_CODES = /* @__PURE__ */ new Set(["payments/provider_unavailable", "payments/rail_not_configured"]);
|
|
71
|
+
/**
|
|
72
|
+
* Request-scoped, keyed on the `Request` object so an entry cannot outlive its request and cannot be reached
|
|
73
|
+
* from another one. Weak, so a dropped request drops its entry with no bookkeeping.
|
|
74
|
+
*/
|
|
75
|
+
const verified = /* @__PURE__ */ new WeakMap();
|
|
76
|
+
/**
|
|
77
|
+
* The verified delivery for this request. Throws when the gate did not run, because a handler reading an
|
|
78
|
+
* absent verification would be a handler processing an unverified notification.
|
|
79
|
+
*/
|
|
80
|
+
function verifiedWebhook(c) {
|
|
81
|
+
const found = verified.get(c.req.raw);
|
|
82
|
+
if (!found) throw new InternalError({ detail: "A payments webhook handler ran without its signed-webhook guard. Put the guard on the route line." });
|
|
83
|
+
return found;
|
|
84
|
+
}
|
|
85
|
+
/** The app `DB` binding, or a wiring failure. */
|
|
86
|
+
function database(c) {
|
|
87
|
+
const binding = c.env.DB;
|
|
88
|
+
if (!binding) throw new InternalError({
|
|
89
|
+
message: "Payments is not configured.",
|
|
90
|
+
action: "Bind a D1 database named DB in wrangler.jsonc.",
|
|
91
|
+
detail: "Payments requires a `DB` D1 binding; none was present on env."
|
|
92
|
+
});
|
|
93
|
+
return binding;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Verify, record, and de-duplicate one rail's notification.
|
|
97
|
+
*
|
|
98
|
+
* Every failure to establish authenticity becomes `payments/webhook_unverified` (401), whatever the rail threw
|
|
99
|
+
* underneath. The rails distinguish malformed from unverified for the client-submission path, where the
|
|
100
|
+
* distinction helps a developer; on a webhook it would only tell a forger how close it got. The original code
|
|
101
|
+
* rides in `detail`, which the HTTP codec strips.
|
|
102
|
+
*
|
|
103
|
+
* **Two codes pass through unchanged, because they are not the caller's failure.** A rail that cannot reach its
|
|
104
|
+
* store (`payments/provider_unavailable`) or cannot use its own credentials (`payments/rail_not_configured`) has
|
|
105
|
+
* not judged the delivery at all — Google's rail must call the Play Developer API to resolve a notification, and
|
|
106
|
+
* an outage there is ours, not the sender's. Reporting either as a failed signature would send an operator
|
|
107
|
+
* hunting for a rotated key while the real answer is in a status page, and would tell the audit trail a forgery
|
|
108
|
+
* arrived when none did. Both remain non-2xx, so the provider still redelivers.
|
|
109
|
+
*/
|
|
110
|
+
function requireSignedWebhook(rail, options) {
|
|
111
|
+
return async (c, next) => {
|
|
112
|
+
const now = options.now?.() ?? /* @__PURE__ */ new Date();
|
|
113
|
+
const secrets = await sharedSecretsStore(c.env, paymentsSecretsRegistry);
|
|
114
|
+
const provider = resolveRailProvider(rail, options.config, secrets.get(PAYMENTS_PROVIDER_SECRET), options.trust ?? {});
|
|
115
|
+
const body = await c.req.text();
|
|
116
|
+
let notification;
|
|
117
|
+
try {
|
|
118
|
+
const deployment = c.env.ENVIRONMENT;
|
|
119
|
+
notification = await provider.parseNotification({
|
|
120
|
+
body,
|
|
121
|
+
headers: c.req.raw.headers
|
|
122
|
+
}, {
|
|
123
|
+
now,
|
|
124
|
+
deployment: typeof deployment === "string" && deployment !== "" ? deployment : void 0
|
|
125
|
+
});
|
|
126
|
+
} catch (cause) {
|
|
127
|
+
if (cause instanceof PithyError && PASS_THROUGH_CODES.has(cause.payload.code)) throw cause;
|
|
128
|
+
await safeEmit(c.var.emit, {
|
|
129
|
+
action: PaymentsAuditActions.webhookUnverified,
|
|
130
|
+
outcome: "denied",
|
|
131
|
+
severity: "warning",
|
|
132
|
+
actorType: "service",
|
|
133
|
+
actorId: rail,
|
|
134
|
+
resourceType: "webhook",
|
|
135
|
+
resourceId: rail,
|
|
136
|
+
metadata: {
|
|
137
|
+
rail,
|
|
138
|
+
step: failingStep(cause)
|
|
139
|
+
}
|
|
140
|
+
}, c.var.log);
|
|
141
|
+
throw new PaymentsWebhookUnverifiedError({ detail: `${rail}: ${cause instanceof PithyError ? `${cause.payload.code} — ${cause.payload.detail ?? cause.payload.message}` : "the notification could not be verified"}` }, { cause });
|
|
142
|
+
}
|
|
143
|
+
const d1 = database(c);
|
|
144
|
+
const db = paymentsDatabase(d1);
|
|
145
|
+
const row = PaymentsWebhookEvent.encode({
|
|
146
|
+
id: crypto.randomUUID(),
|
|
147
|
+
rail,
|
|
148
|
+
providerEventId: notification.providerEventId,
|
|
149
|
+
payload: notification.payload,
|
|
150
|
+
receivedAt: now,
|
|
151
|
+
processedAt: null,
|
|
152
|
+
error: null,
|
|
153
|
+
createdAt: now
|
|
154
|
+
});
|
|
155
|
+
await withD1Retry(() => db.insertInto(PAYMENTS_WEBHOOK_EVENTS_TABLE).values(row).onConflict((oc) => oc.columns(["rail", "providerEventId"]).doNothing()).execute());
|
|
156
|
+
const stored = await db.selectFrom(PAYMENTS_WEBHOOK_EVENTS_TABLE).select([
|
|
157
|
+
"id",
|
|
158
|
+
"processedAt",
|
|
159
|
+
"abandonedAt",
|
|
160
|
+
"error"
|
|
161
|
+
]).where("rail", "=", rail).where("providerEventId", "=", notification.providerEventId).executeTakeFirst();
|
|
162
|
+
if (!stored) throw new InternalError({ detail: `${rail}: notification ${notification.providerEventId} was recorded but could not be read back.` });
|
|
163
|
+
if (isWebhookEventFinished(webhookEventState(stored))) return c.json({
|
|
164
|
+
received: true,
|
|
165
|
+
duplicate: true
|
|
166
|
+
}, 200);
|
|
167
|
+
verified.set(c.req.raw, {
|
|
168
|
+
rail,
|
|
169
|
+
notification,
|
|
170
|
+
eventRowId: stored.id
|
|
171
|
+
});
|
|
172
|
+
await next();
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Mark a recorded notification finished, or record why it is not.
|
|
177
|
+
*
|
|
178
|
+
* The outcome decides whether `processedAt` is written at all — see {@link WebhookCompletion}. Only the
|
|
179
|
+
* repairable case withholds it, and withholding it is what keeps the row reprocessable: the guard's
|
|
180
|
+
* short-circuit and the Paddle sweep's freshness check both read that column.
|
|
181
|
+
*
|
|
182
|
+
* That reverses what this function did before #337. Setting the timestamp beside an error read as "the
|
|
183
|
+
* notification *was* handled" — but the guard short-circuits on that column, so it also meant a failed
|
|
184
|
+
* delivery could never be repaired: the provider's retry was answered 200, a manual replay reuses the same
|
|
185
|
+
* event id and was answered 200, and the sweep skipped it. Nothing in this package repaired a failed
|
|
186
|
+
* delivery, on any rail. #339 is the other edge of the same cut: the fix withheld the timestamp from
|
|
187
|
+
* deliveries that had nothing to repair, and made a repair pass chase them.
|
|
188
|
+
*
|
|
189
|
+
* **`abandonedAt` is never touched here.** It belongs to the repair pass that wrote it, and a webhook that
|
|
190
|
+
* projects one of those rows sets `processedAt` — which wins, so the event reads finished while the row
|
|
191
|
+
* still records that a sweep once gave up on it.
|
|
192
|
+
*
|
|
193
|
+
* A genuine D1 failure propagates rather than being swallowed, and that is the right direction: the store
|
|
194
|
+
* would answer a 5xx by retrying, the projection is idempotent, and an unmarked row would otherwise leave a
|
|
195
|
+
* notification looking permanently unprocessed.
|
|
196
|
+
*/
|
|
197
|
+
async function completeWebhook(d1, eventRowId, outcome) {
|
|
198
|
+
const repairable = "error" in outcome;
|
|
199
|
+
const db = paymentsDatabase(d1);
|
|
200
|
+
await withD1Retry(() => db.updateTable(PAYMENTS_WEBHOOK_EVENTS_TABLE).set({
|
|
201
|
+
processedAt: repairable ? null : outcome.at.getTime(),
|
|
202
|
+
error: "error" in outcome ? outcome.error : "note" in outcome ? outcome.note : null
|
|
203
|
+
}).where("id", "=", eventRowId).execute());
|
|
204
|
+
}
|
|
205
|
+
//#endregion
|
|
206
|
+
export { completeWebhook, requireSignedWebhook, verifiedWebhook };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The package entrypoint — the surface `pithy add payments` wires into `pithy.config.ts`. Deliberately
|
|
3
|
+
* narrow: the capability factory, its catalog types and lookups, the four table schemas, and the projection
|
|
4
|
+
* writer and read path other capabilities and the Workflow host call. Every other module is reached by deep
|
|
5
|
+
* path (`@pithy-sh/payments/src/...`); this is the documented contract, not a barrel over the package.
|
|
6
|
+
*
|
|
7
|
+
* The subject surface is here for one reason, and it is the narrowness rule rather than an exception to it:
|
|
8
|
+
* under `billingSubject: "organization"` an adopter **must** write a `resolveSubject`, and it is the only
|
|
9
|
+
* thing this package asks them to author. A function they cannot name the type of is a function they write
|
|
10
|
+
* against inference and get subtly wrong — so `PaymentsSubjectResolver`, the pair it returns, and the two
|
|
11
|
+
* helpers that answer the same question on a route (`resolvePaymentsSubject`, `requirePaymentsSubject`) sit
|
|
12
|
+
* beside the factory that takes it. `sameSubject` and the reference codec come with it because a resolver's
|
|
13
|
+
* output is compared and encoded by the adopter's own code as often as by ours.
|
|
14
|
+
*
|
|
15
|
+
* The client modules are **not** here, and must not be. `src/client/hooks.ts` imports `react`, and
|
|
16
|
+
* re-exporting it would drag React into every Worker bundle that composes payments; `src/client/api.ts` is
|
|
17
|
+
* its framework-free half and belongs to the same browser surface. Both are reached by their own deep
|
|
18
|
+
* path — `@pithy-sh/payments/src/client/hooks`.
|
|
19
|
+
*/
|
|
20
|
+
export { type PaymentsAuditAction, PaymentsAuditActions } from "./audit/actions";
|
|
21
|
+
export { isPaymentsCapability, PAYMENTS_MIGRATION_ORDER, type PaymentsCapability, type PaymentsOptions, payments, } from "./capability";
|
|
22
|
+
export { entitlementsForProduct, grantableEntitlements, PaymentsAppleProduct, type PaymentsCatalogEntry, PaymentsConfig, type PaymentsConfigInput, PaymentsGoogleProduct, PaymentsGrants, PaymentsLedgerGrant, PaymentsLemonSqueezyProduct, PaymentsLemonSqueezySettings, PaymentsPaddleProduct, PaymentsPaddleSettings, type PaymentsPaddleSettingsInput, PaymentsProduct, type PaymentsProductInput, PaymentsProductType, PaymentsRailToggles, type PaymentsRailTogglesInput, PaymentsStripeProduct, PaymentsStripeSettings, type PaymentsStripeSettingsInput, productForProviderSku, providerProductId, railEnabled, resolveProduct, } from "./config/config";
|
|
23
|
+
export { PaymentsEntitlement } from "./data/entitlement";
|
|
24
|
+
export { minorUnitDigits, minorUnitsFromScaled } from "./data/money";
|
|
25
|
+
export { PaymentsProviderAccount } from "./data/providerAccount";
|
|
26
|
+
export { PaymentsPurchase, PurchaseEnvironment } from "./data/purchase";
|
|
27
|
+
export { PAYMENTS_RAILS, PaymentsRail } from "./data/rail";
|
|
28
|
+
export { PaymentsReconcileRun, RECONCILE_RUN_RETENTION_DAYS, type ReconcileRunInput, recordReconcileRun, } from "./data/reconcileRun";
|
|
29
|
+
export { ACCESS_GRANTING_STATUSES, grantingStatuses, PurchaseStatus, statusGrantsAccess } from "./data/status";
|
|
30
|
+
export { decodeSubjectReference, encodeSubjectReference, PaymentsSubject, PaymentsSubjectType, sameSubject, } from "./data/subject";
|
|
31
|
+
export { PAYMENTS_ENTITLEMENTS_TABLE, PAYMENTS_PROVIDER_ACCOUNTS_TABLE, PAYMENTS_PURCHASES_TABLE, PAYMENTS_RECONCILE_RUNS_TABLE, PAYMENTS_WEBHOOK_EVENTS_TABLE, type PaymentsDatabase, paymentsDatabase, paymentsTables, } from "./data/tables";
|
|
32
|
+
export { PaymentsWebhookEvent, WEBHOOK_EVENT_ORPHANED, webhookEventAwaitsOwner } from "./data/webhookEvent";
|
|
33
|
+
export { grantEntitlement, type ManualEntitlementInput, type ManualEntitlementOptions, revokeEntitlement, } from "./entitlement/manual";
|
|
34
|
+
export { createPaymentsEntitlementResolver, installEntitlementResolver, PAYMENTS_ENTITLEMENT_PROVIDER, } from "./entitlement/resolver";
|
|
35
|
+
export { authenticatedUserSubject, type PaymentsSubjectResolver, type PaymentsSubjectSeam, requirePaymentsSubject, resolvePaymentsSubject, } from "./entitlement/subjectSeam";
|
|
36
|
+
export { type AppliedGrant, applyGrants, type FulfillmentReport, type FulfillPurchaseOptions, fulfillPurchase, type GrantOptions, grantRef, ledgerGrantFor, purchaseIsPaid, } from "./grants/apply";
|
|
37
|
+
export { type ClawbackOutcome, clawbackGrants, clawbackRef } from "./grants/clawback";
|
|
38
|
+
export { type OpenPaymentsLedgerOptions, openPaymentsLedger, type PaymentsLedger, type PaymentsLedgerLoader, } from "./grants/ledgerSeam";
|
|
39
|
+
export { PAYMENTS_RECONCILE_KEY, triggerPaymentsReconcile } from "./http/dispatch";
|
|
40
|
+
export { requireAuth } from "./http/guards";
|
|
41
|
+
export { type PaymentsRoutesOptions, registerPaymentsRoutes } from "./http/routes";
|
|
42
|
+
export { AppleWebhookNotification, CheckoutRequest, EntitlementGrantRequest, EntitlementRevokeRequest, GoogleWebhookNotification, PurchaseSubmission, RestoreRequest, StripeWebhookNotification, } from "./http/schemas";
|
|
43
|
+
export { PAYMENTS_CATALOG_READ_SCOPE, PAYMENTS_CONTROL_PLANE_SCOPES, PAYMENTS_ENTITLEMENT_GRANT_SCOPE, PAYMENTS_ENTITLEMENT_REVOKE_SCOPE, } from "./http/scopes";
|
|
44
|
+
export { ProviderEvent, type ProviderEventInput } from "./projection/event";
|
|
45
|
+
export { ORPHAN_REPAIR_LIMIT, type RepairedOrphans, type RepairOrphansOptions, repairOrphanedEvents, } from "./projection/orphans";
|
|
46
|
+
export { type LinkProviderAccountOptions, linkProviderAccount, type OwnerHints, providerAccountForSubject, resolveNotificationOwner, } from "./projection/owner";
|
|
47
|
+
export { resolveEntitlements } from "./projection/resolve";
|
|
48
|
+
export { type ProjectPurchaseOptions, type PurchaseProjection, projectPurchase } from "./projection/writer";
|
|
49
|
+
export { type PaymentsConfigParams, paymentsWorkerName, resolvePaymentsConfig, } from "./provision/resolvePaymentsConfig";
|
|
50
|
+
export { APPLE_ROOT_CERTIFICATES } from "./rails/apple/certs";
|
|
51
|
+
export type { AppleHttpFetch, AppleHttpRequest, AppleHttpResponse } from "./rails/apple/http";
|
|
52
|
+
export { APPLE_API_AUDIENCE, APPLE_API_BASES, AppleSubscriptionStatuses, appleSubscriptionStatus, mintAppleApiToken, } from "./rails/apple/serverApi";
|
|
53
|
+
export { type CheckoutHandoff, type CheckoutRail, type CheckoutSessionInput, isCheckoutRail, type NotificationNote, noteIsRepairable, noteText, type PaymentsRailProvider, type PortalHandoff, type PortalSessionInput, type PortalSubscriptionLinks, type RailRequestContext, type UnboundProviderEvent, type VerifiedNotification, type VerifiedPurchase, type WebhookDelivery, } from "./rails/contract";
|
|
54
|
+
export type { GoogleHttpFetch, GoogleHttpRequest, GoogleHttpResponse } from "./rails/google/http";
|
|
55
|
+
export { GOOGLE_JWKS_URL, GOOGLE_OIDC_ISSUERS, GoogleJwk, GoogleJwks } from "./rails/google/oidc";
|
|
56
|
+
export { implementedRails, type RailTrustOptions, resolveRailProvider } from "./rails/providers";
|
|
57
|
+
export { STRIPE_API_VERSION, type StripeHttpFetch, type StripeHttpRequest, type StripeHttpResponse, } from "./rails/stripe/api";
|
|
58
|
+
export { STRIPE_METADATA_ACCOUNT_REFERENCE, STRIPE_METADATA_PRICE } from "./rails/stripe/objects";
|
|
59
|
+
export { PAYMENTS_PROVIDER_SECRET, PaymentsAppleCredentials, PaymentsGoogleCredentials, PaymentsProviderCredentials, PaymentsStripeCredentials, paymentsSecretsRegistry, railCredentials, } from "./secret/registry";
|
|
60
|
+
export { paymentsExampleSeed } from "./seeds/example";
|
|
61
|
+
export { batchedRailAccess, type RailAccessOptions, type ReconcileRailAccess, } from "./workflows/railAccess";
|
|
62
|
+
export { type ReconcileDeps, type ReconcileReport, type ReconcileStep, reconcilePayments, } from "./workflows/reconcile";
|
|
63
|
+
export { DEFAULT_EXPIRING_WITHIN_SECONDS, DEFAULT_STALE_AFTER_SECONDS, PAYMENTS_CAPABILITY, PaymentsReconcileParams, paymentsWorkflowRegistry, paymentsWorkflows, } from "./workflows/specs";
|
|
64
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { PAYMENTS_RAILS, PaymentsRail } from "./data/rail.js";
|
|
2
|
+
import { PaymentsSubject, PaymentsSubjectType, decodeSubjectReference, encodeSubjectReference, sameSubject } from "./data/subject.js";
|
|
3
|
+
import { PaymentsAppleProduct, PaymentsConfig, PaymentsGoogleProduct, PaymentsGrants, PaymentsLedgerGrant, PaymentsLemonSqueezyProduct, PaymentsLemonSqueezySettings, PaymentsPaddleProduct, PaymentsPaddleSettings, PaymentsProduct, PaymentsProductType, PaymentsRailToggles, PaymentsStripeProduct, PaymentsStripeSettings, entitlementsForProduct, grantableEntitlements, productForProviderSku, providerProductId, railEnabled, resolveProduct } from "./config/config.js";
|
|
4
|
+
import { PaymentsEntitlement } from "./data/entitlement.js";
|
|
5
|
+
import { PaymentsProviderAccount } from "./data/providerAccount.js";
|
|
6
|
+
import { ACCESS_GRANTING_STATUSES, PurchaseStatus, grantingStatuses, statusGrantsAccess } from "./data/status.js";
|
|
7
|
+
import { PaymentsPurchase, PurchaseEnvironment } from "./data/purchase.js";
|
|
8
|
+
import { PaymentsReconcileRun, RECONCILE_RUN_RETENTION_DAYS, recordReconcileRun } from "./data/reconcileRun.js";
|
|
9
|
+
import { PaymentsWebhookEvent, WEBHOOK_EVENT_ORPHANED, webhookEventAwaitsOwner } from "./data/webhookEvent.js";
|
|
10
|
+
import { PAYMENTS_ENTITLEMENTS_TABLE, PAYMENTS_PROVIDER_ACCOUNTS_TABLE, PAYMENTS_PURCHASES_TABLE, PAYMENTS_RECONCILE_RUNS_TABLE, PAYMENTS_WEBHOOK_EVENTS_TABLE, paymentsDatabase, paymentsTables } from "./data/tables.js";
|
|
11
|
+
import { resolveEntitlements } from "./projection/resolve.js";
|
|
12
|
+
import { authenticatedUserSubject, requirePaymentsSubject, resolvePaymentsSubject } from "./entitlement/subjectSeam.js";
|
|
13
|
+
import { PAYMENTS_ENTITLEMENT_PROVIDER, createPaymentsEntitlementResolver, installEntitlementResolver } from "./entitlement/resolver.js";
|
|
14
|
+
import { PaymentsAuditActions } from "./audit/actions.js";
|
|
15
|
+
import { grantEntitlement, revokeEntitlement } from "./entitlement/manual.js";
|
|
16
|
+
import { openPaymentsLedger } from "./grants/ledgerSeam.js";
|
|
17
|
+
import { clawbackGrants, clawbackRef } from "./grants/clawback.js";
|
|
18
|
+
import { applyGrants, fulfillPurchase, grantRef, ledgerGrantFor, purchaseIsPaid } from "./grants/apply.js";
|
|
19
|
+
import { linkProviderAccount, providerAccountForSubject, resolveNotificationOwner } from "./projection/owner.js";
|
|
20
|
+
import { ProviderEvent } from "./projection/event.js";
|
|
21
|
+
import { projectPurchase } from "./projection/writer.js";
|
|
22
|
+
import { ORPHAN_REPAIR_LIMIT, repairOrphanedEvents } from "./projection/orphans.js";
|
|
23
|
+
import { isCheckoutRail, noteIsRepairable, noteText } from "./rails/contract.js";
|
|
24
|
+
import { PAYMENTS_PROVIDER_SECRET, PaymentsAppleCredentials, PaymentsGoogleCredentials, PaymentsProviderCredentials, PaymentsStripeCredentials, paymentsSecretsRegistry, railCredentials } from "./secret/registry.js";
|
|
25
|
+
import { APPLE_ROOT_CERTIFICATES } from "./rails/apple/certs.js";
|
|
26
|
+
import { minorUnitDigits, minorUnitsFromScaled } from "./data/money.js";
|
|
27
|
+
import { APPLE_API_AUDIENCE, APPLE_API_BASES, AppleSubscriptionStatuses, appleSubscriptionStatus, mintAppleApiToken } from "./rails/apple/serverApi.js";
|
|
28
|
+
import { GOOGLE_JWKS_URL, GOOGLE_OIDC_ISSUERS, GoogleJwk, GoogleJwks } from "./rails/google/oidc.js";
|
|
29
|
+
import { STRIPE_API_VERSION } from "./rails/stripe/api.js";
|
|
30
|
+
import { STRIPE_METADATA_ACCOUNT_REFERENCE, STRIPE_METADATA_PRICE } from "./rails/stripe/objects.js";
|
|
31
|
+
import { implementedRails, resolveRailProvider } from "./rails/providers.js";
|
|
32
|
+
import { DEFAULT_EXPIRING_WITHIN_SECONDS, DEFAULT_STALE_AFTER_SECONDS, PAYMENTS_CAPABILITY, PaymentsReconcileParams, paymentsWorkflowRegistry, paymentsWorkflows } from "./workflows/specs.js";
|
|
33
|
+
import { requireAuth } from "./http/guards.js";
|
|
34
|
+
import { AppleWebhookNotification, CheckoutRequest, EntitlementGrantRequest, EntitlementRevokeRequest, GoogleWebhookNotification, PurchaseSubmission, RestoreRequest, StripeWebhookNotification } from "./http/schemas.js";
|
|
35
|
+
import { PAYMENTS_CATALOG_READ_SCOPE, PAYMENTS_CONTROL_PLANE_SCOPES, PAYMENTS_ENTITLEMENT_GRANT_SCOPE, PAYMENTS_ENTITLEMENT_REVOKE_SCOPE } from "./http/scopes.js";
|
|
36
|
+
import { registerPaymentsRoutes } from "./http/routes.js";
|
|
37
|
+
import { paymentsExampleSeed } from "./seeds/example.js";
|
|
38
|
+
import { PAYMENTS_MIGRATION_ORDER, isPaymentsCapability, payments } from "./capability.js";
|
|
39
|
+
import { PAYMENTS_RECONCILE_KEY, triggerPaymentsReconcile } from "./http/dispatch.js";
|
|
40
|
+
import { paymentsWorkerName, resolvePaymentsConfig } from "./provision/resolvePaymentsConfig.js";
|
|
41
|
+
import { batchedRailAccess } from "./workflows/railAccess.js";
|
|
42
|
+
import { reconcilePayments } from "./workflows/reconcile.js";
|
|
43
|
+
export { ACCESS_GRANTING_STATUSES, APPLE_API_AUDIENCE, APPLE_API_BASES, APPLE_ROOT_CERTIFICATES, AppleSubscriptionStatuses, AppleWebhookNotification, CheckoutRequest, DEFAULT_EXPIRING_WITHIN_SECONDS, DEFAULT_STALE_AFTER_SECONDS, EntitlementGrantRequest, EntitlementRevokeRequest, GOOGLE_JWKS_URL, GOOGLE_OIDC_ISSUERS, GoogleJwk, GoogleJwks, GoogleWebhookNotification, ORPHAN_REPAIR_LIMIT, PAYMENTS_CAPABILITY, PAYMENTS_CATALOG_READ_SCOPE, PAYMENTS_CONTROL_PLANE_SCOPES, PAYMENTS_ENTITLEMENTS_TABLE, PAYMENTS_ENTITLEMENT_GRANT_SCOPE, PAYMENTS_ENTITLEMENT_PROVIDER, PAYMENTS_ENTITLEMENT_REVOKE_SCOPE, PAYMENTS_MIGRATION_ORDER, PAYMENTS_PROVIDER_ACCOUNTS_TABLE, PAYMENTS_PROVIDER_SECRET, PAYMENTS_PURCHASES_TABLE, PAYMENTS_RAILS, PAYMENTS_RECONCILE_KEY, PAYMENTS_RECONCILE_RUNS_TABLE, PAYMENTS_WEBHOOK_EVENTS_TABLE, PaymentsAppleCredentials, PaymentsAppleProduct, PaymentsAuditActions, PaymentsConfig, PaymentsEntitlement, PaymentsGoogleCredentials, PaymentsGoogleProduct, PaymentsGrants, PaymentsLedgerGrant, PaymentsLemonSqueezyProduct, PaymentsLemonSqueezySettings, PaymentsPaddleProduct, PaymentsPaddleSettings, PaymentsProduct, PaymentsProductType, PaymentsProviderAccount, PaymentsProviderCredentials, PaymentsPurchase, PaymentsRail, PaymentsRailToggles, PaymentsReconcileParams, PaymentsReconcileRun, PaymentsStripeCredentials, PaymentsStripeProduct, PaymentsStripeSettings, PaymentsSubject, PaymentsSubjectType, PaymentsWebhookEvent, ProviderEvent, PurchaseEnvironment, PurchaseStatus, PurchaseSubmission, RECONCILE_RUN_RETENTION_DAYS, RestoreRequest, STRIPE_API_VERSION, STRIPE_METADATA_ACCOUNT_REFERENCE, STRIPE_METADATA_PRICE, StripeWebhookNotification, WEBHOOK_EVENT_ORPHANED, appleSubscriptionStatus, applyGrants, authenticatedUserSubject, batchedRailAccess, clawbackGrants, clawbackRef, createPaymentsEntitlementResolver, decodeSubjectReference, encodeSubjectReference, entitlementsForProduct, fulfillPurchase, grantEntitlement, grantRef, grantableEntitlements, grantingStatuses, implementedRails, installEntitlementResolver, isCheckoutRail, isPaymentsCapability, ledgerGrantFor, linkProviderAccount, minorUnitDigits, minorUnitsFromScaled, mintAppleApiToken, noteIsRepairable, noteText, openPaymentsLedger, payments, paymentsDatabase, paymentsExampleSeed, paymentsSecretsRegistry, paymentsTables, paymentsWorkerName, paymentsWorkflowRegistry, paymentsWorkflows, productForProviderSku, projectPurchase, providerAccountForSubject, providerProductId, purchaseIsPaid, railCredentials, railEnabled, reconcilePayments, recordReconcileRun, registerPaymentsRoutes, repairOrphanedEvents, requireAuth, requirePaymentsSubject, resolveEntitlements, resolveNotificationOwner, resolvePaymentsConfig, resolvePaymentsSubject, resolveProduct, resolveRailProvider, revokeEntitlement, sameSubject, statusGrantsAccess, triggerPaymentsReconcile, webhookEventAwaitsOwner };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Migration } from "kysely/migration";
|
|
2
|
+
/**
|
|
3
|
+
* Payments' six tables: the purchase projection, the materialized entitlement read model, the
|
|
4
|
+
* provider-identity map, the raw webhook log, the reconciliation run log, and the sync cursors.
|
|
5
|
+
*
|
|
6
|
+
* camelCase identifiers; `CamelCasePlugin` snake-cases them in the DDL. `down` is the tested inverse.
|
|
7
|
+
*
|
|
8
|
+
* Two constraints carry the design. `UNIQUE (rail, providerTransactionId)` on purchases is what makes
|
|
9
|
+
* three write paths converge on one row — a replayed webhook violates it and the write becomes a
|
|
10
|
+
* no-op update rather than a second purchase. `UNIQUE (subjectType, subjectId, entitlement)` on
|
|
11
|
+
* entitlements is what makes the read model a read model: one row per subject per entitlement, whichever
|
|
12
|
+
* purchase currently grants it. Correctness lives in the schema, not in a hopeful application-level check
|
|
13
|
+
* a race could skip.
|
|
14
|
+
*
|
|
15
|
+
* The holder of a purchase is a **pair** — a kind and an id — and both columns are in that key, ahead of
|
|
16
|
+
* the entitlement, so the per-subject read is a covering prefix of it. Keying on the id alone would let an
|
|
17
|
+
* organization whose id equalled some user's read that user's grants, and nothing anywhere makes those two
|
|
18
|
+
* namespaces disjoint. The kind is closed at the database with a CHECK on all three subject-bearing
|
|
19
|
+
* tables, for the reason `environment` is: a value the whole schema is keyed on is not something to trust
|
|
20
|
+
* from whatever wrote the row.
|
|
21
|
+
*
|
|
22
|
+
* The indexes come in two families. Three serve the questions the capability asks of itself — a buyer's
|
|
23
|
+
* own purchases, the reconciliation sweep, the pending-delivery queue. Three serve the control-plane
|
|
24
|
+
* reads (#247), which ask a different question: the newest rows across every account. The purchases
|
|
25
|
+
* primary key is a **text UUID**, deliberately, so it is unique but not monotonic and is no use as a
|
|
26
|
+
* sort; without those three, every page of a purchases pane sorts a customer's entire order history to
|
|
27
|
+
* return twenty-five rows, and does it again for the next page. That is not a slow query so much as a
|
|
28
|
+
* defect shipped into other people's production databases, which is why the reads' indexes are part of
|
|
29
|
+
* the schema rather than a note about it.
|
|
30
|
+
*
|
|
31
|
+
* This is the whole payments schema, in one migration — see `CONTRIBUTING.md` §Migrations for why that
|
|
32
|
+
* is the shape while nothing is published, and what changes the day something is.
|
|
33
|
+
*/
|
|
34
|
+
export declare const payments_0001_purchases: Migration;
|
|
35
|
+
//# sourceMappingURL=0001_purchases.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"0001_purchases.d.ts","sourceRoot":"","sources":["../../src/migrations/0001_purchases.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,eAAO,MAAM,uBAAuB,EAAE,SAiRrC,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
//#region src/migrations/0001_purchases.ts
|
|
3
|
+
/**
|
|
4
|
+
* Payments' six tables: the purchase projection, the materialized entitlement read model, the
|
|
5
|
+
* provider-identity map, the raw webhook log, the reconciliation run log, and the sync cursors.
|
|
6
|
+
*
|
|
7
|
+
* camelCase identifiers; `CamelCasePlugin` snake-cases them in the DDL. `down` is the tested inverse.
|
|
8
|
+
*
|
|
9
|
+
* Two constraints carry the design. `UNIQUE (rail, providerTransactionId)` on purchases is what makes
|
|
10
|
+
* three write paths converge on one row — a replayed webhook violates it and the write becomes a
|
|
11
|
+
* no-op update rather than a second purchase. `UNIQUE (subjectType, subjectId, entitlement)` on
|
|
12
|
+
* entitlements is what makes the read model a read model: one row per subject per entitlement, whichever
|
|
13
|
+
* purchase currently grants it. Correctness lives in the schema, not in a hopeful application-level check
|
|
14
|
+
* a race could skip.
|
|
15
|
+
*
|
|
16
|
+
* The holder of a purchase is a **pair** — a kind and an id — and both columns are in that key, ahead of
|
|
17
|
+
* the entitlement, so the per-subject read is a covering prefix of it. Keying on the id alone would let an
|
|
18
|
+
* organization whose id equalled some user's read that user's grants, and nothing anywhere makes those two
|
|
19
|
+
* namespaces disjoint. The kind is closed at the database with a CHECK on all three subject-bearing
|
|
20
|
+
* tables, for the reason `environment` is: a value the whole schema is keyed on is not something to trust
|
|
21
|
+
* from whatever wrote the row.
|
|
22
|
+
*
|
|
23
|
+
* The indexes come in two families. Three serve the questions the capability asks of itself — a buyer's
|
|
24
|
+
* own purchases, the reconciliation sweep, the pending-delivery queue. Three serve the control-plane
|
|
25
|
+
* reads (#247), which ask a different question: the newest rows across every account. The purchases
|
|
26
|
+
* primary key is a **text UUID**, deliberately, so it is unique but not monotonic and is no use as a
|
|
27
|
+
* sort; without those three, every page of a purchases pane sorts a customer's entire order history to
|
|
28
|
+
* return twenty-five rows, and does it again for the next page. That is not a slow query so much as a
|
|
29
|
+
* defect shipped into other people's production databases, which is why the reads' indexes are part of
|
|
30
|
+
* the schema rather than a note about it.
|
|
31
|
+
*
|
|
32
|
+
* This is the whole payments schema, in one migration — see `CONTRIBUTING.md` §Migrations for why that
|
|
33
|
+
* is the shape while nothing is published, and what changes the day something is.
|
|
34
|
+
*/
|
|
35
|
+
const payments_0001_purchases = {
|
|
36
|
+
up: async (db) => {
|
|
37
|
+
await db.schema.createTable("pithyPaymentsPurchases").addColumn("id", "text", (c) => c.primaryKey()).addColumn("subjectType", "text", (c) => c.notNull()).addColumn("subjectId", "text", (c) => c.notNull()).addColumn("rail", "text", (c) => c.notNull()).addColumn("providerTransactionId", "text", (c) => c.notNull()).addColumn("productId", "text", (c) => c.notNull()).addColumn("providerProductId", "text", (c) => c.notNull()).addColumn("type", "text", (c) => c.notNull()).addColumn("status", "text", (c) => c.notNull()).addColumn("role", "text", (c) => c.notNull().defaultTo("charge")).addColumn("environment", "text", (c) => c.notNull()).addColumn("purchasedAt", "integer", (c) => c.notNull()).addColumn("expiresAt", "integer").addColumn("revokedAt", "integer").addColumn("resumesAt", "integer").addColumn("originalTransactionId", "text").addColumn("amountMinor", "integer").addColumn("currency", "text").addColumn("providerEventAt", "integer", (c) => c.notNull()).addColumn("payload", "text", (c) => c.notNull()).addColumn("createdAt", "integer", (c) => c.notNull()).addColumn("updatedAt", "integer", (c) => c.notNull()).addUniqueConstraint("pithyPaymentsPurchasesProviderIdx", ["rail", "providerTransactionId"]).addCheckConstraint("pithyPaymentsPurchasesEnvironment", sql`environment in ('production', 'sandbox')`).addCheckConstraint("pithyPaymentsPurchasesAmount", sql`amount_minor is null or amount_minor >= 0`).addCheckConstraint("pithyPaymentsPurchasesRole", sql`role in ('charge', 'state')`).addCheckConstraint("pithyPaymentsPurchasesResumes", sql`resumes_at is null or status = 'paused'`).addCheckConstraint("pithyPaymentsPurchasesSubjectType", sql`subject_type in ('user', 'organization')`).execute();
|
|
38
|
+
await db.schema.createIndex("pithyPaymentsPurchasesOwnerIdx").on("pithyPaymentsPurchases").columns([
|
|
39
|
+
"subjectType",
|
|
40
|
+
"subjectId",
|
|
41
|
+
"purchasedAt"
|
|
42
|
+
]).execute();
|
|
43
|
+
await db.schema.createIndex("pithyPaymentsPurchasesExpiryIdx").on("pithyPaymentsPurchases").columns(["status", "expiresAt"]).execute();
|
|
44
|
+
await db.schema.createIndex("pithyPaymentsPurchasesPurchasedIdx").on("pithyPaymentsPurchases").columns(["purchasedAt", "id"]).execute();
|
|
45
|
+
await db.schema.createIndex("pithyPaymentsPurchasesTypePurchasedIdx").on("pithyPaymentsPurchases").columns([
|
|
46
|
+
"type",
|
|
47
|
+
"purchasedAt",
|
|
48
|
+
"id"
|
|
49
|
+
]).execute();
|
|
50
|
+
await db.schema.createTable("pithyPaymentsEntitlements").addColumn("id", "text", (c) => c.primaryKey()).addColumn("subjectType", "text", (c) => c.notNull()).addColumn("subjectId", "text", (c) => c.notNull()).addColumn("entitlement", "text", (c) => c.notNull()).addColumn("active", "integer", (c) => c.notNull().defaultTo(0)).addColumn("expiresAt", "integer").addColumn("sourcePurchaseId", "text").addColumn("manual", "integer", (c) => c.notNull().defaultTo(0)).addColumn("createdAt", "integer", (c) => c.notNull()).addColumn("updatedAt", "integer", (c) => c.notNull()).addUniqueConstraint("pithyPaymentsEntitlementsOwnerIdx", [
|
|
51
|
+
"subjectType",
|
|
52
|
+
"subjectId",
|
|
53
|
+
"entitlement"
|
|
54
|
+
]).addCheckConstraint("pithyPaymentsEntitlementsActive", sql`active in (0, 1)`).addCheckConstraint("pithyPaymentsEntitlementsManual", sql`manual in (0, 1)`).addCheckConstraint("pithyPaymentsEntitlementsSubjectType", sql`subject_type in ('user', 'organization')`).execute();
|
|
55
|
+
await db.schema.createIndex("pithyPaymentsEntitlementsCreatedIdx").on("pithyPaymentsEntitlements").columns(["createdAt", "id"]).execute();
|
|
56
|
+
await db.schema.createTable("pithyPaymentsProviderAccounts").addColumn("id", "text", (c) => c.primaryKey()).addColumn("rail", "text", (c) => c.notNull()).addColumn("providerAccountId", "text", (c) => c.notNull()).addColumn("subjectType", "text", (c) => c.notNull()).addColumn("subjectId", "text", (c) => c.notNull()).addColumn("createdAt", "integer", (c) => c.notNull()).addUniqueConstraint("pithyPaymentsProviderAccountsIdx", ["rail", "providerAccountId"]).addCheckConstraint("pithyPaymentsProviderAccountsSubjectType", sql`subject_type in ('user', 'organization')`).execute();
|
|
57
|
+
await db.schema.createTable("pithyPaymentsWebhookEvents").addColumn("id", "text", (c) => c.primaryKey()).addColumn("rail", "text", (c) => c.notNull()).addColumn("providerEventId", "text", (c) => c.notNull()).addColumn("payload", "text", (c) => c.notNull()).addColumn("receivedAt", "integer", (c) => c.notNull()).addColumn("processedAt", "integer").addColumn("abandonedAt", "integer").addColumn("error", "text").addColumn("attempts", "integer", (c) => c.notNull().defaultTo(0)).addColumn("createdAt", "integer", (c) => c.notNull()).addUniqueConstraint("pithyPaymentsWebhookEventsIdx", ["rail", "providerEventId"]).execute();
|
|
58
|
+
await db.schema.createIndex("pithyPaymentsWebhookEventsPendingIdx").on("pithyPaymentsWebhookEvents").columns(["processedAt", "receivedAt"]).execute();
|
|
59
|
+
await db.schema.createTable("pithyPaymentsReconcileRuns").addColumn("id", "text", (c) => c.primaryKey()).addColumn("startedAt", "integer", (c) => c.notNull()).addColumn("finishedAt", "integer", (c) => c.notNull()).addColumn("environment", "text", (c) => c.notNull()).addColumn("rail", "text").addColumn("pages", "integer", (c) => c.notNull().defaultTo(0)).addColumn("scanned", "integer", (c) => c.notNull().defaultTo(0)).addColumn("unchanged", "integer", (c) => c.notNull().defaultTo(0)).addColumn("drifted", "integer", (c) => c.notNull().defaultTo(0)).addColumn("superseded", "integer", (c) => c.notNull().defaultTo(0)).addColumn("skipped", "integer", (c) => c.notNull().defaultTo(0)).addColumn("failed", "integer", (c) => c.notNull().defaultTo(0)).addColumn("truncated", "integer", (c) => c.notNull().defaultTo(0)).addColumn("dryRun", "integer", (c) => c.notNull().defaultTo(0)).addColumn("createdAt", "integer", (c) => c.notNull()).addCheckConstraint("pithyPaymentsReconcileRunsEnvironment", sql`environment in ('production', 'sandbox')`).addCheckConstraint("pithyPaymentsReconcileRunsTruncated", sql`truncated in (0, 1)`).addCheckConstraint("pithyPaymentsReconcileRunsDryRun", sql`dry_run in (0, 1)`).addCheckConstraint("pithyPaymentsReconcileRunsCounts", sql`pages >= 0 and scanned >= 0 and unchanged >= 0 and drifted >= 0 and superseded >= 0 and skipped >= 0 and failed >= 0`).execute();
|
|
60
|
+
await db.schema.createIndex("pithyPaymentsReconcileRunsStartedIdx").on("pithyPaymentsReconcileRuns").columns(["startedAt", "id"]).execute();
|
|
61
|
+
await db.schema.createTable("pithyPaymentsSyncCursors").addColumn("id", "text", (c) => c.primaryKey()).addColumn("rail", "text", (c) => c.notNull()).addColumn("name", "text", (c) => c.notNull()).addColumn("cursor", "text").addColumn("updatedAt", "integer", (c) => c.notNull()).addColumn("createdAt", "integer", (c) => c.notNull()).addUniqueConstraint("pithyPaymentsSyncCursorsIdx", ["rail", "name"]).execute();
|
|
62
|
+
},
|
|
63
|
+
down: async (db) => {
|
|
64
|
+
await db.schema.dropTable("pithyPaymentsSyncCursors").execute();
|
|
65
|
+
await db.schema.dropIndex("pithyPaymentsReconcileRunsStartedIdx").execute();
|
|
66
|
+
await db.schema.dropTable("pithyPaymentsReconcileRuns").execute();
|
|
67
|
+
await db.schema.dropIndex("pithyPaymentsWebhookEventsPendingIdx").execute();
|
|
68
|
+
await db.schema.dropTable("pithyPaymentsWebhookEvents").execute();
|
|
69
|
+
await db.schema.dropTable("pithyPaymentsProviderAccounts").execute();
|
|
70
|
+
await db.schema.dropIndex("pithyPaymentsEntitlementsCreatedIdx").execute();
|
|
71
|
+
await db.schema.dropTable("pithyPaymentsEntitlements").execute();
|
|
72
|
+
await db.schema.dropIndex("pithyPaymentsPurchasesTypePurchasedIdx").execute();
|
|
73
|
+
await db.schema.dropIndex("pithyPaymentsPurchasesPurchasedIdx").execute();
|
|
74
|
+
await db.schema.dropIndex("pithyPaymentsPurchasesExpiryIdx").execute();
|
|
75
|
+
await db.schema.dropIndex("pithyPaymentsPurchasesOwnerIdx").execute();
|
|
76
|
+
await db.schema.dropTable("pithyPaymentsPurchases").execute();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
//#endregion
|
|
80
|
+
export { payments_0001_purchases };
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a price quote's location comes from, decided in one place.
|
|
3
|
+
*
|
|
4
|
+
* **A customer is charged from their billing address.** Paddle settles tax on the transaction's address,
|
|
5
|
+
* not on where the browser happened to be — so the browser's IP is a *provisional estimate* and the
|
|
6
|
+
* address on file is the authority. Both are legitimate answers; which one a screen got is a fact the
|
|
7
|
+
* screen has to be able to state, because one of them is what the card will be charged and the other is
|
|
8
|
+
* a guess that resolves at checkout.
|
|
9
|
+
*
|
|
10
|
+
* That is the whole of this module. It takes what a caller knows about a visitor, picks the best source
|
|
11
|
+
* available, and says which one it picked. Nothing here fetches, renders, or formats.
|
|
12
|
+
*
|
|
13
|
+
* **One resolver, not a decision at each call site.** A pricing screen, a paywall and a cart all have to
|
|
14
|
+
* answer "where does this person live", and three answers is how a page quotes from an address the
|
|
15
|
+
* checkout does not charge from. Every site that derives a location for a Paddle price quote calls
|
|
16
|
+
* {@link resolvePriceLocation}, and the honesty label comes off the same object.
|
|
17
|
+
*
|
|
18
|
+
* **No imports, deliberately.** This compiles inside the Worker's program *and* inside an adopter's
|
|
19
|
+
* DOM-typed browser program — `../client/api.ts` explains at length why those are two programs — so it
|
|
20
|
+
* names its own shapes rather than reaching for Zod, for core's codecs, or for Paddle's types. The
|
|
21
|
+
* scaffolded pricing screen hands {@link priceQueryFor}'s answer straight to `usePricePreview`, so
|
|
22
|
+
* TypeScript pins the shape against the real `PaddlePriceQuery` at the one place it matters.
|
|
23
|
+
*
|
|
24
|
+
* **No price, no currency, no country table.** Figures come from Paddle, rendered by Paddle. Nothing in
|
|
25
|
+
* this file knows what anything costs, and nothing in it may learn: a table of amounts here would be
|
|
26
|
+
* wrong in every country it did not list and wrong silently in the ones it did.
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* Where a quote's location came from, in order of authority.
|
|
30
|
+
*
|
|
31
|
+
* - `customer` — a Paddle customer id. Paddle prices from the address it holds for them, which is the
|
|
32
|
+
* address the checkout charges. The best answer there is, and the only one that is not a guess.
|
|
33
|
+
* - `address` — a billing address the caller holds but Paddle does not yet. Better than the network,
|
|
34
|
+
* still not proof of what the checkout will settle on, because the buyer may enter another.
|
|
35
|
+
* - `ip` — nobody said. Paddle resolves the country from the browser's own IP. Right for a marketing
|
|
36
|
+
* page a stranger is reading, and an estimate every time.
|
|
37
|
+
*/
|
|
38
|
+
export type PriceLocationSource = "customer" | "address" | "ip";
|
|
39
|
+
/** A billing address, as far as a price quote cares about one. */
|
|
40
|
+
export interface PriceAddress {
|
|
41
|
+
/** ISO 3166-1 alpha-2 — `"US"`, `"GB"`, `"JP"`. Paddle takes it verbatim. */
|
|
42
|
+
countryCode: string;
|
|
43
|
+
/**
|
|
44
|
+
* The postal code, where the caller has one.
|
|
45
|
+
*
|
|
46
|
+
* Load-bearing rather than decorative. United States tax resolves below the country — 15% in Chicago,
|
|
47
|
+
* 8.875% in New York, 0% in Oregon — and Paddle answers a country-only request with 0% rather than
|
|
48
|
+
* with an error, so a quote without one can be short of what the card is charged.
|
|
49
|
+
*/
|
|
50
|
+
postalCode?: string;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* What the caller knows about this visitor. Every field optional, because "nothing" is a real answer.
|
|
54
|
+
*
|
|
55
|
+
* A stranger on a marketing page is `null`. A signed-in visitor whose account has never bought anything
|
|
56
|
+
* carries an address and no customer. A returning customer carries the `ctm_…` the webhook path recorded
|
|
57
|
+
* against them, which is the same value `POST /payments/checkout` hands Paddle as `customer_id`.
|
|
58
|
+
*/
|
|
59
|
+
export interface PriceVisitor {
|
|
60
|
+
/** The Paddle customer this visitor is, or null. An identifier, not a credential. */
|
|
61
|
+
customerId?: string | null;
|
|
62
|
+
/** A billing address the caller holds, or null. */
|
|
63
|
+
address?: PriceAddress | null;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* The decision: which source was used, and what to send.
|
|
67
|
+
*
|
|
68
|
+
* Both halves matter to a screen. The `customerId`/`address` pair is what goes to Paddle; `source` is
|
|
69
|
+
* what the sentence under the figure has to be true about.
|
|
70
|
+
*/
|
|
71
|
+
export interface PriceLocation {
|
|
72
|
+
/** Which source won. */
|
|
73
|
+
source: PriceLocationSource;
|
|
74
|
+
/** The Paddle customer to price as, or null. */
|
|
75
|
+
customerId: string | null;
|
|
76
|
+
/** The address to price at, or null. */
|
|
77
|
+
address: PriceAddress | null;
|
|
78
|
+
/**
|
|
79
|
+
* Whether this location is a guess about where the buyer lives rather than the address they are
|
|
80
|
+
* charged from.
|
|
81
|
+
*
|
|
82
|
+
* True exactly when `source` is `ip`. Kept as a field rather than left to every reader to derive,
|
|
83
|
+
* because "is this provisional" is the question, and re-deriving it from the union is how a fourth
|
|
84
|
+
* source arrives one day and half the readers keep answering for three.
|
|
85
|
+
*/
|
|
86
|
+
provisional: boolean;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Pick the best location available, and say which one it is.
|
|
90
|
+
*
|
|
91
|
+
* Precedence is authority, not convenience: a Paddle customer beats an address the caller holds, because
|
|
92
|
+
* Paddle prices the customer from the address it will actually charge — and if the two disagree, a
|
|
93
|
+
* traveler or a VPN, the billing address wins for what they are charged, so it wins for what they are
|
|
94
|
+
* shown.
|
|
95
|
+
*
|
|
96
|
+
* **Never a silent default.** `null`, `undefined`, an empty customer id and an address with no country
|
|
97
|
+
* all resolve to `ip` *and say so*, so a caller that meant to pass something and passed nothing gets an
|
|
98
|
+
* estimate labeled as one rather than a guess wearing the authority of an address.
|
|
99
|
+
*/
|
|
100
|
+
export declare function resolvePriceLocation(visitor: PriceVisitor | null | undefined): PriceLocation;
|
|
101
|
+
/** One line of a price request: what to quote, and how many. */
|
|
102
|
+
export interface PriceQuoteItem {
|
|
103
|
+
/** The Paddle price — `pri_…`. */
|
|
104
|
+
priceId: string;
|
|
105
|
+
/** How many units. */
|
|
106
|
+
quantity: number;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* What to ask Paddle, for these items at this location.
|
|
110
|
+
*
|
|
111
|
+
* Structurally a `PaddlePriceQuery`, and deliberately not typed as one — see this module's header. The
|
|
112
|
+
* absent fields are absent rather than `undefined`-valued: `priceQueryKey` serializes the query into an
|
|
113
|
+
* effect dependency, and a key that changed shape between "no address" and "an address of undefined"
|
|
114
|
+
* would re-quote a page that asked the same question twice.
|
|
115
|
+
*/
|
|
116
|
+
export declare function priceQueryFor(items: readonly PriceQuoteItem[], location: PriceLocation): {
|
|
117
|
+
items: readonly PriceQuoteItem[];
|
|
118
|
+
customerId?: string;
|
|
119
|
+
address?: PriceAddress;
|
|
120
|
+
};
|
|
121
|
+
/**
|
|
122
|
+
* Whether the figure has to be labeled an estimate.
|
|
123
|
+
*
|
|
124
|
+
* Two independent reasons, and either one is enough:
|
|
125
|
+
*
|
|
126
|
+
* **The location is provisional.** An IP says where a browser connected from, not where a card is
|
|
127
|
+
* registered. The buyer enters a billing address at the card form and the total settles then — which is
|
|
128
|
+
* how commerce works everywhere, and is expected rather than a broken promise, *provided the figure said
|
|
129
|
+
* it was an estimate first*.
|
|
130
|
+
*
|
|
131
|
+
* **The tax is not fully resolved.** `priceSummary().estimated` is true when Paddle resolved no postal
|
|
132
|
+
* code, and United States tax lives below the country. Pass it in; this is where the two facts meet.
|
|
133
|
+
*
|
|
134
|
+
* Deriving the label from the postal code alone made it right by accident — Paddle resolves no postal
|
|
135
|
+
* code from an IP today — and an accident is not a rule. This states the rule.
|
|
136
|
+
*/
|
|
137
|
+
export declare function quoteIsEstimated(location: PriceLocation, taxUnresolved: boolean): boolean;
|
|
138
|
+
//# sourceMappingURL=location.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"location.d.ts","sourceRoot":"","sources":["../../src/pricing/location.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AAEH;;;;;;;;;GASG;AACH,MAAM,MAAM,mBAAmB,GAAG,UAAU,GAAG,SAAS,GAAG,IAAI,CAAC;AAEhE,kEAAkE;AAClE,MAAM,WAAW,YAAY;IAC3B,6EAA6E;IAC7E,WAAW,EAAE,MAAM,CAAC;IACpB;;;;;;OAMG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;GAMG;AACH,MAAM,WAAW,YAAY;IAC3B,qFAAqF;IACrF,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3B,mDAAmD;IACnD,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI,CAAC;CAC/B;AAED;;;;;GAKG;AACH,MAAM,WAAW,aAAa;IAC5B,wBAAwB;IACxB,MAAM,EAAE,mBAAmB,CAAC;IAC5B,gDAAgD;IAChD,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,wCAAwC;IACxC,OAAO,EAAE,YAAY,GAAG,IAAI,CAAC;IAC7B;;;;;;;OAOG;IACH,WAAW,EAAE,OAAO,CAAC;CACtB;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI,GAAG,SAAS,GAAG,aAAa,CAsB5F;AAED,gEAAgE;AAChE,MAAM,WAAW,cAAc;IAC7B,kCAAkC;IAClC,OAAO,EAAE,MAAM,CAAC;IAChB,sBAAsB;IACtB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,SAAS,cAAc,EAAE,EAChC,QAAQ,EAAE,aAAa,GACtB;IAAE,KAAK,EAAE,SAAS,cAAc,EAAE,CAAC;IAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,CAAC,EAAE,YAAY,CAAA;CAAE,CAInF;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,aAAa,EAAE,aAAa,EAAE,OAAO,GAAG,OAAO,CAEzF"}
|