@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,81 @@
1
+ //#region src/pricing/location.ts
2
+ /**
3
+ * Pick the best location available, and say which one it is.
4
+ *
5
+ * Precedence is authority, not convenience: a Paddle customer beats an address the caller holds, because
6
+ * Paddle prices the customer from the address it will actually charge — and if the two disagree, a
7
+ * traveler or a VPN, the billing address wins for what they are charged, so it wins for what they are
8
+ * shown.
9
+ *
10
+ * **Never a silent default.** `null`, `undefined`, an empty customer id and an address with no country
11
+ * all resolve to `ip` *and say so*, so a caller that meant to pass something and passed nothing gets an
12
+ * estimate labeled as one rather than a guess wearing the authority of an address.
13
+ */
14
+ function resolvePriceLocation(visitor) {
15
+ const customerId = visitor?.customerId;
16
+ if (typeof customerId === "string" && customerId.length > 0) return {
17
+ source: "customer",
18
+ customerId,
19
+ address: null,
20
+ provisional: false
21
+ };
22
+ const address = visitor?.address;
23
+ if (address && typeof address.countryCode === "string" && address.countryCode.length > 0) {
24
+ const postalCode = typeof address.postalCode === "string" && address.postalCode.length > 0 ? address.postalCode : void 0;
25
+ return {
26
+ source: "address",
27
+ customerId: null,
28
+ address: postalCode === void 0 ? { countryCode: address.countryCode } : {
29
+ countryCode: address.countryCode,
30
+ postalCode
31
+ },
32
+ provisional: false
33
+ };
34
+ }
35
+ return {
36
+ source: "ip",
37
+ customerId: null,
38
+ address: null,
39
+ provisional: true
40
+ };
41
+ }
42
+ /**
43
+ * What to ask Paddle, for these items at this location.
44
+ *
45
+ * Structurally a `PaddlePriceQuery`, and deliberately not typed as one — see this module's header. The
46
+ * absent fields are absent rather than `undefined`-valued: `priceQueryKey` serializes the query into an
47
+ * effect dependency, and a key that changed shape between "no address" and "an address of undefined"
48
+ * would re-quote a page that asked the same question twice.
49
+ */
50
+ function priceQueryFor(items, location) {
51
+ if (location.customerId !== null) return {
52
+ items,
53
+ customerId: location.customerId
54
+ };
55
+ if (location.address !== null) return {
56
+ items,
57
+ address: location.address
58
+ };
59
+ return { items };
60
+ }
61
+ /**
62
+ * Whether the figure has to be labeled an estimate.
63
+ *
64
+ * Two independent reasons, and either one is enough:
65
+ *
66
+ * **The location is provisional.** An IP says where a browser connected from, not where a card is
67
+ * registered. The buyer enters a billing address at the card form and the total settles then — which is
68
+ * how commerce works everywhere, and is expected rather than a broken promise, *provided the figure said
69
+ * it was an estimate first*.
70
+ *
71
+ * **The tax is not fully resolved.** `priceSummary().estimated` is true when Paddle resolved no postal
72
+ * code, and United States tax lives below the country. Pass it in; this is where the two facts meet.
73
+ *
74
+ * Deriving the label from the postal code alone made it right by accident — Paddle resolves no postal
75
+ * code from an IP today — and an accident is not a rule. This states the rule.
76
+ */
77
+ function quoteIsEstimated(location, taxUnresolved) {
78
+ return location.provisional || taxUnresolved;
79
+ }
80
+ //#endregion
81
+ export { priceQueryFor, quoteIsEstimated, resolvePriceLocation };
@@ -0,0 +1,56 @@
1
+ import { type PaymentsClientOptions } from "../client/api";
2
+ import type { PriceVisitor } from "./location";
3
+ /**
4
+ * Where a signed-in visitor's Paddle customer comes from: `GET {basePath}/pricing`, asked of this
5
+ * project's own Worker.
6
+ *
7
+ * **The same value the checkout charges, from the same row.** That route reads the provider-account map
8
+ * keyed on the subject the seam resolved — `providerAccountForSubject(db, "paddle", subject)` — and so does
9
+ * `POST /payments/checkout`, which hands it to Paddle as `customer_id`. One reader, one row, so the
10
+ * figure quoted and the figure charged cannot resolve location differently. A screen that derived a
11
+ * customer id any other way would move this problem rather than solve it.
12
+ *
13
+ * **`ctm_…` is an identifier, not a credential.** It names a Paddle customer; it authorizes nothing.
14
+ * Paddle's `PricePreview` reads a price with it and the publishable client token, which is the pair
15
+ * Paddle publishes for exactly this. Nothing here widens what reaches a browser: the route is
16
+ * `requireAuth()` and answers only about its own caller, so a visitor learns their own customer id and
17
+ * no one else's.
18
+ *
19
+ * **Hand-written guard, no schema library, no absolute URL.** For the reason `../client/api.ts` gives at
20
+ * length: this compiles into an adopter's browser program, and dragging the Worker's Zod graph in behind
21
+ * it would break their build. The server's half of this response *is* a Zod object — `http/responses.ts`
22
+ * — so the boundary is validated on both sides, each in the vocabulary its own program can carry.
23
+ *
24
+ * **The request itself is not written here.** `callPayments` is the one producer of a same-origin,
25
+ * cookie-bearing call to this Worker, and this module routes through it like every other reader. It used
26
+ * to own a second fetch — its own base-path default, its own `credentials: "include"`, its own three
27
+ * failure directions — which is the drift this kit keeps paying for (#346).
28
+ *
29
+ * **A failure is not an answer, and this one fails honest.** Unreachable, refused, or unreadable all
30
+ * return `null`, which resolves to the IP location and renders the figure labeled `Estimated.` A guess
31
+ * that says it is a guess is the safe direction; the unsafe one would be treating a failed read as
32
+ * proof there is no address on file.
33
+ */
34
+ /**
35
+ * Where to ask, and what to ask with — the same options every other call on this side of the wire takes.
36
+ *
37
+ * An alias rather than a second declaration. The two shapes agreed by hand until this read started going
38
+ * through `callPayments`; now there is one of them, and a name a scaffolded screen already imports.
39
+ */
40
+ export type PriceVisitorOptions = PaymentsClientOptions;
41
+ /**
42
+ * The Paddle customer on a `GET /pricing` body, or null.
43
+ *
44
+ * Total, and refusing rather than coercing. `quotedFrom` is absent on an older Worker than this bundle,
45
+ * null for a caller who has never bought anything, and a record naming a rail otherwise — three shapes
46
+ * that all mean "no Paddle customer to price as" unless the third names `paddle` and carries a string.
47
+ */
48
+ export declare function readPaddleCustomer(body: unknown): string | null;
49
+ /**
50
+ * Ask this project's Worker who Paddle prices the signed-in caller as.
51
+ *
52
+ * Never throws, and never reports a failure: the caller's only sensible response to one is to quote from
53
+ * the IP and say so, which is what `null` already means.
54
+ */
55
+ export declare function fetchPriceVisitor(options?: PriceVisitorOptions): Promise<PriceVisitor | null>;
56
+ //# sourceMappingURL=visitor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"visitor.d.ts","sourceRoot":"","sources":["../../src/pricing/visitor.ts"],"names":[],"mappings":"AAGA,OAAO,EAAgB,KAAK,qBAAqB,EAAE,MAAM,eAAe,CAAC;AACzE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAAG,qBAAqB,CAAC;AAOxD;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAO/D;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CAAC,OAAO,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,YAAY,GAAG,IAAI,CAAC,CAQnG"}
@@ -0,0 +1,35 @@
1
+ import { callPayments } from "../client/api.js";
2
+ //#region src/pricing/visitor.ts
3
+ /** Whether a value is a plain record — the first step of the guard. */
4
+ function isRecord(value) {
5
+ return typeof value === "object" && value !== null && !Array.isArray(value);
6
+ }
7
+ /**
8
+ * The Paddle customer on a `GET /pricing` body, or null.
9
+ *
10
+ * Total, and refusing rather than coercing. `quotedFrom` is absent on an older Worker than this bundle,
11
+ * null for a caller who has never bought anything, and a record naming a rail otherwise — three shapes
12
+ * that all mean "no Paddle customer to price as" unless the third names `paddle` and carries a string.
13
+ */
14
+ function readPaddleCustomer(body) {
15
+ if (!isRecord(body)) return null;
16
+ const quotedFrom = body.quotedFrom;
17
+ if (!isRecord(quotedFrom)) return null;
18
+ if (quotedFrom.rail !== "paddle") return null;
19
+ const providerAccountId = quotedFrom.providerAccountId;
20
+ return typeof providerAccountId === "string" && providerAccountId.length > 0 ? providerAccountId : null;
21
+ }
22
+ /**
23
+ * Ask this project's Worker who Paddle prices the signed-in caller as.
24
+ *
25
+ * Never throws, and never reports a failure: the caller's only sensible response to one is to quote from
26
+ * the IP and say so, which is what `null` already means.
27
+ */
28
+ async function fetchPriceVisitor(options) {
29
+ const result = await callPayments("/pricing", {}, options, isRecord);
30
+ if (!result.ok) return null;
31
+ const customerId = readPaddleCustomer(result.value);
32
+ return customerId === null ? null : { customerId };
33
+ }
34
+ //#endregion
35
+ export { fetchPriceVisitor, readPaddleCustomer };
@@ -0,0 +1,66 @@
1
+ import { z } from "zod";
2
+ /**
3
+ * A provider event, normalized. This is the writer's whole input, and the seam between "how a rail talks"
4
+ * and "what the projection stores": a rail module verifies a receipt or parses a notification and produces
5
+ * one of these, and nothing past this point knows which store it came from.
6
+ *
7
+ * The shape is deliberately a *state*, not a *delta*. A rail reports what the transaction is now, and the
8
+ * writer projects it — so a renewal, a refund, a grace period, and a revocation are all the same code path
9
+ * rather than four handlers that must each be kept correct.
10
+ *
11
+ * `providerEventAt` is the field the whole design leans on. It is the provider's own timestamp, not ours,
12
+ * because ours would record delivery order and delivery order is exactly what providers do not guarantee.
13
+ *
14
+ * **This is the owner-bound half of a two-part shape.** A rail produces an `UnboundProviderEvent` — this
15
+ * object with the subject pair omitted, declared in `rails/contract.ts` — and the route binds an owner onto
16
+ * it to make one of these. The split is the whole reason a store's bytes cannot decide who is entitled: a
17
+ * rail parses a payload and knows the transaction completely and the holder not at all, so the field it
18
+ * would have to fill in does not exist for it to fill in wrongly.
19
+ */
20
+ export declare const ProviderEvent: z.ZodObject<{
21
+ rail: z.ZodEnum<{
22
+ apple: "apple";
23
+ google: "google";
24
+ lemonSqueezy: "lemonSqueezy";
25
+ paddle: "paddle";
26
+ stripe: "stripe";
27
+ }>;
28
+ providerTransactionId: z.ZodString;
29
+ providerProductId: z.ZodString;
30
+ subjectType: z.ZodEnum<{
31
+ organization: "organization";
32
+ user: "user";
33
+ }>;
34
+ subjectId: z.ZodString;
35
+ status: z.ZodEnum<{
36
+ active: "active";
37
+ canceled: "canceled";
38
+ expired: "expired";
39
+ in_grace: "in_grace";
40
+ never_paid: "never_paid";
41
+ on_hold: "on_hold";
42
+ paused: "paused";
43
+ refunded: "refunded";
44
+ revoked: "revoked";
45
+ }>;
46
+ role: z.ZodDefault<z.ZodEnum<{
47
+ charge: "charge";
48
+ state: "state";
49
+ }>>;
50
+ environment: z.ZodEnum<{
51
+ production: "production";
52
+ sandbox: "sandbox";
53
+ }>;
54
+ purchasedAt: z.ZodDate;
55
+ expiresAt: z.ZodDefault<z.ZodNullable<z.ZodDate>>;
56
+ revokedAt: z.ZodDefault<z.ZodNullable<z.ZodDate>>;
57
+ resumesAt: z.ZodDefault<z.ZodNullable<z.ZodDate>>;
58
+ originalTransactionId: z.ZodDefault<z.ZodNullable<z.ZodString>>;
59
+ amountMinor: z.ZodDefault<z.ZodNullable<z.ZodNumber>>;
60
+ currency: z.ZodDefault<z.ZodNullable<z.ZodString>>;
61
+ providerEventAt: z.ZodDate;
62
+ payload: z.ZodRecord<z.ZodString, z.ZodUnknown>;
63
+ }, z.core.$strip>;
64
+ export type ProviderEvent = z.output<typeof ProviderEvent>;
65
+ export type ProviderEventInput = z.input<typeof ProviderEvent>;
66
+ //# sourceMappingURL=event.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../../src/projection/event.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAMxB;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuEvB,CAAC;AACJ,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,aAAa,CAAC,CAAC;AAC3D,MAAM,MAAM,kBAAkB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,aAAa,CAAC,CAAC"}
@@ -0,0 +1,45 @@
1
+ import { PaymentsRail } from "../data/rail.js";
2
+ import { PaymentsSubject } from "../data/subject.js";
3
+ import { PurchaseStatus } from "../data/status.js";
4
+ import { PurchaseEnvironment, PurchaseRole } from "../data/purchase.js";
5
+ import { z } from "zod";
6
+ //#region src/projection/event.ts
7
+ /**
8
+ * A provider event, normalized. This is the writer's whole input, and the seam between "how a rail talks"
9
+ * and "what the projection stores": a rail module verifies a receipt or parses a notification and produces
10
+ * one of these, and nothing past this point knows which store it came from.
11
+ *
12
+ * The shape is deliberately a *state*, not a *delta*. A rail reports what the transaction is now, and the
13
+ * writer projects it — so a renewal, a refund, a grace period, and a revocation are all the same code path
14
+ * rather than four handlers that must each be kept correct.
15
+ *
16
+ * `providerEventAt` is the field the whole design leans on. It is the provider's own timestamp, not ours,
17
+ * because ours would record delivery order and delivery order is exactly what providers do not guarantee.
18
+ *
19
+ * **This is the owner-bound half of a two-part shape.** A rail produces an `UnboundProviderEvent` — this
20
+ * object with the subject pair omitted, declared in `rails/contract.ts` — and the route binds an owner onto
21
+ * it to make one of these. The split is the whole reason a store's bytes cannot decide who is entitled: a
22
+ * rail parses a payload and knows the transaction completely and the holder not at all, so the field it
23
+ * would have to fill in does not exist for it to fill in wrongly.
24
+ */
25
+ const ProviderEvent = z.object({
26
+ rail: PaymentsRail.describe("Which store the event came from."),
27
+ providerTransactionId: z.string().min(1).describe("The rail's own transaction id. Together with `rail` this is the row's identity forever — one provider transaction is one purchase row."),
28
+ providerProductId: z.string().min(1).describe("The rail's own SKU or price id. The writer resolves the logical product from it."),
29
+ subjectType: PaymentsSubject.shape.subjectType.describe("Whether `subjectId` names a user or an organization. Half the owner, and never separable from the other half — the route binds both together or neither."),
30
+ subjectId: PaymentsSubject.shape.subjectId.describe("The subject the transaction belongs to. A client submission takes it from the authenticated caller through the configured subject seam; a webhook resolves it through the provider-account map or a decoded account reference."),
31
+ status: PurchaseStatus.describe("The transaction's current normalized state, as the rail reports it."),
32
+ role: PurchaseRole.default("charge").describe("Whether this event records money moving or a subscription's standing. Omitted by every rail but Lemon Squeezy, which reports the two separately because its store does."),
33
+ environment: PurchaseEnvironment.describe("Which store environment the transaction happened in. Checked against the deployment's own before anything is written."),
34
+ purchasedAt: z.date().describe("When the store recorded the purchase."),
35
+ expiresAt: z.date().nullable().default(null).describe("When access lapses, or null for a purchase that never expires. Null for a one-time purchase."),
36
+ revokedAt: z.date().nullable().default(null).describe("When the purchase was refunded or revoked, or null while it stands."),
37
+ resumesAt: z.date().nullable().default(null).describe("When a paused subscription resumes, as the provider stated it — never computed, and never set on a status other than `paused`. Null on a paused event is the provider reporting an indefinite pause. Produced only by `pauseResumesAt` in `data/pause.ts`, which is where each rail's source for it is declared."),
38
+ originalTransactionId: z.string().nullable().default(null).describe("The transaction that started the subscription, chaining renewals back to it. Null for a one-off."),
39
+ amountMinor: z.number().int().nullable().default(null).describe("The amount charged in the currency's minor unit, or null when the rail did not report one."),
40
+ currency: z.string().nullable().default(null).describe("The ISO currency of `amountMinor`, or null when the rail did not report one."),
41
+ providerEventAt: z.date().describe("The provider's own timestamp for this event. The monotonic write rule compares against it, so an event staler than the row it would update changes nothing."),
42
+ payload: z.record(z.string(), z.unknown()).describe("The verified provider payload, stored as received. The reconciliation and audit record.")
43
+ }).describe("One provider event, normalized and bound to its owner — the projection writer's input. A rail produces this shape less the subject pair; the route adds it.");
44
+ //#endregion
45
+ export { ProviderEvent };
@@ -0,0 +1,114 @@
1
+ import type { D1Database } from "@cloudflare/workers-types";
2
+ import type { PaymentsConfig } from "../config/config";
3
+ import type { PurchaseEnvironment } from "../data/purchase";
4
+ import type { PaymentsRail } from "../data/rail";
5
+ import type { VerifiedNotification } from "../rails/contract";
6
+ import { type PurchaseProjection } from "./writer";
7
+ /**
8
+ * The repair that runs when an account links: project the purchases that were waiting for exactly that.
9
+ *
10
+ * ## The gap this closes (#341)
11
+ *
12
+ * An orphan is an authentic notification carrying a real purchase and nothing that says whose it is. Both
13
+ * paths that meet one leave the row unfinished on purpose — the webhook handler records the reason and
14
+ * withholds `processedAt`, the sweep stamps `abandonedAt` so its stream can advance — and both then rely on
15
+ * **a later delivery of the same event** to repair it. That is the right bet when the store redelivers. It
16
+ * is no bet at all when the only future event is the account linking: nothing redelivers on that signal,
17
+ * every store's retry window closes within days, and the sweep's cursor is long past.
18
+ *
19
+ * Reproduced on real D1 before this existed: sweep an unstamped `subscription.activated` for an unlinked
20
+ * customer, link the account, sweep ten more times. The purchase is never projected. Not once, ever — the
21
+ * orphan gets exactly one owner resolution in its life, at the moment nobody could answer it.
22
+ *
23
+ * ## Why here and not in the sweep
24
+ *
25
+ * The two halves of the question live in different places. The link path knows *which* customer just became
26
+ * resolvable; the sweep knows *which events* are outstanding. One of them has to ask the other, and asking
27
+ * from the link side is the cheaper direction by a wide margin: it runs once per link rather than once per
28
+ * sweep, the set it looks at is bounded by {@link ORPHAN_REPAIR_LIMIT}, and — decisively — it works on the
29
+ * four rails that have no sweep at all. A rail without a repair pass is exactly where a lost orphan stays
30
+ * lost.
31
+ *
32
+ * ## Which rows, and why not simply "unfinished"
33
+ *
34
+ * {@link webhookEventAwaitsOwner}: unfinished, and carrying the {@link WEBHOOK_EVENT_ORPHANED} marker. An
35
+ * account linking repairs one thing — a missing owner. A quarantined event, a failed projection, an unmapped
36
+ * SKU are all unfinished too, and re-running them on this signal would be an unbounded retry loop triggered
37
+ * by unrelated traffic. The marker is what makes the set the *right* set rather than merely a small one.
38
+ *
39
+ * ## Why a payload replay rather than a stored event
40
+ *
41
+ * The row already holds the notification whole — that is what `payload` is for, and the reason it is stored
42
+ * rather than summarized. So the purchase is recoverable without a second column, a migration, or a shape
43
+ * that could disagree with the payload it was derived from. What it costs is a rail able to re-read its own
44
+ * recorded body, which is {@link PaymentsRailProvider.replay} and is optional for the two rails whose stored
45
+ * body is a signed blob.
46
+ *
47
+ * ## What it deliberately does not do
48
+ *
49
+ * It never *invents* an owner. Every row goes back through {@link resolveNotificationOwner}, against the same
50
+ * table in the same trust order, so a link that resolves nothing changes nothing — and a row for a different
51
+ * holder that happens to be in the same page is left exactly as it stood. Passing the freshly linked subject
52
+ * straight to the writer would be the shortcut, and it would project one customer's purchase onto another's
53
+ * account the first time two orphans shared a page.
54
+ *
55
+ * **Per row, and both halves per row.** The subject is resolved inside the loop, from the row's own hints, so
56
+ * a page of orphans is a page of independent questions rather than one answer applied to all of them. There
57
+ * is no subject computed before the loop for the same reason there is no `subjectType` taken from config: a
58
+ * kind that outlived the row it came from is how an organization's purchase lands on a user with the same id.
59
+ */
60
+ /**
61
+ * How many orphaned rows one link may repair.
62
+ *
63
+ * A bound rather than "all of them", because this runs inside a webhook handler that a store is timing. A
64
+ * link arriving after a long outage could face hundreds of orphans, and a handler that walks them all is a
65
+ * handler the store times out and redelivers — which produces the same walk again, from the top.
66
+ *
67
+ * Small enough to be invisible in a request, and the leftovers are not lost: they still carry the marker, and
68
+ * the next link on that rail takes the next page. The rows are taken oldest first so the queue drains in
69
+ * order rather than starving its own tail.
70
+ */
71
+ export declare const ORPHAN_REPAIR_LIMIT = 25;
72
+ /** What the repair needs to project what it finds: the catalog, the environment it is writing for, the clock. */
73
+ export interface RepairOrphansOptions {
74
+ /** The resolved catalog, handed to the writer unchanged. */
75
+ config: PaymentsConfig;
76
+ /** This deployment's store environment. The writer refuses a row that disagrees. */
77
+ environment: PurchaseEnvironment;
78
+ /** The clock. */
79
+ now: Date;
80
+ /**
81
+ * Re-read one recorded payload — {@link PaymentsRailProvider.replay}, bound to its rail.
82
+ *
83
+ * Passed in rather than resolved here because building a rail provider needs credentials, and this module
84
+ * has no business reading a secret. A caller whose rail cannot replay passes nothing and the repair is a
85
+ * no-op, which is the honest answer for it.
86
+ */
87
+ replay?: (payload: Record<string, unknown>) => Promise<VerifiedNotification | undefined>;
88
+ /** What to do with each repaired projection — the same fulfillment the delivery would have performed. */
89
+ fulfill?: (projection: PurchaseProjection) => Promise<void>;
90
+ /** How many rows to take. Defaults to {@link ORPHAN_REPAIR_LIMIT}. */
91
+ limit?: number;
92
+ }
93
+ /** What one repair pass did. Returned so a caller can log it and a test can assert on it. */
94
+ export interface RepairedOrphans {
95
+ /** Rows examined — orphan-marked and unfinished, on this rail. */
96
+ readonly examined: number;
97
+ /** Rows whose purchase is now projected and whose event is now finished. */
98
+ readonly projected: readonly string[];
99
+ /** Rows still waiting: no owner yet, no replay, or nothing to project. Left exactly as they stood. */
100
+ readonly waiting: number;
101
+ }
102
+ /**
103
+ * Project the orphaned events on one rail that an owner can now be resolved for.
104
+ *
105
+ * Called after a link is written, on every path that writes one. Never throws for a row it cannot repair: a
106
+ * replay that cannot read an old payload, an owner that still does not resolve, a projection the catalog
107
+ * refuses — each leaves that row untouched and the pass moves to the next. The caller is a webhook handler
108
+ * answering a store, and a repair failing must not turn a delivery that succeeded into a non-2xx.
109
+ *
110
+ * A row it *does* project is finished the same way the original delivery would have finished it, so the
111
+ * store's own redelivery of it is a duplicate from then on.
112
+ */
113
+ export declare function repairOrphanedEvents(d1: D1Database, rail: PaymentsRail, options: RepairOrphansOptions): Promise<RepairedOrphans>;
114
+ //# sourceMappingURL=orphans.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"orphans.d.ts","sourceRoot":"","sources":["../../src/projection/orphans.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAE5D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAGjD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAE9D,OAAO,EAAE,KAAK,kBAAkB,EAAmB,MAAM,UAAU,CAAC;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AAEH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,mBAAmB,KAAK,CAAC;AAEtC,iHAAiH;AACjH,MAAM,WAAW,oBAAoB;IACnC,4DAA4D;IAC5D,MAAM,EAAE,cAAc,CAAC;IACvB,oFAAoF;IACpF,WAAW,EAAE,mBAAmB,CAAC;IACjC,iBAAiB;IACjB,GAAG,EAAE,IAAI,CAAC;IACV;;;;;;OAMG;IACH,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,OAAO,CAAC,oBAAoB,GAAG,SAAS,CAAC,CAAC;IACzF,yGAAyG;IACzG,OAAO,CAAC,EAAE,CAAC,UAAU,EAAE,kBAAkB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5D,sEAAsE;IACtE,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,6FAA6F;AAC7F,MAAM,WAAW,eAAe;IAC9B,kEAAkE;IAClE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,4EAA4E;IAC5E,QAAQ,CAAC,SAAS,EAAE,SAAS,MAAM,EAAE,CAAC;IACtC,sGAAsG;IACtG,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;CAC1B;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,oBAAoB,CACxC,EAAE,EAAE,UAAU,EACd,IAAI,EAAE,YAAY,EAClB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,eAAe,CAAC,CA2E1B"}
@@ -0,0 +1,162 @@
1
+ import { PaymentsWebhookEvent, WEBHOOK_EVENT_ORPHANED, webhookEventAwaitsOwner } from "../data/webhookEvent.js";
2
+ import { PAYMENTS_WEBHOOK_EVENTS_TABLE, paymentsDatabase } from "../data/tables.js";
3
+ import { resolveNotificationOwner } from "./owner.js";
4
+ import { projectPurchase } from "./writer.js";
5
+ import { withD1Retry } from "@pithy-sh/core/src/data/withD1Retry";
6
+ //#region src/projection/orphans.ts
7
+ /**
8
+ * The repair that runs when an account links: project the purchases that were waiting for exactly that.
9
+ *
10
+ * ## The gap this closes (#341)
11
+ *
12
+ * An orphan is an authentic notification carrying a real purchase and nothing that says whose it is. Both
13
+ * paths that meet one leave the row unfinished on purpose — the webhook handler records the reason and
14
+ * withholds `processedAt`, the sweep stamps `abandonedAt` so its stream can advance — and both then rely on
15
+ * **a later delivery of the same event** to repair it. That is the right bet when the store redelivers. It
16
+ * is no bet at all when the only future event is the account linking: nothing redelivers on that signal,
17
+ * every store's retry window closes within days, and the sweep's cursor is long past.
18
+ *
19
+ * Reproduced on real D1 before this existed: sweep an unstamped `subscription.activated` for an unlinked
20
+ * customer, link the account, sweep ten more times. The purchase is never projected. Not once, ever — the
21
+ * orphan gets exactly one owner resolution in its life, at the moment nobody could answer it.
22
+ *
23
+ * ## Why here and not in the sweep
24
+ *
25
+ * The two halves of the question live in different places. The link path knows *which* customer just became
26
+ * resolvable; the sweep knows *which events* are outstanding. One of them has to ask the other, and asking
27
+ * from the link side is the cheaper direction by a wide margin: it runs once per link rather than once per
28
+ * sweep, the set it looks at is bounded by {@link ORPHAN_REPAIR_LIMIT}, and — decisively — it works on the
29
+ * four rails that have no sweep at all. A rail without a repair pass is exactly where a lost orphan stays
30
+ * lost.
31
+ *
32
+ * ## Which rows, and why not simply "unfinished"
33
+ *
34
+ * {@link webhookEventAwaitsOwner}: unfinished, and carrying the {@link WEBHOOK_EVENT_ORPHANED} marker. An
35
+ * account linking repairs one thing — a missing owner. A quarantined event, a failed projection, an unmapped
36
+ * SKU are all unfinished too, and re-running them on this signal would be an unbounded retry loop triggered
37
+ * by unrelated traffic. The marker is what makes the set the *right* set rather than merely a small one.
38
+ *
39
+ * ## Why a payload replay rather than a stored event
40
+ *
41
+ * The row already holds the notification whole — that is what `payload` is for, and the reason it is stored
42
+ * rather than summarized. So the purchase is recoverable without a second column, a migration, or a shape
43
+ * that could disagree with the payload it was derived from. What it costs is a rail able to re-read its own
44
+ * recorded body, which is {@link PaymentsRailProvider.replay} and is optional for the two rails whose stored
45
+ * body is a signed blob.
46
+ *
47
+ * ## What it deliberately does not do
48
+ *
49
+ * It never *invents* an owner. Every row goes back through {@link resolveNotificationOwner}, against the same
50
+ * table in the same trust order, so a link that resolves nothing changes nothing — and a row for a different
51
+ * holder that happens to be in the same page is left exactly as it stood. Passing the freshly linked subject
52
+ * straight to the writer would be the shortcut, and it would project one customer's purchase onto another's
53
+ * account the first time two orphans shared a page.
54
+ *
55
+ * **Per row, and both halves per row.** The subject is resolved inside the loop, from the row's own hints, so
56
+ * a page of orphans is a page of independent questions rather than one answer applied to all of them. There
57
+ * is no subject computed before the loop for the same reason there is no `subjectType` taken from config: a
58
+ * kind that outlived the row it came from is how an organization's purchase lands on a user with the same id.
59
+ */
60
+ /**
61
+ * How many orphaned rows one link may repair.
62
+ *
63
+ * A bound rather than "all of them", because this runs inside a webhook handler that a store is timing. A
64
+ * link arriving after a long outage could face hundreds of orphans, and a handler that walks them all is a
65
+ * handler the store times out and redelivers — which produces the same walk again, from the top.
66
+ *
67
+ * Small enough to be invisible in a request, and the leftovers are not lost: they still carry the marker, and
68
+ * the next link on that rail takes the next page. The rows are taken oldest first so the queue drains in
69
+ * order rather than starving its own tail.
70
+ */
71
+ const ORPHAN_REPAIR_LIMIT = 25;
72
+ /**
73
+ * Project the orphaned events on one rail that an owner can now be resolved for.
74
+ *
75
+ * Called after a link is written, on every path that writes one. Never throws for a row it cannot repair: a
76
+ * replay that cannot read an old payload, an owner that still does not resolve, a projection the catalog
77
+ * refuses — each leaves that row untouched and the pass moves to the next. The caller is a webhook handler
78
+ * answering a store, and a repair failing must not turn a delivery that succeeded into a non-2xx.
79
+ *
80
+ * A row it *does* project is finished the same way the original delivery would have finished it, so the
81
+ * store's own redelivery of it is a duplicate from then on.
82
+ */
83
+ async function repairOrphanedEvents(d1, rail, options) {
84
+ const projected = [];
85
+ let examined = 0;
86
+ let waiting = 0;
87
+ if (options.replay === void 0) return {
88
+ examined,
89
+ projected,
90
+ waiting
91
+ };
92
+ const db = paymentsDatabase(d1);
93
+ const rows = await db.selectFrom(PAYMENTS_WEBHOOK_EVENTS_TABLE).selectAll().where("rail", "=", rail).where("processedAt", "is", null).where("error", "like", `${WEBHOOK_EVENT_ORPHANED}%`).orderBy("receivedAt").limit(options.limit ?? 25).execute();
94
+ for (const stored of rows) {
95
+ const row = PaymentsWebhookEvent.parse(stored);
96
+ if (!webhookEventAwaitsOwner(stored)) continue;
97
+ examined += 1;
98
+ const notification = await options.replay(row.payload);
99
+ if (notification?.event === void 0 || notification.event === null) {
100
+ waiting += 1;
101
+ continue;
102
+ }
103
+ const subject = await resolveNotificationOwner(db, rail, {
104
+ providerAccountId: notification.providerAccountId,
105
+ providerTransactionId: notification.event.providerTransactionId,
106
+ originalTransactionId: notification.event.originalTransactionId
107
+ });
108
+ if (subject === void 0) {
109
+ waiting += 1;
110
+ continue;
111
+ }
112
+ let projection;
113
+ try {
114
+ projection = await projectPurchase(d1, {
115
+ ...notification.event,
116
+ ...subject
117
+ }, {
118
+ config: options.config,
119
+ environment: options.environment,
120
+ now: options.now
121
+ });
122
+ if (notification.stateEvent) await projectPurchase(d1, {
123
+ ...notification.stateEvent,
124
+ ...subject
125
+ }, {
126
+ config: options.config,
127
+ environment: options.environment,
128
+ now: options.now
129
+ });
130
+ await options.fulfill?.(projection);
131
+ } catch {
132
+ waiting += 1;
133
+ continue;
134
+ }
135
+ await finish(d1, row.id, options.now);
136
+ projected.push(row.providerEventId);
137
+ }
138
+ return {
139
+ examined,
140
+ projected,
141
+ waiting
142
+ };
143
+ }
144
+ /**
145
+ * Mark a repaired row finished, and clear the reason it carried.
146
+ *
147
+ * `processedAt` because it now genuinely is finished — the purchase is projected, and a redelivery has
148
+ * nothing left to add. The `error` goes with it: it said "no subject could be resolved", which stopped
149
+ * being true, and a stale reason beside a finished timestamp is the contradiction `data/webhookEvent.ts`
150
+ * refuses to store.
151
+ *
152
+ * `abandonedAt` is left exactly where it was. A sweep having given up on this row is a fact about that pass
153
+ * and a record of how close the purchase came to being lost; `finished` already wins over it by construction.
154
+ */
155
+ async function finish(d1, id, now) {
156
+ await withD1Retry(() => paymentsDatabase(d1).updateTable(PAYMENTS_WEBHOOK_EVENTS_TABLE).set({
157
+ processedAt: now.getTime(),
158
+ error: null
159
+ }).where("id", "=", id).execute());
160
+ }
161
+ //#endregion
162
+ export { ORPHAN_REPAIR_LIMIT, repairOrphanedEvents };