@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.
Files changed (402) hide show
  1. package/dist/admin/coverage.d.ts +35 -0
  2. package/dist/admin/coverage.d.ts.map +1 -0
  3. package/dist/admin/coverage.js +56 -0
  4. package/dist/admin/read.d.ts +222 -0
  5. package/dist/admin/read.d.ts.map +1 -0
  6. package/dist/admin/read.js +159 -0
  7. package/dist/audit/actions.d.ts +211 -0
  8. package/dist/audit/actions.d.ts.map +1 -0
  9. package/dist/audit/actions.js +211 -0
  10. package/dist/capability.d.ts +72 -0
  11. package/dist/capability.d.ts.map +1 -0
  12. package/dist/capability.js +228 -0
  13. package/dist/client/api.d.ts +435 -0
  14. package/dist/client/api.d.ts.map +1 -0
  15. package/dist/client/api.js +415 -0
  16. package/dist/client/checkout.d.ts +144 -0
  17. package/dist/client/checkout.d.ts.map +1 -0
  18. package/dist/client/checkout.js +173 -0
  19. package/dist/client/fixtures/price-preview-de.js +89 -0
  20. package/dist/client/fixtures/price-preview-gb.js +89 -0
  21. package/dist/client/fixtures/price-preview-jp-yen.js +89 -0
  22. package/dist/client/fixtures/price-preview-us-country-only.js +89 -0
  23. package/dist/client/fixtures/price-preview-us-new-york.js +89 -0
  24. package/dist/client/fixtures/pricePreview.d.ts +54 -0
  25. package/dist/client/fixtures/pricePreview.d.ts.map +1 -0
  26. package/dist/client/fixtures/pricePreview.js +61 -0
  27. package/dist/client/hooks.d.ts +235 -0
  28. package/dist/client/hooks.d.ts.map +1 -0
  29. package/dist/client/hooks.js +428 -0
  30. package/dist/client/paddle.d.ts +433 -0
  31. package/dist/client/paddle.d.ts.map +1 -0
  32. package/dist/client/paddle.js +391 -0
  33. package/dist/client/paddleCache.d.ts +123 -0
  34. package/dist/client/paddleCache.d.ts.map +1 -0
  35. package/dist/client/paddleCache.js +159 -0
  36. package/dist/client/paddlePrices.d.ts +94 -0
  37. package/dist/client/paddlePrices.d.ts.map +1 -0
  38. package/dist/client/paddlePrices.js +55 -0
  39. package/dist/client/paddlePricesTag.d.ts +127 -0
  40. package/dist/client/paddlePricesTag.d.ts.map +1 -0
  41. package/dist/client/paddlePricesTag.js +207 -0
  42. package/dist/client/projection.d.ts +128 -0
  43. package/dist/client/projection.d.ts.map +1 -0
  44. package/dist/client/projection.js +0 -0
  45. package/dist/client/test-utils/cacheStore.d.ts +18 -0
  46. package/dist/client/test-utils/cacheStore.d.ts.map +1 -0
  47. package/dist/client/test-utils/cacheStore.js +42 -0
  48. package/dist/client/wholeUnits.d.ts +18 -0
  49. package/dist/client/wholeUnits.d.ts.map +1 -0
  50. package/dist/client/wholeUnits.js +120 -0
  51. package/dist/config/config.d.ts +238 -0
  52. package/dist/config/config.d.ts.map +1 -0
  53. package/dist/config/config.js +249 -0
  54. package/dist/data/discount.d.ts +127 -0
  55. package/dist/data/discount.d.ts.map +1 -0
  56. package/dist/data/discount.js +91 -0
  57. package/dist/data/entitlement.d.ts +43 -0
  58. package/dist/data/entitlement.d.ts.map +1 -0
  59. package/dist/data/entitlement.js +43 -0
  60. package/dist/data/money.d.ts +15 -0
  61. package/dist/data/money.d.ts.map +1 -0
  62. package/dist/data/money.js +76 -0
  63. package/dist/data/pause.d.ts +133 -0
  64. package/dist/data/pause.d.ts.map +1 -0
  65. package/dist/data/pause.js +63 -0
  66. package/dist/data/providerAccount.d.ts +40 -0
  67. package/dist/data/providerAccount.d.ts.map +1 -0
  68. package/dist/data/providerAccount.js +34 -0
  69. package/dist/data/purchase.d.ts +107 -0
  70. package/dist/data/purchase.d.ts.map +1 -0
  71. package/dist/data/purchase.js +77 -0
  72. package/dist/data/rail.d.ts +74 -0
  73. package/dist/data/rail.d.ts.map +1 -0
  74. package/dist/data/rail.js +74 -0
  75. package/dist/data/reconcileRun.d.ts +120 -0
  76. package/dist/data/reconcileRun.d.ts.map +1 -0
  77. package/dist/data/reconcileRun.js +120 -0
  78. package/dist/data/renderMoney.d.ts +85 -0
  79. package/dist/data/renderMoney.d.ts.map +1 -0
  80. package/dist/data/renderMoney.js +146 -0
  81. package/dist/data/status.d.ts +58 -0
  82. package/dist/data/status.d.ts.map +1 -0
  83. package/dist/data/status.js +68 -0
  84. package/dist/data/subject.d.ts +111 -0
  85. package/dist/data/subject.d.ts.map +1 -0
  86. package/dist/data/subject.js +125 -0
  87. package/dist/data/subscription.d.ts +624 -0
  88. package/dist/data/subscription.d.ts.map +1 -0
  89. package/dist/data/subscription.js +497 -0
  90. package/dist/data/syncCursor.d.ts +33 -0
  91. package/dist/data/syncCursor.d.ts.map +1 -0
  92. package/dist/data/syncCursor.js +29 -0
  93. package/dist/data/tables.d.ts +37 -0
  94. package/dist/data/tables.d.ts.map +1 -0
  95. package/dist/data/tables.js +44 -0
  96. package/dist/data/webhookEvent.d.ts +134 -0
  97. package/dist/data/webhookEvent.d.ts.map +1 -0
  98. package/dist/data/webhookEvent.js +128 -0
  99. package/dist/entitlement/manual.d.ts +101 -0
  100. package/dist/entitlement/manual.d.ts.map +1 -0
  101. package/dist/entitlement/manual.js +84 -0
  102. package/dist/entitlement/resolver.d.ts +60 -0
  103. package/dist/entitlement/resolver.d.ts.map +1 -0
  104. package/dist/entitlement/resolver.js +101 -0
  105. package/dist/entitlement/subjectSeam.d.ts +102 -0
  106. package/dist/entitlement/subjectSeam.d.ts.map +1 -0
  107. package/dist/entitlement/subjectSeam.js +85 -0
  108. package/dist/error/errors.d.ts +247 -0
  109. package/dist/error/errors.d.ts.map +1 -0
  110. package/dist/error/errors.js +326 -0
  111. package/dist/grants/apply.d.ts +74 -0
  112. package/dist/grants/apply.d.ts.map +1 -0
  113. package/dist/grants/apply.js +148 -0
  114. package/dist/grants/clawback.d.ts +43 -0
  115. package/dist/grants/clawback.d.ts.map +1 -0
  116. package/dist/grants/clawback.js +71 -0
  117. package/dist/grants/ledgerSeam.d.ts +89 -0
  118. package/dist/grants/ledgerSeam.d.ts.map +1 -0
  119. package/dist/grants/ledgerSeam.js +66 -0
  120. package/dist/http/dispatch.d.ts +35 -0
  121. package/dist/http/dispatch.d.ts.map +1 -0
  122. package/dist/http/dispatch.js +40 -0
  123. package/dist/http/guards.d.ts +43 -0
  124. package/dist/http/guards.d.ts.map +1 -0
  125. package/dist/http/guards.js +52 -0
  126. package/dist/http/manifestConfig.d.ts +38 -0
  127. package/dist/http/manifestConfig.d.ts.map +1 -0
  128. package/dist/http/manifestConfig.js +49 -0
  129. package/dist/http/responses.d.ts +1455 -0
  130. package/dist/http/responses.d.ts.map +1 -0
  131. package/dist/http/responses.js +728 -0
  132. package/dist/http/routes.d.ts +315 -0
  133. package/dist/http/routes.d.ts.map +1 -0
  134. package/dist/http/routes.js +1966 -0
  135. package/dist/http/schemas.d.ts +404 -0
  136. package/dist/http/schemas.d.ts.map +1 -0
  137. package/dist/http/schemas.js +359 -0
  138. package/dist/http/scopes.d.ts +160 -0
  139. package/dist/http/scopes.d.ts.map +1 -0
  140. package/dist/http/scopes.js +240 -0
  141. package/dist/http/view.d.ts +96 -0
  142. package/dist/http/view.d.ts.map +1 -0
  143. package/dist/http/view.js +170 -0
  144. package/dist/http/webhookGuard.d.ts +109 -0
  145. package/dist/http/webhookGuard.d.ts.map +1 -0
  146. package/dist/http/webhookGuard.js +206 -0
  147. package/dist/index.d.ts +64 -0
  148. package/dist/index.d.ts.map +1 -0
  149. package/dist/index.js +43 -0
  150. package/dist/migrations/0001_purchases.d.ts +35 -0
  151. package/dist/migrations/0001_purchases.d.ts.map +1 -0
  152. package/dist/migrations/0001_purchases.js +80 -0
  153. package/dist/pricing/location.d.ts +138 -0
  154. package/dist/pricing/location.d.ts.map +1 -0
  155. package/dist/pricing/location.js +81 -0
  156. package/dist/pricing/visitor.d.ts +56 -0
  157. package/dist/pricing/visitor.d.ts.map +1 -0
  158. package/dist/pricing/visitor.js +35 -0
  159. package/dist/projection/event.d.ts +66 -0
  160. package/dist/projection/event.d.ts.map +1 -0
  161. package/dist/projection/event.js +45 -0
  162. package/dist/projection/orphans.d.ts +114 -0
  163. package/dist/projection/orphans.d.ts.map +1 -0
  164. package/dist/projection/orphans.js +162 -0
  165. package/dist/projection/owner.d.ts +121 -0
  166. package/dist/projection/owner.d.ts.map +1 -0
  167. package/dist/projection/owner.js +114 -0
  168. package/dist/projection/resolve.d.ts +30 -0
  169. package/dist/projection/resolve.d.ts.map +1 -0
  170. package/dist/projection/resolve.js +42 -0
  171. package/dist/projection/writer.d.ts +103 -0
  172. package/dist/projection/writer.d.ts.map +1 -0
  173. package/dist/projection/writer.js +280 -0
  174. package/dist/provision/resolvePaymentsConfig.d.ts +48 -0
  175. package/dist/provision/resolvePaymentsConfig.d.ts.map +1 -0
  176. package/dist/provision/resolvePaymentsConfig.js +41 -0
  177. package/dist/rails/apple/certs.d.ts +7 -0
  178. package/dist/rails/apple/certs.d.ts.map +1 -0
  179. package/dist/rails/apple/certs.js +8 -0
  180. package/dist/rails/apple/der.d.ts +107 -0
  181. package/dist/rails/apple/der.d.ts.map +1 -0
  182. package/dist/rails/apple/der.js +217 -0
  183. package/dist/rails/apple/fixtures/chain.d.ts +117 -0
  184. package/dist/rails/apple/fixtures/chain.d.ts.map +1 -0
  185. package/dist/rails/apple/fixtures/chain.js +289 -0
  186. package/dist/rails/apple/http.d.ts +74 -0
  187. package/dist/rails/apple/http.d.ts.map +1 -0
  188. package/dist/rails/apple/http.js +32 -0
  189. package/dist/rails/apple/jws.d.ts +17 -0
  190. package/dist/rails/apple/jws.d.ts.map +1 -0
  191. package/dist/rails/apple/jws.js +75 -0
  192. package/dist/rails/apple/notification.d.ts +116 -0
  193. package/dist/rails/apple/notification.d.ts.map +1 -0
  194. package/dist/rails/apple/notification.js +199 -0
  195. package/dist/rails/apple/rail.d.ts +25 -0
  196. package/dist/rails/apple/rail.d.ts.map +1 -0
  197. package/dist/rails/apple/rail.js +68 -0
  198. package/dist/rails/apple/serverApi.d.ts +120 -0
  199. package/dist/rails/apple/serverApi.d.ts.map +1 -0
  200. package/dist/rails/apple/serverApi.js +244 -0
  201. package/dist/rails/apple/verify.d.ts +36 -0
  202. package/dist/rails/apple/verify.d.ts.map +1 -0
  203. package/dist/rails/apple/verify.js +46 -0
  204. package/dist/rails/apple/x509.d.ts +85 -0
  205. package/dist/rails/apple/x509.d.ts.map +1 -0
  206. package/dist/rails/apple/x509.js +273 -0
  207. package/dist/rails/contract.d.ts +871 -0
  208. package/dist/rails/contract.d.ts.map +1 -0
  209. package/dist/rails/contract.js +60 -0
  210. package/dist/rails/google/fixtures/push.d.ts +71 -0
  211. package/dist/rails/google/fixtures/push.d.ts.map +1 -0
  212. package/dist/rails/google/fixtures/push.js +107 -0
  213. package/dist/rails/google/http.d.ts +77 -0
  214. package/dist/rails/google/http.d.ts.map +1 -0
  215. package/dist/rails/google/http.js +39 -0
  216. package/dist/rails/google/jwt.d.ts +53 -0
  217. package/dist/rails/google/jwt.d.ts.map +1 -0
  218. package/dist/rails/google/jwt.js +94 -0
  219. package/dist/rails/google/oidc.d.ts +112 -0
  220. package/dist/rails/google/oidc.d.ts.map +1 -0
  221. package/dist/rails/google/oidc.js +179 -0
  222. package/dist/rails/google/playApi.d.ts +188 -0
  223. package/dist/rails/google/playApi.d.ts.map +1 -0
  224. package/dist/rails/google/playApi.js +350 -0
  225. package/dist/rails/google/rail.d.ts +57 -0
  226. package/dist/rails/google/rail.d.ts.map +1 -0
  227. package/dist/rails/google/rail.js +70 -0
  228. package/dist/rails/google/rtdn.d.ts +105 -0
  229. package/dist/rails/google/rtdn.d.ts.map +1 -0
  230. package/dist/rails/google/rtdn.js +179 -0
  231. package/dist/rails/google/verify.d.ts +20 -0
  232. package/dist/rails/google/verify.d.ts.map +1 -0
  233. package/dist/rails/google/verify.js +76 -0
  234. package/dist/rails/lemonSqueezy/api.d.ts +92 -0
  235. package/dist/rails/lemonSqueezy/api.d.ts.map +1 -0
  236. package/dist/rails/lemonSqueezy/api.js +121 -0
  237. package/dist/rails/lemonSqueezy/checkout.d.ts +52 -0
  238. package/dist/rails/lemonSqueezy/checkout.d.ts.map +1 -0
  239. package/dist/rails/lemonSqueezy/checkout.js +84 -0
  240. package/dist/rails/lemonSqueezy/discounts.d.ts +50 -0
  241. package/dist/rails/lemonSqueezy/discounts.d.ts.map +1 -0
  242. package/dist/rails/lemonSqueezy/discounts.js +75 -0
  243. package/dist/rails/lemonSqueezy/fixtures/events.d.ts +56 -0
  244. package/dist/rails/lemonSqueezy/fixtures/events.d.ts.map +1 -0
  245. package/dist/rails/lemonSqueezy/fixtures/events.js +144 -0
  246. package/dist/rails/lemonSqueezy/objects.d.ts +229 -0
  247. package/dist/rails/lemonSqueezy/objects.d.ts.map +1 -0
  248. package/dist/rails/lemonSqueezy/objects.js +379 -0
  249. package/dist/rails/lemonSqueezy/portal.d.ts +35 -0
  250. package/dist/rails/lemonSqueezy/portal.d.ts.map +1 -0
  251. package/dist/rails/lemonSqueezy/portal.js +18 -0
  252. package/dist/rails/lemonSqueezy/pricing.d.ts +25 -0
  253. package/dist/rails/lemonSqueezy/pricing.d.ts.map +1 -0
  254. package/dist/rails/lemonSqueezy/pricing.js +54 -0
  255. package/dist/rails/lemonSqueezy/rail.d.ts +41 -0
  256. package/dist/rails/lemonSqueezy/rail.d.ts.map +1 -0
  257. package/dist/rails/lemonSqueezy/rail.js +67 -0
  258. package/dist/rails/lemonSqueezy/read.d.ts +28 -0
  259. package/dist/rails/lemonSqueezy/read.d.ts.map +1 -0
  260. package/dist/rails/lemonSqueezy/read.js +37 -0
  261. package/dist/rails/lemonSqueezy/refresh.d.ts +42 -0
  262. package/dist/rails/lemonSqueezy/refresh.d.ts.map +1 -0
  263. package/dist/rails/lemonSqueezy/refresh.js +40 -0
  264. package/dist/rails/lemonSqueezy/signature.d.ts +56 -0
  265. package/dist/rails/lemonSqueezy/signature.d.ts.map +1 -0
  266. package/dist/rails/lemonSqueezy/signature.js +92 -0
  267. package/dist/rails/lemonSqueezy/verify.d.ts +33 -0
  268. package/dist/rails/lemonSqueezy/verify.d.ts.map +1 -0
  269. package/dist/rails/lemonSqueezy/verify.js +41 -0
  270. package/dist/rails/lemonSqueezy/webhook.d.ts +53 -0
  271. package/dist/rails/lemonSqueezy/webhook.d.ts.map +1 -0
  272. package/dist/rails/lemonSqueezy/webhook.js +179 -0
  273. package/dist/rails/paddle/adjustments.d.ts +60 -0
  274. package/dist/rails/paddle/adjustments.d.ts.map +1 -0
  275. package/dist/rails/paddle/adjustments.js +66 -0
  276. package/dist/rails/paddle/api.d.ts +138 -0
  277. package/dist/rails/paddle/api.d.ts.map +1 -0
  278. package/dist/rails/paddle/api.js +152 -0
  279. package/dist/rails/paddle/checkout.d.ts +69 -0
  280. package/dist/rails/paddle/checkout.d.ts.map +1 -0
  281. package/dist/rails/paddle/checkout.js +100 -0
  282. package/dist/rails/paddle/discounts.d.ts +67 -0
  283. package/dist/rails/paddle/discounts.d.ts.map +1 -0
  284. package/dist/rails/paddle/discounts.js +73 -0
  285. package/dist/rails/paddle/events.d.ts +140 -0
  286. package/dist/rails/paddle/events.d.ts.map +1 -0
  287. package/dist/rails/paddle/events.js +183 -0
  288. package/dist/rails/paddle/fixtures/browserForged.d.ts +42 -0
  289. package/dist/rails/paddle/fixtures/browserForged.d.ts.map +1 -0
  290. package/dist/rails/paddle/fixtures/browserForged.js +52 -0
  291. package/dist/rails/paddle/fixtures/pausedSubscription.d.ts +78 -0
  292. package/dist/rails/paddle/fixtures/pausedSubscription.d.ts.map +1 -0
  293. package/dist/rails/paddle/fixtures/pausedSubscription.js +76 -0
  294. package/dist/rails/paddle/objects.d.ts +519 -0
  295. package/dist/rails/paddle/objects.d.ts.map +1 -0
  296. package/dist/rails/paddle/objects.js +588 -0
  297. package/dist/rails/paddle/portal.d.ts +45 -0
  298. package/dist/rails/paddle/portal.d.ts.map +1 -0
  299. package/dist/rails/paddle/portal.js +42 -0
  300. package/dist/rails/paddle/rail.d.ts +55 -0
  301. package/dist/rails/paddle/rail.d.ts.map +1 -0
  302. package/dist/rails/paddle/rail.js +117 -0
  303. package/dist/rails/paddle/read.d.ts +53 -0
  304. package/dist/rails/paddle/read.d.ts.map +1 -0
  305. package/dist/rails/paddle/read.js +66 -0
  306. package/dist/rails/paddle/recorded.d.ts +49 -0
  307. package/dist/rails/paddle/recorded.d.ts.map +1 -0
  308. package/dist/rails/paddle/recorded.js +79 -0
  309. package/dist/rails/paddle/refresh.d.ts +54 -0
  310. package/dist/rails/paddle/refresh.d.ts.map +1 -0
  311. package/dist/rails/paddle/refresh.js +94 -0
  312. package/dist/rails/paddle/refund.d.ts +82 -0
  313. package/dist/rails/paddle/refund.d.ts.map +1 -0
  314. package/dist/rails/paddle/refund.js +175 -0
  315. package/dist/rails/paddle/signature.d.ts +83 -0
  316. package/dist/rails/paddle/signature.d.ts.map +1 -0
  317. package/dist/rails/paddle/signature.js +163 -0
  318. package/dist/rails/paddle/subscription.d.ts +137 -0
  319. package/dist/rails/paddle/subscription.d.ts.map +1 -0
  320. package/dist/rails/paddle/subscription.js +538 -0
  321. package/dist/rails/paddle/verify.d.ts +46 -0
  322. package/dist/rails/paddle/verify.d.ts.map +1 -0
  323. package/dist/rails/paddle/verify.js +43 -0
  324. package/dist/rails/paddle/webhook.d.ts +98 -0
  325. package/dist/rails/paddle/webhook.d.ts.map +1 -0
  326. package/dist/rails/paddle/webhook.js +181 -0
  327. package/dist/rails/providers.d.ts +73 -0
  328. package/dist/rails/providers.d.ts.map +1 -0
  329. package/dist/rails/providers.js +62 -0
  330. package/dist/rails/stripe/api.d.ts +118 -0
  331. package/dist/rails/stripe/api.d.ts.map +1 -0
  332. package/dist/rails/stripe/api.js +153 -0
  333. package/dist/rails/stripe/checkout.d.ts +60 -0
  334. package/dist/rails/stripe/checkout.d.ts.map +1 -0
  335. package/dist/rails/stripe/checkout.js +86 -0
  336. package/dist/rails/stripe/discounts.d.ts +50 -0
  337. package/dist/rails/stripe/discounts.d.ts.map +1 -0
  338. package/dist/rails/stripe/discounts.js +87 -0
  339. package/dist/rails/stripe/fixtures/events.d.ts +64 -0
  340. package/dist/rails/stripe/fixtures/events.d.ts.map +1 -0
  341. package/dist/rails/stripe/fixtures/events.js +67 -0
  342. package/dist/rails/stripe/objects.d.ts +237 -0
  343. package/dist/rails/stripe/objects.d.ts.map +1 -0
  344. package/dist/rails/stripe/objects.js +389 -0
  345. package/dist/rails/stripe/portal.d.ts +29 -0
  346. package/dist/rails/stripe/portal.d.ts.map +1 -0
  347. package/dist/rails/stripe/portal.js +41 -0
  348. package/dist/rails/stripe/pricing.d.ts +25 -0
  349. package/dist/rails/stripe/pricing.d.ts.map +1 -0
  350. package/dist/rails/stripe/pricing.js +68 -0
  351. package/dist/rails/stripe/rail.d.ts +28 -0
  352. package/dist/rails/stripe/rail.d.ts.map +1 -0
  353. package/dist/rails/stripe/rail.js +81 -0
  354. package/dist/rails/stripe/refresh.d.ts +33 -0
  355. package/dist/rails/stripe/refresh.d.ts.map +1 -0
  356. package/dist/rails/stripe/refresh.js +21 -0
  357. package/dist/rails/stripe/signature.d.ts +71 -0
  358. package/dist/rails/stripe/signature.d.ts.map +1 -0
  359. package/dist/rails/stripe/signature.js +92 -0
  360. package/dist/rails/stripe/verify.d.ts +15 -0
  361. package/dist/rails/stripe/verify.d.ts.map +1 -0
  362. package/dist/rails/stripe/verify.js +63 -0
  363. package/dist/rails/stripe/webhook.d.ts +26 -0
  364. package/dist/rails/stripe/webhook.d.ts.map +1 -0
  365. package/dist/rails/stripe/webhook.js +31 -0
  366. package/dist/secret/registry.d.ts +192 -0
  367. package/dist/secret/registry.d.ts.map +1 -0
  368. package/dist/secret/registry.js +154 -0
  369. package/dist/seeds/example.d.ts +3 -0
  370. package/dist/seeds/example.d.ts.map +1 -0
  371. package/dist/seeds/example.js +171 -0
  372. package/dist/version.generated.d.ts +5 -0
  373. package/dist/version.generated.d.ts.map +1 -0
  374. package/dist/version.generated.js +7 -0
  375. package/dist/workflows/paddleSweep.d.ts +175 -0
  376. package/dist/workflows/paddleSweep.d.ts.map +1 -0
  377. package/dist/workflows/paddleSweep.js +433 -0
  378. package/dist/workflows/railAccess.d.ts +48 -0
  379. package/dist/workflows/railAccess.d.ts.map +1 -0
  380. package/dist/workflows/railAccess.js +36 -0
  381. package/dist/workflows/reconcile.d.ts +161 -0
  382. package/dist/workflows/reconcile.d.ts.map +1 -0
  383. package/dist/workflows/reconcile.js +352 -0
  384. package/dist/workflows/report.d.ts +28 -0
  385. package/dist/workflows/report.d.ts.map +1 -0
  386. package/dist/workflows/report.js +54 -0
  387. package/dist/workflows/retryPolicy.d.ts +29 -0
  388. package/dist/workflows/retryPolicy.d.ts.map +1 -0
  389. package/dist/workflows/retryPolicy.js +37 -0
  390. package/dist/workflows/specs.d.ts +102 -0
  391. package/dist/workflows/specs.d.ts.map +1 -0
  392. package/dist/workflows/specs.js +84 -0
  393. package/dist/workflows/worker.d.ts +62 -0
  394. package/dist/workflows/worker.d.ts.map +1 -0
  395. package/dist/workflows/worker.js +92 -0
  396. package/dist/workflows/workerConfig.d.ts +46 -0
  397. package/dist/workflows/workerConfig.d.ts.map +1 -0
  398. package/dist/workflows/workerConfig.js +38 -0
  399. package/package.json +22 -13
  400. package/pithy.manifest.json +19 -13
  401. package/src/version.generated.ts +1 -1
  402. package/src/client/paddlePrices.iife.ts +0 -43
