@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,175 @@
1
+ import type { D1Database } from "@cloudflare/workers-types";
2
+ import type { PaymentsConfig } from "../config/config";
3
+ import type { PurchaseEnvironment } from "../data/purchase";
4
+ import { fulfillPurchase } from "../grants/apply";
5
+ import { type PaddleEnvironment, type PaddleHttpFetch } from "../rails/paddle/api";
6
+ import { PADDLE_EVENT_RETENTION_DAYS } from "../rails/paddle/events";
7
+ import type { PaymentsPaddleCredentials } from "../secret/registry";
8
+ /**
9
+ * The Paddle events sweep, as a reconciliation step.
10
+ *
11
+ * `refresh` re-reads rows this deployment already holds. A purchase whose webhook was never delivered and
12
+ * which no client submitted has **no row**, so it is invisible to `refresh` forever. Paddle publishes an
13
+ * account-wide event stream, so this rail can find those — and no other rail in this package can.
14
+ *
15
+ * ## Why it writes through the webhook table rather than around it
16
+ *
17
+ * A swept event and its webhook are the *same event*: Paddle's stream carries `event_id` and no
18
+ * `notification_id`, so the id here is the id the delivery would have recorded. Writing through
19
+ * `pithy_payments_webhook_events` with the same `UNIQUE (rail, providerEventId)` is therefore what makes
20
+ * the sweep idempotent against the webhook path *and* against itself — a sweep of an event already
21
+ * delivered inserts nothing and projects nothing.
22
+ *
23
+ * It also keeps one answer to "did we hear about this": an operator reading that table sees every event
24
+ * this deployment acted on, whichever door it came through.
25
+ *
26
+ * ## What a failed event costs, and for how long
27
+ *
28
+ * Two failures are available here and they are mirror images, so neither rule on its own is right.
29
+ *
30
+ * **Always skip.** Advancing past a failure turns a transient D1 fault into a permanently skipped
31
+ * purchase. There is no second pass that would find it — that is the whole reason this sweep exists.
32
+ *
33
+ * **Always halt.** Leaving the cursor in front of the failure and retrying forever is correct for a
34
+ * transient fault and wrong for a permanent one. A malformed event, a deleted transaction, a shape Paddle
35
+ * changed — none of them clears on the next attempt, and the sweep then stands in front of that event for
36
+ * good, so *every later purchase* becomes the one nothing ever finds. That is the same defect with the
37
+ * blame moved.
38
+ *
39
+ * So this halts a **bounded** number of times and then quarantines: see {@link PADDLE_SWEEP_MAX_ATTEMPTS},
40
+ * {@link fail} and {@link quarantine}. Under the bound, both timestamps stay null and the cursor stops in
41
+ * front of the event, so the next run tries it again. At the bound, `abandonedAt` is stamped with an error
42
+ * naming the quarantine and the attempts that earned it, the run's report names the event id, and the
43
+ * cursor moves on. A row nobody can find is the same defect wearing the other hat, so the quarantine is
44
+ * written in words on the row rather than being the absence of one.
45
+ *
46
+ * ## A quarantine bounds a stall; it does not end an event
47
+ *
48
+ * `abandonedAt` and `processedAt` are separate columns for one reason (#337). "This pass has given up" and
49
+ * "this event is finished with" are different claims, and the webhook guard short-circuits on the second.
50
+ * While the quarantine wrote `processedAt`, it also answered Paddle's ordinary retry — and an operator's
51
+ * replay, which reuses the same `event_id` — with `duplicate`, so a quarantined purchase could never be
52
+ * projected even after somebody fixed the cause. A bounded retry that is silently terminal is just the
53
+ * "always skip" rule with extra steps.
54
+ *
55
+ * So an abandoned row is invisible to *this* sweep, which is what stops the attempt count restarting, and
56
+ * fully visible to the webhook path, which is what repairs it. What this sweep repairs is a webhook that
57
+ * was never delivered at all; what repairs a delivery that arrived and failed is the next delivery, and
58
+ * `completeWebhook` no longer writes `processedAt` beside a repairable error.
59
+ *
60
+ * ## An orphan is abandoned, not finished (#339)
61
+ *
62
+ * An orphan is an event whose `custom_data` carries no usable `pithy_user` stamp, for a customer with no
63
+ * `provider_accounts` row. "No usable stamp" now includes a stamp that does not **decode**: the value is an
64
+ * encoded subject reference (`user:ada`, `organization:acme`), and a bare id — the shape every pre-subject
65
+ * checkout wrote — names nobody however well it is proven. This sweep used to call {@link complete} on an
66
+ * orphan — the same `processedAt` that means *finished with*.
67
+ *
68
+ * That was wrong, and it was wrong in the one way that costs a purchase. The webhook path treats its own
69
+ * orphans as outstanding, and both paths write the **same row** under `UNIQUE (rail, providerEventId)`, so
70
+ * whichever ran second decided. When the sweep ran first, the link then arrived and Paddle redelivered, the
71
+ * guard read `processedAt`, answered `duplicate`, and the purchase was never projected. An orphan is the
72
+ * textbook repairable delivery: nothing about the event is wrong, the world is simply missing a row that a
73
+ * checkout, a client submission or an operator will add.
74
+ *
75
+ * The tension is real, though, and it is why `complete` looked reasonable. Leaving an orphan *outstanding*
76
+ * makes {@link record} count it fresh every run, and {@link fail} halts the cursor in front of it — so one
77
+ * customer who never links stalls every event behind them, daily, for as long as the deployment runs.
78
+ *
79
+ * `abandonedAt` is the state that already resolves exactly this, so it is used rather than a fourth one
80
+ * invented for the occasion: abandoned is **invisible to this sweep**, so the cursor advances and nothing
81
+ * stalls, and **not finished to the webhook guard**, so the redelivery that follows the link still repairs
82
+ * it. See {@link abandon}. Unlike a quarantine it costs no attempts, because an orphan is not a failure
83
+ * being retried — no number of sweeps conjures a link, so the first look is as informed as the tenth.
84
+ */
85
+ /** What the sweep needs. Every seam is explicit, so a test drives it without stubbing a module. */
86
+ export interface PaddleSweepDeps {
87
+ /** The app database the `pithy_payments_*` tables live in. */
88
+ d1: D1Database;
89
+ /** The resolved catalog — the writer's product lookup comes from it. */
90
+ config: PaymentsConfig;
91
+ /** This deployment's store environment. A swept sandbox purchase is refused in production, as ever. */
92
+ environment: PurchaseEnvironment;
93
+ /** Paddle's credentials, resolved through the secrets store by the caller. */
94
+ credentials: PaymentsPaddleCredentials;
95
+ /** Which Paddle account to sweep. */
96
+ paddleEnvironment: PaddleEnvironment;
97
+ /** This deployment's `ENVIRONMENT`, for the shared-sandbox fence and the ownership proof. */
98
+ deployment?: string;
99
+ /** The clock. */
100
+ now(): Date;
101
+ /** The row-id minter, injected so a test is deterministic. */
102
+ newId?: () => string;
103
+ /** The HTTP seam. */
104
+ transport?: PaddleHttpFetch;
105
+ /** Fulfillment for a swept purchase. Defaults to {@link fulfillPurchase}, and is safe to repeat. */
106
+ fulfill?: (d1: D1Database, projection: Parameters<typeof fulfillPurchase>[1]) => Promise<unknown>;
107
+ /** How many pages one sweep may walk. A bound on the work, so a first pass finishes. */
108
+ maxPages?: number;
109
+ }
110
+ /** What one sweep did. Returned through a `step.do`, so a replay restores it exactly. */
111
+ export interface PaddleSweepReport {
112
+ /** Events read from the stream. */
113
+ read: number;
114
+ /** Events that produced a purchase row. */
115
+ projected: number;
116
+ /** Events that were authentic and projected nothing — a fenced delivery, a type the map ignores. */
117
+ ignored: number;
118
+ /**
119
+ * Events with a purchase in them and nobody to project it against, walked past and left repairable.
120
+ *
121
+ * **Paddle's own `evt_…` ids, and its own field rather than a share of `ignored`.** An ignored event is
122
+ * one this build was never going to act on; an orphan is a real purchase waiting on a link that has not
123
+ * arrived. Counting the second as the first is how {@link complete} came to be called on one — see the
124
+ * module doc — and a number an operator reads as healthy is the wrong place to hide a stuck sale.
125
+ *
126
+ * Each entry is also a row carrying `abandonedAt` and its reason, repairable by any delivery of the same
127
+ * event id once the link exists. **Named here because this sweep will not look again**, so the report is
128
+ * where an operator learns there is a sale to chase — `refresh` cannot find it either, since an orphan
129
+ * has no purchase row to re-read.
130
+ */
131
+ orphaned: string[];
132
+ /** Events already recorded, so the sweep found nothing new. This is the healthy number. */
133
+ duplicate: number;
134
+ /** Events that could not be projected this run. Advancement stops at the first one that is not quarantined. */
135
+ failed: number;
136
+ /**
137
+ * Events given up on this run — tried {@link PADDLE_SWEEP_MAX_ATTEMPTS} times and walked past.
138
+ *
139
+ * **Paddle's own `evt_…` ids, not a count.** A count says an operator has something to read and then
140
+ * makes them go and find it; the id is what they replay, what they grep the table for, and what they
141
+ * paste into Paddle's replay endpoint. `length` is still the count for a log line.
142
+ *
143
+ * Never empty quietly: each entry is also a row carrying `abandonedAt` and its reason. A run reporting
144
+ * quarantines is a run that moved a purchase out of *its own* repair path, which is a decision somebody
145
+ * should see rather than infer.
146
+ */
147
+ quarantined: string[];
148
+ /** Where the cursor now stands, or null when it has never advanced. */
149
+ cursor: string | null;
150
+ /** A gap this run cannot close — a cursor past Paddle's retention — or null. */
151
+ gap: string | null;
152
+ }
153
+ /**
154
+ * How many sweeps may try one event before it is quarantined and the stream moves on.
155
+ *
156
+ * **The unit is a run, and this sweep runs on a daily cron** — so three attempts is three days. That is
157
+ * long enough for the faults a retry actually fixes: a D1 blip, a Paddle outage, a price somebody has to
158
+ * add to the catalog after being paged. It is short enough that a fault no retry will ever fix costs the
159
+ * stream three days rather than the rest of its life.
160
+ *
161
+ * One would be "always skip" with a counter bolted on. A hundred would be "always halt" with the same.
162
+ */
163
+ export declare const PADDLE_SWEEP_MAX_ATTEMPTS = 3;
164
+ /**
165
+ * Sweep Paddle's event stream and project what it finds.
166
+ *
167
+ * Returns a report rather than throwing on a projection failure: one unprojectable event must not lose the
168
+ * run's whole progress, and the cursor stopping in front of it is what makes it repairable. A store that
169
+ * cannot be *reached* still throws, so the step fails and retries rather than recording a sweep that never
170
+ * happened.
171
+ */
172
+ export declare function sweepPaddle(deps: PaddleSweepDeps): Promise<PaddleSweepReport>;
173
+ /** How long Paddle keeps an event, re-exported so a caller reporting a gap need not import two modules. */
174
+ export { PADDLE_EVENT_RETENTION_DAYS };
175
+ //# sourceMappingURL=paddleSweep.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paddleSweep.d.ts","sourceRoot":"","sources":["../../src/workflows/paddleSweep.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAG5D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAU5D,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAKlD,OAAO,EAAE,KAAK,iBAAiB,EAAE,KAAK,eAAe,EAAmB,MAAM,qBAAqB,CAAC;AACpG,OAAO,EAAE,2BAA2B,EAAqB,MAAM,wBAAwB,CAAC;AAIxF,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,oBAAoB,CAAC;AAEpE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4EG;AAEH,mGAAmG;AACnG,MAAM,WAAW,eAAe;IAC9B,8DAA8D;IAC9D,EAAE,EAAE,UAAU,CAAC;IACf,wEAAwE;IACxE,MAAM,EAAE,cAAc,CAAC;IACvB,uGAAuG;IACvG,WAAW,EAAE,mBAAmB,CAAC;IACjC,8EAA8E;IAC9E,WAAW,EAAE,yBAAyB,CAAC;IACvC,qCAAqC;IACrC,iBAAiB,EAAE,iBAAiB,CAAC;IACrC,6FAA6F;IAC7F,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB;IACjB,GAAG,IAAI,IAAI,CAAC;IACZ,8DAA8D;IAC9D,KAAK,CAAC,EAAE,MAAM,MAAM,CAAC;IACrB,qBAAqB;IACrB,SAAS,CAAC,EAAE,eAAe,CAAC;IAC5B,oGAAoG;IACpG,OAAO,CAAC,EAAE,CAAC,EAAE,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,CAAC,OAAO,eAAe,CAAC,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAClG,wFAAwF;IACxF,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,yFAAyF;AACzF,MAAM,WAAW,iBAAiB;IAChC,mCAAmC;IACnC,IAAI,EAAE,MAAM,CAAC;IACb,2CAA2C;IAC3C,SAAS,EAAE,MAAM,CAAC;IAClB,oGAAoG;IACpG,OAAO,EAAE,MAAM,CAAC;IAChB;;;;;;;;;;;;OAYG;IACH,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,2FAA2F;IAC3F,SAAS,EAAE,MAAM,CAAC;IAClB,+GAA+G;IAC/G,MAAM,EAAE,MAAM,CAAC;IACf;;;;;;;;;;OAUG;IACH,WAAW,EAAE,MAAM,EAAE,CAAC;IACtB,uEAAuE;IACvE,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,gFAAgF;IAChF,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;CACpB;AAKD;;;;;;;;;GASG;AACH,eAAO,MAAM,yBAAyB,IAAI,CAAC;AAiT3C;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAiNnF;AAED,2GAA2G;AAC3G,OAAO,EAAE,2BAA2B,EAAE,CAAC"}
@@ -0,0 +1,433 @@
1
+ import { decodeSubjectReference } from "../data/subject.js";
2
+ import { railEnabled } from "../config/config.js";
3
+ import { PADDLE_EVENTS_CURSOR, PaymentsSyncCursor } from "../data/syncCursor.js";
4
+ import { PaymentsWebhookEvent, WEBHOOK_EVENT_ORPHANED, isWebhookEventOutstanding, webhookEventState } from "../data/webhookEvent.js";
5
+ import { PAYMENTS_SYNC_CURSORS_TABLE, PAYMENTS_WEBHOOK_EVENTS_TABLE, paymentsDatabase } from "../data/tables.js";
6
+ import { fulfillPurchase } from "../grants/apply.js";
7
+ import { linkProviderAccount, resolveNotificationOwner } from "../projection/owner.js";
8
+ import { projectPurchase } from "../projection/writer.js";
9
+ import { repairOrphanedEvents } from "../projection/orphans.js";
10
+ import { noteIsRepairable, noteText } from "../rails/contract.js";
11
+ import { paddleHttpFetch } from "../rails/paddle/api.js";
12
+ import { PaddleEvent } from "../rails/paddle/objects.js";
13
+ import { PADDLE_ADJUSTMENTS_INCLUDE, readTransaction } from "../rails/paddle/read.js";
14
+ import { readPaddleEvent } from "../rails/paddle/webhook.js";
15
+ import { PADDLE_EVENT_RETENTION_DAYS, sweepPaddleEvents } from "../rails/paddle/events.js";
16
+ import { PithyError } from "@pithy-sh/core/src/error/pithyError";
17
+ import { withD1Retry } from "@pithy-sh/core/src/data/withD1Retry";
18
+ //#region src/workflows/paddleSweep.ts
19
+ /** How many pages one sweep walks by default. Two hundred events a page, so this is 2,000 events. */
20
+ const DEFAULT_MAX_PAGES = 10;
21
+ /**
22
+ * How many sweeps may try one event before it is quarantined and the stream moves on.
23
+ *
24
+ * **The unit is a run, and this sweep runs on a daily cron** — so three attempts is three days. That is
25
+ * long enough for the faults a retry actually fixes: a D1 blip, a Paddle outage, a price somebody has to
26
+ * add to the catalog after being paged. It is short enough that a fault no retry will ever fix costs the
27
+ * stream three days rather than the rest of its life.
28
+ *
29
+ * One would be "always skip" with a counter bolted on. A hundred would be "always halt" with the same.
30
+ */
31
+ const PADDLE_SWEEP_MAX_ATTEMPTS = 3;
32
+ /** Read the stored cursor for Paddle's event stream, or null when it has never advanced. */
33
+ async function readCursor(d1) {
34
+ const row = await paymentsDatabase(d1).selectFrom(PAYMENTS_SYNC_CURSORS_TABLE).select(["cursor"]).where("rail", "=", "paddle").where("name", "=", PADDLE_EVENTS_CURSOR).executeTakeFirst();
35
+ return typeof row?.cursor === "string" ? row.cursor : null;
36
+ }
37
+ /** Write the cursor back, creating the row on the first sweep. */
38
+ async function writeCursor(d1, cursor, now, newId) {
39
+ const db = paymentsDatabase(d1);
40
+ const row = PaymentsSyncCursor.encode({
41
+ id: newId(),
42
+ rail: "paddle",
43
+ name: PADDLE_EVENTS_CURSOR,
44
+ cursor,
45
+ updatedAt: now,
46
+ createdAt: now
47
+ });
48
+ await withD1Retry(() => db.insertInto(PAYMENTS_SYNC_CURSORS_TABLE).values(row).onConflict((oc) => oc.columns(["rail", "name"]).doUpdateSet({
49
+ cursor,
50
+ updatedAt: now.getTime()
51
+ })).execute());
52
+ }
53
+ /**
54
+ * Record one swept event, or report that it was already recorded.
55
+ *
56
+ * The same `UNIQUE (rail, providerEventId)` insert the webhook guard makes, against the same table and the
57
+ * same key. That is what makes a sweep of an already-delivered event a no-op rather than a second write.
58
+ *
59
+ * **`fresh` means "this pass still has work to do here", which is not the same as "not yet seen".** It asks
60
+ * {@link isWebhookEventOutstanding}, so an event this sweep recorded and then failed to project — left
61
+ * `pending` or `failed` by {@link fail} — comes back fresh and is tried again, while one that was
62
+ * projected, fenced out, or is a type that projects nothing is finished and does not.
63
+ *
64
+ * **An orphan is not fresh either, and it is not finished — see {@link orphan}.** A row the *webhook* left
65
+ * outstanding as an orphan still is, so the sweep picks it up, re-resolves the owner against a table that
66
+ * may have gained the link since, and either projects it or abandons it. That re-resolution is the one
67
+ * thing a second look at an orphan is worth, and it happens once.
68
+ *
69
+ * **A quarantined event is not fresh either, and that is a different reason.** It is still outstanding to a
70
+ * webhook delivery, which must be allowed to repair it; it is not outstanding to *this* pass, because the
71
+ * bound exists so one unprojectable event cannot hold the stream up for ever, and picking its own
72
+ * quarantines back up would restart the count and stall again. Two readers, two questions — see
73
+ * `data/webhookEvent.ts`.
74
+ *
75
+ * `attempts` comes back with it, because how many times this event has already been tried is what decides
76
+ * whether the next failure halts the stream or gives up on it.
77
+ */
78
+ async function record(d1, eventId, payload, now, newId) {
79
+ const db = paymentsDatabase(d1);
80
+ const row = PaymentsWebhookEvent.encode({
81
+ id: newId(),
82
+ rail: "paddle",
83
+ providerEventId: eventId,
84
+ payload,
85
+ receivedAt: now,
86
+ processedAt: null,
87
+ abandonedAt: null,
88
+ error: null,
89
+ attempts: 0,
90
+ createdAt: now
91
+ });
92
+ await withD1Retry(() => db.insertInto(PAYMENTS_WEBHOOK_EVENTS_TABLE).values(row).onConflict((oc) => oc.columns(["rail", "providerEventId"]).doNothing()).execute());
93
+ const stored = await db.selectFrom(PAYMENTS_WEBHOOK_EVENTS_TABLE).select([
94
+ "id",
95
+ "processedAt",
96
+ "abandonedAt",
97
+ "error",
98
+ "attempts"
99
+ ]).where("rail", "=", "paddle").where("providerEventId", "=", eventId).executeTakeFirst();
100
+ return {
101
+ id: stored?.id ?? row.id,
102
+ fresh: stored === void 0 || isWebhookEventOutstanding(webhookEventState(stored)),
103
+ attempts: stored?.attempts ?? 0
104
+ };
105
+ }
106
+ /**
107
+ * Mark a recorded event **handled**, with the note when it was handled by deciding to do nothing.
108
+ *
109
+ * `processedAt` is the column the webhook guard short-circuits on, so it is set only where the event is
110
+ * genuinely finished with: projected, fenced out, or a type that projects nothing. A failure is not one of
111
+ * those — see {@link fail} — and neither is a quarantine, see {@link quarantine}.
112
+ *
113
+ * **Nor is an orphan, which is the whole of #339.** It reads like one — the sweep can do nothing more with
114
+ * it, so "handled" is tempting — but the guard reads this column, and the event's repair is precisely the
115
+ * redelivery that follows the missing link. See {@link abandon} and the module doc.
116
+ */
117
+ async function complete(d1, id, now, note) {
118
+ await withD1Retry(() => paymentsDatabase(d1).updateTable(PAYMENTS_WEBHOOK_EVENTS_TABLE).set({
119
+ processedAt: now.getTime(),
120
+ error: note ?? null
121
+ }).where("id", "=", id).execute());
122
+ }
123
+ /**
124
+ * Record why an event could not be projected, count the attempt, and **leave both timestamps null**.
125
+ *
126
+ * The nulls are the whole of it. A `processedAt` beside the error would say "finished", and an
127
+ * `abandonedAt` would say "given up on"; either would stop {@link record} counting the event outstanding,
128
+ * the next sweep would advance the cursor past it, and no pass would ever look at it again — a transient
129
+ * D1 fault turned into a permanently lost purchase. Two nulls with a non-null `error` is the honest
130
+ * triple: it arrived, it was tried, it failed, and it is still outstanding to everyone.
131
+ *
132
+ * `receivedAt` beside a null `processedAt` is already the package's documented drift signal, so this
133
+ * reports through a channel operators read rather than inventing a second one.
134
+ */
135
+ async function fail(d1, id, attempt, error) {
136
+ await withD1Retry(() => paymentsDatabase(d1).updateTable(PAYMENTS_WEBHOOK_EVENTS_TABLE).set({
137
+ processedAt: null,
138
+ error,
139
+ attempts: attempt
140
+ }).where("id", "=", id).execute());
141
+ }
142
+ /** The word an operator greps `error` for. One string, so the writer and a runbook cannot drift apart. */
143
+ const QUARANTINED = "quarantined";
144
+ /**
145
+ * The word for the other reason this sweep walks away from an event, likewise greppable.
146
+ *
147
+ * Shared with the webhook handler through `data/webhookEvent.ts` rather than spelled here, because the relink
148
+ * repair queries on it: two writers with two sentences for one condition is a set that cannot be selected.
149
+ */
150
+ const ORPHANED = WEBHOOK_EVENT_ORPHANED;
151
+ /**
152
+ * Stamp `abandonedAt` and say in the row why — the one writer for *this pass has walked away from this*.
153
+ *
154
+ * **The state that is neither finished nor outstanding, and the reason there are two columns.**
155
+ * {@link isWebhookEventOutstanding} reads it false, so this sweep's next run does not pick the event back
156
+ * up and its cursor is free to advance. {@link isWebhookEventFinished} reads it false, so a later delivery
157
+ * of the same event — the provider's retry, or an operator's replay, both reusing the original event id —
158
+ * still runs the handler and can project the purchase.
159
+ *
160
+ * Two callers reach it for two different reasons and both want exactly that pair of answers: a quarantine,
161
+ * which is a bounded retry giving up, and an orphan, which never had anything to retry. `attempts` is
162
+ * written only when a caller counted any, so an orphan's row does not claim a failure it never had.
163
+ */
164
+ async function abandon(d1, id, now, error, attempts) {
165
+ await withD1Retry(() => paymentsDatabase(d1).updateTable(PAYMENTS_WEBHOOK_EVENTS_TABLE).set({
166
+ abandonedAt: now.getTime(),
167
+ error,
168
+ ...attempts === void 0 ? {} : { attempts }
169
+ }).where("id", "=", id).execute());
170
+ }
171
+ /**
172
+ * Walk past an event with a purchase in it and nobody to project it against, and leave it repairable.
173
+ *
174
+ * **Not {@link complete}, and that reversal is #339.** Writing `processedAt` here told the webhook guard the
175
+ * event was finished, and the two paths share one row — so a sweep that reached an orphan first made the
176
+ * redelivery that arrives *after* the account links answer `duplicate`, and the purchase was never
177
+ * projected. The one repair path for an orphan is the very delivery that write silenced.
178
+ *
179
+ * Not {@link fail} either, and that is the other half. A failure halts the cursor so the next run retries,
180
+ * which is right for a fault that might clear; an orphan clears when a *link* appears, and no amount of
181
+ * re-reading this event produces one. Halting would stall every event behind one customer, daily, for good.
182
+ *
183
+ * So: abandoned. Invisible to this sweep, still repairable by a delivery. The row says which of the two
184
+ * reasons put it there, and names the command for an operator who would rather not wait for a redelivery.
185
+ */
186
+ async function orphan(d1, id, now) {
187
+ await abandon(d1, id, now, `${ORPHANED} no subject could be resolved for this swept event, so the sweep has moved past it. It is not finished — the account linking re-examines it (see \`projection/orphans.ts\`), and any later delivery of this event id projects it too, including a replay from Paddle.`);
188
+ }
189
+ /**
190
+ * Give up on an event: stamp `abandonedAt` so this pass moves past it, and say in the row why.
191
+ *
192
+ * **`abandonedAt`, not `processedAt`, and the two columns are the whole fix for #337.** What this sweep
193
+ * needs is for its own next run to stop counting the event fresh and restarting the attempt count — that is
194
+ * a fact about *this pass*, and {@link record} reads it through {@link isWebhookEventOutstanding}. What it
195
+ * emphatically must not do is tell the webhook guard the event is finished: writing `processedAt` here made
196
+ * a quarantine answer Paddle's ordinary retry, and an operator's replay, with `duplicate` — so a
197
+ * quarantined purchase could never be projected even once its cause was fixed. A quarantine bounds a
198
+ * stall; it was never meant to be terminal.
199
+ *
200
+ * The row still says so in words. `error` names {@link QUARANTINED}, the attempt count, and the last
201
+ * failure that earned it, so an operator reading the table has the reason and not only a timestamp.
202
+ */
203
+ async function quarantine(d1, id, attempt, now, error) {
204
+ await abandon(d1, id, now, `${QUARANTINED}: after ${attempt} attempts; the sweep has moved past it. Last failure: ${error}`, attempt);
205
+ }
206
+ /**
207
+ * Count one failed attempt against an event, and say whether the sweep may walk past it.
208
+ *
209
+ * `true` means quarantined — the caller advances its cursor. `false` means the bound has not been reached,
210
+ * so the caller halts in front of the event and the next run tries again.
211
+ */
212
+ async function attemptFailed(d1, id, attempts, now, reason) {
213
+ const attempt = attempts + 1;
214
+ if (attempt < 3) {
215
+ await fail(d1, id, attempt, reason);
216
+ return false;
217
+ }
218
+ await quarantine(d1, id, attempt, now, reason);
219
+ return true;
220
+ }
221
+ /**
222
+ * Project the orphans this link just made resolvable, and never let that fail the sweep.
223
+ *
224
+ * The events are gone from this pass's point of view — the cursor moved past them the run they were
225
+ * abandoned — so an account linking is the only signal left that anything changed. The repair replays each
226
+ * row's own recorded payload through the same map this sweep already uses, resolves the owner against the
227
+ * table the link just landed in, and finishes only what it actually projected. See `projection/orphans.ts`.
228
+ *
229
+ * Swallowing here rather than in the repair: a sweep is a Workflow step, and a step that throws is retried
230
+ * from the top — re-reading a page of Paddle events to repair a row that was never this page's business.
231
+ */
232
+ async function repairOrphans(deps, at) {
233
+ const base = {
234
+ credentials: deps.credentials,
235
+ environment: deps.paddleEnvironment,
236
+ transport: deps.transport ?? paddleHttpFetch
237
+ };
238
+ await repairOrphanedEvents(deps.d1, "paddle", {
239
+ config: deps.config,
240
+ environment: deps.environment,
241
+ now: at,
242
+ replay: async (payload) => {
243
+ const parsed = PaddleEvent.safeParse(payload);
244
+ if (!parsed.success) return void 0;
245
+ return await readPaddleEvent(parsed.data, {
246
+ credentials: deps.credentials,
247
+ environment: deps.paddleEnvironment,
248
+ now: at,
249
+ deployment: deps.deployment,
250
+ readTransaction: (id) => readTransaction(id, base, PADDLE_ADJUSTMENTS_INCLUDE)
251
+ });
252
+ },
253
+ fulfill: async (projection) => {
254
+ await (deps.fulfill ?? ((d1, value) => fulfillPurchase(d1, value, {
255
+ config: deps.config,
256
+ now: () => at.getTime()
257
+ })))(deps.d1, projection);
258
+ }
259
+ });
260
+ }
261
+ /** What to write in `error` for a thrown cause. A `PithyError`'s `detail` is throw-site context, not client text. */
262
+ function reasonFor(cause) {
263
+ if (cause instanceof PithyError) return `${cause.payload.code}: ${cause.payload.detail ?? cause.payload.message}`;
264
+ return "projection failed";
265
+ }
266
+ /**
267
+ * Sweep Paddle's event stream and project what it finds.
268
+ *
269
+ * Returns a report rather than throwing on a projection failure: one unprojectable event must not lose the
270
+ * run's whole progress, and the cursor stopping in front of it is what makes it repairable. A store that
271
+ * cannot be *reached* still throws, so the step fails and retries rather than recording a sweep that never
272
+ * happened.
273
+ */
274
+ async function sweepPaddle(deps) {
275
+ const newId = deps.newId ?? (() => crypto.randomUUID());
276
+ const report = {
277
+ read: 0,
278
+ projected: 0,
279
+ ignored: 0,
280
+ orphaned: [],
281
+ duplicate: 0,
282
+ failed: 0,
283
+ quarantined: [],
284
+ cursor: null,
285
+ gap: null
286
+ };
287
+ if (!railEnabled(deps.config, "paddle")) return report;
288
+ let cursor = await readCursor(deps.d1);
289
+ report.cursor = cursor;
290
+ const maxPages = deps.maxPages ?? DEFAULT_MAX_PAGES;
291
+ for (let page = 0; page < maxPages; page += 1) {
292
+ const now = deps.now();
293
+ const swept = await sweepPaddleEvents({
294
+ credentials: deps.credentials,
295
+ environment: deps.paddleEnvironment,
296
+ now,
297
+ deployment: deps.deployment,
298
+ cursor: cursor ?? void 0,
299
+ transport: deps.transport
300
+ });
301
+ if (swept.gap !== null) {
302
+ report.gap = swept.gap;
303
+ return report;
304
+ }
305
+ if (swept.events.length === 0) return report;
306
+ for (const event of swept.events) {
307
+ report.read += 1;
308
+ const at = deps.now();
309
+ if (event.failure !== null) {
310
+ const { id, fresh, attempts } = await record(deps.d1, event.eventId, event.failure.payload, at, newId);
311
+ if (!fresh) {
312
+ report.duplicate += 1;
313
+ cursor = event.eventId;
314
+ continue;
315
+ }
316
+ report.failed += 1;
317
+ if (await attemptFailed(deps.d1, id, attempts, at, reasonFor(event.failure.cause))) {
318
+ report.quarantined.push(event.eventId);
319
+ cursor = event.eventId;
320
+ continue;
321
+ }
322
+ report.cursor = cursor;
323
+ if (cursor !== null) await writeCursor(deps.d1, cursor, at, newId);
324
+ return report;
325
+ }
326
+ if (event.notification === null) {
327
+ report.ignored += 1;
328
+ cursor = event.eventId;
329
+ continue;
330
+ }
331
+ const { notification } = event;
332
+ const { id, fresh, attempts } = await record(deps.d1, event.eventId, notification.payload, at, newId);
333
+ if (!fresh) {
334
+ report.duplicate += 1;
335
+ cursor = event.eventId;
336
+ continue;
337
+ }
338
+ /**
339
+ * The pairing is worth keeping even for an event that projects nothing, and it is proven — the rail
340
+ * returns a reference only when a MAC this deployment's secret produced sits beside it.
341
+ *
342
+ * **Decoded, never split by hand, and never read as a bare id.** `accountReference` is the one
343
+ * single-field slot Paddle gives us, so it carries the subject as one string, and
344
+ * `decodeSubjectReference` is the only reader of that format. It answers `undefined` for anything
345
+ * that is not exactly the encoding — including `ada`, the shape every pre-subject checkout stamped —
346
+ * and `undefined` here means no link is written at all. That is the safe direction: a permanent
347
+ * binding is what `linkProviderAccount` writes and it never rebinds, so a guessed holder is a
348
+ * customer's renewals delivered to a stranger for as long as the deployment lives.
349
+ */
350
+ const referenced = notification.accountReference ? decodeSubjectReference(notification.accountReference) : void 0;
351
+ if (notification.providerAccountId && referenced !== void 0) {
352
+ await linkProviderAccount(deps.d1, "paddle", notification.providerAccountId, referenced, { now: at });
353
+ await repairOrphans(deps, at);
354
+ }
355
+ if (notification.event === null) {
356
+ if (noteIsRepairable(notification.note)) {
357
+ report.failed += 1;
358
+ if (await attemptFailed(deps.d1, id, attempts, at, noteText(notification.note) ?? "unreadable")) {
359
+ report.quarantined.push(event.eventId);
360
+ cursor = event.eventId;
361
+ continue;
362
+ }
363
+ report.cursor = cursor;
364
+ if (cursor !== null) await writeCursor(deps.d1, cursor, at, newId);
365
+ return report;
366
+ }
367
+ report.ignored += 1;
368
+ await complete(deps.d1, id, at, noteText(notification.note));
369
+ cursor = event.eventId;
370
+ continue;
371
+ }
372
+ /**
373
+ * Who holds it — **resolved from rows, because a Workflow has nothing else**.
374
+ *
375
+ * There is no request here, so no `c.var.auth` and no adopter `resolveSubject`; see
376
+ * `workflows/worker.ts`. Every source `resolveNotificationOwner` consults is a row this server wrote
377
+ * or a reference it stamped and Paddle returned, and it answers with the **pair** read off one row.
378
+ */
379
+ const subject = await resolveNotificationOwner(paymentsDatabase(deps.d1), "paddle", {
380
+ providerAccountId: notification.providerAccountId,
381
+ providerTransactionId: notification.event.providerTransactionId,
382
+ originalTransactionId: notification.event.originalTransactionId
383
+ });
384
+ if (subject === void 0) {
385
+ report.orphaned.push(event.eventId);
386
+ await orphan(deps.d1, id, at);
387
+ cursor = event.eventId;
388
+ continue;
389
+ }
390
+ try {
391
+ const projection = await projectPurchase(deps.d1, {
392
+ ...notification.event,
393
+ ...subject
394
+ }, {
395
+ config: deps.config,
396
+ environment: deps.environment,
397
+ now: at
398
+ });
399
+ if (notification.stateEvent) await projectPurchase(deps.d1, {
400
+ ...notification.stateEvent,
401
+ ...subject
402
+ }, {
403
+ config: deps.config,
404
+ environment: deps.environment,
405
+ now: at
406
+ });
407
+ await (deps.fulfill ?? ((d1, value) => fulfillPurchase(d1, value, {
408
+ config: deps.config,
409
+ now: () => at.getTime()
410
+ })))(deps.d1, projection);
411
+ await complete(deps.d1, id, at);
412
+ report.projected += 1;
413
+ cursor = event.eventId;
414
+ } catch (cause) {
415
+ report.failed += 1;
416
+ if (await attemptFailed(deps.d1, id, attempts, at, reasonFor(cause))) {
417
+ report.quarantined.push(event.eventId);
418
+ cursor = event.eventId;
419
+ continue;
420
+ }
421
+ report.cursor = cursor;
422
+ if (cursor !== null) await writeCursor(deps.d1, cursor, at, newId);
423
+ return report;
424
+ }
425
+ }
426
+ report.cursor = cursor;
427
+ if (cursor !== null) await writeCursor(deps.d1, cursor, deps.now(), newId);
428
+ if (!swept.hasMore) return report;
429
+ }
430
+ return report;
431
+ }
432
+ //#endregion
433
+ export { PADDLE_EVENT_RETENTION_DAYS, PADDLE_SWEEP_MAX_ATTEMPTS, sweepPaddle };
@@ -0,0 +1,48 @@
1
+ import type { PaymentsConfig } from "../config/config";
2
+ import type { PaymentsRail } from "../data/rail";
3
+ import type { PaymentsRailProvider } from "../rails/contract";
4
+ import { type RailTrustOptions } from "../rails/providers";
5
+ import { type PaymentsProviderCredentials } from "../secret/registry";
6
+ /**
7
+ * How the reconciliation pass gets a rail, and why it is not simply `resolveRailProvider`.
8
+ *
9
+ * Two of the three rails buy their API access with a signature and a round-trip: Apple mints an ES256 App
10
+ * Store Connect token, Google signs a service-account assertion and POSTs it for an OAuth token. Resolving a
11
+ * rail per purchase would double every lookup — a hundred subscriptions would be two hundred round-trips and a
12
+ * hundred signatures — so a batch mints once and passes the token down through {@link RailTrustOptions}.
13
+ *
14
+ * **Once per page, not once per run.** A minted token expires (Apple's inside the hour, Google's on the hour)
15
+ * and a durable Workflow can be retried long after its first step ran, so a token held for a whole pass would
16
+ * start failing part way through the very run that is meant to repair things. A page is bounded work inside
17
+ * one step, which is exactly the lifetime a token comfortably covers — so the access object is built inside
18
+ * each step and dropped with it.
19
+ *
20
+ * That also keeps the secret handling honest: a bearer credential derived from a secret is never cached in a
21
+ * module variable, which is what CLAUDE.md requires and what a run-scoped cache would quietly become.
22
+ */
23
+ /** One page's rail access: a provider per rail, with its batch token already minted. */
24
+ export interface ReconcileRailAccess {
25
+ /** The provider for one rail. Throws `payments/rail_not_configured` when the rail is off or unprovisioned. */
26
+ providerFor(rail: PaymentsRail): Promise<PaymentsRailProvider>;
27
+ }
28
+ /** What building one page's rail access needs: the catalog, the credentials, the clock, and the seams. */
29
+ export interface RailAccessOptions {
30
+ /** The resolved catalog. Decides whether a rail is enabled at all. */
31
+ config: PaymentsConfig;
32
+ /** Every rail's credentials, read through the one reader by the caller. */
33
+ credentials: PaymentsProviderCredentials;
34
+ /** The clock. Dates each minted token. */
35
+ now: Date;
36
+ /** Transports and additional trust, forwarded to each rail. Empty in production. */
37
+ trust?: RailTrustOptions;
38
+ }
39
+ /**
40
+ * Build one page's rail access, minting each rail's batch token on first use.
41
+ *
42
+ * Lazy per rail: a page holding only Stripe purchases never signs an Apple assertion, and a project that
43
+ * enabled one rail never pays for the other two. The mint happens inside `providerFor`, so a rail whose
44
+ * credentials are missing raises `payments/rail_not_configured` at the same place a disabled rail does — one
45
+ * refusal for the caller to handle rather than two.
46
+ */
47
+ export declare function batchedRailAccess(options: RailAccessOptions): ReconcileRailAccess;
48
+ //# sourceMappingURL=railAccess.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"railAccess.d.ts","sourceRoot":"","sources":["../../src/workflows/railAccess.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACvD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAEjD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAE9D,OAAO,EAAE,KAAK,gBAAgB,EAAuB,MAAM,oBAAoB,CAAC;AAChF,OAAO,EAAE,KAAK,2BAA2B,EAAmB,MAAM,oBAAoB,CAAC;AAEvF;;;;;;;;;;;;;;;;GAgBG;AAEH,wFAAwF;AACxF,MAAM,WAAW,mBAAmB;IAClC,8GAA8G;IAC9G,WAAW,CAAC,IAAI,EAAE,YAAY,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;CAChE;AAED,0GAA0G;AAC1G,MAAM,WAAW,iBAAiB;IAChC,sEAAsE;IACtE,MAAM,EAAE,cAAc,CAAC;IACvB,2EAA2E;IAC3E,WAAW,EAAE,2BAA2B,CAAC;IACzC,0CAA0C;IAC1C,GAAG,EAAE,IAAI,CAAC;IACV,oFAAoF;IACpF,KAAK,CAAC,EAAE,gBAAgB,CAAC;CAC1B;AAED;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,iBAAiB,GAAG,mBAAmB,CA+BjF"}