@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,391 @@
|
|
|
1
|
+
import { PAYMENTS_NO_BROWSER, PAYMENTS_UNREADABLE } from "./api.js";
|
|
2
|
+
import { cachedAnswer, readQuoteCache, rememberAnswer } from "./paddleCache.js";
|
|
3
|
+
import { withoutZeroFraction } from "./wholeUnits.js";
|
|
4
|
+
import { initializePaddle } from "@paddle/paddle-js";
|
|
5
|
+
//#region src/client/paddle.ts
|
|
6
|
+
/**
|
|
7
|
+
* Paddle.js: loading it once, asking it what this visitor pays, and reading the answer safely.
|
|
8
|
+
*
|
|
9
|
+
* **This is the half of the Paddle rail a redirect cannot do.** The server mints transactions and takes
|
|
10
|
+
* webhooks; nothing on that side can tell a visitor in Berlin that the $5.00 they see includes $0.80 of
|
|
11
|
+
* VAT while a visitor in Chicago will be charged $5.75. Only a script running in that browser, talking to
|
|
12
|
+
* Paddle from that IP, knows — so the whole of localized pricing lives here or it does not exist.
|
|
13
|
+
*
|
|
14
|
+
* **Never format a price yourself here, and never write one down.** Paddle returns `formattedTotals`
|
|
15
|
+
* already rendered for the visitor: the right symbol, the right separators, and whole units for the
|
|
16
|
+
* zero-decimal currencies (¥725 is `725`, not `72500`). The raw amounts are exposed too, but they are for
|
|
17
|
+
* **comparing**, never for showing.
|
|
18
|
+
*
|
|
19
|
+
* **The reason this line used to give was false, and it was measured false (#465, 2026-08-28).** It said a
|
|
20
|
+
* kit reaching for `Intl.NumberFormat` "would have to carry a table of which currencies have two decimals,
|
|
21
|
+
* and would get one wrong". `Intl` carries that table, from CLDR:
|
|
22
|
+
* `new Intl.NumberFormat(locale, { style: "currency", currency }).resolvedOptions().maximumFractionDigits`
|
|
23
|
+
* answers 2 for `USD` and `GBP` and 0 for `JPY`, `KRW` and `CLP`, and renders `$65.82`, `¥6,582` and
|
|
24
|
+
* `₩6,582` from the same 6582. Paddle's own documentation instructs adopters to do exactly this — "use a
|
|
25
|
+
* currency library to format monetary values to the correct number of decimals… symbols and decimal
|
|
26
|
+
* separators are placed correctly" (api-reference/about/data-types).
|
|
27
|
+
*
|
|
28
|
+
* **What the kit still refuses is deriving an AMOUNT, and that is the rule this paragraph is load-bearing
|
|
29
|
+
* for.** The figure is Paddle's; a renderer places a point and attaches a symbol and decides nothing about
|
|
30
|
+
* how much money there is. Where Paddle already rendered the string — here, on the pricing surfaces, where
|
|
31
|
+
* `formattedTotals` exists — that string is what ships, because a second rendering of one price is a second
|
|
32
|
+
* spelling of it and the visitor's browser locale is not the one Paddle used. Where Paddle renders nothing
|
|
33
|
+
* — `subscriptions.preview` and `transactions.preview` return no formatted field at any depth, verified
|
|
34
|
+
* against the recordings — the alternative to rendering is a confirmation screen with no figure on it. That
|
|
35
|
+
* case is `data/renderMoney.ts`, on the server, from the negotiated locale, and it carries the measurement
|
|
36
|
+
* that the exponent must come from ISO 4217 rather than from CLDR's display digits.
|
|
37
|
+
*
|
|
38
|
+
* **Amended: removing a zero fraction from Paddle's string is not composing one (Jim, 2026-08-21).** The
|
|
39
|
+
* rule above was read as forbidding any touch of a formatted total, and two adopter surfaces each wrote
|
|
40
|
+
* their own `total.replace(/([.,])00(?=\D*$)/, "")` rather than break it — the same wrong regex, twice,
|
|
41
|
+
* in two repositories (`#427`). Nothing is assembled by dropping an all-zero fraction: the symbol, the
|
|
42
|
+
* separators, the grouping and the digits are still the ones Paddle chose, and one run of characters is
|
|
43
|
+
* gone. What the rule actually forbids is *deciding* what a price looks like from raw minor units, and
|
|
44
|
+
* that decision is still Paddle's. So {@link PriceSummaryOptions.wholeUnits} exists, is off unless a
|
|
45
|
+
* caller asks, and removes only a fraction the arithmetic in `./wholeUnits` has already proved is zero —
|
|
46
|
+
* which is the part neither adopter could do, because `totals` never leaves this package.
|
|
47
|
+
*
|
|
48
|
+
* **What "localized" actually means, measured rather than assumed.** With no `unit_price_overrides` on
|
|
49
|
+
* the price, every country is billed in the catalog's currency — a preview from a UK address on a USD
|
|
50
|
+
* price comes back in dollars. Currency is a catalog decision. What this delivers without one is tax
|
|
51
|
+
* and formatting, which is real: the US adds tax to the listed figure and settles it at the postal code,
|
|
52
|
+
* while the EU, the UK and Japan take it out of an inclusive one. {@link priceSummary} is where that
|
|
53
|
+
* difference stops being a footnote and becomes two different sentences.
|
|
54
|
+
*
|
|
55
|
+
* **No API key is reachable from this module or any other under `src/client/`.** The client token is
|
|
56
|
+
* publishable by design and is the only credential that belongs in a browser. `paddle.test.ts` sweeps the
|
|
57
|
+
* directory for the shapes of the ones that do not.
|
|
58
|
+
*
|
|
59
|
+
* Cookie/session and same-origin are irrelevant here: none of this talks to the adopter's Worker. It
|
|
60
|
+
* talks to Paddle, with Paddle's own publishable token, exactly as Paddle's documentation intends.
|
|
61
|
+
*/
|
|
62
|
+
/** Paddle.js could not be loaded at all: an ad blocker, a Content Security Policy, or no network. */
|
|
63
|
+
const PADDLE_UNAVAILABLE = {
|
|
64
|
+
code: "client/paddle_unavailable",
|
|
65
|
+
message: "We couldn't load the payment provider.",
|
|
66
|
+
action: "Check your connection or any content blockers, then reload."
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Paddle.js loaded and then refused to initialize. A token from the wrong account is the usual cause.
|
|
70
|
+
*
|
|
71
|
+
* Distinct from {@link PADDLE_UNAVAILABLE} because the two are fixed by different people: one is the
|
|
72
|
+
* visitor's browser, the other is the project's configuration.
|
|
73
|
+
*/
|
|
74
|
+
const PADDLE_NOT_INITIALIZED = {
|
|
75
|
+
code: "client/paddle_not_initialized",
|
|
76
|
+
message: "The payment provider wouldn't start.",
|
|
77
|
+
action: "Reload. If it keeps happening, this project's payment settings are wrong."
|
|
78
|
+
};
|
|
79
|
+
/** A second, different Paddle account was asked for on a page that already has one. */
|
|
80
|
+
const PADDLE_ACCOUNT_CONFLICT = {
|
|
81
|
+
code: "client/paddle_account_conflict",
|
|
82
|
+
message: "The payment provider is already running for a different account.",
|
|
83
|
+
action: "One page serves one Paddle account. Reload before switching."
|
|
84
|
+
};
|
|
85
|
+
/** Paddle refused the price request itself — an unknown price id, or a token scoped elsewhere. */
|
|
86
|
+
const PADDLE_PREVIEW_REFUSED = {
|
|
87
|
+
code: "client/paddle_preview_refused",
|
|
88
|
+
message: "We couldn't get a price for you.",
|
|
89
|
+
action: "Try again in a moment."
|
|
90
|
+
};
|
|
91
|
+
/** The page's registry. Module state on purpose: a page is exactly the scope this is about. */
|
|
92
|
+
const PAGE = {};
|
|
93
|
+
/** Which account a setup names, as one comparable string. */
|
|
94
|
+
function setupKey(setup) {
|
|
95
|
+
return `${setup.environment}:${setup.clientToken}`;
|
|
96
|
+
}
|
|
97
|
+
/** The real loader. Wrapped rather than passed directly so the seam's shape is this kit's, not Paddle's. */
|
|
98
|
+
const defaultInitializer = (options) => initializePaddle(options);
|
|
99
|
+
/**
|
|
100
|
+
* Load and initialize Paddle.js, once per page.
|
|
101
|
+
*
|
|
102
|
+
* Idempotent by construction rather than by documentation: the first call's promise is remembered and
|
|
103
|
+
* every later call for the same account is handed the same one, so two components mounting at once
|
|
104
|
+
* produce one script and one `Initialize`. A call for a *different* account is refused — Paddle.js holds
|
|
105
|
+
* one environment and one token per page, `Environment.set` after `Initialize` leaves it half-moved, and
|
|
106
|
+
* silently re-pointing a live account at a sandbox is not a failure anyone would notice until a real
|
|
107
|
+
* card was declined.
|
|
108
|
+
*
|
|
109
|
+
* **`Environment.set` runs for sandbox and for production alike**, before `Initialize`. Passing it
|
|
110
|
+
* explicitly both ways is what makes the environment a declared fact rather than a default nobody looked
|
|
111
|
+
* at — and the two are separate accounts with separate tokens, so a token used against the wrong one is
|
|
112
|
+
* refused outright.
|
|
113
|
+
*
|
|
114
|
+
* Nothing here throws. A blocked script, a server render with no `window`, and a token the account
|
|
115
|
+
* rejects are three different {@link PaymentsFailure}s, because they are three different problems.
|
|
116
|
+
*/
|
|
117
|
+
function loadPaddle(setup, options) {
|
|
118
|
+
const registry = options?.registry ?? PAGE;
|
|
119
|
+
const key = setupKey(setup);
|
|
120
|
+
const held = registry.current;
|
|
121
|
+
if (held) {
|
|
122
|
+
if (held.key === key) return held.paddle;
|
|
123
|
+
return Promise.resolve({
|
|
124
|
+
ok: false,
|
|
125
|
+
failure: PADDLE_ACCOUNT_CONFLICT
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const initialize = options?.initialize ?? defaultInitializer;
|
|
129
|
+
const paddle = new Promise((resolve) => {
|
|
130
|
+
resolve(initialize({
|
|
131
|
+
token: setup.clientToken,
|
|
132
|
+
environment: setup.environment
|
|
133
|
+
}));
|
|
134
|
+
}).then((instance) => {
|
|
135
|
+
if (!instance) return {
|
|
136
|
+
ok: false,
|
|
137
|
+
failure: PAYMENTS_NO_BROWSER
|
|
138
|
+
};
|
|
139
|
+
if (!instance.Initialized) return {
|
|
140
|
+
ok: false,
|
|
141
|
+
failure: PADDLE_NOT_INITIALIZED
|
|
142
|
+
};
|
|
143
|
+
return {
|
|
144
|
+
ok: true,
|
|
145
|
+
value: instance
|
|
146
|
+
};
|
|
147
|
+
}, () => ({
|
|
148
|
+
ok: false,
|
|
149
|
+
failure: PADDLE_UNAVAILABLE
|
|
150
|
+
})).then((result) => {
|
|
151
|
+
if (!result.ok && registry.current?.key === key) registry.current = void 0;
|
|
152
|
+
return result;
|
|
153
|
+
});
|
|
154
|
+
registry.current = {
|
|
155
|
+
key,
|
|
156
|
+
paddle
|
|
157
|
+
};
|
|
158
|
+
return paddle;
|
|
159
|
+
}
|
|
160
|
+
/** Whether a value is a plain record. */
|
|
161
|
+
function isRecord(value) {
|
|
162
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
163
|
+
}
|
|
164
|
+
/** Whether every one of the four amounts is a string. */
|
|
165
|
+
function readTotals(value) {
|
|
166
|
+
if (!isRecord(value)) return null;
|
|
167
|
+
const { subtotal, discount, tax, total } = value;
|
|
168
|
+
if (typeof subtotal !== "string") return null;
|
|
169
|
+
if (typeof discount !== "string") return null;
|
|
170
|
+
if (typeof tax !== "string") return null;
|
|
171
|
+
if (typeof total !== "string") return null;
|
|
172
|
+
return {
|
|
173
|
+
subtotal,
|
|
174
|
+
discount,
|
|
175
|
+
tax,
|
|
176
|
+
total
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
/** A non-empty string, or null. */
|
|
180
|
+
function optionalString(value) {
|
|
181
|
+
return typeof value === "string" && value.length > 0 ? value : null;
|
|
182
|
+
}
|
|
183
|
+
/** The price's listed amount for this quote's country, following any override that covers it. */
|
|
184
|
+
function listedAmount(price, countryCode) {
|
|
185
|
+
const overrides = price.unitPriceOverrides;
|
|
186
|
+
if (countryCode !== null && Array.isArray(overrides)) for (const override of overrides) {
|
|
187
|
+
if (!isRecord(override) || !Array.isArray(override.countryCodes)) continue;
|
|
188
|
+
if (!override.countryCodes.includes(countryCode)) continue;
|
|
189
|
+
const unit = override.unitPrice;
|
|
190
|
+
if (!isRecord(unit) || typeof unit.amount !== "string" || typeof unit.currencyCode !== "string") return null;
|
|
191
|
+
return {
|
|
192
|
+
amount: unit.amount,
|
|
193
|
+
currency: unit.currencyCode
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
const unit = price.unitPrice;
|
|
197
|
+
if (!isRecord(unit) || typeof unit.amount !== "string" || typeof unit.currencyCode !== "string") return null;
|
|
198
|
+
return {
|
|
199
|
+
amount: unit.amount,
|
|
200
|
+
currency: unit.currencyCode
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Which of the quoted figures is the price the catalog lists.
|
|
205
|
+
*
|
|
206
|
+
* Per **unit**, so quantity never enters the comparison, and against the override that covers this
|
|
207
|
+
* country where there is one. Every branch here was measured against the live sandbox; the fixtures
|
|
208
|
+
* beside this module are those measurements.
|
|
209
|
+
*/
|
|
210
|
+
function taxTreatment(unitTotals, price, currencyCode, countryCode) {
|
|
211
|
+
if (unitTotals.tax === "0") return "none";
|
|
212
|
+
const listed = listedAmount(price, countryCode);
|
|
213
|
+
if (listed === null || listed.currency !== currencyCode) return "unknown";
|
|
214
|
+
if (unitTotals.subtotal === listed.amount) return "added";
|
|
215
|
+
if (unitTotals.total === listed.amount) return "included";
|
|
216
|
+
return "unknown";
|
|
217
|
+
}
|
|
218
|
+
/** One line item of Paddle's answer, or null when it is not one. */
|
|
219
|
+
function readLine(value, currencyCode, countryCode) {
|
|
220
|
+
if (!isRecord(value)) return null;
|
|
221
|
+
const price = value.price;
|
|
222
|
+
const product = value.product;
|
|
223
|
+
if (!isRecord(price) || !isRecord(product)) return null;
|
|
224
|
+
if (typeof price.id !== "string" || typeof product.name !== "string") return null;
|
|
225
|
+
if (typeof value.quantity !== "number" || typeof value.taxRate !== "string") return null;
|
|
226
|
+
const unitTotals = readTotals(value.unitTotals);
|
|
227
|
+
const totals = readTotals(value.totals);
|
|
228
|
+
const formattedUnitTotals = readTotals(value.formattedUnitTotals);
|
|
229
|
+
const formattedTotals = readTotals(value.formattedTotals);
|
|
230
|
+
if (!unitTotals || !totals || !formattedUnitTotals || !formattedTotals) return null;
|
|
231
|
+
const cycle = price.billingCycle;
|
|
232
|
+
const billingCycle = isRecord(cycle) && typeof cycle.interval === "string" && typeof cycle.frequency === "number" ? {
|
|
233
|
+
interval: cycle.interval,
|
|
234
|
+
frequency: cycle.frequency
|
|
235
|
+
} : null;
|
|
236
|
+
return {
|
|
237
|
+
priceId: price.id,
|
|
238
|
+
productName: product.name,
|
|
239
|
+
priceName: optionalString(price.name),
|
|
240
|
+
quantity: value.quantity,
|
|
241
|
+
taxRate: value.taxRate,
|
|
242
|
+
billingCycle,
|
|
243
|
+
unitTotals,
|
|
244
|
+
totals,
|
|
245
|
+
formattedUnitTotals,
|
|
246
|
+
formattedTotals,
|
|
247
|
+
taxTreatment: taxTreatment(unitTotals, price, currencyCode, countryCode)
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Narrow Paddle's answer, or refuse it.
|
|
252
|
+
*
|
|
253
|
+
* **This is a trust boundary and it is treated as one.** The response comes from a third party's script
|
|
254
|
+
* over a network this kit does not control, and a page that read `lineItems[0].formattedTotals.total`
|
|
255
|
+
* off whatever arrived would render `undefined` as a price. Every field a caller can reach is checked
|
|
256
|
+
* here; nothing partial gets through, because half a price is worse than none.
|
|
257
|
+
*
|
|
258
|
+
* Hand-written guards rather than Zod, for the reason `api.ts` gives: this compiles into a browser
|
|
259
|
+
* bundle and must not drag the server's schema graph in behind it.
|
|
260
|
+
*/
|
|
261
|
+
function readPricePreview(answer) {
|
|
262
|
+
if (!isRecord(answer)) return null;
|
|
263
|
+
const value = isRecord(answer.data) ? answer.data : answer;
|
|
264
|
+
if (typeof value.currencyCode !== "string") return null;
|
|
265
|
+
const details = value.details;
|
|
266
|
+
if (!isRecord(details) || !Array.isArray(details.lineItems)) return null;
|
|
267
|
+
const address = isRecord(value.address) ? value.address : null;
|
|
268
|
+
const countryCode = address ? optionalString(address.countryCode) : null;
|
|
269
|
+
const postalCode = address ? optionalString(address.postalCode) : null;
|
|
270
|
+
const lines = [];
|
|
271
|
+
for (const item of details.lineItems) {
|
|
272
|
+
const line = readLine(item, value.currencyCode, countryCode);
|
|
273
|
+
if (line === null) return null;
|
|
274
|
+
lines.push(line);
|
|
275
|
+
}
|
|
276
|
+
return {
|
|
277
|
+
currencyCode: value.currencyCode,
|
|
278
|
+
countryCode,
|
|
279
|
+
postalCode,
|
|
280
|
+
lines
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Ask Paddle what this visitor pays.
|
|
285
|
+
*
|
|
286
|
+
* Loads Paddle.js if it is not loaded, which is why a screen needs no separate setup step. Every failure
|
|
287
|
+
* on the way — a blocked script, a refused token, an unreadable answer — arrives as a
|
|
288
|
+
* {@link PaymentsFailure} and never as a thrown error and never as a number.
|
|
289
|
+
*/
|
|
290
|
+
async function previewPrices(setup, query, options) {
|
|
291
|
+
const cache = options?.cache === void 0 ? null : readQuoteCache(options.cache);
|
|
292
|
+
const asked = cache === null ? "" : `${setupKey(setup)}|${priceQueryKey(query)}`;
|
|
293
|
+
if (cache !== null) {
|
|
294
|
+
const held = cachedAnswer(cache, asked);
|
|
295
|
+
const remembered = held === null ? null : readPricePreview(held);
|
|
296
|
+
if (remembered !== null) return {
|
|
297
|
+
ok: true,
|
|
298
|
+
value: remembered
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
const loaded = await loadPaddle(setup, options);
|
|
302
|
+
if (!loaded.ok) return loaded;
|
|
303
|
+
const answer = await new Promise((resolve) => resolve(loaded.value.PricePreview(query))).then((value) => ({
|
|
304
|
+
answered: true,
|
|
305
|
+
value
|
|
306
|
+
}), () => ({
|
|
307
|
+
answered: false,
|
|
308
|
+
value: void 0
|
|
309
|
+
}));
|
|
310
|
+
if (!answer.answered) return {
|
|
311
|
+
ok: false,
|
|
312
|
+
failure: PADDLE_PREVIEW_REFUSED
|
|
313
|
+
};
|
|
314
|
+
const preview = readPricePreview(answer.value);
|
|
315
|
+
if (preview === null) return {
|
|
316
|
+
ok: false,
|
|
317
|
+
failure: PAYMENTS_UNREADABLE
|
|
318
|
+
};
|
|
319
|
+
if (cache !== null) rememberAnswer(cache, asked, answer.value);
|
|
320
|
+
return {
|
|
321
|
+
ok: true,
|
|
322
|
+
value: preview
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* The one number to show, and the one sentence that makes it true.
|
|
327
|
+
*
|
|
328
|
+
* **The headline is not the same field in every country, and that is the point.** Where tax is added on
|
|
329
|
+
* top the listed price is the subtotal, and quoting the total would advertise a New Yorker's local sales
|
|
330
|
+
* tax as part of the price. Where tax is taken out of an inclusive figure the listed price *is* the
|
|
331
|
+
* total, and quoting the subtotal would advertise €4.20 for something that costs €5.00. One hardcoded
|
|
332
|
+
* string cannot mean "before tax" in Denver and "including VAT" in Berlin, so this returns two.
|
|
333
|
+
*
|
|
334
|
+
* It lives in the package rather than in the scaffolded screen for the reason the hooks do: an adopter's
|
|
335
|
+
* `pricing.tsx` is written once and never rewritten, and tax conventions are not a thing to freeze into
|
|
336
|
+
* somebody else's repository.
|
|
337
|
+
*
|
|
338
|
+
* Per unit, always. A pricing page quotes "$5.00 a month", not "$15.00 for the three seats you have not
|
|
339
|
+
* chosen yet"; a cart that wants the line total reads `formattedTotals` itself.
|
|
340
|
+
*/
|
|
341
|
+
function priceSummary(preview, line, options) {
|
|
342
|
+
const estimated = preview.postalCode === null;
|
|
343
|
+
const totals = line.formattedUnitTotals;
|
|
344
|
+
const headline = (field) => options?.wholeUnits === true ? withoutZeroFraction({
|
|
345
|
+
formatted: totals[field],
|
|
346
|
+
minorAmount: line.unitTotals[field],
|
|
347
|
+
currency: preview.currencyCode
|
|
348
|
+
}) : totals[field];
|
|
349
|
+
if (line.taxTreatment === "added") return {
|
|
350
|
+
headline: headline("subtotal"),
|
|
351
|
+
note: `Plus ${totals.tax} tax.`,
|
|
352
|
+
estimated
|
|
353
|
+
};
|
|
354
|
+
if (line.taxTreatment === "included") return {
|
|
355
|
+
headline: headline("total"),
|
|
356
|
+
note: `Includes ${totals.tax} tax.`,
|
|
357
|
+
estimated
|
|
358
|
+
};
|
|
359
|
+
if (line.taxTreatment === "none") return {
|
|
360
|
+
headline: headline("total"),
|
|
361
|
+
note: estimated ? "Tax is settled at checkout." : null,
|
|
362
|
+
estimated
|
|
363
|
+
};
|
|
364
|
+
return {
|
|
365
|
+
headline: headline("total"),
|
|
366
|
+
note: `Includes ${totals.tax} tax.`,
|
|
367
|
+
estimated
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* A stable key for a query, so an effect can depend on what was asked rather than on the object.
|
|
372
|
+
*
|
|
373
|
+
* A screen writes `usePricePreview(setup, { items: [{ priceId, quantity: 1 }] })` inline, which is a new
|
|
374
|
+
* object every render. An effect depending on it would fetch forever. Depending on this instead means
|
|
375
|
+
* the request repeats when — and only when — something about the request changed.
|
|
376
|
+
*
|
|
377
|
+
* Field order is fixed here rather than taken from the object, because `{ a, b }` and `{ b, a }` are the
|
|
378
|
+
* same query and `JSON.stringify` disagrees.
|
|
379
|
+
*/
|
|
380
|
+
function priceQueryKey(query) {
|
|
381
|
+
return JSON.stringify([
|
|
382
|
+
query.items.map((item) => [item.priceId, item.quantity]),
|
|
383
|
+
query.address ? [query.address.countryCode, query.address.postalCode ?? null] : null,
|
|
384
|
+
query.customerId ?? null,
|
|
385
|
+
query.customerIpAddress ?? null,
|
|
386
|
+
query.currencyCode ?? null,
|
|
387
|
+
query.discountId ?? null
|
|
388
|
+
]);
|
|
389
|
+
}
|
|
390
|
+
//#endregion
|
|
391
|
+
export { PADDLE_ACCOUNT_CONFLICT, PADDLE_NOT_INITIALIZED, PADDLE_PREVIEW_REFUSED, PADDLE_UNAVAILABLE, loadPaddle, previewPrices, priceQueryKey, priceSummary, readPricePreview };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A price nobody has to fetch twice — opt-in, and only ever where the caller said to put it.
|
|
3
|
+
*
|
|
4
|
+
* One built artifact quotes the marketing site and the dashboard both, and each of them wants the same
|
|
5
|
+
* thing for a different reason: a marketing page a visitor moves around inside should not spend a Paddle
|
|
6
|
+
* round trip per page, and a dashboard that re-mounts a pricing pane should not re-ask a question it
|
|
7
|
+
* asked ten seconds ago. So the quote path can cache. What it may not do is decide any part of how.
|
|
8
|
+
*
|
|
9
|
+
* **Nothing is cached unless the caller names a store, a namespace and a lifetime.** All three, together.
|
|
10
|
+
* A quote resolved from `customerId` is one customer's price, resolved from the address on their account
|
|
11
|
+
* — so where it rests is not a detail this module may pick a default for. `sessionStorage` and
|
|
12
|
+
* `localStorage` are the same interface and very different promises about a shared machine, and only the
|
|
13
|
+
* program that knows who is signed in can choose between them. There is no default. There is no
|
|
14
|
+
* "sensible" fallback. A caller that wants a cache says where.
|
|
15
|
+
*
|
|
16
|
+
* **A lifetime is stated too, for the same reason.** Every figure here is Paddle's, and Paddle's figures
|
|
17
|
+
* move: two recordings of one Japanese price taken minutes apart came back ¥797 and ¥798, because the
|
|
18
|
+
* FX rate moved between the calls. Tax rules change, and a customer changes their billing address. A
|
|
19
|
+
* cached figure is a figure nobody re-checked, so how long that may last is the caller's call and nobody
|
|
20
|
+
* inherits a number they never looked at.
|
|
21
|
+
*
|
|
22
|
+
* **A partial cache is a mistake, and it says so.** Two of the three is what a caller reaches for first,
|
|
23
|
+
* and silently ignoring it would look exactly like caching that works — until the day somebody wonders
|
|
24
|
+
* why the network tab shows a request per page. It warns, names the parts that are missing, and quotes
|
|
25
|
+
* from the network. It never fails the quote: a broken cache must not be able to take a price off a page.
|
|
26
|
+
*
|
|
27
|
+
* **The question is inside the entry's key**, which is what makes caching a signed-in customer's price
|
|
28
|
+
* safe at all. Two visitors are two questions, so one can never be handed the other's answer; and the
|
|
29
|
+
* account is in there beside them, so a sandbox answer cannot survive into production.
|
|
30
|
+
*
|
|
31
|
+
* `console` rather than a logger, and this is the one file in the package that may. The kit's logger is
|
|
32
|
+
* a Worker's — resolved from a request context that does not exist here — and this module compiles into
|
|
33
|
+
* a browser program where `console` *is* the sink an adopter reads. `biome.jsonc` carries the exemption
|
|
34
|
+
* and this sentence is its reason.
|
|
35
|
+
*/
|
|
36
|
+
/**
|
|
37
|
+
* The slice of a storage this needs.
|
|
38
|
+
*
|
|
39
|
+
* `localStorage` and `sessionStorage` both satisfy it as they are, and so does a `Map` wrapper, an
|
|
40
|
+
* in-memory object, or a caller's own store with a quota policy of its own. Structural for the reason
|
|
41
|
+
* every seam in this directory is: an adopter must be able to satisfy it without importing anything.
|
|
42
|
+
*/
|
|
43
|
+
export interface PaddleCacheStore {
|
|
44
|
+
/** The stored text under a key, or null when there is none. */
|
|
45
|
+
getItem(key: string): string | null;
|
|
46
|
+
/** Store text under a key, replacing whatever was there. */
|
|
47
|
+
setItem(key: string, value: string): void;
|
|
48
|
+
/** Forget a key. Called on every entry this module decides it may no longer trust. */
|
|
49
|
+
removeItem(key: string): void;
|
|
50
|
+
/**
|
|
51
|
+
* How many entries the store holds, if it can say.
|
|
52
|
+
*
|
|
53
|
+
* `Storage` has this and `key` both, so `localStorage` and `sessionStorage` satisfy them as they are.
|
|
54
|
+
* They are optional because a caller's own three-method store is still a store — it simply does not
|
|
55
|
+
* get swept, which is the difference between a cache that tidies up after itself and one that does not.
|
|
56
|
+
*/
|
|
57
|
+
readonly length?: number;
|
|
58
|
+
/** The nth key, if the store can enumerate. */
|
|
59
|
+
key?(index: number): string | null;
|
|
60
|
+
}
|
|
61
|
+
/** A cache, complete: where entries rest, what namespaces them, and how long one may stand. */
|
|
62
|
+
export interface PaddleQuoteCache {
|
|
63
|
+
/** The namespace entries are stored under. Two surfaces sharing a store share nothing else. */
|
|
64
|
+
readonly key: string;
|
|
65
|
+
/** Where entries rest. Named by the caller, always. */
|
|
66
|
+
readonly store: PaddleCacheStore;
|
|
67
|
+
/** How long an entry may stand, in milliseconds. Stated by the caller, always. */
|
|
68
|
+
readonly ttlMs: number;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* The three parts as they arrive from outside TypeScript — a script tag's attributes, a JavaScript
|
|
72
|
+
* caller, a config file. Each may be absent, and each may be nonsense.
|
|
73
|
+
*/
|
|
74
|
+
export interface PaddleQuoteCacheParts {
|
|
75
|
+
/** The intended namespace. */
|
|
76
|
+
readonly key?: string | null;
|
|
77
|
+
/**
|
|
78
|
+
* The intended store, or the **name** of one that did not resolve.
|
|
79
|
+
*
|
|
80
|
+
* A string is how a caller says "somebody named a store and nothing came back" — a script tag whose
|
|
81
|
+
* `data-paddle-cache-store` is a typo, an environment with no storage. It is not a store, so it is
|
|
82
|
+
* reported missing; but it *was* asked for, so the request is a partial cache and gets a line rather
|
|
83
|
+
* than the silence a caller who asked for nothing gets.
|
|
84
|
+
*/
|
|
85
|
+
readonly store?: PaddleCacheStore | string | null;
|
|
86
|
+
/** The intended lifetime, in milliseconds. */
|
|
87
|
+
readonly ttlMs?: number | null;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Read a cache from parts that may be anything, or refuse it out loud.
|
|
91
|
+
*
|
|
92
|
+
* All three or none. Nothing given is a caller who wants no cache, and it says nothing at all. Anything
|
|
93
|
+
* given that does not add up to a cache is a caller who wanted one and will not get one, and that is
|
|
94
|
+
* worth a line in their console rather than a silence they would read as success.
|
|
95
|
+
*/
|
|
96
|
+
export declare function readQuoteCache(parts: PaddleQuoteCacheParts): PaddleQuoteCache | null;
|
|
97
|
+
/**
|
|
98
|
+
* The answer this cache holds for a question, while it is still fresh.
|
|
99
|
+
*
|
|
100
|
+
* Null for a miss, and *every* way of being wrong is a miss: no entry, unreadable text, an entry from a
|
|
101
|
+
* version that stored something else, one past its lifetime, one stamped in a future the clock has since
|
|
102
|
+
* moved back from, a store that throws on read. Each of those is a reason to ask Paddle again, and none
|
|
103
|
+
* of them is a reason to show a visitor no price.
|
|
104
|
+
*
|
|
105
|
+
* An entry it will not trust is thrown away rather than left to expire, so a store that has collected
|
|
106
|
+
* something unreadable stops being asked about it.
|
|
107
|
+
*/
|
|
108
|
+
export declare function cachedAnswer(cache: PaddleQuoteCache, of: string): unknown | null;
|
|
109
|
+
/**
|
|
110
|
+
* Remember an answer to a question, if the store will have it.
|
|
111
|
+
*
|
|
112
|
+
* Silent about a refusal, deliberately. A full quota and Safari's private browsing both throw on write,
|
|
113
|
+
* and neither is a thing the visitor looking at the price can do anything about — the page already has
|
|
114
|
+
* its figure, and the only cost is asking again next time. The warning this module does emit is for a
|
|
115
|
+
* cache the *caller* got wrong, which is a thing they can fix.
|
|
116
|
+
*
|
|
117
|
+
* **It sweeps first.** Nothing reads a departed customer's key again, so nothing would ever expire it:
|
|
118
|
+
* a dashboard caching per customer on a shared machine writes one entry per person who ever signs in,
|
|
119
|
+
* the quota fills, and every write from then on throws into the silence above — caching stops working
|
|
120
|
+
* for everybody, and by design nobody is told. Expiring on the way past is what bounds it.
|
|
121
|
+
*/
|
|
122
|
+
export declare function rememberAnswer(cache: PaddleQuoteCache, of: string, answer: unknown): void;
|
|
123
|
+
//# sourceMappingURL=paddleCache.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paddleCache.d.ts","sourceRoot":"","sources":["../../src/client/paddleCache.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB;IAC/B,+DAA+D;IAC/D,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;IACpC,4DAA4D;IAC5D,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1C,sFAAsF;IACtF,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B;;;;;;OAMG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,+CAA+C;IAC/C,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAC;CACpC;AAED,+FAA+F;AAC/F,MAAM,WAAW,gBAAgB;IAC/B,+FAA+F;IAC/F,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,uDAAuD;IACvD,QAAQ,CAAC,KAAK,EAAE,gBAAgB,CAAC;IACjC,kFAAkF;IAClF,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED;;;GAGG;AACH,MAAM,WAAW,qBAAqB;IACpC,8BAA8B;IAC9B,QAAQ,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC7B;;;;;;;OAOG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,gBAAgB,GAAG,MAAM,GAAG,IAAI,CAAC;IAClD,8CAA8C;IAC9C,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CAChC;AAgBD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,qBAAqB,GAAG,gBAAgB,GAAG,IAAI,CAgBpF;AAgDD;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,gBAAgB,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAehF;AA+BD;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,gBAAgB,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAG,IAAI,CAczF"}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
//#region src/client/paddleCache.ts
|
|
2
|
+
/** What every entry this module writes is stored under, so a store shared with a page stays legible. */
|
|
3
|
+
const PREFIX = "pithy.paddle.price";
|
|
4
|
+
/** Whether a value can actually store: all three methods, not merely an object that arrived. */
|
|
5
|
+
function isStore(value) {
|
|
6
|
+
if (typeof value !== "object" || value === null) return false;
|
|
7
|
+
const candidate = value;
|
|
8
|
+
return typeof candidate.getItem === "function" && typeof candidate.setItem === "function" && typeof candidate.removeItem === "function";
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Read a cache from parts that may be anything, or refuse it out loud.
|
|
12
|
+
*
|
|
13
|
+
* All three or none. Nothing given is a caller who wants no cache, and it says nothing at all. Anything
|
|
14
|
+
* given that does not add up to a cache is a caller who wanted one and will not get one, and that is
|
|
15
|
+
* worth a line in their console rather than a silence they would read as success.
|
|
16
|
+
*/
|
|
17
|
+
function readQuoteCache(parts) {
|
|
18
|
+
const key = typeof parts.key === "string" ? parts.key.trim() : "";
|
|
19
|
+
const store = isStore(parts.store) ? parts.store : null;
|
|
20
|
+
const ttlMs = typeof parts.ttlMs === "number" && Number.isFinite(parts.ttlMs) && parts.ttlMs > 0 ? parts.ttlMs : null;
|
|
21
|
+
if (key !== "" && store !== null && ttlMs !== null) return {
|
|
22
|
+
key,
|
|
23
|
+
store,
|
|
24
|
+
ttlMs
|
|
25
|
+
};
|
|
26
|
+
const asked = parts.key ?? parts.store ?? parts.ttlMs;
|
|
27
|
+
if (asked === void 0 || asked === null) return null;
|
|
28
|
+
const missing = [
|
|
29
|
+
key === "" ? "key" : null,
|
|
30
|
+
store === null ? "store" : null,
|
|
31
|
+
ttlMs === null ? "ttlMs" : null
|
|
32
|
+
].filter((part) => part !== null).join(", ");
|
|
33
|
+
console.warn(`Paddle price cache ignored. Missing: ${missing}. A cache needs key, store and ttlMs together, so that where a customer's price rests and how long it stands are both stated. Pass all three, or none.`);
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
/** Everything one cache stores sits under this. */
|
|
37
|
+
function namespace(cache) {
|
|
38
|
+
return `${PREFIX}.${cache.key}.`;
|
|
39
|
+
}
|
|
40
|
+
/** Where one answer to one question rests. */
|
|
41
|
+
function entryKey(cache, of) {
|
|
42
|
+
return `${namespace(cache)}${of}`;
|
|
43
|
+
}
|
|
44
|
+
/** What one entry holds, or null for text this module will not trust. */
|
|
45
|
+
function readEntry(stored) {
|
|
46
|
+
if (stored === null) return null;
|
|
47
|
+
let parsed;
|
|
48
|
+
try {
|
|
49
|
+
parsed = JSON.parse(stored);
|
|
50
|
+
} catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) return null;
|
|
54
|
+
const entry = parsed;
|
|
55
|
+
if (typeof entry.at !== "number" || !("answer" in entry)) return null;
|
|
56
|
+
return {
|
|
57
|
+
at: entry.at,
|
|
58
|
+
answer: entry.answer
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Whether an entry has stopped standing.
|
|
63
|
+
*
|
|
64
|
+
* Both directions. A negative age is a clock that moved backwards — a laptop waking up, a device
|
|
65
|
+
* correcting itself — and an entry stamped in the future would otherwise stand until the clock caught up
|
|
66
|
+
* with it, which is the one expiry nobody can predict the length of.
|
|
67
|
+
*/
|
|
68
|
+
function stale(at, ttlMs) {
|
|
69
|
+
const age = Date.now() - at;
|
|
70
|
+
return age < 0 || age > ttlMs;
|
|
71
|
+
}
|
|
72
|
+
/** Forget an entry, and never mind if the store will not let us. */
|
|
73
|
+
function forget(cache, of) {
|
|
74
|
+
try {
|
|
75
|
+
cache.store.removeItem(entryKey(cache, of));
|
|
76
|
+
} catch {}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The answer this cache holds for a question, while it is still fresh.
|
|
80
|
+
*
|
|
81
|
+
* Null for a miss, and *every* way of being wrong is a miss: no entry, unreadable text, an entry from a
|
|
82
|
+
* version that stored something else, one past its lifetime, one stamped in a future the clock has since
|
|
83
|
+
* moved back from, a store that throws on read. Each of those is a reason to ask Paddle again, and none
|
|
84
|
+
* of them is a reason to show a visitor no price.
|
|
85
|
+
*
|
|
86
|
+
* An entry it will not trust is thrown away rather than left to expire, so a store that has collected
|
|
87
|
+
* something unreadable stops being asked about it.
|
|
88
|
+
*/
|
|
89
|
+
function cachedAnswer(cache, of) {
|
|
90
|
+
let stored;
|
|
91
|
+
try {
|
|
92
|
+
stored = cache.store.getItem(entryKey(cache, of));
|
|
93
|
+
} catch {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
if (stored === null) return null;
|
|
97
|
+
const entry = readEntry(stored);
|
|
98
|
+
if (entry === null || stale(entry.at, cache.ttlMs)) {
|
|
99
|
+
forget(cache, of);
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
return entry.answer;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Throw away this cache's own expired entries.
|
|
106
|
+
*
|
|
107
|
+
* **Its own, and judged by its own lifetime.** Two surfaces can share one store under different names
|
|
108
|
+
* and different ttls, and sweeping a neighbor's entries against *this* cache's number would throw away
|
|
109
|
+
* answers that are perfectly fresh by the rule they were kept under. Anything outside the namespace —
|
|
110
|
+
* another cache's, or the page's own — is not this module's to read or to delete.
|
|
111
|
+
*
|
|
112
|
+
* Silent, total, and skipped entirely where the store cannot enumerate. A sweep that fails costs the
|
|
113
|
+
* page nothing; the entry it did not reach expires on its own clock the next time anything asks for it.
|
|
114
|
+
*/
|
|
115
|
+
function sweep(cache) {
|
|
116
|
+
const store = cache.store;
|
|
117
|
+
if (typeof store.key !== "function" || typeof store.length !== "number") return;
|
|
118
|
+
try {
|
|
119
|
+
const expired = [];
|
|
120
|
+
const mine = namespace(cache);
|
|
121
|
+
for (let index = 0; index < store.length; index += 1) {
|
|
122
|
+
const key = store.key(index);
|
|
123
|
+
if (key === null || key === void 0 || !key.startsWith(mine)) continue;
|
|
124
|
+
const entry = readEntry(store.getItem(key));
|
|
125
|
+
if (entry === null || stale(entry.at, cache.ttlMs)) expired.push(key);
|
|
126
|
+
}
|
|
127
|
+
for (const key of expired) store.removeItem(key);
|
|
128
|
+
} catch {}
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Remember an answer to a question, if the store will have it.
|
|
132
|
+
*
|
|
133
|
+
* Silent about a refusal, deliberately. A full quota and Safari's private browsing both throw on write,
|
|
134
|
+
* and neither is a thing the visitor looking at the price can do anything about — the page already has
|
|
135
|
+
* its figure, and the only cost is asking again next time. The warning this module does emit is for a
|
|
136
|
+
* cache the *caller* got wrong, which is a thing they can fix.
|
|
137
|
+
*
|
|
138
|
+
* **It sweeps first.** Nothing reads a departed customer's key again, so nothing would ever expire it:
|
|
139
|
+
* a dashboard caching per customer on a shared machine writes one entry per person who ever signs in,
|
|
140
|
+
* the quota fills, and every write from then on throws into the silence above — caching stops working
|
|
141
|
+
* for everybody, and by design nobody is told. Expiring on the way past is what bounds it.
|
|
142
|
+
*/
|
|
143
|
+
function rememberAnswer(cache, of, answer) {
|
|
144
|
+
sweep(cache);
|
|
145
|
+
let text;
|
|
146
|
+
try {
|
|
147
|
+
text = JSON.stringify({
|
|
148
|
+
at: Date.now(),
|
|
149
|
+
answer
|
|
150
|
+
});
|
|
151
|
+
} catch {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
try {
|
|
155
|
+
cache.store.setItem(entryKey(cache, of), text);
|
|
156
|
+
} catch {}
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
export { cachedAnswer, readQuoteCache, rememberAnswer };
|