@@ -0,0 +1,121 @@
1
+ import type { D1Database } from "@cloudflare/workers-types";
2
+ import type { PaymentsRail } from "../data/rail";
3
+ import { PaymentsSubject } from "../data/subject";
4
+ import { type PaymentsDatabase } from "../data/tables";
5
+ /**
6
+ * Who a purchase belongs to — the question a webhook cannot answer for itself.
7
+ *
8
+ * A notification arrives carrying the store's own identifiers and no Pithy identity at all. The projection
9
+ * writer takes an owner as an input and refuses to infer one, deliberately: a writer that could derive a
10
+ * holder from a payload could be talked into deriving the wrong one. So resolution happens here, once,
11
+ * against rows the server itself wrote.
12
+ *
13
+ * **The answer is a subject, not a user id** — the pair, and always both halves. Under organization billing
14
+ * the person who clicked is not the holder, and `data/subject.ts` states why an id on its own identifies
15
+ * nobody: nothing keeps an organization id from equalling some user's, so a resolution that returned an id
16
+ * alone would let one collect the other's renewals. Every lookup below therefore selects both columns **from
17
+ * the same row**, and the pair is never assembled from two places — not a kind from config beside an id from
18
+ * a row, not a kind from one table beside an id from another.
19
+ *
20
+ * **Three sources, and the order they are consulted in is a trust order.**
21
+ *
22
+ * 1. **The same transaction, already projected.** A redelivery of a notification we handled is never
23
+ * orphaned, whatever the app set.
24
+ * 2. **The transaction that started the subscription.** This is what makes renewals work for an app that set
25
+ * no account token at all: the first purchase was submitted by its owner, and every renewal chains back to
26
+ * it through `originalTransactionId`.
27
+ * 3. **The account link** — `pithy_payments_provider_accounts`, written when the purchase was submitted,
28
+ * through the hook every store provides: Apple's `appAccountToken`, Google's `obfuscatedAccountId`,
29
+ * Stripe's `client_reference_id`.
30
+ *
31
+ * The first two are facts this server established; the third is a value a client chose. That is the whole
32
+ * ordering, and {@link resolveNotificationOwner} spells out the attack it prevents.
33
+ *
34
+ * When nothing answers, the answer is undefined — an orphan. The notification is still recorded, so it is
35
+ * visible and replayable once a link arrives. Guessing would be the alternative, and a guess here grants
36
+ * somebody else's subscription.
37
+ */
38
+ /** What a notification offers as identification. Every field is optional; a rail supplies what it carries. */
39
+ export interface OwnerHints {
40
+ /** The store's account identifier, if the purchase carried one. */
41
+ providerAccountId?: string | null;
42
+ /** This transaction's own id. Matches a purchase already projected. */
43
+ providerTransactionId?: string | null;
44
+ /** The transaction that started the subscription. Matches the purchase a renewal descends from. */
45
+ originalTransactionId?: string | null;
46
+ }
47
+ /** Options for the account link. The clock is injected so a link's `createdAt` is deterministic in tests. */
48
+ export interface LinkProviderAccountOptions {
49
+ /** The clock. */
50
+ now?: Date;
51
+ /** The id minter. */
52
+ newId?: () => string;
53
+ }
54
+ /**
55
+ * Bind a store account identifier to a subject, and return the subject it is bound to.
56
+ *
57
+ * Idempotent, and **never rebinding**: `UNIQUE (rail, providerAccountId)` plus `ON CONFLICT DO NOTHING`
58
+ * means the first binding wins and a later one is a no-op. That matters because a client re-submits its
59
+ * receipt on every launch, and because rebinding would let a second holder capture the first one's renewal
60
+ * notifications simply by submitting a receipt from the same store account.
61
+ *
62
+ * **The key is deliberately not widened by the subject**, and the kind is no escape from it either: an
63
+ * organization whose id equals a user's does not get its own link, because `organization:acme` and
64
+ * `user:acme` would still be two rows competing for one `(rail, providerAccountId)`. One provider identity
65
+ * resolves to one holder, and which holder is decided once.
66
+ *
67
+ * The returned subject is the one actually bound, which is not always the one passed in. A caller that cares
68
+ * about the difference compares them with `sameSubject`; the purchase-level owner check in the writer is
69
+ * what refuses a transaction outright.
70
+ *
71
+ * **A link is also a repair signal, and this function does not act on it.** Purchases that arrived before
72
+ * their owner was knowable are sitting in `pithy_payments_webhook_events` waiting for exactly this row —
73
+ * see `projection/orphans.ts`, and #341 for what it cost while nothing looked. Acting on it here would mean
74
+ * this function taking a catalog, an environment and a rail able to replay its own payloads, which is three
75
+ * arguments a link has no business knowing about; so every call site calls `repairOrphanedEvents` beside
76
+ * this one instead. **A fourth call site owes the same call.**
77
+ */
78
+ export declare function linkProviderAccount(d1: D1Database, rail: PaymentsRail, providerAccountId: string, subject: PaymentsSubject, options?: LinkProviderAccountOptions): Promise<PaymentsSubject>;
79
+ /**
80
+ * The store account this subject already has on this rail, or undefined.
81
+ *
82
+ * The reverse of {@link resolveNotificationOwner}, and it exists for the two hosted Stripe flows. `/checkout`
83
+ * passes it so a returning buyer keeps one Stripe customer instead of minting a new one per purchase — without
84
+ * that, the billing portal their first purchase created would not show their second. `/portal` needs it because
85
+ * a portal session is *about* a customer, and taking that from a request would let any caller open somebody
86
+ * else's billing.
87
+ *
88
+ * **Both halves filter, and that is what the portal rests on.** An id-only filter would hand `organization:acme`
89
+ * the portal session belonging to `user:acme` — a live billing session over somebody else's card, invoices and
90
+ * cancellation, reachable by anybody who can act for a subject whose id collides.
91
+ *
92
+ * The oldest link wins. A subject with two accounts on one rail is already an anomaly — the link is never
93
+ * rebound, so it takes two purchases made under two store accounts — and the first is the one every earlier
94
+ * purchase is filed under.
95
+ */
96
+ export declare function providerAccountForSubject(db: PaymentsDatabase, rail: PaymentsRail, subject: PaymentsSubject): Promise<string | undefined>;
97
+ /**
98
+ * The subject a notification belongs to, or undefined when nothing identifies one.
99
+ *
100
+ * **The order is a trust order, not a convenience order.** The two transaction lookups ask a question this
101
+ * server already answered: who owned the purchase we projected for an authenticated caller. The account link
102
+ * asks a question the *app* answered, because on Apple and Google `providerAccountId` is `appAccountToken` /
103
+ * `obfuscatedAccountId` — values a client puts in a purchase and may put anything in. `contract.ts` says as
104
+ * much about `accountReference`, and the same reasoning applies one hop further out: a value a client chose
105
+ * must never outrank a fact this server established.
106
+ *
107
+ * Consulting the link first is a cross-subject entitlement hijack. An attacker who can guess a victim's token
108
+ * value makes one purchase carrying it, submits it as themselves, and owns the link; the victim's own
109
+ * notifications then resolve to the attacker, and because a renewal carries a fresh transaction id the
110
+ * writer's owner check never fires. Last is where a client's claim belongs.
111
+ *
112
+ * It still belongs *somewhere*: on Stripe the link is written from `client_reference_id`, which `/checkout`
113
+ * set from the authenticated buyer, so it is server-established and it is the only thing that can attribute
114
+ * a first invoice. Hence last rather than gone.
115
+ *
116
+ * Each source answers with the whole pair or with nothing. A source that matched a row contributes that
117
+ * row's `subjectType` **and** its `subjectId`, so a purchase filed against an organization can never resolve
118
+ * to the user with the same id — see {@link subjectOf}.
119
+ */
120
+ export declare function resolveNotificationOwner(db: PaymentsDatabase, rail: PaymentsRail, hints: OwnerHints): Promise<PaymentsSubject | undefined>;
121
+ //# sourceMappingURL=owner.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"owner.d.ts","sourceRoot":"","sources":["../../src/projection/owner.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAG5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAGL,KAAK,gBAAgB,EAEtB,MAAM,gBAAgB,CAAC;AAExB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,8GAA8G;AAC9G,MAAM,WAAW,UAAU;IACzB,mEAAmE;IACnE,iBAAiB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,uEAAuE;IACvE,qBAAqB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtC,mGAAmG;IACnG,qBAAqB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;CACvC;AAED,6GAA6G;AAC7G,MAAM,WAAW,0BAA0B;IACzC,iBAAiB;IACjB,GAAG,CAAC,EAAE,IAAI,CAAC;IACX,qBAAqB;IACrB,KAAK,CAAC,EAAE,MAAM,MAAM,CAAC;CACtB;AAeD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,mBAAmB,CACvC,EAAE,EAAE,UAAU,EACd,IAAI,EAAE,YAAY,EAClB,iBAAiB,EAAE,MAAM,EACzB,OAAO,EAAE,eAAe,EACxB,OAAO,GAAE,0BAA+B,GACvC,OAAO,CAAC,eAAe,CAAC,CA4B1B;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,yBAAyB,CAC7C,EAAE,EAAE,gBAAgB,EACpB,IAAI,EAAE,YAAY,EAClB,OAAO,EAAE,eAAe,GACvB,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAU7B;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,wBAAwB,CAC5C,EAAE,EAAE,gBAAgB,EACpB,IAAI,EAAE,YAAY,EAClB,KAAK,EAAE,UAAU,GAChB,OAAO,CAAC,eAAe,GAAG,SAAS,CAAC,CA8CtC"}
@@ -0,0 +1,114 @@
1
+ import { PaymentsSubject } from "../data/subject.js";
2
+ import { PaymentsProviderAccount } from "../data/providerAccount.js";
3
+ import { PAYMENTS_PROVIDER_ACCOUNTS_TABLE, PAYMENTS_PURCHASES_TABLE, paymentsDatabase } from "../data/tables.js";
4
+ import { withD1Retry } from "@pithy-sh/core/src/data/withD1Retry";
5
+ //#region src/projection/owner.ts
6
+ /**
7
+ * The pair, read off the one row it was found in.
8
+ *
9
+ * A function rather than an object literal at each lookup, because the literal is exactly where the two
10
+ * halves could come from two places: a `subjectType` taken from config beside a `subjectId` taken from a row
11
+ * typechecks perfectly and names a holder nobody meant. Taking a row and reading both columns off it leaves
12
+ * nowhere to pass a mismatched pair. The parse is the D1 boundary check every other decoded row in this
13
+ * package gets, over a value the table's own CHECK constraint already closes.
14
+ */
15
+ function subjectOf(row) {
16
+ return PaymentsSubject.parse(row);
17
+ }
18
+ /**
19
+ * Bind a store account identifier to a subject, and return the subject it is bound to.
20
+ *
21
+ * Idempotent, and **never rebinding**: `UNIQUE (rail, providerAccountId)` plus `ON CONFLICT DO NOTHING`
22
+ * means the first binding wins and a later one is a no-op. That matters because a client re-submits its
23
+ * receipt on every launch, and because rebinding would let a second holder capture the first one's renewal
24
+ * notifications simply by submitting a receipt from the same store account.
25
+ *
26
+ * **The key is deliberately not widened by the subject**, and the kind is no escape from it either: an
27
+ * organization whose id equals a user's does not get its own link, because `organization:acme` and
28
+ * `user:acme` would still be two rows competing for one `(rail, providerAccountId)`. One provider identity
29
+ * resolves to one holder, and which holder is decided once.
30
+ *
31
+ * The returned subject is the one actually bound, which is not always the one passed in. A caller that cares
32
+ * about the difference compares them with `sameSubject`; the purchase-level owner check in the writer is
33
+ * what refuses a transaction outright.
34
+ *
35
+ * **A link is also a repair signal, and this function does not act on it.** Purchases that arrived before
36
+ * their owner was knowable are sitting in `pithy_payments_webhook_events` waiting for exactly this row —
37
+ * see `projection/orphans.ts`, and #341 for what it cost while nothing looked. Acting on it here would mean
38
+ * this function taking a catalog, an environment and a rail able to replay its own payloads, which is three
39
+ * arguments a link has no business knowing about; so every call site calls `repairOrphanedEvents` beside
40
+ * this one instead. **A fourth call site owes the same call.**
41
+ */
42
+ async function linkProviderAccount(d1, rail, providerAccountId, subject, options = {}) {
43
+ const db = paymentsDatabase(d1);
44
+ const row = PaymentsProviderAccount.encode({
45
+ id: options.newId?.() ?? crypto.randomUUID(),
46
+ rail,
47
+ providerAccountId,
48
+ subjectType: subject.subjectType,
49
+ subjectId: subject.subjectId,
50
+ createdAt: options.now ?? /* @__PURE__ */ new Date()
51
+ });
52
+ await withD1Retry(() => db.insertInto(PAYMENTS_PROVIDER_ACCOUNTS_TABLE).values(row).onConflict((oc) => oc.columns(["rail", "providerAccountId"]).doNothing()).execute());
53
+ const bound = await db.selectFrom(PAYMENTS_PROVIDER_ACCOUNTS_TABLE).select(["subjectType", "subjectId"]).where("rail", "=", rail).where("providerAccountId", "=", providerAccountId).executeTakeFirst();
54
+ return bound === void 0 ? subject : subjectOf(bound);
55
+ }
56
+ /**
57
+ * The store account this subject already has on this rail, or undefined.
58
+ *
59
+ * The reverse of {@link resolveNotificationOwner}, and it exists for the two hosted Stripe flows. `/checkout`
60
+ * passes it so a returning buyer keeps one Stripe customer instead of minting a new one per purchase — without
61
+ * that, the billing portal their first purchase created would not show their second. `/portal` needs it because
62
+ * a portal session is *about* a customer, and taking that from a request would let any caller open somebody
63
+ * else's billing.
64
+ *
65
+ * **Both halves filter, and that is what the portal rests on.** An id-only filter would hand `organization:acme`
66
+ * the portal session belonging to `user:acme` — a live billing session over somebody else's card, invoices and
67
+ * cancellation, reachable by anybody who can act for a subject whose id collides.
68
+ *
69
+ * The oldest link wins. A subject with two accounts on one rail is already an anomaly — the link is never
70
+ * rebound, so it takes two purchases made under two store accounts — and the first is the one every earlier
71
+ * purchase is filed under.
72
+ */
73
+ async function providerAccountForSubject(db, rail, subject) {
74
+ return (await db.selectFrom(PAYMENTS_PROVIDER_ACCOUNTS_TABLE).select("providerAccountId").where("rail", "=", rail).where("subjectType", "=", subject.subjectType).where("subjectId", "=", subject.subjectId).orderBy("createdAt").executeTakeFirst())?.providerAccountId;
75
+ }
76
+ /**
77
+ * The subject a notification belongs to, or undefined when nothing identifies one.
78
+ *
79
+ * **The order is a trust order, not a convenience order.** The two transaction lookups ask a question this
80
+ * server already answered: who owned the purchase we projected for an authenticated caller. The account link
81
+ * asks a question the *app* answered, because on Apple and Google `providerAccountId` is `appAccountToken` /
82
+ * `obfuscatedAccountId` — values a client puts in a purchase and may put anything in. `contract.ts` says as
83
+ * much about `accountReference`, and the same reasoning applies one hop further out: a value a client chose
84
+ * must never outrank a fact this server established.
85
+ *
86
+ * Consulting the link first is a cross-subject entitlement hijack. An attacker who can guess a victim's token
87
+ * value makes one purchase carrying it, submits it as themselves, and owns the link; the victim's own
88
+ * notifications then resolve to the attacker, and because a renewal carries a fresh transaction id the
89
+ * writer's owner check never fires. Last is where a client's claim belongs.
90
+ *
91
+ * It still belongs *somewhere*: on Stripe the link is written from `client_reference_id`, which `/checkout`
92
+ * set from the authenticated buyer, so it is server-established and it is the only thing that can attribute
93
+ * a first invoice. Hence last rather than gone.
94
+ *
95
+ * Each source answers with the whole pair or with nothing. A source that matched a row contributes that
96
+ * row's `subjectType` **and** its `subjectId`, so a purchase filed against an organization can never resolve
97
+ * to the user with the same id — see {@link subjectOf}.
98
+ */
99
+ async function resolveNotificationOwner(db, rail, hints) {
100
+ if (hints.providerTransactionId) {
101
+ const owner = await db.selectFrom(PAYMENTS_PURCHASES_TABLE).select(["subjectType", "subjectId"]).where("rail", "=", rail).where("providerTransactionId", "=", hints.providerTransactionId).executeTakeFirst();
102
+ if (owner) return subjectOf(owner);
103
+ }
104
+ if (hints.originalTransactionId) {
105
+ const owner = await db.selectFrom(PAYMENTS_PURCHASES_TABLE).select(["subjectType", "subjectId"]).where("rail", "=", rail).where((eb) => eb.or([eb("providerTransactionId", "=", hints.originalTransactionId), eb("originalTransactionId", "=", hints.originalTransactionId)])).orderBy("purchasedAt").executeTakeFirst();
106
+ if (owner) return subjectOf(owner);
107
+ }
108
+ if (hints.providerAccountId) {
109
+ const linked = await db.selectFrom(PAYMENTS_PROVIDER_ACCOUNTS_TABLE).select(["subjectType", "subjectId"]).where("rail", "=", rail).where("providerAccountId", "=", hints.providerAccountId).executeTakeFirst();
110
+ if (linked) return subjectOf(linked);
111
+ }
112
+ }
113
+ //#endregion
114
+ export { linkProviderAccount, providerAccountForSubject, resolveNotificationOwner };
@@ -0,0 +1,30 @@
1
+ import type { Entitlement } from "@pithy-sh/core/src/entitlement/entitlement";
2
+ import type { PaymentsSubject } from "../data/subject";
3
+ import { type PaymentsDatabase } from "../data/tables";
4
+ /**
5
+ * The read path: every entitlement a subject holds, as the core seam's shape.
6
+ *
7
+ * **A read never writes.** Repairing a stale row is the reconciliation Workflow's job, which is what keeps
8
+ * this the hot path it needs to be — one indexed lookup per request, no KV cache, no token claim, so a
9
+ * revocation is immediate and the truth has one home (issue #79 decision 5).
10
+ *
11
+ * The recheck is why the read exists as its own function rather than a `selectAll`. A subscription can lapse
12
+ * with **no notification arriving at all** — the store simply stops renewing, and nothing tells us. So the
13
+ * stored `active` flag is an optimization and `expiresAt` is the truth, and the read applies the timestamp
14
+ * itself rather than trusting the flag. A row that says `active = 1` with an expiry in the past does not
15
+ * grant, and it does not need a write to stop granting.
16
+ *
17
+ * Lapsed rows are returned rather than filtered out, with `active` false. A paywall wants to say "your Pro
18
+ * ended on the 4th", and it can only do that if the row and its date survive the read.
19
+ *
20
+ * **Both halves of the subject are in the `where`, and neither is decorative.** Nothing in the kit makes the
21
+ * user id space and the adopter's organization id space disjoint — they are minted by different things, and
22
+ * neither knows the other exists — so a read filtered on the id alone would hand `user:acme` whatever
23
+ * `organization:acme` bought. The pair arrives as one object for exactly that reason: it is read from and
24
+ * compared as a unit, and no call site here pairs a kind from config with an id from somewhere else.
25
+ *
26
+ * Both columns lead `UNIQUE (subjectType, subjectId, entitlement)`, so this stays the one indexed lookup it
27
+ * has to be — the index answers the whole predicate rather than filtering a kind out afterwards.
28
+ */
29
+ export declare function resolveEntitlements(db: PaymentsDatabase, subject: PaymentsSubject, now: Date): Promise<readonly Entitlement[]>;
30
+ //# sourceMappingURL=resolve.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolve.d.ts","sourceRoot":"","sources":["../../src/projection/resolve.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4CAA4C,CAAC;AAE9E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AACvD,OAAO,EAA+B,KAAK,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAEpF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAsB,mBAAmB,CACvC,EAAE,EAAE,gBAAgB,EACpB,OAAO,EAAE,eAAe,EACxB,GAAG,EAAE,IAAI,GACR,OAAO,CAAC,SAAS,WAAW,EAAE,CAAC,CAmBjC"}
@@ -0,0 +1,42 @@
1
+ import { PaymentsEntitlement } from "../data/entitlement.js";
2
+ import { PAYMENTS_ENTITLEMENTS_TABLE } from "../data/tables.js";
3
+ //#region src/projection/resolve.ts
4
+ /**
5
+ * The read path: every entitlement a subject holds, as the core seam's shape.
6
+ *
7
+ * **A read never writes.** Repairing a stale row is the reconciliation Workflow's job, which is what keeps
8
+ * this the hot path it needs to be — one indexed lookup per request, no KV cache, no token claim, so a
9
+ * revocation is immediate and the truth has one home (issue #79 decision 5).
10
+ *
11
+ * The recheck is why the read exists as its own function rather than a `selectAll`. A subscription can lapse
12
+ * with **no notification arriving at all** — the store simply stops renewing, and nothing tells us. So the
13
+ * stored `active` flag is an optimization and `expiresAt` is the truth, and the read applies the timestamp
14
+ * itself rather than trusting the flag. A row that says `active = 1` with an expiry in the past does not
15
+ * grant, and it does not need a write to stop granting.
16
+ *
17
+ * Lapsed rows are returned rather than filtered out, with `active` false. A paywall wants to say "your Pro
18
+ * ended on the 4th", and it can only do that if the row and its date survive the read.
19
+ *
20
+ * **Both halves of the subject are in the `where`, and neither is decorative.** Nothing in the kit makes the
21
+ * user id space and the adopter's organization id space disjoint — they are minted by different things, and
22
+ * neither knows the other exists — so a read filtered on the id alone would hand `user:acme` whatever
23
+ * `organization:acme` bought. The pair arrives as one object for exactly that reason: it is read from and
24
+ * compared as a unit, and no call site here pairs a kind from config with an id from somewhere else.
25
+ *
26
+ * Both columns lead `UNIQUE (subjectType, subjectId, entitlement)`, so this stays the one indexed lookup it
27
+ * has to be — the index answers the whole predicate rather than filtering a kind out afterwards.
28
+ */
29
+ async function resolveEntitlements(db, subject, now) {
30
+ return (await db.selectFrom(PAYMENTS_ENTITLEMENTS_TABLE).selectAll().where("subjectType", "=", subject.subjectType).where("subjectId", "=", subject.subjectId).orderBy("entitlement").execute()).map((row) => {
31
+ const entitlement = PaymentsEntitlement.parse(row);
32
+ const lapsed = entitlement.expiresAt !== null && entitlement.expiresAt.getTime() <= now.getTime();
33
+ return {
34
+ key: entitlement.entitlement,
35
+ active: entitlement.active && !lapsed,
36
+ expiresAt: entitlement.expiresAt,
37
+ source: entitlement.sourcePurchaseId
38
+ };
39
+ });
40
+ }
41
+ //#endregion
42
+ export { resolveEntitlements };
@@ -0,0 +1,103 @@
1
+ import type { D1Database, D1PreparedStatement } from "@cloudflare/workers-types";
2
+ import { type PaymentsConfig } from "../config/config";
3
+ import { PaymentsEntitlement } from "../data/entitlement";
4
+ import { PaymentsPurchase, type PaymentsPurchaseRow, type PurchaseEnvironment } from "../data/purchase";
5
+ import { type ProviderEventInput } from "./event";
6
+ /**
7
+ * The projection writer — the one place a purchase is ever written, and the heart of the package.
8
+ *
9
+ * Five triggers converge here, across four modules — `http/routes.ts` twice, and one each from
10
+ * `workflows/reconcile.ts`, `workflows/paddleSweep.ts` and `projection/orphans.ts`:
11
+ *
12
+ * - the buyer's app submitting its receipt to `POST {base}/purchases`;
13
+ * - the provider's webhook, on the route that verified its signature;
14
+ * - the reconciliation Workflow re-verifying what the webhook missed, through each rail's `refresh`;
15
+ * - the Paddle events sweep, which finds a purchase that has **no row** at all, so `refresh` never sees it;
16
+ * - the orphan repair that runs when an account links, projecting the notifications that arrived before
17
+ * anyone could say whose they were (#341).
18
+ *
19
+ * All five produce the identical row, which is what makes a dropped client call cost nothing and a replayed
20
+ * webhook change nothing — and it is why refunds, renewals, and revocations need no handler of their own.
21
+ * They are states, and this projects a state. The count is prose and nothing holds it: the honest check is
22
+ * `grep -rn projectPurchase src/`.
23
+ *
24
+ * Four properties hold, and each is enforced by the database rather than by careful code:
25
+ *
26
+ * - **Idempotent.** `UNIQUE (rail, providerTransactionId)` means one provider transaction is one row
27
+ * forever. A repeat routes into the upsert's `ON CONFLICT` branch instead of inserting a second row.
28
+ * - **Monotonic on the provider's event time.** Providers do not guarantee delivery order, so an `expired`
29
+ * notification can arrive after the `renewed` that superseded it, and last-write-wins would silently
30
+ * revoke a paying subscriber. An event no newer than the row it would update is ignored entirely — the
31
+ * comparison is a SQL predicate on the `ON CONFLICT` branch as well as a pre-read, so two concurrent
32
+ * writers cannot order themselves wrongly.
33
+ * - **Never rebound.** A transaction already projected against another subject raises
34
+ * `payments/receipt_already_owned` (409) rather than moving. Together with the UNIQUE constraint that is
35
+ * what makes a stolen receipt worthless. The `ON CONFLICT` branch carries the owner check too, so the
36
+ * refusal survives a race the pre-read cannot see. The owner is a **pair**, and every comparison of it
37
+ * here is both halves: nothing keeps an organization id from equalling some user's, so an id-only check
38
+ * would let `organization:acme` take `user:acme`'s transaction and the entitlement behind it.
39
+ * - **Environment-isolated.** The deployment's own store environment is an input, and a mismatch is refused
40
+ * outright. A sandbox StoreKit transaction granting a real entitlement is the most common
41
+ * in-app-purchase security defect there is; the safe design is for such a row never to exist.
42
+ *
43
+ * The affected entitlement rows are re-derived **in the same `DB.batch` as the purchase write**, from the
44
+ * purchases table itself rather than from a value this function computed. D1 runs a batch as one
45
+ * transaction, so the read model cannot disagree with the purchases that produced it: there is no window
46
+ * in which a purchase is stored and its entitlement is not. An entitlement is granted when some purchase
47
+ * granting it sits in an access-granting status and has not expired, and the row carries the winning
48
+ * purchase — the one whose access runs longest — as provenance.
49
+ *
50
+ * Which rows those are is a question about the catalog as well as the event, so a write also clears the keys
51
+ * this subject holds that the catalog has stopped granting — see {@link keysToDerive}. A catalog edit
52
+ * produces no event of its own, and nothing else in the package would ever repair such a row.
53
+ *
54
+ * The ledger credit a `grants` clause asks for is deliberately **not** here. This function stays pure D1,
55
+ * so its tests need no optional package resolved; fulfillment acts on what it returns. Nothing is lost —
56
+ * the credit's idempotency rests on the ledger's own `UNIQUE (ref)`, not on call ordering.
57
+ */
58
+ /** What one projection did, and to what. The caller acts on this — fulfillment reads it, a route renders it. */
59
+ export interface PurchaseProjection {
60
+ /**
61
+ * `created` for a transaction seen for the first time, `updated` when a newer event moved an existing
62
+ * row, `ignored` when the event was no newer than the row it would have updated. `ignored` is a normal,
63
+ * successful outcome: a replay is a 200, not an error.
64
+ */
65
+ outcome: "created" | "updated" | "ignored";
66
+ /** The purchase row as it now stands — the existing one, unchanged, on the `ignored` path. */
67
+ purchase: PaymentsPurchase;
68
+ /** The catalog product this transaction maps to, and the entitlement keys it grants. */
69
+ product: {
70
+ id: string;
71
+ entitlements: readonly string[];
72
+ };
73
+ /** The entitlement rows this projection re-derived, after the write. */
74
+ entitlements: readonly PaymentsEntitlement[];
75
+ }
76
+ /** What the writer needs beyond the event: the catalog, the deployment's environment, and the clock. */
77
+ export interface ProjectPurchaseOptions {
78
+ /** The resolved catalog. The product, its type, and its entitlement keys all come from here. */
79
+ config: PaymentsConfig;
80
+ /**
81
+ * This deployment's store environment. An input rather than something inferred from the payload, because
82
+ * inferring it is what lets a sandbox purchase grant a production entitlement.
83
+ */
84
+ environment: PurchaseEnvironment;
85
+ /** The clock, for `createdAt`/`updatedAt` and the expiry check. Injected so tests are deterministic. */
86
+ now?: Date;
87
+ /** The id minter. Injected for the same reason. */
88
+ newId?: () => string;
89
+ }
90
+ /**
91
+ * Project one normalized provider event. Returns what changed; throws only the three refusals above and
92
+ * whatever D1 raises that is not a constraint we expect.
93
+ */
94
+ export declare function projectPurchase(d1: D1Database, input: ProviderEventInput, options: ProjectPurchaseOptions): Promise<PurchaseProjection>;
95
+ /**
96
+ * The purchase upsert, compiled to the statement that joins the batch. **The only place either commit-time
97
+ * guard is expressed**, and exported for that reason: a test can compile it, let the table move underneath
98
+ * the compiled statement, and execute it — which is the one way to reach the `ON CONFLICT` predicate.
99
+ * Driving `projectPurchase` cannot do it, because its pre-read and post-batch checks serialize around the
100
+ * predicate and cover the same outcomes, so a test at that level passes with the predicate deleted.
101
+ */
102
+ export declare function upsertPurchaseStatement(d1: D1Database, row: PaymentsPurchaseRow): D1PreparedStatement;
103
+ //# sourceMappingURL=writer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"writer.d.ts","sourceRoot":"","sources":["../../src/projection/writer.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAKjF,OAAO,EAEL,KAAK,cAAc,EAGpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,KAAK,mBAAmB,EAAE,KAAK,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAcxG,OAAO,EAAiB,KAAK,kBAAkB,EAAE,MAAM,SAAS,CAAC;AAEjE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AAEH,gHAAgH;AAChH,MAAM,WAAW,kBAAkB;IACjC;;;;OAIG;IACH,OAAO,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;IAC3C,8FAA8F;IAC9F,QAAQ,EAAE,gBAAgB,CAAC;IAC3B,wFAAwF;IACxF,OAAO,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,SAAS,MAAM,EAAE,CAAA;KAAE,CAAC;IACzD,wEAAwE;IACxE,YAAY,EAAE,SAAS,mBAAmB,EAAE,CAAC;CAC9C;AAED,wGAAwG;AACxG,MAAM,WAAW,sBAAsB;IACrC,gGAAgG;IAChG,MAAM,EAAE,cAAc,CAAC;IACvB;;;OAGG;IACH,WAAW,EAAE,mBAAmB,CAAC;IACjC,wGAAwG;IACxG,GAAG,CAAC,EAAE,IAAI,CAAC;IACX,mDAAmD;IACnD,KAAK,CAAC,EAAE,MAAM,MAAM,CAAC;CACtB;AAED;;;GAGG;AACH,wBAAsB,eAAe,CACnC,EAAE,EAAE,UAAU,EACd,KAAK,EAAE,kBAAkB,EACzB,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,kBAAkB,CAAC,CAgH7B;AA2HD;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,EAAE,EAAE,UAAU,EAAE,GAAG,EAAE,mBAAmB,GAAG,mBAAmB,CAErG"}