@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,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The one door out to Apple, and the mapping from how it answered to what that means.
|
|
3
|
+
*
|
|
4
|
+
* **Why the Apple rail reaches the network at all, when verification does not.** A StoreKit transaction is
|
|
5
|
+
* signed, so a client submission and a notification both verify offline against the pinned chain. Neither
|
|
6
|
+
* tells us anything about a subscription we have not heard from — and that silence is precisely what
|
|
7
|
+
* reconciliation exists for: Apple stops renewing and sends nothing, or the notification it did send never
|
|
8
|
+
* arrived. Answering that needs the App Store Server API, and this is the only module that talks to it.
|
|
9
|
+
*
|
|
10
|
+
* ## How an answer is read
|
|
11
|
+
*
|
|
12
|
+
* **A 429 or a 5xx is `payments/provider_unavailable` (503).** Apple is up and not answering, or is asking us
|
|
13
|
+
* to slow down. The Workflow step fails and is retried, which is what durable execution is for.
|
|
14
|
+
*
|
|
15
|
+
* **Every other 4xx is `payments/rail_not_configured` (404), the same call Stripe's door makes.** A request
|
|
16
|
+
* here is built entirely from our own credentials and from rows we wrote, so a 401 (the signing key or the
|
|
17
|
+
* issuer id is wrong), a 400, and a 403 are one statement: this project's Apple rail is not set up to do what
|
|
18
|
+
* it was asked. 503 would ask a caller to retry something that will never succeed, and no client is involved
|
|
19
|
+
* to blame with a 400.
|
|
20
|
+
*
|
|
21
|
+
* **A 404 is an absent purchase only for a caller that said it was probing.** Apple answers 404 for a
|
|
22
|
+
* transaction id it does not know — a sandbox id asked of production, or a purchase from a different app —
|
|
23
|
+
* and only a caller that knows the difference between that and a misconfigured account may claim it.
|
|
24
|
+
*
|
|
25
|
+
* **No refusal carries the URL or the response body.** The URL ends in a transaction id and Apple's error
|
|
26
|
+
* bodies quote the request. `detail` reaches an operator's log, and the same rule that keeps a receipt out of
|
|
27
|
+
* one keeps these out too.
|
|
28
|
+
*/
|
|
29
|
+
/** One outbound request, narrowed to what the App Store Server API needs. Every call is an authorized GET. */
|
|
30
|
+
export interface AppleHttpRequest {
|
|
31
|
+
/** The HTTP method. Defaults to GET. */
|
|
32
|
+
method?: string;
|
|
33
|
+
/** Request headers — the bearer assertion, and nothing else. */
|
|
34
|
+
headers?: Record<string, string>;
|
|
35
|
+
}
|
|
36
|
+
/** The response shape this module reads. Structural, so a test's transport need not be a whole `Response`. */
|
|
37
|
+
export interface AppleHttpResponse {
|
|
38
|
+
/** Whether the status is 2xx. */
|
|
39
|
+
ok: boolean;
|
|
40
|
+
/** The status code, which is what decides the mapping. */
|
|
41
|
+
status: number;
|
|
42
|
+
/** The body as text. Read as text rather than JSON so a non-JSON answer is a diagnosis, not a throw. */
|
|
43
|
+
text(): Promise<string>;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The HTTP seam for the App Store Server API.
|
|
47
|
+
*
|
|
48
|
+
* Injectable for one reason: **no test may reach a live store**, and a rail whose network call could not be
|
|
49
|
+
* substituted would have to be tested through a stub of itself. One explicit parameter beats reassigning a
|
|
50
|
+
* global, which leaks between suites and hides which module was exercised.
|
|
51
|
+
*/
|
|
52
|
+
export type AppleHttpFetch = (url: string, init?: AppleHttpRequest) => Promise<AppleHttpResponse>;
|
|
53
|
+
/** The default transport: the runtime's own `fetch`. */
|
|
54
|
+
export declare const appleHttpFetch: AppleHttpFetch;
|
|
55
|
+
/** What one request is for, and how its answers should be read. */
|
|
56
|
+
export interface AppleJsonOptions {
|
|
57
|
+
/** What is being fetched, in a `detail` line: "the subscription statuses". Never the URL. */
|
|
58
|
+
what: string;
|
|
59
|
+
/** Request headers — the bearer assertion. */
|
|
60
|
+
headers?: Record<string, string>;
|
|
61
|
+
/**
|
|
62
|
+
* Whether a 404 means "no such transaction" rather than a misconfigured account. Set only by a caller that
|
|
63
|
+
* is deliberately probing for something that may legitimately not exist.
|
|
64
|
+
*/
|
|
65
|
+
absentOn404?: boolean;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* One request to Apple, and its parsed JSON body — or `undefined` when a 404 was a legitimate answer.
|
|
69
|
+
*
|
|
70
|
+
* The body comes back as `unknown`. Reaching an endpoint proves who answered, never what they said, so every
|
|
71
|
+
* caller Zod-parses its own shape.
|
|
72
|
+
*/
|
|
73
|
+
export declare function appleJson(transport: AppleHttpFetch, url: string, options: AppleJsonOptions): Promise<unknown | undefined>;
|
|
74
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../../src/rails/apple/http.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,8GAA8G;AAC9G,MAAM,WAAW,gBAAgB;IAC/B,wCAAwC;IACxC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gEAAgE;IAChE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED,8GAA8G;AAC9G,MAAM,WAAW,iBAAiB;IAChC,iCAAiC;IACjC,EAAE,EAAE,OAAO,CAAC;IACZ,0DAA0D;IAC1D,MAAM,EAAE,MAAM,CAAC;IACf,wGAAwG;IACxG,IAAI,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;CACzB;AAED;;;;;;GAMG;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,gBAAgB,KAAK,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAElG,wDAAwD;AACxD,eAAO,MAAM,cAAc,EAAE,cAAyF,CAAC;AAEvH,mEAAmE;AACnE,MAAM,WAAW,gBAAgB;IAC/B,6FAA6F;IAC7F,IAAI,EAAE,MAAM,CAAC;IACb,8CAA8C;IAC9C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC;;;OAGG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED;;;;;GAKG;AACH,wBAAsB,SAAS,CAC7B,SAAS,EAAE,cAAc,EACzB,GAAG,EAAE,MAAM,EACX,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,OAAO,GAAG,SAAS,CAAC,CAwC9B"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { PaymentsProviderUnavailableError, PaymentsRailNotConfiguredError } from "../../error/errors.js";
|
|
2
|
+
//#region src/rails/apple/http.ts
|
|
3
|
+
/** The default transport: the runtime's own `fetch`. */
|
|
4
|
+
const appleHttpFetch = (url, init) => fetch(url, init);
|
|
5
|
+
/**
|
|
6
|
+
* One request to Apple, and its parsed JSON body — or `undefined` when a 404 was a legitimate answer.
|
|
7
|
+
*
|
|
8
|
+
* The body comes back as `unknown`. Reaching an endpoint proves who answered, never what they said, so every
|
|
9
|
+
* caller Zod-parses its own shape.
|
|
10
|
+
*/
|
|
11
|
+
async function appleJson(transport, url, options) {
|
|
12
|
+
let response;
|
|
13
|
+
try {
|
|
14
|
+
response = await transport(url, { headers: options.headers });
|
|
15
|
+
} catch (cause) {
|
|
16
|
+
throw new PaymentsProviderUnavailableError({ detail: `Apple did not answer when asked for ${options.what}.` }, { cause });
|
|
17
|
+
}
|
|
18
|
+
if (response.status === 404 && options.absentOn404) return void 0;
|
|
19
|
+
if (!response.ok) {
|
|
20
|
+
if (response.status === 429 || response.status >= 500) throw new PaymentsProviderUnavailableError({ detail: `Apple answered ${response.status} when asked for ${options.what}.` });
|
|
21
|
+
const hint = response.status === 401 ? " Check the App Store Connect key id, issuer id, and private key in the payments provider secret — a token Apple will not accept is a credential problem, not an outage." : "";
|
|
22
|
+
throw new PaymentsRailNotConfiguredError({ detail: `Apple answered ${response.status} when asked for ${options.what}.${hint}` });
|
|
23
|
+
}
|
|
24
|
+
const text = await response.text();
|
|
25
|
+
try {
|
|
26
|
+
return JSON.parse(text);
|
|
27
|
+
} catch (cause) {
|
|
28
|
+
throw new PaymentsProviderUnavailableError({ detail: `Apple answered with a non-JSON body when asked for ${options.what}.` }, { cause });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//#endregion
|
|
32
|
+
export { appleHttpFetch, appleJson };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Which roots to trust and what clock to judge certificate validity against. */
|
|
2
|
+
export interface VerifyAppleJwsOptions {
|
|
3
|
+
/** base64 DER of every acceptable root. Defaults to the pinned Apple roots. */
|
|
4
|
+
roots?: readonly string[];
|
|
5
|
+
/** The clock. Injected so a certificate-window refusal is deterministic in tests. */
|
|
6
|
+
now?: Date;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Verify a compact JWS against Apple's pinned certificate chain and return its decoded payload.
|
|
10
|
+
*
|
|
11
|
+
* Throws `payments/invalid_receipt` when the JWS is malformed — wrong segment count, a header Apple would
|
|
12
|
+
* never send, an unreadable certificate — and `payments/verification_failed` when it is well-formed but
|
|
13
|
+
* does not verify. Both are 400s; the distinction is for the operator reading `detail`, and the webhook
|
|
14
|
+
* guard maps either to `payments/webhook_unverified` on its own path.
|
|
15
|
+
*/
|
|
16
|
+
export declare function verifyAppleJws(jws: string, options: VerifyAppleJwsOptions): Promise<unknown>;
|
|
17
|
+
//# sourceMappingURL=jws.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jws.d.ts","sourceRoot":"","sources":["../../../src/rails/apple/jws.ts"],"names":[],"mappings":"AAqDA,iFAAiF;AACjF,MAAM,WAAW,qBAAqB;IACpC,+EAA+E;IAC/E,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1B,qFAAqF;IACrF,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ;AAED;;;;;;;GAOG;AACH,wBAAsB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,qBAAqB,GAAG,OAAO,CAAC,OAAO,CAAC,CA0ClG"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { PaymentsInvalidReceiptError, PaymentsVerificationFailedError } from "../../error/errors.js";
|
|
2
|
+
import { APPLE_ROOT_CERTIFICATES } from "./certs.js";
|
|
3
|
+
import { decodeBase64, decodeBase64Url } from "./der.js";
|
|
4
|
+
import { verifyCertificateChain } from "./x509.js";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
//#region src/rails/apple/jws.ts
|
|
7
|
+
/**
|
|
8
|
+
* App Store Server Notification V2 signature verification — the authenticity boundary on Apple's webhook,
|
|
9
|
+
* and on every transaction a client submits.
|
|
10
|
+
*
|
|
11
|
+
* Apple signs its notifications and transactions as compact JWS, carrying the whole issuing certificate
|
|
12
|
+
* chain in the header's `x5c`. Verifying one means three things, in this order, and the order matters:
|
|
13
|
+
*
|
|
14
|
+
* 1. **Pin the algorithm before touching the key.** `alg` is attacker-controlled — it is in a header nobody
|
|
15
|
+
* has verified yet — so it is parsed against a literal `ES256` rather than looked up in a table. That is
|
|
16
|
+
* what makes the two published confusions unreachable: `none`, which asks for the signature to be
|
|
17
|
+
* skipped, and `HS256`, which asks the verifier to HMAC using the public key it just found in the
|
|
18
|
+
* header as if it were a shared secret. Neither survives a literal.
|
|
19
|
+
* 2. **Verify the chain, not the leaf.** `x5c` is a claim, not evidence. The chain must link, every window
|
|
20
|
+
* must contain the clock, and the root must be one shipped in `certs.ts` — see `x509.ts`.
|
|
21
|
+
* 3. **Verify the signature with the leaf's key, over the exact received bytes** — `header.payload` as
|
|
22
|
+
* encoded, not re-serialized. A JSON round-trip would reorder keys and change what was signed.
|
|
23
|
+
*
|
|
24
|
+
* `alg: ES256` also fixes the curve at P-256, so a P-384 leaf under an ES256 header is refused. That is the
|
|
25
|
+
* same confusion one layer down, and checking only the string would let it through.
|
|
26
|
+
*
|
|
27
|
+
* The payload is returned as `unknown`. Verification proves who sent the bytes, never what they mean, so
|
|
28
|
+
* the caller Zod-parses. Handing back a typed object here would blur the two.
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* The JWS header, exactly as tightly as it can be. `alg` is a literal for the reason above; `x5c` must be
|
|
32
|
+
* at least two certificates because a single one would be trusted on its own word.
|
|
33
|
+
*/
|
|
34
|
+
const AppleJwsHeader = z.object({
|
|
35
|
+
alg: z.literal("ES256").describe("The signing algorithm. A literal, not a lookup: `alg` arrives in an unverified header, and accepting `none` or `HS256` from it is how JWS verifiers are bypassed."),
|
|
36
|
+
x5c: z.array(z.string().min(1).describe("One certificate, base64-encoded DER.")).min(2).describe("The issuing certificate chain, leaf first and root last. A claim rather than evidence — every link is verified and the root must be one pinned in certs.ts.")
|
|
37
|
+
}).describe("The header of an Apple-signed JWS, constrained to the one shape Apple sends.");
|
|
38
|
+
/**
|
|
39
|
+
* Verify a compact JWS against Apple's pinned certificate chain and return its decoded payload.
|
|
40
|
+
*
|
|
41
|
+
* Throws `payments/invalid_receipt` when the JWS is malformed — wrong segment count, a header Apple would
|
|
42
|
+
* never send, an unreadable certificate — and `payments/verification_failed` when it is well-formed but
|
|
43
|
+
* does not verify. Both are 400s; the distinction is for the operator reading `detail`, and the webhook
|
|
44
|
+
* guard maps either to `payments/webhook_unverified` on its own path.
|
|
45
|
+
*/
|
|
46
|
+
async function verifyAppleJws(jws, options) {
|
|
47
|
+
const segments = jws.split(".");
|
|
48
|
+
if (segments.length !== 3) throw new PaymentsInvalidReceiptError({ detail: `JWS: ${segments.length} segments, expected 3.` });
|
|
49
|
+
const [head, body, mac] = segments;
|
|
50
|
+
if (head.length === 0 || body.length === 0 || mac.length === 0) throw new PaymentsInvalidReceiptError({ detail: "JWS: a segment is empty; an unsigned token is not accepted." });
|
|
51
|
+
const header = AppleJwsHeader.safeParse(decodeJson(head, "header"));
|
|
52
|
+
if (!header.success) throw new PaymentsInvalidReceiptError({ detail: `JWS: header rejected — ${header.error.issues.map((issue) => `${issue.path.join(".") || "<root>"}:${issue.code}`).join(", ")}.` });
|
|
53
|
+
const leaf = await verifyCertificateChain(header.data.x5c.map(decodeBase64), {
|
|
54
|
+
roots: options.roots ?? APPLE_ROOT_CERTIFICATES,
|
|
55
|
+
now: options.now ?? /* @__PURE__ */ new Date()
|
|
56
|
+
});
|
|
57
|
+
if (leaf.curve !== "P-256") throw new PaymentsVerificationFailedError({ detail: `JWS: header claims ES256 but the leaf certificate is on ${leaf.curve}.` });
|
|
58
|
+
const signed = new TextEncoder().encode(`${head}.${body}`);
|
|
59
|
+
if (!await crypto.subtle.verify({
|
|
60
|
+
name: "ECDSA",
|
|
61
|
+
hash: "SHA-256"
|
|
62
|
+
}, leaf.key, decodeBase64Url(mac), signed)) throw new PaymentsVerificationFailedError({ detail: "JWS: the signature does not match the signed segments." });
|
|
63
|
+
return decodeJson(body, "payload");
|
|
64
|
+
}
|
|
65
|
+
/** One base64url JSON segment. `unknown` out — a verified byte string is not yet a known shape. */
|
|
66
|
+
function decodeJson(segment, label) {
|
|
67
|
+
const text = new TextDecoder().decode(decodeBase64Url(segment));
|
|
68
|
+
try {
|
|
69
|
+
return JSON.parse(text);
|
|
70
|
+
} catch (cause) {
|
|
71
|
+
throw new PaymentsInvalidReceiptError({ detail: `JWS: ${label} is not JSON.` }, { cause });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
export { verifyAppleJws };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { PurchaseEnvironment } from "../../data/purchase";
|
|
3
|
+
import type { PurchaseStatus } from "../../data/status";
|
|
4
|
+
import type { VerifiedNotification } from "../contract";
|
|
5
|
+
/**
|
|
6
|
+
* App Store Server Notifications V2 → the normalized provider event.
|
|
7
|
+
*
|
|
8
|
+
* This is where Apple's vocabulary stops. Apple has more than twenty notification types and a dozen
|
|
9
|
+
* subtypes; the projection has eight statuses and does not branch on any store's names. Getting a row of
|
|
10
|
+
* that table wrong revokes or grants access to somebody real, which is why each mapping below carries the
|
|
11
|
+
* reason rather than only the answer.
|
|
12
|
+
*
|
|
13
|
+
* Two decisions shape the rest:
|
|
14
|
+
*
|
|
15
|
+
* **A notification can be authentic and mean nothing about a purchase.** A test notification, a consumption
|
|
16
|
+
* request, a declined refund, a bulk renewal-extension summary — all real, none a state change. And Apple
|
|
17
|
+
* adds types: a type this package has never heard of must not be an error, because answering non-2xx makes
|
|
18
|
+
* Apple retry it forever and reads as a broken endpoint. So `event` is nullable, the row is recorded either
|
|
19
|
+
* way, and the reconciliation pass is what notices if something meaningful was missed.
|
|
20
|
+
*
|
|
21
|
+
* **The bundle id is checked, twice.** A valid Apple signature proves Apple signed the notification, not
|
|
22
|
+
* that it is about our app — Apple signs every developer's notifications with the same chain. Without a
|
|
23
|
+
* bundle check, another developer's transaction verifies here, and any SKU string that happens to match our
|
|
24
|
+
* catalog becomes a free entitlement. Both the envelope's `data.bundleId` and the nested transaction's own
|
|
25
|
+
* `bundleId` are checked, because a notification assembled around somebody else's transaction would pass a
|
|
26
|
+
* check on either one alone.
|
|
27
|
+
*/
|
|
28
|
+
/** Apple's price scale: `price` is in thousandths of the currency unit, not in minor units. */
|
|
29
|
+
export declare const APPLE_PRICE_SCALE = 1000;
|
|
30
|
+
/**
|
|
31
|
+
* The transaction payload, as much of it as the projection uses. `.loose()` keeps the rest: Apple adds
|
|
32
|
+
* fields, the whole payload is stored for reconciliation, and a strict object would reject a notification
|
|
33
|
+
* for carrying something new.
|
|
34
|
+
*/
|
|
35
|
+
export declare const AppleTransactionInfo: z.ZodObject<{
|
|
36
|
+
transactionId: z.ZodString;
|
|
37
|
+
originalTransactionId: z.ZodOptional<z.ZodString>;
|
|
38
|
+
bundleId: z.ZodString;
|
|
39
|
+
productId: z.ZodString;
|
|
40
|
+
purchaseDate: z.ZodNumber;
|
|
41
|
+
expiresDate: z.ZodOptional<z.ZodNumber>;
|
|
42
|
+
revocationDate: z.ZodOptional<z.ZodNumber>;
|
|
43
|
+
signedDate: z.ZodNumber;
|
|
44
|
+
environment: z.ZodString;
|
|
45
|
+
appAccountToken: z.ZodOptional<z.ZodString>;
|
|
46
|
+
price: z.ZodOptional<z.ZodNumber>;
|
|
47
|
+
currency: z.ZodOptional<z.ZodString>;
|
|
48
|
+
}, z.core.$loose>;
|
|
49
|
+
/**
|
|
50
|
+
* The renewal state beside a transaction, as much of it as the projection reads. `.loose()` for the same
|
|
51
|
+
* reason every other Apple schema is: Apple adds fields, and the payload is stored whole.
|
|
52
|
+
*
|
|
53
|
+
* Two fields, and both decide access rather than describe it. Neither is on the transaction, which is the
|
|
54
|
+
* whole reason this payload is read at all: a transaction says when the paid period ended, and only the
|
|
55
|
+
* renewal info says whether a card is being retried past it and until when.
|
|
56
|
+
*/
|
|
57
|
+
export declare const AppleRenewalInfo: z.ZodObject<{
|
|
58
|
+
autoRenewStatus: z.ZodOptional<z.ZodNumber>;
|
|
59
|
+
gracePeriodExpiresDate: z.ZodOptional<z.ZodNumber>;
|
|
60
|
+
}, z.core.$loose>;
|
|
61
|
+
/** What parsing a notification needs: our bundle id, the roots to trust, and the clock. */
|
|
62
|
+
export interface ParseAppleNotificationOptions {
|
|
63
|
+
/** Our own bundle id. A notification for any other app is refused. */
|
|
64
|
+
bundleId: string;
|
|
65
|
+
/** base64 DER of every acceptable root. Defaults to the pinned Apple roots. */
|
|
66
|
+
roots?: readonly string[];
|
|
67
|
+
/** The clock, for certificate validity. */
|
|
68
|
+
now?: Date;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Apple's store environment, normalized. Anything that is not literally `Production` is sandbox — including
|
|
72
|
+
* Xcode's `LocalTesting`, and including a value Apple has not published yet. Defaulting the other way is the
|
|
73
|
+
* most common in-app-purchase security defect there is, so the safe side is the default side.
|
|
74
|
+
*/
|
|
75
|
+
export declare function appleEnvironment(environment: string): PurchaseEnvironment;
|
|
76
|
+
/**
|
|
77
|
+
* Apple's notificationType/subtype pair → the normalized status, or null when the notification reports no
|
|
78
|
+
* transaction state.
|
|
79
|
+
*
|
|
80
|
+
* Every branch carries its reason. The two worth reading twice: `AUTO_RENEW_DISABLED` is `canceled` and
|
|
81
|
+
* `canceled` still grants — a user who declines the *next* period keeps the one they paid for, and
|
|
82
|
+
* `expiresAt` is what ends it. And `DID_FAIL_TO_RENEW` means two different things depending on its subtype:
|
|
83
|
+
* with `GRACE_PERIOD` the retry window is running and access continues; bare, it is not, and it does not.
|
|
84
|
+
*/
|
|
85
|
+
export declare function appleStatus(notificationType: string, subtype: string | undefined): PurchaseStatus | null;
|
|
86
|
+
/**
|
|
87
|
+
* When access ends: the paid period, extended to the grace window while a card is being retried.
|
|
88
|
+
*
|
|
89
|
+
* Grace exists so a failed renewal does not lock a paying subscriber out, and that only works if the row says
|
|
90
|
+
* so — `in_grace` with the expiry that just passed is read as lapsed by every access check, which revokes the
|
|
91
|
+
* subscriber in the same commit that recorded the grace period.
|
|
92
|
+
*
|
|
93
|
+
* Three rules, each holding a real payload up: the window is read **only** for `in_grace`, because Apple
|
|
94
|
+
* leaves `gracePeriodExpiresDate` on the renewal info after the retry is over and reading it on an `EXPIRED`
|
|
95
|
+
* would extend a subscription that has ended. It is the **later** of the two dates, so a grace date behind the
|
|
96
|
+
* expiry cannot shorten a period already paid for. And **no expiry stays no expiry** — a purchase that does not
|
|
97
|
+
* end is not narrowed by a window.
|
|
98
|
+
*/
|
|
99
|
+
export declare function appleExpiresAt(status: PurchaseStatus, expiresDate: number | undefined, gracePeriodExpiresDate: number | undefined): Date | null;
|
|
100
|
+
/**
|
|
101
|
+
* Refuse a payload that belongs to another app. `detail` names both ids; the client is told neither.
|
|
102
|
+
*
|
|
103
|
+
* Exported because the client-submission path needs the identical check: a signature proves Apple signed the
|
|
104
|
+
* transaction, never that the transaction is about our app, and one copy of that rule is the only way the two
|
|
105
|
+
* paths cannot diverge.
|
|
106
|
+
*/
|
|
107
|
+
export declare function requireAppleBundle(found: string, expected: string, where: string): void;
|
|
108
|
+
/**
|
|
109
|
+
* Verify and parse one App Store Server Notification V2.
|
|
110
|
+
*
|
|
111
|
+
* Both the outer notification and the nested transaction are verified independently. The outer signature
|
|
112
|
+
* already covers the inner JWS bytes, so verifying the inner one is belt as well as braces — but it costs
|
|
113
|
+
* one more chain check and removes a whole class of assumption about what the outer signature implies.
|
|
114
|
+
*/
|
|
115
|
+
export declare function parseAppleNotification(signedPayload: string, options: ParseAppleNotificationOptions): Promise<VerifiedNotification>;
|
|
116
|
+
//# sourceMappingURL=notification.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notification.d.ts","sourceRoot":"","sources":["../../../src/rails/apple/notification.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExD,OAAO,KAAK,EAAwB,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAG9E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,+FAA+F;AAC/F,eAAO,MAAM,iBAAiB,OAAO,CAAC;AAEtC;;;;GAIG;AACH,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;iBA6C0D,CAAC;AAE5F;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB;;;iBAkB+E,CAAC;AA8C7G,2FAA2F;AAC3F,MAAM,WAAW,6BAA6B;IAC5C,sEAAsE;IACtE,QAAQ,EAAE,MAAM,CAAC;IACjB,+EAA+E;IAC/E,KAAK,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1B,2CAA2C;IAC3C,GAAG,CAAC,EAAE,IAAI,CAAC;CACZ;AAED;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,WAAW,EAAE,MAAM,GAAG,mBAAmB,CAEzE;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,gBAAgB,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,cAAc,GAAG,IAAI,CAuCxG;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,cAAc,EACtB,WAAW,EAAE,MAAM,GAAG,SAAS,EAC/B,sBAAsB,EAAE,MAAM,GAAG,SAAS,GACzC,IAAI,GAAG,IAAI,CAIb;AAmBD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAOvF;AAED;;;;;;GAMG;AACH,wBAAsB,sBAAsB,CAC1C,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,6BAA6B,GACrC,OAAO,CAAC,oBAAoB,CAAC,CA2D/B"}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { PaymentsInvalidReceiptError, PaymentsVerificationFailedError } from "../../error/errors.js";
|
|
2
|
+
import { minorUnitsFromScaled } from "../../data/money.js";
|
|
3
|
+
import { verifyAppleJws } from "./jws.js";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
//#region src/rails/apple/notification.ts
|
|
6
|
+
/**
|
|
7
|
+
* App Store Server Notifications V2 → the normalized provider event.
|
|
8
|
+
*
|
|
9
|
+
* This is where Apple's vocabulary stops. Apple has more than twenty notification types and a dozen
|
|
10
|
+
* subtypes; the projection has eight statuses and does not branch on any store's names. Getting a row of
|
|
11
|
+
* that table wrong revokes or grants access to somebody real, which is why each mapping below carries the
|
|
12
|
+
* reason rather than only the answer.
|
|
13
|
+
*
|
|
14
|
+
* Two decisions shape the rest:
|
|
15
|
+
*
|
|
16
|
+
* **A notification can be authentic and mean nothing about a purchase.** A test notification, a consumption
|
|
17
|
+
* request, a declined refund, a bulk renewal-extension summary — all real, none a state change. And Apple
|
|
18
|
+
* adds types: a type this package has never heard of must not be an error, because answering non-2xx makes
|
|
19
|
+
* Apple retry it forever and reads as a broken endpoint. So `event` is nullable, the row is recorded either
|
|
20
|
+
* way, and the reconciliation pass is what notices if something meaningful was missed.
|
|
21
|
+
*
|
|
22
|
+
* **The bundle id is checked, twice.** A valid Apple signature proves Apple signed the notification, not
|
|
23
|
+
* that it is about our app — Apple signs every developer's notifications with the same chain. Without a
|
|
24
|
+
* bundle check, another developer's transaction verifies here, and any SKU string that happens to match our
|
|
25
|
+
* catalog becomes a free entitlement. Both the envelope's `data.bundleId` and the nested transaction's own
|
|
26
|
+
* `bundleId` are checked, because a notification assembled around somebody else's transaction would pass a
|
|
27
|
+
* check on either one alone.
|
|
28
|
+
*/
|
|
29
|
+
/** Apple's price scale: `price` is in thousandths of the currency unit, not in minor units. */
|
|
30
|
+
const APPLE_PRICE_SCALE = 1e3;
|
|
31
|
+
/**
|
|
32
|
+
* The transaction payload, as much of it as the projection uses. `.loose()` keeps the rest: Apple adds
|
|
33
|
+
* fields, the whole payload is stored for reconciliation, and a strict object would reject a notification
|
|
34
|
+
* for carrying something new.
|
|
35
|
+
*/
|
|
36
|
+
const AppleTransactionInfo = z.object({
|
|
37
|
+
transactionId: z.string().min(1).describe("Apple's own id for this transaction — the projection's idempotency anchor."),
|
|
38
|
+
originalTransactionId: z.string().min(1).optional().describe("The transaction that started the subscription, chaining renewals back to it. Absent for a one-off."),
|
|
39
|
+
bundleId: z.string().min(1).describe("The app the purchase was made in. Checked against our own — a signature alone does not prove the app."),
|
|
40
|
+
productId: z.string().min(1).describe("The App Store Connect product identifier — the SKU the catalog maps."),
|
|
41
|
+
purchaseDate: z.number().int().describe("When the store recorded the purchase, in milliseconds since the epoch."),
|
|
42
|
+
expiresDate: z.number().int().optional().describe("When the paid period ends, for a subscription. Absent for a one-time purchase."),
|
|
43
|
+
revocationDate: z.number().int().optional().describe("When the purchase was refunded or revoked, if it was."),
|
|
44
|
+
signedDate: z.number().int().describe("When Apple signed this transaction — its own clock, not ours."),
|
|
45
|
+
environment: z.string().min(1).describe("`Production`, `Sandbox`, or a local-testing value. Anything but Production is treated as sandbox."),
|
|
46
|
+
appAccountToken: z.string().min(1).optional().describe("The UUID the app set at purchase time. The only hook back to a Pithy user, so a purchase without one arrives orphaned."),
|
|
47
|
+
price: z.number().int().optional().describe("The price in thousandths of the currency unit — Apple's milliunits, not minor units."),
|
|
48
|
+
currency: z.string().min(1).optional().describe("The ISO currency the price is in.")
|
|
49
|
+
}).loose().describe("Apple's JWSTransactionDecodedPayload, as much of it as the projection reads.");
|
|
50
|
+
/**
|
|
51
|
+
* The renewal state beside a transaction, as much of it as the projection reads. `.loose()` for the same
|
|
52
|
+
* reason every other Apple schema is: Apple adds fields, and the payload is stored whole.
|
|
53
|
+
*
|
|
54
|
+
* Two fields, and both decide access rather than describe it. Neither is on the transaction, which is the
|
|
55
|
+
* whole reason this payload is read at all: a transaction says when the paid period ended, and only the
|
|
56
|
+
* renewal info says whether a card is being retried past it and until when.
|
|
57
|
+
*/
|
|
58
|
+
const AppleRenewalInfo = z.object({
|
|
59
|
+
autoRenewStatus: z.number().int().optional().describe("Whether the subscription will renew — 1 on, 0 off. Off with an active status is `canceled`, which still grants until the paid period ends."),
|
|
60
|
+
gracePeriodExpiresDate: z.number().int().optional().describe("When the billing-retry grace window ends, in milliseconds. This is what access runs to while Apple retries the card — the transaction's own `expiresDate` has already passed by then.")
|
|
61
|
+
}).loose().describe("Apple's JWSRenewalInfoDecodedPayload, as much of it as the status and the access window read.");
|
|
62
|
+
/** The notification envelope. Also `.loose()`, and for the same reason. */
|
|
63
|
+
const AppleNotificationPayload = z.object({
|
|
64
|
+
notificationType: z.string().min(1).describe("What happened, in Apple's vocabulary. Mapped to a normalized status."),
|
|
65
|
+
subtype: z.string().min(1).optional().describe("The qualifier that decides several mappings — GRACE_PERIOD, AUTO_RENEW_DISABLED, INITIAL_BUY."),
|
|
66
|
+
notificationUUID: z.string().min(1).describe("Apple's id for this delivery. The dedupe key: Apple delivers at-least-once and retries."),
|
|
67
|
+
signedDate: z.number().int().describe("When Apple signed the notification. This is the provider event time the monotonic write rule compares."),
|
|
68
|
+
data: z.object({
|
|
69
|
+
bundleId: z.string().min(1).describe("The app the notification is about. Checked against our own."),
|
|
70
|
+
environment: z.string().min(1).describe("The store environment the event happened in."),
|
|
71
|
+
signedTransactionInfo: z.string().min(1).optional().describe("The transaction, as its own signed JWS. Absent for a notification that reports no transaction."),
|
|
72
|
+
signedRenewalInfo: z.string().min(1).optional().describe("The subscription's renewal state, as its own signed JWS.")
|
|
73
|
+
}).loose().optional().describe("The event's data. Absent on the notification types that carry a summary instead.")
|
|
74
|
+
}).loose().describe("Apple's responseBodyV2DecodedPayload — one App Store Server Notification V2.");
|
|
75
|
+
/**
|
|
76
|
+
* Apple's store environment, normalized. Anything that is not literally `Production` is sandbox — including
|
|
77
|
+
* Xcode's `LocalTesting`, and including a value Apple has not published yet. Defaulting the other way is the
|
|
78
|
+
* most common in-app-purchase security defect there is, so the safe side is the default side.
|
|
79
|
+
*/
|
|
80
|
+
function appleEnvironment(environment) {
|
|
81
|
+
return environment === "Production" ? "production" : "sandbox";
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Apple's notificationType/subtype pair → the normalized status, or null when the notification reports no
|
|
85
|
+
* transaction state.
|
|
86
|
+
*
|
|
87
|
+
* Every branch carries its reason. The two worth reading twice: `AUTO_RENEW_DISABLED` is `canceled` and
|
|
88
|
+
* `canceled` still grants — a user who declines the *next* period keeps the one they paid for, and
|
|
89
|
+
* `expiresAt` is what ends it. And `DID_FAIL_TO_RENEW` means two different things depending on its subtype:
|
|
90
|
+
* with `GRACE_PERIOD` the retry window is running and access continues; bare, it is not, and it does not.
|
|
91
|
+
*/
|
|
92
|
+
function appleStatus(notificationType, subtype) {
|
|
93
|
+
switch (notificationType) {
|
|
94
|
+
case "SUBSCRIBED":
|
|
95
|
+
case "DID_RENEW":
|
|
96
|
+
case "OFFER_REDEEMED":
|
|
97
|
+
case "RENEWAL_EXTENDED":
|
|
98
|
+
case "ONE_TIME_CHARGE": return "active";
|
|
99
|
+
case "REFUND_REVERSED": return "active";
|
|
100
|
+
case "DID_CHANGE_RENEWAL_PREF": return "active";
|
|
101
|
+
case "DID_CHANGE_RENEWAL_STATUS": return subtype === "AUTO_RENEW_DISABLED" ? "canceled" : "active";
|
|
102
|
+
case "DID_FAIL_TO_RENEW": return subtype === "GRACE_PERIOD" ? "in_grace" : "on_hold";
|
|
103
|
+
case "GRACE_PERIOD_EXPIRED": return "on_hold";
|
|
104
|
+
case "EXPIRED": return "expired";
|
|
105
|
+
case "REFUND": return "refunded";
|
|
106
|
+
case "REVOKE": return "revoked";
|
|
107
|
+
default: return null;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* When access ends: the paid period, extended to the grace window while a card is being retried.
|
|
112
|
+
*
|
|
113
|
+
* Grace exists so a failed renewal does not lock a paying subscriber out, and that only works if the row says
|
|
114
|
+
* so — `in_grace` with the expiry that just passed is read as lapsed by every access check, which revokes the
|
|
115
|
+
* subscriber in the same commit that recorded the grace period.
|
|
116
|
+
*
|
|
117
|
+
* Three rules, each holding a real payload up: the window is read **only** for `in_grace`, because Apple
|
|
118
|
+
* leaves `gracePeriodExpiresDate` on the renewal info after the retry is over and reading it on an `EXPIRED`
|
|
119
|
+
* would extend a subscription that has ended. It is the **later** of the two dates, so a grace date behind the
|
|
120
|
+
* expiry cannot shorten a period already paid for. And **no expiry stays no expiry** — a purchase that does not
|
|
121
|
+
* end is not narrowed by a window.
|
|
122
|
+
*/
|
|
123
|
+
function appleExpiresAt(status, expiresDate, gracePeriodExpiresDate) {
|
|
124
|
+
if (expiresDate === void 0) return null;
|
|
125
|
+
const grace = status === "in_grace" ? gracePeriodExpiresDate : void 0;
|
|
126
|
+
return new Date(grace === void 0 ? expiresDate : Math.max(expiresDate, grace));
|
|
127
|
+
}
|
|
128
|
+
/** Verify a nested JWS and parse it against `schema`, naming the field in any refusal. */
|
|
129
|
+
async function verifyNested(jws, schema, field, options) {
|
|
130
|
+
const payload = await verifyAppleJws(jws, {
|
|
131
|
+
roots: options.roots,
|
|
132
|
+
now: options.now
|
|
133
|
+
});
|
|
134
|
+
const parsed = schema.safeParse(payload);
|
|
135
|
+
if (!parsed.success) throw new PaymentsInvalidReceiptError({ detail: `Apple: ${field} rejected — ${parsed.error.issues.map((issue) => `${issue.path.join(".") || "<root>"}:${issue.code}`).join(", ")}.` });
|
|
136
|
+
return parsed.data;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Refuse a payload that belongs to another app. `detail` names both ids; the client is told neither.
|
|
140
|
+
*
|
|
141
|
+
* Exported because the client-submission path needs the identical check: a signature proves Apple signed the
|
|
142
|
+
* transaction, never that the transaction is about our app, and one copy of that rule is the only way the two
|
|
143
|
+
* paths cannot diverge.
|
|
144
|
+
*/
|
|
145
|
+
function requireAppleBundle(found, expected, where) {
|
|
146
|
+
if (found === expected) return;
|
|
147
|
+
throw new PaymentsVerificationFailedError({
|
|
148
|
+
message: "That notification is not for this app.",
|
|
149
|
+
action: "Check that the App Store Connect app and this deployment's bundle id are the same.",
|
|
150
|
+
detail: `Apple: ${where} bundle id is "${found}", this app is "${expected}".`
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Verify and parse one App Store Server Notification V2.
|
|
155
|
+
*
|
|
156
|
+
* Both the outer notification and the nested transaction are verified independently. The outer signature
|
|
157
|
+
* already covers the inner JWS bytes, so verifying the inner one is belt as well as braces — but it costs
|
|
158
|
+
* one more chain check and removes a whole class of assumption about what the outer signature implies.
|
|
159
|
+
*/
|
|
160
|
+
async function parseAppleNotification(signedPayload, options) {
|
|
161
|
+
const notification = await verifyNested(signedPayload, AppleNotificationPayload, "notification", options);
|
|
162
|
+
if (notification.data) requireAppleBundle(notification.data.bundleId, options.bundleId, "notification");
|
|
163
|
+
const status = appleStatus(notification.notificationType, notification.subtype);
|
|
164
|
+
const payload = notification;
|
|
165
|
+
if (status === null) return {
|
|
166
|
+
providerEventId: notification.notificationUUID,
|
|
167
|
+
payload,
|
|
168
|
+
event: null,
|
|
169
|
+
providerAccountId: null
|
|
170
|
+
};
|
|
171
|
+
const signedTransaction = notification.data?.signedTransactionInfo;
|
|
172
|
+
if (!signedTransaction) throw new PaymentsInvalidReceiptError({ detail: `Apple: ${notification.notificationType} carries no signedTransactionInfo, so there is no transaction to project.` });
|
|
173
|
+
const transaction = await verifyNested(signedTransaction, AppleTransactionInfo, "transaction", options);
|
|
174
|
+
requireAppleBundle(transaction.bundleId, options.bundleId, "transaction");
|
|
175
|
+
const renewal = status === "in_grace" && notification.data?.signedRenewalInfo !== void 0 ? await verifyNested(notification.data.signedRenewalInfo, AppleRenewalInfo, "renewal info", options) : void 0;
|
|
176
|
+
const event = {
|
|
177
|
+
rail: "apple",
|
|
178
|
+
providerTransactionId: transaction.transactionId,
|
|
179
|
+
providerProductId: transaction.productId,
|
|
180
|
+
status,
|
|
181
|
+
environment: appleEnvironment(transaction.environment),
|
|
182
|
+
purchasedAt: new Date(transaction.purchaseDate),
|
|
183
|
+
expiresAt: appleExpiresAt(status, transaction.expiresDate, renewal?.gracePeriodExpiresDate),
|
|
184
|
+
revokedAt: transaction.revocationDate === void 0 ? null : new Date(transaction.revocationDate),
|
|
185
|
+
originalTransactionId: transaction.originalTransactionId ?? null,
|
|
186
|
+
amountMinor: minorUnitsFromScaled(transaction.price ?? null, APPLE_PRICE_SCALE, transaction.currency ?? null),
|
|
187
|
+
currency: transaction.currency ?? null,
|
|
188
|
+
providerEventAt: new Date(notification.signedDate),
|
|
189
|
+
payload
|
|
190
|
+
};
|
|
191
|
+
return {
|
|
192
|
+
providerEventId: notification.notificationUUID,
|
|
193
|
+
payload,
|
|
194
|
+
event,
|
|
195
|
+
providerAccountId: transaction.appAccountToken ?? null
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
//#endregion
|
|
199
|
+
export { APPLE_PRICE_SCALE, AppleRenewalInfo, AppleTransactionInfo, appleEnvironment, appleExpiresAt, appleStatus, parseAppleNotification, requireAppleBundle };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { PaymentsAppleCredentials } from "../../secret/registry";
|
|
2
|
+
import type { PaymentsRailProvider } from "../contract";
|
|
3
|
+
import type { AppleHttpFetch } from "./http";
|
|
4
|
+
/**
|
|
5
|
+
* How the Apple rail's trust set may be widened, and the only two callers with a reason to.
|
|
6
|
+
*
|
|
7
|
+
* Apple's pinned roots are always trusted and can never be removed — `trustedRoots` is additive, so no caller
|
|
8
|
+
* can narrow production's trust to nothing. It exists because two legitimate chains are not Apple's: the tests,
|
|
9
|
+
* which mint their own chain so signature verification is exercised for real, and **Xcode's StoreKit local
|
|
10
|
+
* testing**, whose transactions are signed by a per-machine root that only exists on the developer's Mac.
|
|
11
|
+
*/
|
|
12
|
+
export interface AppleRailOptions {
|
|
13
|
+
/** Roots to accept *in addition to* Apple's pinned ones, base64 DER. Empty in production. */
|
|
14
|
+
trustedRoots?: readonly string[];
|
|
15
|
+
/** The HTTP seam the App Store Server API is reached through. Defaults to the runtime's `fetch`. */
|
|
16
|
+
transport?: AppleHttpFetch;
|
|
17
|
+
/**
|
|
18
|
+
* An App Store Connect token already minted, so a batch of refreshes pays for one. Passed down by the
|
|
19
|
+
* reconciliation Workflow; absent everywhere else, where one call mints its own and drops it.
|
|
20
|
+
*/
|
|
21
|
+
apiToken?: string;
|
|
22
|
+
}
|
|
23
|
+
/** The Apple rail. Verifies client submissions locally, and App Store Server Notifications V2 on the webhook. */
|
|
24
|
+
export declare function appleRail(credentials: PaymentsAppleCredentials, options?: AppleRailOptions): PaymentsRailProvider;
|
|
25
|
+
//# sourceMappingURL=rail.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rail.d.ts","sourceRoot":"","sources":["../../../src/rails/apple/rail.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,uBAAuB,CAAC;AACtE,OAAO,KAAK,EACV,oBAAoB,EAMrB,MAAM,aAAa,CAAC;AAErB,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAmC7C;;;;;;;GAOG;AACH,MAAM,WAAW,gBAAgB;IAC/B,6FAA6F;IAC7F,YAAY,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACjC,oGAAoG;IACpG,SAAS,CAAC,EAAE,cAAc,CAAC;IAC3B;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,iHAAiH;AACjH,wBAAgB,SAAS,CAAC,WAAW,EAAE,wBAAwB,EAAE,OAAO,GAAE,gBAAqB,GAAG,oBAAoB,CAyCrH"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { PaymentsInvalidReceiptError } from "../../error/errors.js";
|
|
2
|
+
import { APPLE_ROOT_CERTIFICATES } from "./certs.js";
|
|
3
|
+
import { parseAppleNotification } from "./notification.js";
|
|
4
|
+
import { refreshAppleSubscription } from "./serverApi.js";
|
|
5
|
+
import { verifyAppleTransaction } from "./verify.js";
|
|
6
|
+
import { z } from "zod";
|
|
7
|
+
//#region src/rails/apple/rail.ts
|
|
8
|
+
/**
|
|
9
|
+
* The Apple rail as one provider object — the two halves of the contract, closed over the app's identity.
|
|
10
|
+
*
|
|
11
|
+
* Built per request from credentials the caller resolved through the secrets store, rather than reading them
|
|
12
|
+
* itself. That keeps the rail a pure function of its inputs (so every test above runs with no secrets
|
|
13
|
+
* machinery), and it keeps the read at the point of need, which is what CLAUDE.md asks for.
|
|
14
|
+
*
|
|
15
|
+
* Apple needs no credential to *verify* anything: authenticity rests on the public certificate chain pinned
|
|
16
|
+
* in `certs.ts`. What it needs from the bundle is the `bundleId` — the app's identity, checked on every
|
|
17
|
+
* payload, because Apple signs every developer's notifications with the same chain. The App Store Connect key
|
|
18
|
+
* in the same block is for calling the App Store Server API, which the reconciliation Workflow does.
|
|
19
|
+
*/
|
|
20
|
+
/**
|
|
21
|
+
* Apple's webhook body: one field, carrying the whole signed notification.
|
|
22
|
+
*
|
|
23
|
+
* Declared here as well as in `http/schemas.ts`, and the duplication is the lesser evil. The rail parses the
|
|
24
|
+
* bytes it was handed in order to *verify* them; the route's validator gives the handler a typed value. The
|
|
25
|
+
* rail is the lower layer, so it must not import an HTTP schema, and re-exporting one across the seam would
|
|
26
|
+
* be worse than six lines. The two must agree — change one, change the other.
|
|
27
|
+
*/
|
|
28
|
+
const AppleWebhookBody = z.object({ signedPayload: z.string().min(1).describe("The App Store Server Notification V2, as a compact JWS. Everything else about the delivery is in it.") }).describe("The body Apple POSTs to the notification endpoint.");
|
|
29
|
+
/** The Apple rail. Verifies client submissions locally, and App Store Server Notifications V2 on the webhook. */
|
|
30
|
+
function appleRail(credentials, options = {}) {
|
|
31
|
+
const roots = options.trustedRoots?.length ? [...APPLE_ROOT_CERTIFICATES, ...options.trustedRoots] : void 0;
|
|
32
|
+
return {
|
|
33
|
+
rail: "apple",
|
|
34
|
+
async verify(receipt, context) {
|
|
35
|
+
return verifyAppleTransaction(receipt, {
|
|
36
|
+
bundleId: credentials.bundleId,
|
|
37
|
+
roots,
|
|
38
|
+
now: context.now
|
|
39
|
+
});
|
|
40
|
+
},
|
|
41
|
+
async parseNotification(delivery, context) {
|
|
42
|
+
let body;
|
|
43
|
+
try {
|
|
44
|
+
body = JSON.parse(delivery.body);
|
|
45
|
+
} catch (cause) {
|
|
46
|
+
throw new PaymentsInvalidReceiptError({ detail: "Apple: the notification body is not JSON." }, { cause });
|
|
47
|
+
}
|
|
48
|
+
const parsed = AppleWebhookBody.safeParse(body);
|
|
49
|
+
if (!parsed.success) throw new PaymentsInvalidReceiptError({ detail: "Apple: the notification body carries no signedPayload." });
|
|
50
|
+
return parseAppleNotification(parsed.data.signedPayload, {
|
|
51
|
+
bundleId: credentials.bundleId,
|
|
52
|
+
roots,
|
|
53
|
+
now: context.now
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
async refresh(purchase, context) {
|
|
57
|
+
return await refreshAppleSubscription(purchase, {
|
|
58
|
+
credentials,
|
|
59
|
+
now: context.now,
|
|
60
|
+
transport: options.transport,
|
|
61
|
+
roots,
|
|
62
|
+
token: options.apiToken
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
export { appleRail };
|