@owlmeans/server-payment 0.1.18-rc.9 → 0.1.19-rc.0

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 (537) hide show
  1. package/README.md +157 -26
  2. package/agent-meta/manifest.json +30 -4
  3. package/agent-meta/skills/server-payment/SKILL.md +639 -54
  4. package/agent-meta/skills/server-payment/parts/reference.md +174 -0
  5. package/build/access/types.d.ts +46 -0
  6. package/build/access/types.d.ts.map +1 -0
  7. package/build/access/types.js +2 -0
  8. package/build/access/types.js.map +1 -0
  9. package/build/access.d.ts +6 -0
  10. package/build/access.d.ts.map +1 -0
  11. package/build/access.js +45 -0
  12. package/build/access.js.map +1 -0
  13. package/build/actions/index.d.ts +1 -0
  14. package/build/actions/index.d.ts.map +1 -1
  15. package/build/actions/index.js +1 -0
  16. package/build/actions/index.js.map +1 -1
  17. package/build/actions/resync-subscriptions.d.ts +3 -0
  18. package/build/actions/resync-subscriptions.d.ts.map +1 -0
  19. package/build/actions/resync-subscriptions.js +7 -0
  20. package/build/actions/resync-subscriptions.js.map +1 -0
  21. package/build/actions/resync.d.ts +6 -1
  22. package/build/actions/resync.d.ts.map +1 -1
  23. package/build/actions/resync.js +15 -5
  24. package/build/actions/resync.js.map +1 -1
  25. package/build/actions/webhook.d.ts +2 -1
  26. package/build/actions/webhook.d.ts.map +1 -1
  27. package/build/actions/webhook.js +10 -4
  28. package/build/actions/webhook.js.map +1 -1
  29. package/build/bootstrap/types.d.ts +16 -0
  30. package/build/bootstrap/types.d.ts.map +1 -0
  31. package/build/bootstrap/types.js +2 -0
  32. package/build/bootstrap/types.js.map +1 -0
  33. package/build/bootstrap.d.ts +6 -0
  34. package/build/bootstrap.d.ts.map +1 -0
  35. package/build/bootstrap.js +26 -0
  36. package/build/bootstrap.js.map +1 -0
  37. package/build/catalogue/types.d.ts +38 -0
  38. package/build/catalogue/types.d.ts.map +1 -0
  39. package/build/catalogue/types.js +2 -0
  40. package/build/catalogue/types.js.map +1 -0
  41. package/build/catalogue.d.ts +6 -0
  42. package/build/catalogue.d.ts.map +1 -0
  43. package/build/catalogue.js +110 -0
  44. package/build/catalogue.js.map +1 -0
  45. package/build/commit/types.d.ts +22 -0
  46. package/build/commit/types.d.ts.map +1 -0
  47. package/build/commit/types.js +2 -0
  48. package/build/commit/types.js.map +1 -0
  49. package/build/commit.d.ts +6 -0
  50. package/build/commit.d.ts.map +1 -0
  51. package/build/commit.js +125 -0
  52. package/build/commit.js.map +1 -0
  53. package/build/config.d.ts +44 -5
  54. package/build/config.d.ts.map +1 -1
  55. package/build/config.js +216 -10
  56. package/build/config.js.map +1 -1
  57. package/build/consts.d.ts +74 -16
  58. package/build/consts.d.ts.map +1 -1
  59. package/build/consts.js +86 -23
  60. package/build/consts.js.map +1 -1
  61. package/build/consts.local.d.ts +94 -0
  62. package/build/consts.local.d.ts.map +1 -0
  63. package/build/consts.local.js +44 -0
  64. package/build/consts.local.js.map +1 -0
  65. package/build/consumer/cancellation/types.d.ts +11 -0
  66. package/build/consumer/cancellation/types.d.ts.map +1 -0
  67. package/build/consumer/cancellation/types.js +2 -0
  68. package/build/consumer/cancellation/types.js.map +1 -0
  69. package/build/consumer/cancellation.d.ts +6 -0
  70. package/build/consumer/cancellation.d.ts.map +1 -0
  71. package/build/consumer/cancellation.js +33 -0
  72. package/build/consumer/cancellation.js.map +1 -0
  73. package/build/consumer/capture/types.d.ts +50 -0
  74. package/build/consumer/capture/types.d.ts.map +1 -0
  75. package/build/consumer/capture/types.js +2 -0
  76. package/build/consumer/capture/types.js.map +1 -0
  77. package/build/consumer/capture.d.ts +6 -0
  78. package/build/consumer/capture.d.ts.map +1 -0
  79. package/build/consumer/capture.js +285 -0
  80. package/build/consumer/capture.js.map +1 -0
  81. package/build/consumer/consts.local.d.ts +10 -0
  82. package/build/consumer/consts.local.d.ts.map +1 -0
  83. package/build/consumer/consts.local.js +11 -0
  84. package/build/consumer/consts.local.js.map +1 -0
  85. package/build/consumer/format/types.d.ts +30 -0
  86. package/build/consumer/format/types.d.ts.map +1 -0
  87. package/build/consumer/format/types.js +2 -0
  88. package/build/consumer/format/types.js.map +1 -0
  89. package/build/consumer/format.d.ts +4 -0
  90. package/build/consumer/format.d.ts.map +1 -0
  91. package/build/consumer/format.js +73 -0
  92. package/build/consumer/format.js.map +1 -0
  93. package/build/consumer/handlers.d.ts +21 -0
  94. package/build/consumer/handlers.d.ts.map +1 -0
  95. package/build/consumer/handlers.js +174 -0
  96. package/build/consumer/handlers.js.map +1 -0
  97. package/build/consumer/index.d.ts +10 -0
  98. package/build/consumer/index.d.ts.map +1 -0
  99. package/build/consumer/index.js +6 -0
  100. package/build/consumer/index.js.map +1 -0
  101. package/build/consumer/mail/types.d.ts +29 -0
  102. package/build/consumer/mail/types.d.ts.map +1 -0
  103. package/build/consumer/mail/types.js +2 -0
  104. package/build/consumer/mail/types.js.map +1 -0
  105. package/build/consumer/mail.d.ts +6 -0
  106. package/build/consumer/mail.d.ts.map +1 -0
  107. package/build/consumer/mail.js +313 -0
  108. package/build/consumer/mail.js.map +1 -0
  109. package/build/consumer/observers/types.d.ts +15 -0
  110. package/build/consumer/observers/types.d.ts.map +1 -0
  111. package/build/consumer/observers/types.js +2 -0
  112. package/build/consumer/observers/types.js.map +1 -0
  113. package/build/consumer/observers.d.ts +6 -0
  114. package/build/consumer/observers.d.ts.map +1 -0
  115. package/build/consumer/observers.js +74 -0
  116. package/build/consumer/observers.js.map +1 -0
  117. package/build/consumer/origin/types.d.ts +17 -0
  118. package/build/consumer/origin/types.d.ts.map +1 -0
  119. package/build/consumer/origin/types.js +2 -0
  120. package/build/consumer/origin/types.js.map +1 -0
  121. package/build/consumer/origin.d.ts +8 -0
  122. package/build/consumer/origin.d.ts.map +1 -0
  123. package/build/consumer/origin.js +43 -0
  124. package/build/consumer/origin.js.map +1 -0
  125. package/build/consumer/reconcile/types.d.ts +13 -0
  126. package/build/consumer/reconcile/types.d.ts.map +1 -0
  127. package/build/consumer/reconcile/types.js +2 -0
  128. package/build/consumer/reconcile/types.js.map +1 -0
  129. package/build/consumer/reconcile.d.ts +5 -0
  130. package/build/consumer/reconcile.d.ts.map +1 -0
  131. package/build/consumer/reconcile.js +321 -0
  132. package/build/consumer/reconcile.js.map +1 -0
  133. package/build/consumer/records/types.d.ts +66 -0
  134. package/build/consumer/records/types.d.ts.map +1 -0
  135. package/build/consumer/records/types.js +2 -0
  136. package/build/consumer/records/types.js.map +1 -0
  137. package/build/consumer/records.d.ts +6 -0
  138. package/build/consumer/records.d.ts.map +1 -0
  139. package/build/consumer/records.js +243 -0
  140. package/build/consumer/records.js.map +1 -0
  141. package/build/consumer/service.d.ts +28 -0
  142. package/build/consumer/service.d.ts.map +1 -0
  143. package/build/consumer/service.js +706 -0
  144. package/build/consumer/service.js.map +1 -0
  145. package/build/consumer/types.d.ts +84 -0
  146. package/build/consumer/types.d.ts.map +1 -0
  147. package/build/consumer/types.js +2 -0
  148. package/build/consumer/types.js.map +1 -0
  149. package/build/consumer/types.local.d.ts +26 -0
  150. package/build/consumer/types.local.d.ts.map +1 -0
  151. package/build/consumer/types.local.js +2 -0
  152. package/build/consumer/types.local.js.map +1 -0
  153. package/build/consumer/withdrawal/types.d.ts +36 -0
  154. package/build/consumer/withdrawal/types.d.ts.map +1 -0
  155. package/build/consumer/withdrawal/types.js +2 -0
  156. package/build/consumer/withdrawal/types.js.map +1 -0
  157. package/build/consumer/withdrawal.d.ts +6 -0
  158. package/build/consumer/withdrawal.d.ts.map +1 -0
  159. package/build/consumer/withdrawal.js +237 -0
  160. package/build/consumer/withdrawal.js.map +1 -0
  161. package/build/entitlement.d.ts +4 -0
  162. package/build/entitlement.d.ts.map +1 -0
  163. package/build/entitlement.js +72 -0
  164. package/build/entitlement.js.map +1 -0
  165. package/build/entrypoints.d.ts +19 -2
  166. package/build/entrypoints.d.ts.map +1 -1
  167. package/build/entrypoints.js +16 -4
  168. package/build/entrypoints.js.map +1 -1
  169. package/build/gate/request.d.ts +4 -0
  170. package/build/gate/request.d.ts.map +1 -0
  171. package/build/gate/request.js +17 -0
  172. package/build/gate/request.js.map +1 -0
  173. package/build/gate/types.d.ts +15 -0
  174. package/build/gate/types.d.ts.map +1 -0
  175. package/build/gate/types.js +2 -0
  176. package/build/gate/types.js.map +1 -0
  177. package/build/gate.d.ts +13 -9
  178. package/build/gate.d.ts.map +1 -1
  179. package/build/gate.js +36 -21
  180. package/build/gate.js.map +1 -1
  181. package/build/index.d.ts +50 -3
  182. package/build/index.d.ts.map +1 -1
  183. package/build/index.js +29 -3
  184. package/build/index.js.map +1 -1
  185. package/build/limit.d.ts +11 -0
  186. package/build/limit.d.ts.map +1 -0
  187. package/build/limit.js +48 -0
  188. package/build/limit.js.map +1 -0
  189. package/build/log.d.ts +3 -0
  190. package/build/log.d.ts.map +1 -0
  191. package/build/log.js +4 -0
  192. package/build/log.js.map +1 -0
  193. package/build/models/declarations/types.d.ts +22 -0
  194. package/build/models/declarations/types.d.ts.map +1 -0
  195. package/build/models/declarations/types.js +2 -0
  196. package/build/models/declarations/types.js.map +1 -0
  197. package/build/models/declarations.d.ts +4 -0
  198. package/build/models/declarations.d.ts.map +1 -0
  199. package/build/models/declarations.js +26 -0
  200. package/build/models/declarations.js.map +1 -0
  201. package/build/models/purchase/types.d.ts +13 -0
  202. package/build/models/purchase/types.d.ts.map +1 -0
  203. package/build/models/purchase/types.js +2 -0
  204. package/build/models/purchase/types.js.map +1 -0
  205. package/build/models/purchase.d.ts +4 -0
  206. package/build/models/purchase.d.ts.map +1 -0
  207. package/build/models/purchase.js +37 -0
  208. package/build/models/purchase.js.map +1 -0
  209. package/build/observer.d.ts +9 -0
  210. package/build/observer.d.ts.map +1 -1
  211. package/build/observer.js +38 -12
  212. package/build/observer.js.map +1 -1
  213. package/build/plan/types.d.ts +21 -0
  214. package/build/plan/types.d.ts.map +1 -0
  215. package/build/plan/types.js +2 -0
  216. package/build/plan/types.js.map +1 -0
  217. package/build/plan.d.ts +7 -0
  218. package/build/plan.d.ts.map +1 -0
  219. package/build/plan.js +29 -0
  220. package/build/plan.js.map +1 -0
  221. package/build/plugins/checkout-plugins/types.d.ts +25 -0
  222. package/build/plugins/checkout-plugins/types.d.ts.map +1 -0
  223. package/build/plugins/checkout-plugins/types.js +2 -0
  224. package/build/plugins/checkout-plugins/types.js.map +1 -0
  225. package/build/plugins/checkout-plugins.d.ts +12 -0
  226. package/build/plugins/checkout-plugins.d.ts.map +1 -0
  227. package/build/plugins/checkout-plugins.js +72 -0
  228. package/build/plugins/checkout-plugins.js.map +1 -0
  229. package/build/plugins/checkout-policy/types.d.ts +18 -0
  230. package/build/plugins/checkout-policy/types.d.ts.map +1 -0
  231. package/build/plugins/checkout-policy/types.js +2 -0
  232. package/build/plugins/checkout-policy/types.js.map +1 -0
  233. package/build/plugins/checkout-policy.d.ts +4 -0
  234. package/build/plugins/checkout-policy.d.ts.map +1 -0
  235. package/build/plugins/checkout-policy.js +26 -0
  236. package/build/plugins/checkout-policy.js.map +1 -0
  237. package/build/plugins/checkout-registry/types.d.ts +7 -0
  238. package/build/plugins/checkout-registry/types.d.ts.map +1 -0
  239. package/build/plugins/checkout-registry/types.js +2 -0
  240. package/build/plugins/checkout-registry/types.js.map +1 -0
  241. package/build/plugins/checkout-registry.d.ts +8 -0
  242. package/build/plugins/checkout-registry.d.ts.map +1 -0
  243. package/build/plugins/checkout-registry.js +21 -0
  244. package/build/plugins/checkout-registry.js.map +1 -0
  245. package/build/plugins/consts.local.d.ts +13 -0
  246. package/build/plugins/consts.local.d.ts.map +1 -0
  247. package/build/plugins/consts.local.js +22 -0
  248. package/build/plugins/consts.local.js.map +1 -0
  249. package/build/plugins/customer-lock/types.d.ts +6 -0
  250. package/build/plugins/customer-lock/types.d.ts.map +1 -0
  251. package/build/plugins/customer-lock/types.js +2 -0
  252. package/build/plugins/customer-lock/types.js.map +1 -0
  253. package/build/plugins/customer-lock.d.ts +5 -0
  254. package/build/plugins/customer-lock.d.ts.map +1 -0
  255. package/build/plugins/customer-lock.js +22 -0
  256. package/build/plugins/customer-lock.js.map +1 -0
  257. package/build/plugins/estimate/cache.d.ts +4 -0
  258. package/build/plugins/estimate/cache.d.ts.map +1 -0
  259. package/build/plugins/estimate/cache.js +7 -0
  260. package/build/plugins/estimate/cache.js.map +1 -0
  261. package/build/plugins/estimate/types.d.ts +27 -0
  262. package/build/plugins/estimate/types.d.ts.map +1 -0
  263. package/build/plugins/estimate/types.js +2 -0
  264. package/build/plugins/estimate/types.js.map +1 -0
  265. package/build/plugins/estimate.d.ts +6 -0
  266. package/build/plugins/estimate.d.ts.map +1 -0
  267. package/build/plugins/estimate.js +271 -0
  268. package/build/plugins/estimate.js.map +1 -0
  269. package/build/plugins/events/types.d.ts +12 -0
  270. package/build/plugins/events/types.d.ts.map +1 -0
  271. package/build/plugins/events/types.js +2 -0
  272. package/build/plugins/events/types.js.map +1 -0
  273. package/build/plugins/events.d.ts +6 -13
  274. package/build/plugins/events.d.ts.map +1 -1
  275. package/build/plugins/events.js +445 -145
  276. package/build/plugins/events.js.map +1 -1
  277. package/build/plugins/fx/types.d.ts +33 -0
  278. package/build/plugins/fx/types.d.ts.map +1 -0
  279. package/build/plugins/fx/types.js +2 -0
  280. package/build/plugins/fx/types.js.map +1 -0
  281. package/build/plugins/fx.d.ts +6 -0
  282. package/build/plugins/fx.d.ts.map +1 -0
  283. package/build/plugins/fx.js +83 -0
  284. package/build/plugins/fx.js.map +1 -0
  285. package/build/plugins/portal/types.d.ts +44 -0
  286. package/build/plugins/portal/types.d.ts.map +1 -0
  287. package/build/plugins/portal/types.js +2 -0
  288. package/build/plugins/portal/types.js.map +1 -0
  289. package/build/plugins/portal.d.ts +6 -0
  290. package/build/plugins/portal.d.ts.map +1 -0
  291. package/build/plugins/portal.js +255 -0
  292. package/build/plugins/portal.js.map +1 -0
  293. package/build/plugins/refunds/types.d.ts +35 -0
  294. package/build/plugins/refunds/types.d.ts.map +1 -0
  295. package/build/plugins/refunds/types.js +2 -0
  296. package/build/plugins/refunds/types.js.map +1 -0
  297. package/build/plugins/refunds.d.ts +6 -0
  298. package/build/plugins/refunds.d.ts.map +1 -0
  299. package/build/plugins/refunds.js +81 -0
  300. package/build/plugins/refunds.js.map +1 -0
  301. package/build/plugins/session/types.d.ts +34 -0
  302. package/build/plugins/session/types.d.ts.map +1 -0
  303. package/build/plugins/session/types.js +2 -0
  304. package/build/plugins/session/types.js.map +1 -0
  305. package/build/plugins/session.d.ts +4 -0
  306. package/build/plugins/session.d.ts.map +1 -0
  307. package/build/plugins/session.js +71 -0
  308. package/build/plugins/session.js.map +1 -0
  309. package/build/plugins/status.d.ts +8 -0
  310. package/build/plugins/status.d.ts.map +1 -0
  311. package/build/plugins/status.js +21 -0
  312. package/build/plugins/status.js.map +1 -0
  313. package/build/plugins/stripe/types.d.ts +20 -0
  314. package/build/plugins/stripe/types.d.ts.map +1 -0
  315. package/build/plugins/stripe/types.js +2 -0
  316. package/build/plugins/stripe/types.js.map +1 -0
  317. package/build/plugins/stripe.d.ts +5 -13
  318. package/build/plugins/stripe.d.ts.map +1 -1
  319. package/build/plugins/stripe.js +481 -129
  320. package/build/plugins/stripe.js.map +1 -1
  321. package/build/plugins/subscriptions/types.d.ts +41 -0
  322. package/build/plugins/subscriptions/types.d.ts.map +1 -0
  323. package/build/plugins/subscriptions/types.js +2 -0
  324. package/build/plugins/subscriptions/types.js.map +1 -0
  325. package/build/plugins/subscriptions.d.ts +6 -0
  326. package/build/plugins/subscriptions.d.ts.map +1 -0
  327. package/build/plugins/subscriptions.js +169 -0
  328. package/build/plugins/subscriptions.js.map +1 -0
  329. package/build/plugins/types.d.ts +7 -0
  330. package/build/plugins/types.d.ts.map +1 -0
  331. package/build/plugins/types.js +2 -0
  332. package/build/plugins/types.js.map +1 -0
  333. package/build/plugins/types.local.d.ts +75 -0
  334. package/build/plugins/types.local.d.ts.map +1 -0
  335. package/build/plugins/types.local.js +2 -0
  336. package/build/plugins/types.local.js.map +1 -0
  337. package/build/plugins/webhook-manager/types.d.ts +62 -0
  338. package/build/plugins/webhook-manager/types.d.ts.map +1 -0
  339. package/build/plugins/webhook-manager/types.js +2 -0
  340. package/build/plugins/webhook-manager/types.js.map +1 -0
  341. package/build/plugins/webhook-manager.d.ts +8 -0
  342. package/build/plugins/webhook-manager.d.ts.map +1 -0
  343. package/build/plugins/webhook-manager.js +245 -0
  344. package/build/plugins/webhook-manager.js.map +1 -0
  345. package/build/protocol.d.ts +13 -0
  346. package/build/protocol.d.ts.map +1 -0
  347. package/build/protocol.js +17 -0
  348. package/build/protocol.js.map +1 -0
  349. package/build/reconcile/types.d.ts +17 -0
  350. package/build/reconcile/types.d.ts.map +1 -0
  351. package/build/reconcile/types.js +2 -0
  352. package/build/reconcile/types.js.map +1 -0
  353. package/build/reconcile.d.ts +12 -0
  354. package/build/reconcile.d.ts.map +1 -0
  355. package/build/reconcile.js +91 -0
  356. package/build/reconcile.js.map +1 -0
  357. package/build/resource.d.ts +12 -1
  358. package/build/resource.d.ts.map +1 -1
  359. package/build/resource.js +97 -6
  360. package/build/resource.js.map +1 -1
  361. package/build/schemas.d.ts +19 -0
  362. package/build/schemas.d.ts.map +1 -0
  363. package/build/schemas.js +244 -0
  364. package/build/schemas.js.map +1 -0
  365. package/build/service.d.ts +28 -3
  366. package/build/service.d.ts.map +1 -1
  367. package/build/service.js +140 -19
  368. package/build/service.js.map +1 -1
  369. package/build/subscription/types.d.ts +21 -0
  370. package/build/subscription/types.d.ts.map +1 -0
  371. package/build/subscription/types.js +2 -0
  372. package/build/subscription/types.js.map +1 -0
  373. package/build/subscription.d.ts +4 -0
  374. package/build/subscription.d.ts.map +1 -0
  375. package/build/subscription.js +84 -0
  376. package/build/subscription.js.map +1 -0
  377. package/build/sync/types.d.ts +25 -0
  378. package/build/sync/types.d.ts.map +1 -0
  379. package/build/sync/types.js +2 -0
  380. package/build/sync/types.js.map +1 -0
  381. package/build/sync.d.ts +4 -3
  382. package/build/sync.d.ts.map +1 -1
  383. package/build/sync.js +231 -79
  384. package/build/sync.js.map +1 -1
  385. package/build/types.d.ts +1262 -36
  386. package/build/types.d.ts.map +1 -1
  387. package/build/types.local.d.ts +22 -0
  388. package/build/types.local.d.ts.map +1 -0
  389. package/build/types.local.js +2 -0
  390. package/build/types.local.js.map +1 -0
  391. package/build/usage/types.d.ts +56 -0
  392. package/build/usage/types.d.ts.map +1 -0
  393. package/build/usage/types.js +2 -0
  394. package/build/usage/types.js.map +1 -0
  395. package/build/usage.d.ts +6 -0
  396. package/build/usage.d.ts.map +1 -0
  397. package/build/usage.js +365 -0
  398. package/build/usage.js.map +1 -0
  399. package/build/utils/types.d.ts +31 -0
  400. package/build/utils/types.d.ts.map +1 -0
  401. package/build/utils/types.js +2 -0
  402. package/build/utils/types.js.map +1 -0
  403. package/build/utils.d.ts +21 -7
  404. package/build/utils.d.ts.map +1 -1
  405. package/build/utils.js +56 -17
  406. package/build/utils.js.map +1 -1
  407. package/package.json +22 -17
  408. package/src/access/types.ts +52 -0
  409. package/src/access.ts +86 -0
  410. package/src/actions/index.ts +1 -0
  411. package/src/actions/resync-subscriptions.ts +10 -0
  412. package/src/actions/resync.ts +14 -5
  413. package/src/actions/webhook.ts +9 -4
  414. package/src/bootstrap/types.ts +16 -0
  415. package/src/bootstrap.ts +31 -0
  416. package/src/catalogue/types.ts +32 -0
  417. package/src/catalogue.ts +135 -0
  418. package/src/commit/types.ts +27 -0
  419. package/src/commit.ts +152 -0
  420. package/src/config.ts +234 -14
  421. package/src/consts.local.ts +62 -0
  422. package/src/consts.ts +100 -36
  423. package/src/consumer/cancellation/types.ts +13 -0
  424. package/src/consumer/cancellation.ts +43 -0
  425. package/src/consumer/capture/types.ts +53 -0
  426. package/src/consumer/capture.ts +322 -0
  427. package/src/consumer/consts.local.ts +18 -0
  428. package/src/consumer/format/types.ts +29 -0
  429. package/src/consumer/format.ts +85 -0
  430. package/src/consumer/handlers.ts +204 -0
  431. package/src/consumer/index.ts +9 -0
  432. package/src/consumer/mail/types.ts +31 -0
  433. package/src/consumer/mail.ts +353 -0
  434. package/src/consumer/observers/types.ts +22 -0
  435. package/src/consumer/observers.ts +97 -0
  436. package/src/consumer/origin/types.ts +17 -0
  437. package/src/consumer/origin.ts +61 -0
  438. package/src/consumer/reconcile/types.ts +13 -0
  439. package/src/consumer/reconcile.ts +331 -0
  440. package/src/consumer/records/types.ts +64 -0
  441. package/src/consumer/records.ts +288 -0
  442. package/src/consumer/service.ts +768 -0
  443. package/src/consumer/types.local.ts +22 -0
  444. package/src/consumer/types.ts +96 -0
  445. package/src/consumer/withdrawal/types.ts +37 -0
  446. package/src/consumer/withdrawal.ts +268 -0
  447. package/src/entitlement.ts +85 -0
  448. package/src/entrypoints.ts +17 -4
  449. package/src/gate/request.ts +24 -0
  450. package/src/gate/types.ts +13 -0
  451. package/src/gate.ts +47 -30
  452. package/src/index.ts +55 -3
  453. package/src/limit.ts +54 -0
  454. package/src/log.ts +4 -0
  455. package/src/models/declarations/types.ts +22 -0
  456. package/src/models/declarations.ts +30 -0
  457. package/src/models/purchase/types.ts +13 -0
  458. package/src/models/purchase.ts +43 -0
  459. package/src/observer.ts +44 -11
  460. package/src/plan/types.ts +21 -0
  461. package/src/plan.ts +46 -0
  462. package/src/plugins/checkout-plugins/types.ts +25 -0
  463. package/src/plugins/checkout-plugins.ts +90 -0
  464. package/src/plugins/checkout-policy/types.ts +18 -0
  465. package/src/plugins/checkout-policy.ts +34 -0
  466. package/src/plugins/checkout-registry/types.ts +7 -0
  467. package/src/plugins/checkout-registry.ts +23 -0
  468. package/src/plugins/consts.local.ts +30 -0
  469. package/src/plugins/customer-lock/types.ts +5 -0
  470. package/src/plugins/customer-lock.ts +25 -0
  471. package/src/plugins/estimate/cache.ts +12 -0
  472. package/src/plugins/estimate/types.ts +28 -0
  473. package/src/plugins/estimate.ts +305 -0
  474. package/src/plugins/events/types.ts +11 -0
  475. package/src/plugins/events.ts +470 -133
  476. package/src/plugins/fx/types.ts +40 -0
  477. package/src/plugins/fx.ts +101 -0
  478. package/src/plugins/portal/types.ts +45 -0
  479. package/src/plugins/portal.ts +284 -0
  480. package/src/plugins/refunds/types.ts +31 -0
  481. package/src/plugins/refunds.ts +95 -0
  482. package/src/plugins/session/types.ts +30 -0
  483. package/src/plugins/session.ts +86 -0
  484. package/src/plugins/status.ts +22 -0
  485. package/src/plugins/stripe/types.ts +20 -0
  486. package/src/plugins/stripe.ts +523 -135
  487. package/src/plugins/subscriptions/types.ts +39 -0
  488. package/src/plugins/subscriptions.ts +195 -0
  489. package/src/plugins/types.local.ts +61 -0
  490. package/src/plugins/types.ts +7 -0
  491. package/src/plugins/webhook-manager/types.ts +63 -0
  492. package/src/plugins/webhook-manager.ts +288 -0
  493. package/src/protocol.ts +29 -0
  494. package/src/reconcile/types.ts +17 -0
  495. package/src/reconcile.ts +110 -0
  496. package/src/resource.ts +158 -11
  497. package/src/schemas.ts +271 -0
  498. package/src/service.ts +161 -19
  499. package/src/subscription/types.ts +23 -0
  500. package/src/subscription.ts +104 -0
  501. package/src/sync/types.ts +25 -0
  502. package/src/sync.ts +255 -72
  503. package/src/types.local.ts +18 -0
  504. package/src/types.ts +1369 -32
  505. package/src/usage/types.ts +51 -0
  506. package/src/usage.ts +432 -0
  507. package/src/utils/types.ts +30 -0
  508. package/src/utils.ts +94 -29
  509. package/tests/checkout-consumer.spec.ts +366 -0
  510. package/tests/checkout-locks.spec.ts +161 -0
  511. package/tests/checkout-plugins.spec.ts +164 -0
  512. package/tests/checkout.spec.ts +271 -93
  513. package/tests/consumer-events.spec.ts +215 -0
  514. package/tests/consumer-fixtures.ts +141 -0
  515. package/tests/consumer-ops.spec.ts +361 -0
  516. package/tests/consumer-rights.integration.spec.ts +147 -0
  517. package/tests/consumer-rights.spec.ts +606 -0
  518. package/tests/context.ts +110 -0
  519. package/tests/entitlement.spec.ts +139 -0
  520. package/tests/estimate.spec.ts +350 -0
  521. package/tests/events.spec.ts +367 -0
  522. package/tests/fake-stripe.ts +987 -0
  523. package/tests/gate-handlers.spec.ts +120 -0
  524. package/tests/gate.spec.ts +63 -72
  525. package/tests/gateway-options.spec.ts +180 -0
  526. package/tests/limit-gate.spec.ts +66 -0
  527. package/tests/portal.spec.ts +202 -0
  528. package/tests/protocol.spec.ts +17 -3
  529. package/tests/sync.spec.ts +114 -0
  530. package/tests/usage.integration.spec.ts +133 -0
  531. package/tests/usage.spec.ts +169 -0
  532. package/tests/webhook-manager.spec.ts +149 -0
  533. package/build/model.d.ts +0 -6
  534. package/build/model.d.ts.map +0 -1
  535. package/build/model.js +0 -33
  536. package/build/model.js.map +0 -1
  537. package/src/model.ts +0 -37
package/src/types.ts CHANGED
@@ -1,17 +1,31 @@
1
+ import type Stripe from 'stripe'
1
2
  import type { InitializedService, LazyService } from '@owlmeans/context'
2
3
  import type { ResourceRecord } from '@owlmeans/resource'
3
4
  import type { MongoResource } from '@owlmeans/mongo-resource'
4
5
  import type { PluginConfig } from '@owlmeans/config'
5
6
  import type { PermissionSet } from '@owlmeans/auth'
7
+ import type { AbstractRequest } from '@owlmeans/entrypoint'
8
+ import type { MailMessage } from '@owlmeans/mailer'
6
9
  import type { Config as ApiConfig, Context as ApiContext } from '@owlmeans/server-api'
7
10
  import type {
8
- AmountCheckoutPolicy, CheckoutPricingMode, LimitConfig, PlanDuration, Product, ProductPlan,
9
- ProductType, QuantityCheckoutPolicy, SubscriptionStatus,
11
+ AmountCheckoutPolicy, AmountNarrowing, AmountPolicyView, BillingProfileView, CancellationBody,
12
+ CancellationKind, CancellationReceipt, CancellationStatus, CheckoutPricingMode, ConsentKind, ConsumerRegion,
13
+ ConsumerRightsDeclaration, ConsumerRightsLinks, ConsumerRightsPolicy, DeclarationChannel, DeclarationKind,
14
+ EntitlementView, LimitDeclaration, LimitView, PerformanceConsentBody, PerformanceConsentResponse,
15
+ PerformanceConsentView, PlanCapability, PlanDuration, PlanPriceView, PlanWithdrawalComponent, PortalFlow,
16
+ PriceEstimate, PricingPolicy, Product, ProductPlan, ProductType, PurchaseKind, PurchaseView,
17
+ QuantityCheckoutPolicy, SubscriptionStartBody, SubscriptionStartResponse, SubscriptionStartView,
18
+ SubscriptionStatus, WithdrawalBody, WithdrawalCandidateList, WithdrawalReceipt, WithdrawalStatus,
10
19
  } from '@owlmeans/payment'
11
20
 
21
+
12
22
  export interface Config extends ApiConfig {}
13
23
  export interface Context<C extends Config = Config> extends ApiContext<C> {}
14
24
 
25
+ // ---------------------------------------------------------------------------------------------
26
+ // Catalogue
27
+ // ---------------------------------------------------------------------------------------------
28
+
15
29
  export interface PaymentProduct extends Product {
16
30
  taxCode?: string
17
31
  unitLabel?: string
@@ -25,6 +39,11 @@ export interface PaymentPlan extends ProductPlan {
25
39
  pricingMode?: CheckoutPricingMode
26
40
  amountPolicy?: AmountCheckoutPolicy
27
41
  quantityPolicy?: QuantityCheckoutPolicy
42
+ /**
43
+ * Exact prices in further currencies, major units by lowercase ISO 4217 code (`{ usd: 20 }`).
44
+ * Synced as the reusable Price's `currency_options`, never converted.
45
+ */
46
+ currencyPrices?: Record<string, number>
28
47
  }
29
48
 
30
49
  export interface PaymentProductDef {
@@ -44,14 +63,20 @@ export interface PaymentPlanDef {
44
63
  productSku: string
45
64
  sku: string
46
65
  duration: PlanDuration
66
+ /** Position among the plans of one product; a higher rank is an upgrade. Absent reads as `0`. */
67
+ rank?: number
68
+ /** Held without paying: `price: 0` and no gateway. */
69
+ free?: boolean
70
+ /** Paygates the plan is sold through. Absent: the product's gateways. A free plan has none. */
71
+ gateways?: string[]
47
72
  /** Reference unit price in major units; amount checkout uses it only for fulfillment conversion. */
48
73
  price: number
49
74
  currency?: string
50
75
  recurring?: { interval: 'month' | 'year' }
51
76
  order?: number
52
77
  title?: string
53
- capabilities?: PermissionSet[]
54
- limits?: { [key: string]: LimitConfig }
78
+ capabilities?: PlanCapability[]
79
+ limits?: { [key: string]: LimitDeclaration }
55
80
  pricingMode?: CheckoutPricingMode
56
81
  amountPolicy?: AmountCheckoutPolicy
57
82
  quantityPolicy?: QuantityCheckoutPolicy
@@ -59,9 +84,116 @@ export interface PaymentPlanDef {
59
84
  minQuantity?: number
60
85
  maxQuantity?: number
61
86
  defaultQuantity?: number
87
+ /**
88
+ * Exact prices in further currencies, major units by ISO 4217 code (`{ usd: 20 }`): a recurring
89
+ * or quantity plan's synced Price carries each as a `currency_options` entry at exactly this
90
+ * amount, while its default currency (the settlement currency) is converted as before.
91
+ */
92
+ currencyPrices?: Record<string, number>
93
+ /**
94
+ * The separately priced parts of a subscription for a withdrawal (CJEU C-641/19): their
95
+ * `shareMinor` sum to `round(price × 100)`. Absent: the whole price is one `time` component.
96
+ */
97
+ withdrawal?: { components: PlanWithdrawalComponent[] }
98
+ }
99
+
100
+ /** File paths (or values) of the Stripe secrets. The webhook secret is an optional override. */
101
+ export interface StripeSecretsDef { api: string; webhook?: string }
102
+ export interface StripePluginConfig extends PluginConfig { api: string; webhook?: string }
103
+
104
+ /** Stripe-only pricing settings — never a browser-visible field (see `PricingPolicy` for those). */
105
+ export interface StripePricingDef {
106
+ /** Enable promotion-code entry on amount checkouts; absent/false keeps it disabled. */
107
+ amountPromotionCodes?: boolean
108
+ /** Overrides `STRIPE_FX_QUOTES_API_VERSION`, when Stripe moves or renames the preview. */
109
+ fxApiVersion?: string
110
+ /**
111
+ * Stripe settlement currency used when a catalogue price is declared in another currency.
112
+ * Recurring prices are converted during product sync; amount checkout is converted per session.
113
+ */
114
+ settlementCurrency?: string
115
+ /** Explicit Stripe payment methods for subscription Checkout; absent keeps Stripe's dynamic selection. */
116
+ subscriptionPaymentMethodTypes?: string[]
117
+ /** Payment methods for a specific subscription charge currency; overrides the default list. */
118
+ subscriptionPaymentMethodTypesByCurrency?: Record<string, string[]>
119
+ /**
120
+ * Let a matching `unspecified` price take the declared `tax.behavior` even when the Stripe
121
+ * account's own tax-settings default resolves to the opposite one — which changes what an
122
+ * existing subscriber is charged at their next renewal. Absent/`false`: such a price is left
123
+ * `unspecified` and a `log.error` explains why.
124
+ */
125
+ migrateUnspecifiedPrices?: boolean
126
+ /**
127
+ * The Stripe customer's e-mail is the application's: every checkout must pass
128
+ * `CreateLinkParams.email` (else `PaygateError('customer-email')`), which is written to the
129
+ * customer before the session — Checkout shows a customer's valid e-mail read-only, so no other
130
+ * address can be typed — and the portal configuration never offers to edit it.
131
+ */
132
+ lockCustomerEmail?: boolean
133
+ /**
134
+ * The billing country a checkout knows — the locked profile's, else `CreateLinkParams.country` —
135
+ * is pinned on the Stripe customer (replacing a saved address of another country before any lock)
136
+ * and Checkout never collects another (`customer_update.address: 'never'`, collection `'auto'`):
137
+ * tax is calculated on it whatever the card form's "Country or region" says, the first completed
138
+ * purchase locks it, and the portal never offers to edit the address. Where Stripe Tax needs more
139
+ * than a country (US: a postal code; CA, IN: a postal code or province) and the customer carries
140
+ * none, Checkout still collects the address — Stripe refuses a session otherwise.
141
+ */
142
+ lockCustomerCountry?: boolean
143
+ }
144
+ export interface StripePricingPluginConfig extends PluginConfig, StripePricingDef {}
145
+
146
+ /** `declarePaymentPricing`'s argument: the browser-safe `PricingPolicy` plus Stripe-only settings. */
147
+ export interface PricingDef extends PricingPolicy {
148
+ stripe?: StripePricingDef
149
+ }
150
+
151
+ /**
152
+ * The business the consumer contracts with — backend only, never advertised. `name` is what the
153
+ * express statements say (`{{trader}}` in a consent or start request); the mails' identity line,
154
+ * the withdrawal information and the model form ("To: …") say `legalName`, `address` and `email`.
155
+ */
156
+ // A type alias, not an interface: it is stored inside a plugin config record, whose values must be
157
+ // index-signature compatible.
158
+ // Kept as a type: it is stored in config values that need an implicit index signature.
159
+ export type TraderDef = {
160
+ name: string
161
+ legalName: string
162
+ /** Postal address; without it (or `email`) the mails render without it and the boot warns. */
163
+ address?: string
164
+ email?: string
165
+ website?: string
166
+ }
167
+
168
+ /** The consumer-rights mail options — backend only, never advertised. */
169
+ export interface ConsumerMailDef {
170
+ /** The mailer service alias. Default `MAILER_SERVICE`. */
171
+ alias?: string
172
+ from?: string
173
+ replyTo?: string
174
+ /** Evidence archive addresses: each receives its own copy of every consumer-rights mail. */
175
+ bcc?: string[]
176
+ }
177
+ export interface ConsumerMailPluginConfig extends PluginConfig, ConsumerMailDef {
178
+ trader?: TraderDef
179
+ }
180
+
181
+ /** `declareConsumerRights`'s argument: the advertised policy plus the backend-only trader and mail options. */
182
+ export interface ConsumerRightsDef extends ConsumerRightsDeclaration { trader?: TraderDef, mail?: ConsumerMailDef }
183
+
184
+ /** What the managed customer portal configuration shows. */
185
+ export interface PortalBrandingDef {
186
+ headline?: string
187
+ privacyPolicyUrl?: string
188
+ termsOfServiceUrl?: string
189
+ /** Where the portal's "return" link leads when a session names none. Absolute URL. */
190
+ returnUrl: string
62
191
  }
192
+ export interface PortalBrandingConfig extends PluginConfig, PortalBrandingDef {}
63
193
 
64
- export interface StripePluginConfig extends PluginConfig { api: string; webhook: string }
194
+ // ---------------------------------------------------------------------------------------------
195
+ // Gateway
196
+ // ---------------------------------------------------------------------------------------------
65
197
 
66
198
  export interface CreateLinkParams {
67
199
  productSku: string
@@ -71,21 +203,235 @@ export interface CreateLinkParams {
71
203
  service: string
72
204
  planSku?: string
73
205
  amountMinor?: number
206
+ /** Stripe Checkout and future paid-invoice language. Caller supplies a Stripe-supported locale. */
207
+ locale?: string
208
+ /**
209
+ * Trusted application copy shown beside the Checkout confirmation button — never caller-provided
210
+ * text. A function receives the charge currency and the price the session charges.
211
+ */
212
+ submitText?: string | ((context: CheckoutTextContext) => string)
74
213
  successUrl?: string
75
214
  cancelUrl?: string
215
+ /** The billing country the buyer declared (ISO 3166-1 alpha-2). A locked profile overrides it. */
216
+ country?: string
217
+ /** The subscription start request recorded right before a subscription checkout. */
218
+ startRequestId?: string
219
+ /** The language the buyer is shown legal copy in; default: the billing country's. */
220
+ consumerLanguage?: string
221
+ /** The request's geolocated country (`cf-ipcountry`) — second location evidence, stored at lock. */
222
+ ipCountry?: string
223
+ /**
224
+ * The buyer's e-mail as the application verified it (the signed-in person's account). Read only
225
+ * under `stripe.lockCustomerEmail`, where it is required and pinned on the Stripe customer.
226
+ */
227
+ email?: string
76
228
  }
77
229
 
78
- export interface PaymentPlugin {
79
- createLink: (ctx: ApiContext, params: CreateLinkParams) => Promise<string>
80
- handleWebhook: (request: unknown, ctx: ApiContext) => Promise<void>
81
- initialize?: (ctx: ApiContext) => Promise<void>
82
- manageSubscription: (ctx: ApiContext, entityId: string, returnUrl: string) => Promise<string>
230
+ /** What a submit-text function is told about the session it labels. */
231
+ export interface CheckoutTextContext {
232
+ language: string
233
+ /** The charge currency, lowercase. */
234
+ currency: string
235
+ /** A subscription's unit price in the charge currency; an amount checkout's pre-tax charge. */
236
+ unitAmountMinor?: number
237
+ interval?: 'month' | 'year'
238
+ region: ConsumerRegion | null
239
+ country?: string
240
+ }
241
+
242
+ // ---------------------------------------------------------------------------------------------
243
+ // Checkout plugins
244
+ // ---------------------------------------------------------------------------------------------
245
+
246
+ /** What a plugin narrows: one entity's amount checkout of one plan, at one instant. */
247
+ export interface CheckoutNarrowInput {
248
+ entityId: string
249
+ productSku: string
250
+ planSku?: string
251
+ /** The plan's own amount policy — the same for everyone. */
252
+ base: AmountCheckoutPolicy
253
+ at: Date
254
+ }
255
+
256
+ /** A checkout about to be created — what `admit` may veto. */
257
+ export interface CheckoutAttempt {
258
+ entityId: string
259
+ productSku: string
260
+ planSku?: string
261
+ mode: 'amount' | 'quantity' | 'subscription'
262
+ /** Amount mode: the net value bought, in the amount policy's currency. */
263
+ amountMinor?: number
264
+ /** The amount policy's currency (amount mode), else the charge currency. */
265
+ amountCurrency?: string
266
+ /** Amount mode: the pre-tax charge in `currency`. */
267
+ chargeMinor?: number
268
+ currency: string
269
+ /** The session lifetime the gateway will set; absent: Stripe's default (24 h). */
270
+ sessionTtlSeconds?: number
271
+ expiresAt?: Date
272
+ at: Date
273
+ }
274
+
275
+ /** A plugin's admission: an optional reservation it binds to the session in `created`. */
276
+ export interface CheckoutAdmission {
277
+ reservationId?: string
83
278
  }
279
+
280
+ /** The session a checkout became. */
281
+ export interface CheckoutCreated extends CheckoutAttempt {
282
+ sessionId: string
283
+ url: string
284
+ /** What this plugin's own `admit` answered. */
285
+ reservationId?: string
286
+ }
287
+
288
+ export type CheckoutOutcome = 'paid' | 'expired' | 'failed'
289
+
290
+ /**
291
+ * How a checkout ended: `paid` (completed and paid), `expired`, or `failed` — an asynchronous
292
+ * payment failure, or a session that never became usable (a later plugin vetoed, Stripe refused,
293
+ * a `created` threw; then `sessionId` may be absent and `reservationId` names the hold to release).
294
+ */
295
+ export interface CheckoutSettled {
296
+ entityId: string
297
+ productSku?: string
298
+ planSku?: string
299
+ sessionId?: string
300
+ reservationId?: string
301
+ outcome: CheckoutOutcome
302
+ amountMinor?: number
303
+ at: Date
304
+ }
305
+
306
+ /**
307
+ * A seam around amount and subscription checkout, registered with
308
+ * `paymentAccessOf(ctx).gateway().use(plugin)` (a plugin with an `alias` registered twice replaces
309
+ * the first). Errors from `narrow` and `admit` propagate — a plugin fails closed; `settled` errors
310
+ * are logged, since holds carry their own TTL.
311
+ */
312
+ export interface CheckoutPlugin {
313
+ alias?: string
314
+ /** Lower what this entity may buy now; `null` for no opinion. */
315
+ narrow?: (ctx: ApiContext, input: CheckoutNarrowInput) => Promise<AmountNarrowing | null>
316
+ /** Veto (throw, e.g. `CheckoutLimitExceeded`) or hold a reservation for the session. */
317
+ admit?: (ctx: ApiContext, attempt: CheckoutAttempt) => Promise<CheckoutAdmission | void>
318
+ /** The session exists — bind the reservation. A throw expires the session and rethrows. */
319
+ created?: (ctx: ApiContext, created: CheckoutCreated) => Promise<void>
320
+ /** From the webhook (paid, expired, failed) or from a checkout that never became usable. */
321
+ settled?: (ctx: ApiContext, settled: CheckoutSettled) => Promise<void>
322
+ /** The longest a session may stay open; the gateway uses the smallest, clamped to 30 min–24 h. */
323
+ sessionTtlSeconds?: number
324
+ }
325
+
326
+ export interface PortalLinkOptions {
327
+ flow: PortalFlow
328
+ /** The target plan of `PortalFlow.Change`. */
329
+ planSku?: string
330
+ returnUrl: string
331
+ }
332
+
333
+ export interface PriceEstimateParams {
334
+ productSku: string
335
+ /** Stable database key resolved by the application before this in-process call. */
336
+ entityId: string
337
+ /** Absent: the product's own reference plan (an amount-priced consumable). */
338
+ planSku?: string
339
+ /** ISO 3166-1 alpha-2. Absent: taken from the entity's paygate customer address. */
340
+ country?: string
341
+ }
342
+
343
+ export interface GrantInternalPlanOptions {
344
+ /** Required to grant a plan that is not `free`. */
345
+ force?: boolean
346
+ /** When the grant stops entitling. Absent: never. */
347
+ periodEnd?: Date
348
+ }
349
+
350
+ export interface SubscriptionRef {
351
+ entityId?: string
352
+ /** The paygate subscription id (`sub_…`). */
353
+ subscriptionId?: string
354
+ }
355
+
84
356
  export interface GatewayService extends InitializedService {
85
- createLink: PaymentPlugin['createLink']
86
- manageSubscription: PaymentPlugin['manageSubscription']
357
+ /** `false` when registered with `manage: false`: every Stripe-calling method throws. */
358
+ managed: boolean
359
+ /** Whether the boot-time `bootstrapStripe` runs: the `bootstrap` option, else `managed`. */
360
+ bootstrap: boolean
361
+ /** The alias whose host and base form the webhook URL: the `webhookService` option, else `cfg.service`. */
362
+ readonly webhookService: string
363
+ /** The key of this deployment's Stripe rows: the `owner` option, else `cfg.service`. */
364
+ readonly owner: string
365
+ /** The Stripe client every paygate call of this gateway uses: the `stripe` option, else the configured secret. */
366
+ stripe: StripeFactory
367
+ createLink: (ctx: ApiContext, params: CreateLinkParams) => Promise<string>
368
+ portalLink: (ctx: ApiContext, entityId: string, opts: PortalLinkOptions) => Promise<string>
369
+ /** @throws PaygateError('unmanaged') when `managed` is `false`. */
370
+ estimatePrice: (ctx: ApiContext, params: PriceEstimateParams) => Promise<PriceEstimate>
371
+ grantInternalPlan: (
372
+ ctx: ApiContext, entityId: string, planSku: string, opts?: GrantInternalPlanOptions,
373
+ ) => Promise<PaymentSubscriptionRecord>
374
+ /** @returns how many subscription rows changed */
375
+ resyncSubscription: (ctx: ApiContext, ref: SubscriptionRef) => Promise<number>
376
+ resyncAll: (ctx: ApiContext) => Promise<{ scanned: number; updated: number }>
377
+ /** Register a checkout plugin; one with an `alias` already registered replaces it. */
378
+ use: (plugin: CheckoutPlugin) => void
379
+ /** The registered checkout plugins, in registration order. */
380
+ checkoutPlugins: () => readonly CheckoutPlugin[]
381
+ /**
382
+ * The entity's amount policy of an amount plan as narrowed NOW — the very computation
383
+ * `createLink` enforces. Mongo and plugins only; works unmanaged.
384
+ */
385
+ amountPolicy: (ctx: ApiContext, entityId: string, productSku: string, planSku?: string) => Promise<AmountPolicyView>
386
+ /** The synced prices of a product's plans, per currency — stored rows, no Stripe call. */
387
+ planPrices: (ctx: ApiContext, productSku: string) => Promise<PlanPriceView[]>
87
388
  }
88
- export interface PaymentResourceOptions { dbAlias?: string; serviceAlias?: string }
389
+
390
+ export interface PaymentGatewayOptions {
391
+ dbAlias?: string
392
+ serviceAlias?: string
393
+ /**
394
+ * `false` for a process that must read entitlements but never talk to Stripe: no Stripe
395
+ * bootstrap at init, and `createLink` / `portalLink` / `resync*` / the webhook route throw
396
+ * `PaygateError('unmanaged')`.
397
+ */
398
+ manage?: boolean
399
+ /**
400
+ * Whether this process brings Stripe to the declared state at boot (`bootstrapStripe`: products,
401
+ * portal configuration, webhook endpoint). Default: `manage`. A managed process with `false`
402
+ * still serves checkout, the portal, estimates and the resyncs, and still runs a FORCED
403
+ * bootstrap (`resync`, an application's `stripeBootstrapOf(ctx).bootstrapStripe(stripe, { force: true })`).
404
+ * `true` on an unmanaged gateway is refused at construction.
405
+ */
406
+ bootstrap?: boolean
407
+ /**
408
+ * The alias (in `cfg.services`) whose host and base form the webhook URL (`webhookUrlOf`) — also
409
+ * the portal configuration's deployment key. Default: `cfg.service`. Name the process that
410
+ * RECEIVES the webhook, in every process of the deployment, so every bootstrap computes one URL.
411
+ * A managed, bootstrapping gateway refuses to initialize when the alias is not declared.
412
+ */
413
+ webhookService?: string
414
+ /**
415
+ * The key this deployment's Stripe rows are stored under: the `payment-webhook` rows' `service`,
416
+ * the signing-secret lookup, the endpoint's `owlmeans:<owner>` description and `service`
417
+ * metadata, the `portal:<owner>` fingerprint and the portal configuration's `service` metadata.
418
+ * Default: `cfg.service`. Give every process sharing the database the same value, so they read
419
+ * and replace one another's rows.
420
+ */
421
+ owner?: string
422
+ /** The Stripe client for this gateway's paygate calls. Default: the configured secret (`stripeClient`). */
423
+ stripe?: StripeFactory
424
+ }
425
+
426
+ /** A Stripe client for one context — the default reads the configured secret. */
427
+ export interface StripeFactory { (ctx: ApiContext): Promise<Stripe> }
428
+
429
+ /** @deprecated use `PaymentGatewayOptions` */
430
+ export type PaymentResourceOptions = PaymentGatewayOptions
431
+
432
+ // ---------------------------------------------------------------------------------------------
433
+ // Observer
434
+ // ---------------------------------------------------------------------------------------------
89
435
 
90
436
  interface TopUpBase {
91
437
  entityId: string
@@ -93,6 +439,7 @@ interface TopUpBase {
93
439
  planSku?: string
94
440
  service: string
95
441
  paygate: string
442
+ /** The checkout session id — the consumer's idempotency key (`payment:<externalId>`). */
96
443
  externalId: string
97
444
  }
98
445
  export interface QuantityTopUpCompletion extends TopUpBase {
@@ -101,57 +448,1047 @@ export interface QuantityTopUpCompletion extends TopUpBase {
101
448
  }
102
449
  export interface AmountTopUpCompletion extends TopUpBase {
103
450
  mode: 'amount'
451
+ /** Net value in the amount policy's catalogue currency. */
104
452
  amountMinor: number
453
+ /** Grossed-up value in the amount policy's catalogue currency, before settlement conversion. */
454
+ sourceChargeAmountMinor: number
455
+ /** The amount policy's catalogue currency. */
456
+ amountCurrency: string
457
+ /** Pre-tax subtotal actually charged by Stripe, in `currency`. */
105
458
  chargeAmountMinor: number
459
+ /** Stripe integration/settlement currency. */
106
460
  currency: string
107
461
  }
108
462
  export type TopUpCompletion = QuantityTopUpCompletion | AmountTopUpCompletion
109
463
 
110
- export interface SubscriptionCompletion {
464
+ export type SubscriptionChange = 'created' | 'renewed' | 'upgraded' | 'downgraded' | 'cancel-scheduled'
465
+ | 'cancel-undone' | 'canceled' | 'paused' | 'resumed' | 'past-due' | 'suspended' | 'trial-ending'
466
+
467
+ export interface SubscriptionSnapshot {
111
468
  entityId: string
112
- status: SubscriptionStatus
113
- productSku: string
114
469
  planSku: string
115
- service: string
470
+ productSku: string
471
+ rank: number
472
+ status: SubscriptionStatus
116
473
  paygate: string
474
+ /** The subscription row's `externalId`. */
475
+ subscriptionId: string
476
+ service: string
477
+ periodStart?: Date
478
+ periodEnd?: Date
479
+ cancelAtPeriodEnd?: boolean
480
+ trialEnd?: Date
481
+ pausedAt?: Date
482
+ createdAt?: Date
483
+ capabilities?: PlanCapability[]
484
+ limits?: { [key: string]: LimitDeclaration }
485
+ }
486
+
487
+ export interface SubscriptionEvent {
488
+ change: SubscriptionChange
489
+ /** The state last propagated to observers; `null` for `created`. */
490
+ previous: SubscriptionSnapshot | null
491
+ current: SubscriptionSnapshot
492
+ /** `ENTITLING_STATUSES.includes(current.status)`. */
493
+ active: boolean
494
+ eventKey: string
495
+ invoiceId?: string
496
+ externalEventId?: string
497
+ }
498
+
499
+ export type PaymentTargetKind = 'fulfillment' | 'subscription'
500
+
501
+ export interface RefundEvent {
502
+ entityId: string
503
+ target: PaymentTargetKind
504
+ productSku?: string
505
+ planSku?: string
506
+ /** The target row's `externalId` (checkout session or subscription). */
117
507
  externalId: string
118
- isNew: boolean
119
- capabilities?: PermissionSet[]
120
- limits?: { [key: string]: LimitConfig }
508
+ refundId: string
509
+ paymentIntentId?: string
510
+ invoiceId?: string
511
+ /** This refund. */
512
+ amountMinor: number
513
+ /** Everything refunded on the charge so far, this refund included. */
514
+ refundedTotalMinor: number
515
+ /** What the customer paid on the refunded charge, tax included. */
516
+ paidMinor?: number
517
+ currency: string
518
+ /** Less than the whole charge has been refunded. */
519
+ partial: boolean
520
+ eventKey: string
521
+ /** Fulfillment targets: the net value credited at checkout. */
522
+ netAmountMinor?: number
523
+ /** Fulfillment targets: the pre-tax subtotal charged at checkout. */
524
+ chargeAmountMinor?: number
525
+ /** The refund's own metadata. */
526
+ metadata?: Record<string, string>
527
+ /**
528
+ * Set when the refund executes a withdrawal (`refund.metadata.withdrawalId`): the `onWithdrawal`
529
+ * observer takes back exactly the unused units — an `onRefund` observer must skip its own
530
+ * claw-back for such a refund, or the units are taken back twice.
531
+ */
532
+ withdrawalId?: string
533
+ }
534
+
535
+ export type DisputePhase = 'opened' | 'funds-withdrawn' | 'funds-reinstated' | 'closed'
536
+
537
+ export interface DisputeEvent {
538
+ entityId: string
539
+ target: PaymentTargetKind
540
+ productSku?: string
541
+ planSku?: string
542
+ externalId: string
543
+ disputeId: string
544
+ phase: DisputePhase
545
+ /** The paygate's dispute status (`needs_response`, `won`, `lost`, …). */
546
+ status: string
547
+ amountMinor: number
548
+ /** What the customer paid on the disputed charge, tax included, in the dispute currency. */
549
+ paidMinor?: number
550
+ currency: string
551
+ eventKey: string
552
+ netAmountMinor?: number
553
+ chargeAmountMinor?: number
554
+ }
555
+
556
+ export interface PaymentFailedEvent {
557
+ entityId: string
558
+ kind: 'checkout' | 'invoice'
559
+ /** The checkout session or invoice id. */
560
+ externalId: string
561
+ subscriptionId?: string
562
+ invoiceId?: string
563
+ attempt?: number
564
+ actionRequired?: boolean
565
+ nextAttemptAt?: Date
566
+ eventKey: string
567
+ }
568
+
569
+ /** A consumer's express request was recorded (performance consent or subscription start). */
570
+ export interface ConsentEvent {
571
+ kind: ConsentKind
572
+ consentId: string
573
+ entityId: string
574
+ profileId?: string
575
+ /** The purchases the consent covers (none for a start request, which precedes its purchase). */
576
+ purchaseIds: string[]
577
+ planSku?: string
578
+ textVersion: string
579
+ language: string
580
+ decidedAt: Date
581
+ expiresAt?: Date
582
+ /** `consent:<consentId>`. */
583
+ eventKey: string
584
+ }
585
+
586
+ /** A withdrawal executed (`refunded`) or left to an operator (`review`). */
587
+ export interface WithdrawalEvent {
588
+ withdrawalId: string
589
+ /** `withdrawal:<withdrawalId>`. */
590
+ eventKey: string
591
+ entityId: string
592
+ channel: DeclarationChannel
593
+ purchase: PurchaseRef
594
+ declaredAt: Date
595
+ status: WithdrawalStatus
596
+ refund: {
597
+ amountMinor: number
598
+ currency: string
599
+ netMinor?: number
600
+ refundId?: string
601
+ creditNoteId?: string
602
+ }
603
+ /**
604
+ * The purchase's units as the usage meter read them: `returned` is what the application takes
605
+ * back (`granted − used`), `null` without a meter.
606
+ */
607
+ units: { granted: number, used: number, returned: number } | null
608
+ subscriptionCanceled: boolean
609
+ }
610
+
611
+ /** A cancellation was declared (matched to a subscription or not). */
612
+ export interface CancellationEvent {
613
+ cancellationId: string
614
+ /** `cancellation:<cancellationId>`. */
615
+ eventKey: string
616
+ entityId?: string
617
+ matched: boolean
618
+ channel: DeclarationChannel
619
+ kind: CancellationKind
620
+ status: CancellationStatus
621
+ subscriptionId?: string
622
+ effectiveAt?: Date
623
+ declaredAt: Date
121
624
  }
625
+
122
626
  export interface TopUpCallback { (completion: TopUpCompletion, ctx: ApiContext): Promise<void> }
123
- export interface SubscriptionCallback { (completion: SubscriptionCompletion, ctx: ApiContext): Promise<void> }
627
+ export interface SubscriptionCallback { (event: SubscriptionEvent, ctx: ApiContext): Promise<void> }
628
+ export interface RefundCallback { (event: RefundEvent, ctx: ApiContext): Promise<void> }
629
+ export interface DisputeCallback { (event: DisputeEvent, ctx: ApiContext): Promise<void> }
630
+ export interface PaymentFailedCallback { (event: PaymentFailedEvent, ctx: ApiContext): Promise<void> }
631
+ export interface ConsentCallback { (event: ConsentEvent, ctx: ApiContext): Promise<void> }
632
+ export interface WithdrawalCallback { (event: WithdrawalEvent, ctx: ApiContext): Promise<void> }
633
+ export interface CancellationCallback { (event: CancellationEvent, ctx: ApiContext): Promise<void> }
634
+
635
+ /**
636
+ * Callbacks run sequentially and are awaited. A paygate callback's throw escapes so the paygate
637
+ * retries; a consumer-rights callback (`onConsent`, `onWithdrawal`, `onCancellation`) runs AFTER
638
+ * the records and the paygate steps, and its throw is recorded and retried by `reconcile()`.
639
+ */
124
640
  export interface CompletionObserver extends LazyService {
125
641
  onTopUp: (cb: TopUpCallback) => void
126
642
  onSubscription: (cb: SubscriptionCallback) => void
643
+ onRefund: (cb: RefundCallback) => void
644
+ onDispute: (cb: DisputeCallback) => void
645
+ onPaymentFailed: (cb: PaymentFailedCallback) => void
646
+ onConsent: (cb: ConsentCallback) => void
647
+ onWithdrawal: (cb: WithdrawalCallback) => void
648
+ onCancellation: (cb: CancellationCallback) => void
127
649
  propagateTopUp: (completion: TopUpCompletion, ctx: ApiContext) => Promise<void>
128
- propagateSubscription: (completion: SubscriptionCompletion, ctx: ApiContext) => Promise<void>
650
+ propagateSubscription: (event: SubscriptionEvent, ctx: ApiContext) => Promise<void>
651
+ propagateRefund: (event: RefundEvent, ctx: ApiContext) => Promise<void>
652
+ propagateDispute: (event: DisputeEvent, ctx: ApiContext) => Promise<void>
653
+ propagatePaymentFailed: (event: PaymentFailedEvent, ctx: ApiContext) => Promise<void>
654
+ propagateConsent: (event: ConsentEvent, ctx: ApiContext) => Promise<void>
655
+ propagateWithdrawal: (event: WithdrawalEvent, ctx: ApiContext) => Promise<void>
656
+ propagateCancellation: (event: CancellationEvent, ctx: ApiContext) => Promise<void>
129
657
  }
130
658
 
659
+ // ---------------------------------------------------------------------------------------------
660
+ // Records
661
+ // ---------------------------------------------------------------------------------------------
662
+
131
663
  export interface PaygateCustomerRecord extends ResourceRecord {
132
- paygate: string; externalId: string; entityId?: string; profileId?: string
133
- email?: string; name?: string; taxId?: string
664
+ paygate: string
665
+ externalId: string
666
+ entityId?: string
667
+ profileId?: string
668
+ email?: string
669
+ name?: string
670
+ taxId?: string
671
+ /** The paygate customer's billing address country, as its last webhook stated it. */
672
+ country?: string
673
+ /** The paygate customer's own currency (set by its first subscription or invoice). */
674
+ currency?: string
675
+ deletedAt?: Date
134
676
  }
135
677
  export interface PaygateCustomerResource extends MongoResource<PaygateCustomerRecord> {
136
678
  loadByPgId: (externalId: string, paygate: string) => Promise<PaygateCustomerRecord | null>
137
679
  byEntity: (entityId: string, paygate: string) => Promise<PaygateCustomerRecord | null>
138
680
  }
681
+
682
+ /** The classification inputs last propagated to observers. */
683
+ export interface PropagatedState {
684
+ planSku: string
685
+ rank: number
686
+ status: SubscriptionStatus
687
+ cancelAtPeriodEnd?: boolean
688
+ pausedAt?: Date
689
+ /** The renewal invoice last reported as `renewed` — a renewal is reported once per invoice. */
690
+ renewedInvoiceId?: string
691
+ }
692
+
693
+ /**
694
+ * What ONE subscription holds instead of its plan's static declaration. Written by an operator,
695
+ * never by the paygate: every sync and re-grant carries it over with the rest of the row.
696
+ */
697
+ export interface SubscriptionOverrides {
698
+ /** By limit key: the ceiling in force for this subscription, whatever the plan or its promo says. */
699
+ limits?: { [key: string]: { limit: number } }
700
+ }
701
+
702
+ /** One row per subscription — paygate (`sub_…`) or internal (`free:<entityId>`, `internal:<planSku>:<entityId>`). */
139
703
  export interface PaymentSubscriptionRecord extends ResourceRecord {
140
- sku: string; productSku: string; entityId: string; service: string; paygate: string
141
- externalId: string; status: SubscriptionStatus; kind: 'consumable' | 'subscription'
142
- capabilities?: PermissionSet[]; limits?: { [key: string]: LimitConfig }
143
- pricingMode?: CheckoutPricingMode; units?: number; amountMinor?: number
144
- chargeAmountMinor?: number; currency?: string
145
- createdAt: Date; endsAt?: Date; trialUntil?: Date; canceledAt?: Date
146
- fulfilledAt?: Date; initialPropagatedAt?: Date
704
+ entityId: string
705
+ planSku: string
706
+ productSku: string
707
+ service: string
708
+ paygate: string
709
+ externalId: string
710
+ itemId?: string
711
+ priceId?: string
712
+ status: SubscriptionStatus
713
+ externalStatus?: string
714
+ rank: number
715
+ periodStart?: Date
716
+ periodEnd?: Date
717
+ cancelAtPeriodEnd?: boolean
718
+ canceledAt?: Date
719
+ endedAt?: Date
720
+ pausedAt?: Date
721
+ trialEnd?: Date
722
+ latestInvoiceId?: string
723
+ customerId?: string
724
+ disputedAt?: Date
725
+ disputeStatus?: string
726
+ createdAt: Date
727
+ updatedAt?: Date
728
+ /** The paygate-side instant of the state stored; an older event payload is not applied. */
729
+ syncedAt?: Date
730
+ lastEventId?: string
731
+ initialPropagatedAt?: Date
732
+ propagated?: PropagatedState
733
+ /** The subscription's currency (lowercase). */
734
+ currency?: string
735
+ // Checkout evidence — written once the checkout that created the subscription completed.
736
+ checkoutSessionId?: string
737
+ purchaseId?: string
738
+ firstInvoiceId?: string
739
+ country?: string
740
+ email?: string
741
+ amountTotalMinor?: number
742
+ amountTaxMinor?: number
743
+ termsAccepted?: boolean
744
+ startRequestId?: string
745
+ withdrawnAt?: Date
746
+ /** This subscription's own plan parameters — `resolveEffectivePlan` answers the plan with them applied. */
747
+ overrides?: SubscriptionOverrides
147
748
  }
148
749
  export interface PaymentSubscriptionResource extends MongoResource<PaymentSubscriptionRecord> {
149
750
  byExternalId: (externalId: string, paygate: string) => Promise<PaymentSubscriptionRecord | null>
751
+ byItemId: (itemId: string, paygate: string) => Promise<PaymentSubscriptionRecord | null>
752
+ }
753
+
754
+ /** A one-time checkout session: pending until observers succeed, then `fulfilledAt`. */
755
+ export interface PaymentFulfillmentRecord extends ResourceRecord {
756
+ entityId: string
757
+ productSku: string
758
+ planSku?: string
759
+ service: string
760
+ paygate: string
761
+ externalId: string
762
+ paymentIntentId?: string
763
+ chargeId?: string
764
+ invoiceId?: string
765
+ mode: CheckoutPricingMode
766
+ units?: number
767
+ amountMinor?: number
768
+ sourceChargeAmountMinor?: number
769
+ amountCurrency?: string
770
+ chargeAmountMinor?: number
771
+ currency?: string
772
+ createdAt: Date
773
+ fulfilledAt?: Date
774
+ failedAt?: Date
775
+ refundedMinor?: number
776
+ refundedAt?: Date
777
+ disputedAt?: Date
778
+ disputeStatus?: string
779
+ // Checkout evidence.
780
+ country?: string
781
+ email?: string
782
+ profileId?: string
783
+ amountTotalMinor?: number
784
+ amountTaxMinor?: number
785
+ termsAccepted?: boolean
786
+ purchaseId?: string
787
+ }
788
+ export interface PaymentFulfillmentResource extends MongoResource<PaymentFulfillmentRecord> {
789
+ byExternalId: (externalId: string, paygate: string) => Promise<PaymentFulfillmentRecord | null>
150
790
  }
791
+
792
+ /** The paygate webhook endpoint this deployment owns, with its signing secret. */
793
+ export interface PaymentWebhookRecord extends ResourceRecord {
794
+ paygate: string
795
+ /** The gateway's `owner` (default `cfg.service`) — the key every process of one owner shares. */
796
+ service: string
797
+ /** The endpoint's URL (`webhookUrlOf`: the `webhookService` alias's host and base). */
798
+ url: string
799
+ externalId: string
800
+ secret: string
801
+ apiVersion: string
802
+ events: string[]
803
+ hash: string
804
+ createdAt: Date
805
+ updatedAt?: Date
806
+ }
807
+ export interface PaymentWebhookResource extends MongoResource<PaymentWebhookRecord> {}
808
+
809
+ /** A usage event — the source of truth of every limit counter. */
810
+ export interface PaymentUsageRecord extends ResourceRecord {
811
+ entityId: string
812
+ limitKey: string
813
+ /** `windowKeyOf` of the limit when the event was written. */
814
+ window: string
815
+ /** `+n` consumed, `-n` released, any sign for a reconciliation adjustment. */
816
+ delta: number
817
+ eventKey: string
818
+ ref?: string
819
+ reason?: string
820
+ planSku?: string
821
+ createdAt: Date
822
+ releasedAt?: Date
823
+ }
824
+ export interface PaymentUsageResource extends MongoResource<PaymentUsageRecord> {}
825
+
826
+ /** The projection of the usage ledger per (entity, limit, window). */
827
+ export interface PaymentUsageCounterRecord extends ResourceRecord {
828
+ entityId: string
829
+ limitKey: string
830
+ window: string
831
+ used: number
832
+ limit: number
833
+ planSku?: string
834
+ overSince?: Date
835
+ updatedAt: Date
836
+ reconciledAt?: Date
837
+ }
838
+ export interface PaymentUsageCounterResource extends MongoResource<PaymentUsageCounterRecord> {}
839
+
840
+ /** One currency option of a synced price. */
841
+ export interface SyncedPriceOption {
842
+ currency: string
843
+ unitAmount: number
844
+ }
845
+
846
+ /** A reusable price as the last sync left it — what `planPrices` and checkout read without Stripe. */
847
+ export interface SyncedPrice {
848
+ planSku: string
849
+ priceId: string
850
+ lookupKey: string
851
+ /** The default currency and its amount. */
852
+ currency: string
853
+ unitAmount: number
854
+ /** `currency_options` besides the default currency. */
855
+ options: SyncedPriceOption[]
856
+ taxBehavior?: string
857
+ interval?: 'month' | 'year'
858
+ sourceUnitAmount: number
859
+ sourceCurrency: string
860
+ syncedAt: Date
861
+ }
862
+
151
863
  export interface FingerprintRecord extends ResourceRecord {
152
- sku: string; hash: string; productId?: string; updatedAt: Date
864
+ sku: string
865
+ hash: string
866
+ productId?: string
867
+ /** The paygate object the fingerprint describes (a portal configuration id). */
868
+ externalId?: string
869
+ /** A product's synced reusable prices. */
870
+ prices?: SyncedPrice[]
871
+ updatedAt: Date
153
872
  }
154
873
  export interface FingerprintResource extends MongoResource<FingerprintRecord> {
155
874
  bySku: (sku: string) => Promise<FingerprintRecord | null>
156
875
  clear: () => Promise<void>
157
876
  }
877
+
878
+ // ---------------------------------------------------------------------------------------------
879
+ // Entitlements
880
+ // ---------------------------------------------------------------------------------------------
881
+
882
+ export interface EffectivePlan {
883
+ plan: PaymentPlan
884
+ /** The entitling row behind the plan; `null` when the entity falls back to the free plan. */
885
+ subscription: PaymentSubscriptionRecord | null
886
+ /** The declared free plan, when one exists. */
887
+ fallback: PaymentPlan | null
888
+ }
889
+
890
+ export interface ConsumeRequest {
891
+ entityId: string
892
+ limitKey: string
893
+ /** Idempotency key — key it by the record the unit pays for. */
894
+ eventKey: string
895
+ /** Positive safe integer; default `1`. */
896
+ amount?: number
897
+ ref?: string
898
+ reason?: string
899
+ }
900
+
901
+ export interface ReleaseRequest {
902
+ entityId: string
903
+ limitKey: string
904
+ /** The event key the unit was consumed under. */
905
+ eventKey: string
906
+ /** Default: the consumed amount. */
907
+ amount?: number
908
+ }
909
+
910
+ export interface LimitOutcome {
911
+ /** A consume: the unit is held. A release: the release is in effect. */
912
+ admitted: boolean
913
+ /** The event key had been seen before; nothing new was written. */
914
+ replayed: boolean
915
+ limitKey: string
916
+ window: string
917
+ used: number
918
+ limit: number
919
+ remaining: number
920
+ resetsAt?: Date
921
+ eventKey: string
922
+ }
923
+
924
+ export interface OccupancyOutcome {
925
+ limitKey: string
926
+ used: number
927
+ limit: number
928
+ /** `max(0, used - limit)`. */
929
+ over: number
930
+ /** When the entity first went over; absent while it is within the limit. */
931
+ overSince?: Date
932
+ }
933
+
934
+ export interface CounterReconciliation {
935
+ counters: number
936
+ repaired: number
937
+ }
938
+
939
+ export interface EntitlementService extends InitializedService {
940
+ effectivePlan: (entityId: string) => Promise<EffectivePlan>
941
+ entitlements: (entityId: string) => Promise<EntitlementView>
942
+ hasCapability: (entityId: string, param: string) => Promise<boolean>
943
+ /** @throws LimitUnknown */
944
+ limitState: (entityId: string, key: string) => Promise<LimitView>
945
+ /** @throws LimitExhausted | LimitUnknown */
946
+ consume: (req: ConsumeRequest) => Promise<LimitOutcome>
947
+ release: (req: ReleaseRequest) => Promise<LimitOutcome>
948
+ /** @throws LimitUnknown | LimitMisdeclared */
949
+ reconcileOccupancy: (entityId: string, limitKey: string, actual: number) => Promise<OccupancyOutcome>
950
+ reconcileCounters: (entityId?: string) => Promise<CounterReconciliation>
951
+ /**
952
+ * The ACTIVE consume event for this key (not released), or null. This is how a consumer asks
953
+ * "was a unit already spent on this record?" — a consumer keys allowances by the record they pay
954
+ * for (`eventKey = <anything>:<recordId>`, `ref = recordId`) instead of writing a marker onto
955
+ * the record, so a retry replays the same event and a resumed run can re-derive that it is free.
956
+ */
957
+ consumption: (entityId: string, limitKey: string, eventKey: string) => Promise<PaymentUsageRecord | null>
958
+ /**
959
+ * The newest ACTIVE consume event (not released) whose `ref` is this record, or null. For a unit
960
+ * consumed under a fresh event key per cycle (`<key>:<recordId>:<timestamp>`, `ref = recordId`):
961
+ * whether the record holds a unit now, and the event key to release it by.
962
+ */
963
+ consumptionByRef: (entityId: string, limitKey: string, ref: string) => Promise<PaymentUsageRecord | null>
964
+ }
965
+
966
+ export interface ReconcileEntityOptions {
967
+ /** Grant this plan (an internal row) when the entity holds no entitling subscription. */
968
+ freePlanSku?: string
969
+ /** Re-read the entity's paygate subscriptions first (managed gateways only). */
970
+ resync?: boolean
971
+ }
972
+
973
+ export interface ReconcileAllOptions extends ReconcileEntityOptions {
974
+ /** Entities to cover besides those the payment store already knows. */
975
+ entities?: Iterable<string> | AsyncIterable<string>
976
+ }
977
+
978
+ export interface ReconcileAllResult {
979
+ entities: number
980
+ granted: number
981
+ counters: number
982
+ repaired: number
983
+ failed: number
984
+ }
985
+
986
+ // ---------------------------------------------------------------------------------------------
987
+ // Consumer rights — records
988
+ // ---------------------------------------------------------------------------------------------
989
+
990
+ /** Where a billing profile's country came from. */
991
+ export type BillingProfileSource = 'checkout' | 'customer' | 'manual'
992
+
993
+ /** One per organization: the billing country fixed at the first purchase (first write wins). */
994
+ export interface BillingProfileRecord extends ResourceRecord {
995
+ entityId: string
996
+ country: string
997
+ region: ConsumerRegion
998
+ /** The charge currency, fixed with the country. */
999
+ currency: string
1000
+ /** The language of the legal copy. */
1001
+ language: string
1002
+ source: BillingProfileSource
1003
+ paygate: string
1004
+ customerId?: string
1005
+ sessionId?: string
1006
+ /** The geolocated country of the request that started the locking checkout — VAT evidence. */
1007
+ ipCountry?: string
1008
+ email?: string
1009
+ name?: string
1010
+ /** A business tax id was given at the locking checkout. */
1011
+ business?: boolean
1012
+ lockedAt: Date
1013
+ createdAt: Date
1014
+ updatedAt?: Date
1015
+ }
1016
+ export interface BillingProfileResource extends MongoResource<BillingProfileRecord> {
1017
+ byEntity: (entityId: string) => Promise<BillingProfileRecord | null>
1018
+ }
1019
+
1020
+ /**
1021
+ * One purchase — a contract and its withdrawal window: a paid one-time checkout (`top-up`) or a
1022
+ * subscription's first invoice (`subscription`). Renewals are not purchases.
1023
+ */
1024
+ export interface PurchaseRecord extends ResourceRecord {
1025
+ /** `stripe:<session id>` (one-time) or `stripe:<subscription id>`. */
1026
+ purchaseId: string
1027
+ /** `CR-YYMMDD-XXXXXX` — what a consumer quotes. */
1028
+ contractRef: string
1029
+ entityId: string
1030
+ kind: PurchaseKind
1031
+ paygate: string
1032
+ sessionId?: string
1033
+ subscriptionId?: string
1034
+ paymentIntentId?: string
1035
+ invoiceId?: string
1036
+ invoiceNumber?: string
1037
+ invoiceLineId?: string
1038
+ productSku: string
1039
+ planSku?: string
1040
+ profileId?: string
1041
+ country?: string
1042
+ region?: ConsumerRegion
1043
+ ipCountry?: string
1044
+ inScope: boolean
1045
+ language: string
1046
+ email?: string
1047
+ name?: string
1048
+ business?: boolean
1049
+ /** The charge currency. */
1050
+ currency: string
1051
+ amountSubtotalMinor: number
1052
+ amountTaxMinor: number
1053
+ amountTotalMinor: number
1054
+ presentmentCurrency?: string
1055
+ presentmentAmountMinor?: number
1056
+ /** Amount checkout: the net value credited, in `amountCurrency`. */
1057
+ netAmountMinor?: number
1058
+ amountCurrency?: string
1059
+ /** Quantity checkout: the units bought. */
1060
+ units?: number
1061
+ taxBehavior?: string
1062
+ /**
1063
+ * Stripe's own `consent.terms_of_service` of the checkout — absent when the session did not
1064
+ * collect it (the policy's `checkoutTerms` off, or the fallback of a Dashboard without a terms URL).
1065
+ */
1066
+ termsAccepted?: boolean
1067
+ textVersion?: string
1068
+ copyVersion?: string
1069
+ startRequestId?: string
1070
+ /** When the consumer expressly requested the services (the start request) — time deductions count from here. */
1071
+ servicesStartedAt?: Date
1072
+ /** When the confirmation mail was claimed — one claim per purchase, whatever the outcome of the send. */
1073
+ confirmationMailAt?: Date
1074
+ purchasedAt: Date
1075
+ /** Exclusive end of the withdrawal window; absent outside the consumer-rights territories. */
1076
+ deadline?: Date
1077
+ consentId?: string
1078
+ consentedAt?: Date
1079
+ withdrawalId?: string
1080
+ withdrawnAt?: Date
1081
+ /** Refunded so far on this purchase's payment, tax included. */
1082
+ refundedMinor?: number
1083
+ /** Set once the whole payment is refunded — the window closes. */
1084
+ refundedAt?: Date
1085
+ cancellationId?: string
1086
+ cancelEffectiveAt?: Date
1087
+ createdAt: Date
1088
+ updatedAt?: Date
1089
+ }
1090
+ export interface PurchaseResource extends MongoResource<PurchaseRecord> {
1091
+ byPurchaseId: (purchaseId: string) => Promise<PurchaseRecord | null>
1092
+ }
1093
+
1094
+ /** The request evidence a consumer act is recorded with (`requestOriginOf`). */
1095
+ export interface RequestOrigin {
1096
+ /** `cf-connecting-ip`, else the LAST `x-forwarded-for` entry, else `x-real-ip`, else the socket. */
1097
+ ip?: string
1098
+ /** The raw `x-forwarded-for` header. */
1099
+ forwardedFor?: string
1100
+ /** `user-agent`, at most 512 characters. */
1101
+ userAgent?: string
1102
+ /** `cf-ipcountry`. */
1103
+ ipCountry?: string
1104
+ acceptLanguage?: string
1105
+ /** The channel the request came through (`http`, `mcp`, …), when the application knows. */
1106
+ via?: string
1107
+ }
1108
+
1109
+ /** Append-only: one express request — a performance consent or a subscription start request. */
1110
+ export interface ConsumerConsentRecord extends ResourceRecord, RequestOrigin {
1111
+ kind: ConsentKind
1112
+ entityId: string
1113
+ profileId?: string
1114
+ /** The person who gave it — the confirmation is mailed here. */
1115
+ name?: string
1116
+ email?: string
1117
+ purchaseIds: string[]
1118
+ planSku?: string
1119
+ /** The plan's name as the statement said it (`{{plan}}`). */
1120
+ planName?: string
1121
+ textVersion: string
1122
+ copyVersion: string
1123
+ /** The language the statement was shown in. */
1124
+ language: string
1125
+ uiLanguage?: string
1126
+ trader: string
1127
+ /**
1128
+ * The copy variant the statement was rendered with: the policy's `consentContext` for a
1129
+ * performance consent, `startContextOf(plan)` (`'units'`) for a start request; absent for the base.
1130
+ */
1131
+ context?: string
1132
+ /** The statement exactly as rendered and recorded. */
1133
+ text: { request: string, acknowledgement: string, checkbox: string }
1134
+ links: ConsumerRightsLinks
1135
+ /** The latest deadline among the covered purchases. */
1136
+ deadline?: Date
1137
+ decidedAt: Date
1138
+ /** A start request stops being usable at this instant. */
1139
+ expiresAt?: Date
1140
+ }
1141
+ export interface ConsumerConsentResource extends MongoResource<ConsumerConsentRecord> {}
1142
+
1143
+ /** Append-only: one withdrawal or cancellation declaration, matched to a contract or not. */
1144
+ export interface ConsumerDeclarationRecord extends ResourceRecord, RequestOrigin {
1145
+ kind: DeclarationKind
1146
+ channel: DeclarationChannel
1147
+ entityId?: string
1148
+ purchaseId?: string
1149
+ subscriptionId?: string
1150
+ /** The contract as the consumer named it. */
1151
+ contractRef?: string
1152
+ name: string
1153
+ email: string
1154
+ cancellationKind?: CancellationKind
1155
+ reason?: string
1156
+ effective?: 'earliest' | 'date'
1157
+ requestedDate?: string
1158
+ language: string
1159
+ textVersion?: string
1160
+ copyVersion: string
1161
+ receivedAt: Date
1162
+ matched: boolean
1163
+ profileId?: string
1164
+ /** A repeated declaration of a contract already withdrawn from: the original's id. */
1165
+ duplicateOf?: string
1166
+ /** The status decided on receipt (`WithdrawalStatus` / `CancellationStatus`); execution steps are events. */
1167
+ status: string
1168
+ refundMinor?: number
1169
+ currency?: string
1170
+ effectiveAt?: Date
1171
+ }
1172
+ export interface ConsumerDeclarationResource extends MongoResource<ConsumerDeclarationRecord> {}
1173
+
1174
+ export type ConsumerEventAction =
1175
+ | 'mail' | 'computed' | 'meter' | 'refund' | 'credit-note' | 'subscription-cancel' | 'cancel-scheduled'
1176
+ | 'observers' | 'lock' | 'lock-mismatch' | 'relock' | 'unlock' | 'duplicate' | 'checkout-terms-fallback'
1177
+
1178
+ export type ConsumerRecordKind = 'purchase' | 'consent' | 'declaration' | 'profile' | 'checkout'
1179
+
1180
+ /** Append-only: one execution or audit step. */
1181
+ export interface ConsumerEventRecord extends ResourceRecord {
1182
+ /** The purchase id, consent id, declaration id, or the entity id of a profile or a checkout. */
1183
+ recordId: string
1184
+ recordKind: ConsumerRecordKind
1185
+ entityId?: string
1186
+ action: ConsumerEventAction
1187
+ /** The mail kind of a `mail` event; the observer family of an `observers` one. */
1188
+ step?: string
1189
+ ok: boolean
1190
+ /** A mail deliberately not sent (a reserved domain, a renderer that suppressed it). */
1191
+ skipped?: boolean
1192
+ externalId?: string
1193
+ amountMinor?: number
1194
+ currency?: string
1195
+ /** JSON. */
1196
+ detail?: string
1197
+ error?: string
1198
+ at: Date
1199
+ }
1200
+ export interface ConsumerEventResource extends MongoResource<ConsumerEventRecord> {}
1201
+
1202
+ // ---------------------------------------------------------------------------------------------
1203
+ // Consumer rights — service
1204
+ // ---------------------------------------------------------------------------------------------
1205
+
1206
+ /** What a usage meter is told about the purchase it reads. */
1207
+ export interface PurchaseRef {
1208
+ purchaseId: string
1209
+ kind: PurchaseKind
1210
+ entityId: string
1211
+ contractRef: string
1212
+ productSku: string
1213
+ planSku?: string
1214
+ /** One-time purchases: the checkout session id. */
1215
+ sessionId?: string
1216
+ /** Subscription purchases: the paygate subscription id. */
1217
+ subscriptionId?: string
1218
+ invoiceId?: string
1219
+ purchasedAt: Date
1220
+ }
1221
+
1222
+ export interface UsageQuery {
1223
+ entityId: string
1224
+ purchase: PurchaseRef
1225
+ /** The purchase's consent — before it the consumer bears no cost; absent: nothing is deductible. */
1226
+ after?: Date
1227
+ at: Date
1228
+ }
1229
+
1230
+ export interface UsageReading {
1231
+ /** Units the purchase granted. */
1232
+ granted: number
1233
+ /** Units of it used so far. */
1234
+ used: number
1235
+ /** Units of it used strictly after `after` (0 without `after`). */
1236
+ usedAfter: number
1237
+ /** Units of it that paid off an earlier overdraft when it was granted. */
1238
+ settled?: number
1239
+ /** Units of it already taken back by an earlier (money) refund. */
1240
+ clawed?: number
1241
+ remaining?: number
1242
+ }
1243
+
1244
+ /**
1245
+ * The application's reading of how much of one purchase was used — for a withdrawal's refund.
1246
+ * The deduction is `usedAfter + settled + clawed`.
1247
+ */
1248
+ export interface UsageMeter {
1249
+ used: (ctx: ApiContext, query: UsageQuery) => Promise<UsageReading>
1250
+ }
1251
+
1252
+ /** Who acts: the organization, and the person's prefill. */
1253
+ export interface ConsumerSubject {
1254
+ entityId: string
1255
+ profileId?: string
1256
+ name?: string
1257
+ email?: string
1258
+ /** `public` when an application matched a public declaration to this organization itself. */
1259
+ channel?: DeclarationChannel
1260
+ }
1261
+
1262
+ export type ConsumerMailKind = 'purchase' | 'consent' | 'start' | 'withdrawal' | 'cancellation'
1263
+
1264
+ /** What a consumer-rights mail is rendered from. */
1265
+ export interface ConsumerMailData {
1266
+ kind: ConsumerMailKind
1267
+ /** The purchase id, consent id or declaration id. */
1268
+ recordId: string
1269
+ entityId?: string
1270
+ language: string
1271
+ to: string
1272
+ name?: string
1273
+ trader: TraderDef
1274
+ links: ConsumerRightsLinks
1275
+ purchase?: PurchaseRecord
1276
+ purchases?: PurchaseRecord[]
1277
+ consent?: ConsumerConsentRecord
1278
+ declaration?: ConsumerDeclarationRecord
1279
+ }
1280
+
1281
+ /**
1282
+ * Replace or suppress a consumer-rights mail: answer a message to send it instead, `null` to send
1283
+ * nothing (recorded as skipped), `undefined` to send the rendered one.
1284
+ */
1285
+ export interface ConsumerMailRenderer { (kind: ConsumerMailKind, data: ConsumerMailData, rendered: MailMessage): MailMessage | null | undefined | Promise<MailMessage | null | undefined> }
1286
+
1287
+ export interface LockOptions {
1288
+ customerId?: string
1289
+ sessionId?: string
1290
+ ipCountry?: string
1291
+ email?: string
1292
+ name?: string
1293
+ business?: boolean
1294
+ /** The charge currency; default: an active subscription's, else the region's. */
1295
+ currency?: string
1296
+ language?: string
1297
+ /** `manual` only: replace an existing lock (audited as a `relock` event). */
1298
+ force?: boolean
1299
+ by?: string
1300
+ reason?: string
1301
+ }
1302
+
1303
+ /** Who removes a lock, and why — kept on the `unlock` event. */
1304
+ export interface UnlockOptions {
1305
+ by?: string
1306
+ reason?: string
1307
+ }
1308
+
1309
+ export interface ConsumerReconcileOptions {
1310
+ /** How far back declarations, mails and observers are retried. Default 30 days. */
1311
+ since?: Date
1312
+ /** The most items per step. Default 50. */
1313
+ limit?: number
1314
+ }
1315
+
1316
+ export interface ConsumerReconcileResult {
1317
+ retried: number
1318
+ mailed: number
1319
+ observed: number
1320
+ backfilled: number
1321
+ locked: number
1322
+ failed: number
1323
+ }
1324
+
1325
+ /**
1326
+ * Lazy: reachable while the application is still being wired (`useMeter`, `useMailRenderer` before
1327
+ * the context initializes); the gateway's initialization initializes it at boot.
1328
+ */
1329
+ export interface ConsumerRightsService extends LazyService {
1330
+ /**
1331
+ * `false`: every paygate-calling method (`withdraw`, `cancel`, `reconcile`'s paygate steps) refuses.
1332
+ * An explicit `manage` of `appendConsumerRights` wins over the gateway's, whatever the order.
1333
+ */
1334
+ readonly managed: boolean
1335
+ policy: () => Promise<ConsumerRightsPolicy | null>
1336
+ /** The locked billing profile, or `null`. */
1337
+ profile: (entityId: string) => Promise<BillingProfileView | null>
1338
+ /** Lock the billing country; the first write wins unless a `manual` lock is `force`d. */
1339
+ lock: (entityId: string, country: string, source: BillingProfileSource, opts?: LockOptions) => Promise<BillingProfileView>
1340
+ /**
1341
+ * An operator removes the lock: the profile is deleted and an `unlock` event keeps it. No lazy lock
1342
+ * from the paygate customer follows (checkout, reconcile); the next completed purchase locks again.
1343
+ * @returns the profile as it was, or `null` when nothing was locked
1344
+ */
1345
+ unlock: (entityId: string, opts?: UnlockOptions) => Promise<BillingProfileView | null>
1346
+ purchases: (entityId: string, opts?: { open?: boolean, at?: Date }) => Promise<PurchaseView[]>
1347
+ consentView: (entityId: string, at?: Date) => Promise<PerformanceConsentView>
1348
+ /** @throws PerformanceConsentRequired (428) for a stale text version */
1349
+ recordConsent: (subject: ConsumerSubject, body: PerformanceConsentBody, origin?: RequestOrigin) => Promise<PerformanceConsentResponse>
1350
+ /** @throws PerformanceConsentRequired while an open in-scope window has no consent */
1351
+ assertConsent: (entityId: string, at?: Date) => Promise<void>
1352
+ /** `context` = `startContextOf(plan)`: the statement variant the dialog shows and the request records. */
1353
+ startView: (entityId: string, planSku: string, opts?: { language?: string }) => Promise<SubscriptionStartView>
1354
+ /**
1355
+ * Records the statement in the variant of the plan (`startContextOf`), the one the start view
1356
+ * named. `plan`: the plan's short name the statement says (default: its localized catalogue title).
1357
+ * @throws SubscriptionStartRequired (428) for a stale text version
1358
+ */
1359
+ recordStartRequest: (
1360
+ subject: ConsumerSubject, body: SubscriptionStartBody, origin?: RequestOrigin, opts?: { plan?: string },
1361
+ ) => Promise<SubscriptionStartResponse>
1362
+ /**
1363
+ * The fresh start request bound to this entity and plan, or `null` when none is required.
1364
+ * @throws SubscriptionStartRequired
1365
+ */
1366
+ assertStartRequest: (entityId: string, planSku: string, startRequestId?: string) => Promise<ConsumerConsentRecord | null>
1367
+ withdrawalCandidates: (entityId: string, subject?: Partial<ConsumerSubject>) => Promise<WithdrawalCandidateList>
1368
+ /** `subject: null` — the public function: matched by contract reference and e-mail, never disclosed. */
1369
+ withdraw: (subject: ConsumerSubject | null, body: WithdrawalBody, origin?: RequestOrigin) => Promise<WithdrawalReceipt>
1370
+ cancel: (subject: ConsumerSubject | null, body: CancellationBody, origin?: RequestOrigin) => Promise<CancellationReceipt>
1371
+ useMeter: (meter: UsageMeter | null) => void
1372
+ useMailRenderer: (renderer: ConsumerMailRenderer | null) => void
1373
+ usageMeter: () => UsageMeter | null
1374
+ mailRenderer: () => ConsumerMailRenderer | null
1375
+ /** Retry refunds, credit notes, cancellations, mails and observers; backfill purchases and legacy locks. */
1376
+ reconcile: (opts?: ConsumerReconcileOptions) => Promise<ConsumerReconcileResult>
1377
+ }
1378
+
1379
+ /**
1380
+ * `appendConsumerRights` options. `manage`, `usage` and `stripe` apply to the service whenever the
1381
+ * call comes — before or after the gateway registered it; the resources' `dbAlias` / `serviceAlias`
1382
+ * are those of the first registration.
1383
+ */
1384
+ export interface ConsumerRightsOptions {
1385
+ /**
1386
+ * `false` in a process that never talks to the paygate (reads and consent asserts only). Absent:
1387
+ * the gateway's `manage`, else managed.
1388
+ */
1389
+ manage?: boolean
1390
+ /** The application's usage meter; without one a withdrawal is left to an operator (`review`). */
1391
+ usage?: UsageMeter
1392
+ alias?: string
1393
+ /** The paygate client; default: the configured Stripe secret. */
1394
+ stripe?: StripeFactory
1395
+ dbAlias?: string
1396
+ serviceAlias?: string
1397
+ }
1398
+
1399
+ // ---------------------------------------------------------------------------------------------
1400
+ // Consumer rights — handlers
1401
+ // ---------------------------------------------------------------------------------------------
1402
+
1403
+ /** What a public declaration is throttled by. */
1404
+ export interface ConsumerThrottleKey {
1405
+ action: 'withdrawal' | 'cancellation'
1406
+ email: string
1407
+ ip?: string
1408
+ }
1409
+
1410
+ /**
1411
+ * The hooks of `consumerRightsEntrypoints`. Every hook gets the request's context last, so an
1412
+ * application keeps its counters and lookups on the context, never in module state.
1413
+ */
1414
+ export interface ConsumerRightsHandlerOptions {
1415
+ /** The organization a signed-in request acts for. Default: `req.entity.id`. */
1416
+ resolveEntity?: (req: AbstractRequest, ctx: ApiContext) => string | null | undefined
1417
+ /** The person's profile id and prefill. */
1418
+ subjectOf?: (req: AbstractRequest, ctx: ApiContext) => Partial<ConsumerSubject> | Promise<Partial<ConsumerSubject>>
1419
+ /** Refuse a money-moving act (consent, start, withdraw, cancel) — e.g. an API-key request. */
1420
+ guardMoney?: (req: AbstractRequest, action: string, ctx: ApiContext) => void | Promise<void>
1421
+ /** REQUIRED when the public subtree is bound: throw (e.g. 429) to refuse a public declaration. */
1422
+ throttle?: (req: AbstractRequest, key: ConsumerThrottleKey, ctx: ApiContext) => void | Promise<void>
1423
+ /** The request evidence; default `requestOriginOf`. */
1424
+ metaOf?: (req: AbstractRequest, ctx: ApiContext) => RequestOrigin
1425
+ /** The least time a public declaration takes to answer, matched or not. Default 1000 ms. */
1426
+ publicMinMs?: number
1427
+ /** The plan's short name a start request's statement says (default: its localized catalogue title). */
1428
+ planNameOf?: (
1429
+ planSku: string, language: string, req: AbstractRequest, ctx: ApiContext,
1430
+ ) => string | undefined | Promise<string | undefined>
1431
+ serviceAlias?: string
1432
+ }
1433
+
1434
+ export interface CheckoutReadHandlerOptions {
1435
+ resolveEntity?: (req: AbstractRequest, ctx: ApiContext) => string | null | undefined
1436
+ gatewayAlias?: string
1437
+ }
1438
+
1439
+ export interface PaygateParams { paygate: string }
1440
+
1441
+ export interface ResyncResult { ok: boolean }
1442
+
1443
+ export interface ResyncSubscriptionsResult { scanned: number; updated: number }
1444
+
1445
+ export interface EntityResolverOption {
1446
+ /** The stable organization id a request acts for. Default: `req.entity.id`, else the token's entity. */
1447
+ resolveEntity?: (req: AbstractRequest) => string | null
1448
+ }
1449
+
1450
+ export interface CapabilityGateOptions extends EntityResolverOption {
1451
+ /** Refuse unless the effective plan belongs to one of these products. */
1452
+ productSkus?: string[]
1453
+ /**
1454
+ * Also require the token to grant the permission (IAM `hasPermission`). Off by default: a
1455
+ * platform token carries no permissions, and the subscription is the authority.
1456
+ */
1457
+ requirePermission?: boolean
1458
+ }
1459
+
1460
+ /** @deprecated use `CapabilityGateOptions` */
1461
+ export type EntitlementGateOptions = CapabilityGateOptions
1462
+
1463
+ export interface LimitGateOptions extends EntityResolverOption {}
1464
+
1465
+ export interface StripeBootstrapOptions {
1466
+ /** Re-verify what the stored fingerprints say is in place. */
1467
+ force?: boolean
1468
+ }
1469
+
1470
+ /** The gateway options a service instance takes — the resource aliases belong to the registration. */
1471
+ export interface GatewayServiceOptions extends Omit<PaymentGatewayOptions, 'dbAlias' | 'serviceAlias'> {}
1472
+
1473
+ export interface CommitOptions {
1474
+ /** The paygate event being applied; a repeat of the last applied one is ignored. */
1475
+ eventId?: string
1476
+ /** A paid renewal invoice. */
1477
+ renewal?: boolean
1478
+ invoiceId?: string
1479
+ /** The paygate announced the trial ends soon. */
1480
+ trialEnding?: boolean
1481
+ /**
1482
+ * Runs after the new state is written and BEFORE observers hear the change — what must exist
1483
+ * before an observer grants anything (a subscription's purchase row). A throw propagates: the
1484
+ * observers are not told and the paygate retries.
1485
+ */
1486
+ beforePropagate?: (record: PaymentSubscriptionRecord, change: SubscriptionChange) => Promise<void>
1487
+ }
1488
+
1489
+ export interface CommitResult {
1490
+ record: PaymentSubscriptionRecord | null
1491
+ change: SubscriptionChange | null
1492
+ /** The stored subscription state changed. */
1493
+ updated: boolean
1494
+ }