@owlmeans/server-payment 0.1.21 → 0.2.0-alpha.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 (296) hide show
  1. package/README.md +52 -54
  2. package/agent-meta/manifest.json +2 -2
  3. package/agent-meta/skills/server-payment/SKILL.md +272 -330
  4. package/agent-meta/skills/server-payment/parts/reference.md +3 -3
  5. package/build/access/types.d.ts +7 -16
  6. package/build/access/types.d.ts.map +1 -1
  7. package/build/access.d.ts.map +1 -1
  8. package/build/access.js +6 -8
  9. package/build/access.js.map +1 -1
  10. package/build/actions/resync.d.ts +3 -3
  11. package/build/actions/resync.js +5 -5
  12. package/build/actions/resync.js.map +1 -1
  13. package/build/actions/webhook.d.ts +4 -1
  14. package/build/actions/webhook.d.ts.map +1 -1
  15. package/build/actions/webhook.js +8 -4
  16. package/build/actions/webhook.js.map +1 -1
  17. package/build/bootstrap/types.d.ts +13 -7
  18. package/build/bootstrap/types.d.ts.map +1 -1
  19. package/build/bootstrap.d.ts +4 -4
  20. package/build/bootstrap.d.ts.map +1 -1
  21. package/build/bootstrap.js +19 -10
  22. package/build/bootstrap.js.map +1 -1
  23. package/build/catalogue/types.d.ts +3 -3
  24. package/build/catalogue/types.d.ts.map +1 -1
  25. package/build/catalogue.d.ts.map +1 -1
  26. package/build/catalogue.js +4 -8
  27. package/build/catalogue.js.map +1 -1
  28. package/build/checkout/types.d.ts +21 -0
  29. package/build/checkout/types.d.ts.map +1 -0
  30. package/build/{plugins/stripe → checkout}/types.js.map +1 -1
  31. package/build/checkout.d.ts +6 -0
  32. package/build/checkout.d.ts.map +1 -0
  33. package/build/checkout.js +374 -0
  34. package/build/checkout.js.map +1 -0
  35. package/build/config.d.ts +10 -16
  36. package/build/config.d.ts.map +1 -1
  37. package/build/config.js +18 -59
  38. package/build/config.js.map +1 -1
  39. package/build/consts.d.ts +3 -49
  40. package/build/consts.d.ts.map +1 -1
  41. package/build/consts.js +3 -61
  42. package/build/consts.js.map +1 -1
  43. package/build/consumer/cancellation/types.d.ts +6 -4
  44. package/build/consumer/cancellation/types.d.ts.map +1 -1
  45. package/build/consumer/cancellation.d.ts.map +1 -1
  46. package/build/consumer/cancellation.js +11 -5
  47. package/build/consumer/cancellation.js.map +1 -1
  48. package/build/consumer/capture/types.d.ts +8 -12
  49. package/build/consumer/capture/types.d.ts.map +1 -1
  50. package/build/consumer/capture.d.ts.map +1 -1
  51. package/build/consumer/capture.js +48 -53
  52. package/build/consumer/capture.js.map +1 -1
  53. package/build/consumer/index.d.ts +4 -0
  54. package/build/consumer/index.d.ts.map +1 -1
  55. package/build/consumer/index.js +2 -0
  56. package/build/consumer/index.js.map +1 -1
  57. package/build/consumer/reconcile.d.ts.map +1 -1
  58. package/build/consumer/reconcile.js +82 -72
  59. package/build/consumer/reconcile.js.map +1 -1
  60. package/build/consumer/records/types.d.ts +4 -4
  61. package/build/consumer/records/types.d.ts.map +1 -1
  62. package/build/consumer/records.d.ts.map +1 -1
  63. package/build/consumer/records.js +16 -13
  64. package/build/consumer/records.js.map +1 -1
  65. package/build/consumer/service.d.ts +2 -2
  66. package/build/consumer/service.d.ts.map +1 -1
  67. package/build/consumer/service.js +28 -21
  68. package/build/consumer/service.js.map +1 -1
  69. package/build/consumer/types.d.ts +1 -4
  70. package/build/consumer/types.d.ts.map +1 -1
  71. package/build/consumer/types.local.d.ts +1 -9
  72. package/build/consumer/types.local.d.ts.map +1 -1
  73. package/build/consumer/withdrawal/types.d.ts +14 -12
  74. package/build/consumer/withdrawal/types.d.ts.map +1 -1
  75. package/build/consumer/withdrawal.d.ts.map +1 -1
  76. package/build/consumer/withdrawal.js +55 -71
  77. package/build/consumer/withdrawal.js.map +1 -1
  78. package/build/index.d.ts +12 -6
  79. package/build/index.d.ts.map +1 -1
  80. package/build/index.js +7 -4
  81. package/build/index.js.map +1 -1
  82. package/build/paygate/providers.d.ts +13 -0
  83. package/build/paygate/providers.d.ts.map +1 -0
  84. package/build/paygate/providers.js +38 -0
  85. package/build/paygate/providers.js.map +1 -0
  86. package/build/paygate/registry.d.ts +6 -0
  87. package/build/paygate/registry.d.ts.map +1 -0
  88. package/build/paygate/registry.js +37 -0
  89. package/build/paygate/registry.js.map +1 -0
  90. package/build/paygate/types.d.ts +577 -0
  91. package/build/paygate/types.d.ts.map +1 -0
  92. package/build/{plugins/session → paygate}/types.js.map +1 -1
  93. package/build/plan/types.d.ts +6 -1
  94. package/build/plan/types.d.ts.map +1 -1
  95. package/build/plan.d.ts.map +1 -1
  96. package/build/plan.js +16 -2
  97. package/build/plan.js.map +1 -1
  98. package/build/plugins/checkout-policy/types.d.ts +5 -3
  99. package/build/plugins/checkout-policy/types.d.ts.map +1 -1
  100. package/build/plugins/checkout-policy.d.ts.map +1 -1
  101. package/build/plugins/checkout-policy.js +5 -3
  102. package/build/plugins/checkout-policy.js.map +1 -1
  103. package/build/plugins/consts.local.d.ts +8 -10
  104. package/build/plugins/consts.local.d.ts.map +1 -1
  105. package/build/plugins/consts.local.js +2 -19
  106. package/build/plugins/consts.local.js.map +1 -1
  107. package/build/plugins/customer-lock/types.d.ts +2 -2
  108. package/build/plugins/customer-lock/types.d.ts.map +1 -1
  109. package/build/plugins/customer-lock.js +2 -2
  110. package/build/plugins/customer-lock.js.map +1 -1
  111. package/build/plugins/estimate/cache.js +1 -1
  112. package/build/plugins/estimate/cache.js.map +1 -1
  113. package/build/plugins/estimate/types.d.ts +10 -10
  114. package/build/plugins/estimate/types.d.ts.map +1 -1
  115. package/build/plugins/estimate.d.ts.map +1 -1
  116. package/build/plugins/estimate.js +48 -122
  117. package/build/plugins/estimate.js.map +1 -1
  118. package/build/plugins/events/types.d.ts +17 -10
  119. package/build/plugins/events/types.d.ts.map +1 -1
  120. package/build/plugins/events.d.ts +6 -5
  121. package/build/plugins/events.d.ts.map +1 -1
  122. package/build/plugins/events.js +162 -173
  123. package/build/plugins/events.js.map +1 -1
  124. package/build/plugins/fx/types.d.ts +10 -21
  125. package/build/plugins/fx/types.d.ts.map +1 -1
  126. package/build/plugins/fx.d.ts +1 -1
  127. package/build/plugins/fx.d.ts.map +1 -1
  128. package/build/plugins/fx.js +17 -54
  129. package/build/plugins/fx.js.map +1 -1
  130. package/build/plugins/portal/types.d.ts +8 -37
  131. package/build/plugins/portal/types.d.ts.map +1 -1
  132. package/build/plugins/portal.d.ts.map +1 -1
  133. package/build/plugins/portal.js +38 -208
  134. package/build/plugins/portal.js.map +1 -1
  135. package/build/plugins/refunds/types.d.ts +11 -11
  136. package/build/plugins/refunds/types.d.ts.map +1 -1
  137. package/build/plugins/refunds.d.ts.map +1 -1
  138. package/build/plugins/refunds.js +14 -39
  139. package/build/plugins/refunds.js.map +1 -1
  140. package/build/plugins/snapshot/types.d.ts +11 -0
  141. package/build/plugins/snapshot/types.d.ts.map +1 -0
  142. package/build/plugins/snapshot/types.js +2 -0
  143. package/build/plugins/snapshot/types.js.map +1 -0
  144. package/build/plugins/snapshot.d.ts +4 -0
  145. package/build/plugins/snapshot.d.ts.map +1 -0
  146. package/build/plugins/snapshot.js +19 -0
  147. package/build/plugins/snapshot.js.map +1 -0
  148. package/build/plugins/subscriptions/types.d.ts +17 -17
  149. package/build/plugins/subscriptions/types.d.ts.map +1 -1
  150. package/build/plugins/subscriptions.d.ts +4 -4
  151. package/build/plugins/subscriptions.d.ts.map +1 -1
  152. package/build/plugins/subscriptions.js +69 -77
  153. package/build/plugins/subscriptions.js.map +1 -1
  154. package/build/plugins/types.local.d.ts +9 -43
  155. package/build/plugins/types.local.d.ts.map +1 -1
  156. package/build/plugins/webhook-manager/types.d.ts +15 -43
  157. package/build/plugins/webhook-manager/types.d.ts.map +1 -1
  158. package/build/plugins/webhook-manager.d.ts +3 -3
  159. package/build/plugins/webhook-manager.d.ts.map +1 -1
  160. package/build/plugins/webhook-manager.js +21 -199
  161. package/build/plugins/webhook-manager.js.map +1 -1
  162. package/build/protocol.d.ts +1 -1
  163. package/build/protocol.d.ts.map +1 -1
  164. package/build/protocol.js +1 -1
  165. package/build/protocol.js.map +1 -1
  166. package/build/service.d.ts +11 -11
  167. package/build/service.d.ts.map +1 -1
  168. package/build/service.js +59 -23
  169. package/build/service.js.map +1 -1
  170. package/build/sync/types.d.ts +17 -13
  171. package/build/sync/types.d.ts.map +1 -1
  172. package/build/sync.d.ts.map +1 -1
  173. package/build/sync.js +22 -122
  174. package/build/sync.js.map +1 -1
  175. package/build/types.d.ts +46 -101
  176. package/build/types.d.ts.map +1 -1
  177. package/build/utils/types.d.ts +0 -8
  178. package/build/utils/types.d.ts.map +1 -1
  179. package/build/utils.d.ts +1 -4
  180. package/build/utils.d.ts.map +1 -1
  181. package/build/utils.js +1 -15
  182. package/build/utils.js.map +1 -1
  183. package/package.json +20 -22
  184. package/src/access/types.ts +7 -16
  185. package/src/access.ts +7 -15
  186. package/src/actions/resync.ts +5 -5
  187. package/src/actions/webhook.ts +8 -4
  188. package/src/bootstrap/types.ts +13 -7
  189. package/src/bootstrap.ts +23 -13
  190. package/src/catalogue/types.ts +3 -3
  191. package/src/catalogue.ts +4 -8
  192. package/src/checkout/types.ts +21 -0
  193. package/src/checkout.ts +414 -0
  194. package/src/config.ts +19 -66
  195. package/src/consts.ts +3 -69
  196. package/src/consumer/cancellation/types.ts +6 -4
  197. package/src/consumer/cancellation.ts +13 -7
  198. package/src/consumer/capture/types.ts +8 -12
  199. package/src/consumer/capture.ts +50 -59
  200. package/src/consumer/index.ts +4 -0
  201. package/src/consumer/reconcile.ts +69 -68
  202. package/src/consumer/records/types.ts +4 -4
  203. package/src/consumer/records.ts +19 -14
  204. package/src/consumer/service.ts +35 -22
  205. package/src/consumer/types.local.ts +1 -8
  206. package/src/consumer/types.ts +1 -4
  207. package/src/consumer/withdrawal/types.ts +14 -12
  208. package/src/consumer/withdrawal.ts +61 -77
  209. package/src/index.ts +12 -6
  210. package/src/paygate/providers.ts +48 -0
  211. package/src/paygate/registry.ts +53 -0
  212. package/src/paygate/types.ts +640 -0
  213. package/src/plan/types.ts +6 -1
  214. package/src/plan.ts +21 -3
  215. package/src/plugins/checkout-policy/types.ts +5 -3
  216. package/src/plugins/checkout-policy.ts +6 -5
  217. package/src/plugins/consts.local.ts +2 -26
  218. package/src/plugins/customer-lock/types.ts +2 -2
  219. package/src/plugins/customer-lock.ts +2 -2
  220. package/src/plugins/estimate/cache.ts +1 -1
  221. package/src/plugins/estimate/types.ts +10 -10
  222. package/src/plugins/estimate.ts +52 -128
  223. package/src/plugins/events/types.ts +17 -9
  224. package/src/plugins/events.ts +172 -190
  225. package/src/plugins/fx/types.ts +10 -23
  226. package/src/plugins/fx.ts +28 -64
  227. package/src/plugins/portal/types.ts +8 -38
  228. package/src/plugins/portal.ts +48 -229
  229. package/src/plugins/refunds/types.ts +11 -11
  230. package/src/plugins/refunds.ts +16 -40
  231. package/src/plugins/snapshot/types.ts +11 -0
  232. package/src/plugins/snapshot.ts +22 -0
  233. package/src/plugins/subscriptions/types.ts +17 -17
  234. package/src/plugins/subscriptions.ts +81 -81
  235. package/src/plugins/types.local.ts +9 -33
  236. package/src/plugins/webhook-manager/types.ts +15 -43
  237. package/src/plugins/webhook-manager.ts +24 -220
  238. package/src/protocol.ts +1 -1
  239. package/src/service.ts +66 -30
  240. package/src/sync/types.ts +17 -13
  241. package/src/sync.ts +25 -143
  242. package/src/types.ts +47 -97
  243. package/src/utils/types.ts +0 -9
  244. package/src/utils.ts +2 -20
  245. package/tests/capabilities.spec.ts +219 -0
  246. package/tests/entitlement.spec.ts +4 -4
  247. package/tests/gate-handlers.spec.ts +6 -6
  248. package/tests/gate.spec.ts +2 -2
  249. package/tests/grants.spec.ts +2 -2
  250. package/tests/internal-plan.spec.ts +2 -2
  251. package/tests/limit-gate.spec.ts +2 -2
  252. package/tests/paygates.spec.ts +132 -0
  253. package/tests/protocol.spec.ts +4 -5
  254. package/tests/usage.spec.ts +2 -2
  255. package/tests/webhook.spec.ts +137 -0
  256. package/build/plugins/session/types.d.ts +0 -34
  257. package/build/plugins/session/types.d.ts.map +0 -1
  258. package/build/plugins/session.d.ts +0 -4
  259. package/build/plugins/session.d.ts.map +0 -1
  260. package/build/plugins/session.js +0 -71
  261. package/build/plugins/session.js.map +0 -1
  262. package/build/plugins/status.d.ts +0 -8
  263. package/build/plugins/status.d.ts.map +0 -1
  264. package/build/plugins/status.js +0 -21
  265. package/build/plugins/status.js.map +0 -1
  266. package/build/plugins/stripe/types.d.ts +0 -20
  267. package/build/plugins/stripe/types.d.ts.map +0 -1
  268. package/build/plugins/stripe.d.ts +0 -6
  269. package/build/plugins/stripe.d.ts.map +0 -1
  270. package/build/plugins/stripe.js +0 -518
  271. package/build/plugins/stripe.js.map +0 -1
  272. package/src/plugins/session/types.ts +0 -30
  273. package/src/plugins/session.ts +0 -86
  274. package/src/plugins/status.ts +0 -22
  275. package/src/plugins/stripe/types.ts +0 -20
  276. package/src/plugins/stripe.ts +0 -571
  277. package/tests/checkout-consumer.spec.ts +0 -366
  278. package/tests/checkout-locks.spec.ts +0 -161
  279. package/tests/checkout-plugins.spec.ts +0 -164
  280. package/tests/checkout.spec.ts +0 -313
  281. package/tests/consumer-events.spec.ts +0 -215
  282. package/tests/consumer-fixtures.ts +0 -141
  283. package/tests/consumer-ops.spec.ts +0 -361
  284. package/tests/consumer-rights.integration.spec.ts +0 -147
  285. package/tests/consumer-rights.spec.ts +0 -606
  286. package/tests/context.ts +0 -110
  287. package/tests/estimate.spec.ts +0 -350
  288. package/tests/events.spec.ts +0 -367
  289. package/tests/fake-stripe.ts +0 -1004
  290. package/tests/gateway-options.spec.ts +0 -180
  291. package/tests/portal.spec.ts +0 -202
  292. package/tests/sync.spec.ts +0 -114
  293. package/tests/usage.integration.spec.ts +0 -165
  294. package/tests/webhook-manager.spec.ts +0 -149
  295. /package/build/{plugins/session → checkout}/types.js +0 -0
  296. /package/build/{plugins/stripe → paygate}/types.js +0 -0
@@ -0,0 +1,640 @@
1
+ import type { LazyService } from '@owlmeans/context'
2
+ import type { PortalFlow, SubscriptionStatus, TaxBehavior, TaxEstimate } from '@owlmeans/payment'
3
+ import type { Context as ApiContext } from '@owlmeans/server-api'
4
+ import type { DisputePhase, PaymentPlan, PaymentProduct, PaymentWebhookRecord, SyncedPriceOption } from '../types.js'
5
+
6
+ // ---------------------------------------------------------------------------------------------
7
+ // The provider contract
8
+ // ---------------------------------------------------------------------------------------------
9
+
10
+ /**
11
+ * What a paygate can put on a checkout beyond the required core. The host compares them with the
12
+ * declared policies when the gateway initializes: a declared policy feature a registered paygate
13
+ * lacks fails the boot (`PaygateError('feature:<flag>:<paygate>')`).
14
+ */
15
+ export interface PaygateFeatures {
16
+ /** The session calculates tax itself — required by the pricing policy's `tax.automatic`. */
17
+ automaticTax: boolean
18
+ /** The session collects a business tax id — required by `tax.collectTaxId`. */
19
+ taxIdCollection: boolean
20
+ /** The session shows and charges the buyer's local currency — required by `currency.adaptive`. */
21
+ adaptiveCurrency: boolean
22
+ /** The session collects the acceptance of the terms — required by the consumer-rights `checkoutTerms`. */
23
+ termsConsent: boolean
24
+ /** The longest text the session shows beside a control. Absent: no length is asserted. */
25
+ customTextMax?: number
26
+ /**
27
+ * The session lifetime the paygate accepts. The smallest `sessionTtlSeconds` of the checkout
28
+ * plugins is clamped into it; absent, a session takes no lifetime from the plugins at all.
29
+ */
30
+ sessionTtl?: { minSeconds: number, maxSeconds: number }
31
+ }
32
+
33
+ /**
34
+ * A payment provider: one paygate implementation, registered with
35
+ * `ensurePaymentProviders(ctx).register(provider)` (a provider package's `append…` helper does it).
36
+ * `bind` builds the paygate of one context; `paygatesOf(ctx)` calls it once per context and provider.
37
+ */
38
+ export interface PaymentProvider {
39
+ /** The paygate id stored on every record the paygate produced (`paygate`) and named in its webhook path. */
40
+ readonly id: string
41
+ readonly features: PaygateFeatures
42
+ bind: (ctx: ApiContext) => Paygate
43
+ }
44
+
45
+ /**
46
+ * One paygate bound to a context. `customers` and `checkout` are required; every other capability is
47
+ * optional and its absence has a defined host reaction:
48
+ *
49
+ * | Absent | Host reaction |
50
+ * |---|---|
51
+ * | `subscriptions` | recurring plans are not for sale through it; resync skips it; a subscription withdrawal is `review`; a cancellation is only received |
52
+ * | `payments` | every withdrawal is `review`; refunds and disputes resolve without charge or invoice reads |
53
+ * | `payments.creditNotes` | a withdrawal refunds proportionally, without a corrective document |
54
+ * | `webhook` | its webhook route throws `PaygateError('capability:webhook')`; the bootstrap skips the endpoint |
55
+ * | `catalogue` | the product sync skips it; a quantity or subscription checkout throws `ProductError('price:<lookup key>')` |
56
+ * | `portal` | a portal link throws `PortalUnavailable('paygate')`; the bootstrap skips the configuration |
57
+ * | `tax` | an estimate answers `at-checkout` |
58
+ * | `fx` | a conversion between two currencies throws `PaygateError('fx-rate:<from>:<to>')`; an estimate has no local line |
59
+ * | `checkout.listCompleted` | the consumer-rights reconcile backfills no purchase |
60
+ */
61
+ export interface Paygate {
62
+ readonly id: string
63
+ readonly features: PaygateFeatures
64
+ /** The currency the paygate settles in (lowercase), when it declares one; catalogue prices are converted into it. */
65
+ settlementCurrency: () => Promise<string | undefined>
66
+ customers: PaygateCustomers
67
+ checkout: PaygateCheckout
68
+ subscriptions?: PaygateSubscriptions
69
+ payments?: PaygatePayments
70
+ webhook?: PaygateWebhook
71
+ catalogue?: PaygateCatalogue
72
+ portal?: PaygatePortal
73
+ tax?: PaygateTax
74
+ fx?: PaygateFx
75
+ }
76
+
77
+ /** Every call that moves money or ends a contract carries the host's idempotency key and a note. */
78
+ export interface PaygateCall {
79
+ /** The key the paygate deduplicates a retried call by (`withdrawal:<id>:<step>[:<attempt>]`, `cancellation:<id>:schedule[:<attempt>]`). */
80
+ idempotencyKey: string
81
+ /** A note kept on the changed object (`withdrawal:<id>`, `cancellation:<id>`). */
82
+ comment?: string
83
+ }
84
+
85
+ /** Whose paygate state a call touches: the gateway's `owner`. */
86
+ export interface PaygateOwner {
87
+ owner: string
88
+ }
89
+
90
+ /** One deployment: its owner and its public webhook URL (`webhookOf(ctx).webhookUrlOf(paygate)`). */
91
+ export interface PaygateDeployment extends PaygateOwner {
92
+ webhookUrl: string
93
+ }
94
+
95
+ // ---------------------------------------------------------------------------------------------
96
+ // Customers
97
+ // ---------------------------------------------------------------------------------------------
98
+
99
+ export interface PaygateTaxId {
100
+ type: string
101
+ value: string
102
+ country?: string
103
+ }
104
+
105
+ /** A paygate customer as the host reads it. */
106
+ export interface PaygateCustomer {
107
+ id: string
108
+ email?: string
109
+ /** The saved address's country, as the paygate keeps it. */
110
+ country?: string
111
+ /** The saved address alone lets the paygate calculate tax (a country, plus what some countries need). */
112
+ taxLocatable: boolean
113
+ /** Read only when asked for (`retrieve(id, { taxIds: true })`). */
114
+ taxIds?: PaygateTaxId[]
115
+ taxExempt?: 'none' | 'exempt' | 'reverse'
116
+ }
117
+
118
+ export interface CustomerInput {
119
+ entityId: string
120
+ profileId?: string
121
+ service: string
122
+ /** The checkout locale, kept as the customer's preferred locale. */
123
+ locale?: string
124
+ /** Written to the customer — only passed under the paygate's own e-mail lock. */
125
+ email?: string
126
+ }
127
+
128
+ /** The customer fields the application pins on the paygate customer (provider configuration). */
129
+ export interface CustomerLocks {
130
+ /** Every checkout passes the buyer's verified e-mail, which the customer carries. */
131
+ email: boolean
132
+ /** The known billing country is pinned on the customer and Checkout keeps it. */
133
+ country: boolean
134
+ }
135
+
136
+ export interface PaygateCustomers {
137
+ /**
138
+ * The entity's customer — the stored one (its locale and, given, e-mail brought up to date), else a
139
+ * new one; the host's `payment-paygate-customer` row follows.
140
+ */
141
+ ensure: (input: CustomerInput) => Promise<PaygateCustomer>
142
+ /** A customer by id, or `null` when the paygate has none (deleted or missing). */
143
+ retrieve: (customerId: string, opts?: { taxIds?: boolean }) => Promise<PaygateCustomer | null>
144
+ /**
145
+ * Save a billing country on the customer: `replace` writes a country-only address (any other line
146
+ * cleared); without it the country is only preselected on an address-less customer.
147
+ */
148
+ setCountry: (customerId: string, country: string, opts: { replace: boolean }) => Promise<PaygateCustomer>
149
+ locks: () => Promise<CustomerLocks>
150
+ }
151
+
152
+ // ---------------------------------------------------------------------------------------------
153
+ // Checkout
154
+ // ---------------------------------------------------------------------------------------------
155
+
156
+ export type CheckoutMode = 'amount' | 'quantity' | 'subscription'
157
+
158
+ /** An amount checkout's single inline line. */
159
+ export interface AmountSessionLine {
160
+ kind: 'amount'
161
+ productSku: string
162
+ currency: string
163
+ unitAmountMinor: number
164
+ taxBehavior?: TaxBehavior
165
+ }
166
+
167
+ /** A quantity checkout's line on the product's reusable price, the quantity adjustable. */
168
+ export interface QuantitySessionLine {
169
+ kind: 'quantity'
170
+ priceId: string
171
+ minimum: number
172
+ maximum: number
173
+ default: number
174
+ }
175
+
176
+ /** A subscription's line on the plan's reusable price, quantity 1. */
177
+ export interface SubscriptionSessionLine {
178
+ kind: 'subscription'
179
+ priceId: string
180
+ }
181
+
182
+ export type SessionLine = AmountSessionLine | QuantitySessionLine | SubscriptionSessionLine
183
+
184
+ /** The texts a session shows; each already within `features.customTextMax`. */
185
+ export interface SessionTexts {
186
+ submit?: string
187
+ afterSubmit?: string
188
+ /** The label of the terms acceptance — only with `collectTerms`. */
189
+ terms?: string
190
+ }
191
+
192
+ /** One checkout session as the host decided it. */
193
+ export interface SessionInput {
194
+ mode: CheckoutMode
195
+ entityId: string
196
+ productSku: string
197
+ planSku: string
198
+ customerId: string
199
+ line: SessionLine
200
+ /** The session's charge currency (lowercase). */
201
+ currency: string
202
+ /** Force a quantity or subscription session onto `currency` (its reusable price carries it). */
203
+ forceCurrency: boolean
204
+ successUrl?: string
205
+ cancelUrl?: string
206
+ locale?: string
207
+ expiresAt?: Date
208
+ automaticTax: boolean
209
+ collectTaxId: boolean
210
+ /** The customer's saved address is kept: tax follows it and nothing typed at the paygate moves it. */
211
+ keepAddress: boolean
212
+ /** Show and charge the buyer's local currency on this session. */
213
+ adaptiveCurrency: boolean
214
+ /** Collect the acceptance of the terms, labelled `texts.terms`. */
215
+ collectTerms: boolean
216
+ texts: SessionTexts
217
+ /** The session's metadata — the host reads it back from every checkout event. */
218
+ metadata: Record<string, string>
219
+ /** A subscription session: the metadata of the subscription it creates. */
220
+ subscriptionMetadata?: Record<string, string>
221
+ }
222
+
223
+ export interface PaygateSession {
224
+ id: string
225
+ /** Where the buyer pays; `null` when the paygate made no usable session. */
226
+ url: string | null
227
+ }
228
+
229
+ export type CheckoutPaymentStatus = 'paid' | 'unpaid' | 'not-required'
230
+
231
+ /** What a checkout session says about its buyer. */
232
+ export interface CheckoutBuyer {
233
+ /** The billing address's country, as the paygate keeps it. */
234
+ country?: string
235
+ email?: string
236
+ name?: string
237
+ /** A business tax id was given, or the buyer is reverse-charged. */
238
+ business?: boolean
239
+ }
240
+
241
+ /** A checkout session as the host reads it from an event, a listing or a backfill. */
242
+ export interface CheckoutSnapshot {
243
+ id: string
244
+ /** A one-time `payment`, a `subscription`, or any other kind the host does not fulfil. */
245
+ mode: 'payment' | 'subscription' | 'other'
246
+ status?: string
247
+ payment: CheckoutPaymentStatus
248
+ /** Promotion codes could be entered. */
249
+ promotionCodes: boolean
250
+ metadata: Record<string, string>
251
+ customerId?: string
252
+ currency?: string
253
+ amountSubtotalMinor?: number | null
254
+ amountTotalMinor?: number | null
255
+ amountTaxMinor?: number | null
256
+ amountDiscountMinor?: number | null
257
+ paymentId?: string
258
+ invoiceId?: string
259
+ subscriptionId?: string
260
+ buyer: CheckoutBuyer
261
+ /** What the buyer saw in their own currency (local-currency pricing). */
262
+ presentment?: { currency?: string, amountMinor?: number }
263
+ /** The terms acceptance the session collected; absent when it collected none. */
264
+ termsAccepted?: boolean
265
+ createdAt?: Date
266
+ }
267
+
268
+ export interface CheckoutPage {
269
+ items: CheckoutSnapshot[]
270
+ /** Pass back for the next page; absent on the last one. */
271
+ cursor?: string
272
+ }
273
+
274
+ export interface PaygateCheckout {
275
+ /** Create the session; a session the paygate refuses for its own configuration alone may be created once more without that option. */
276
+ createSession: (input: SessionInput) => Promise<PaygateSession>
277
+ expireSession: (sessionId: string) => Promise<void>
278
+ /** The units a completed quantity checkout bought. */
279
+ units: (sessionId: string) => Promise<number>
280
+ /** Completed sessions created since `since`, one page at a time. */
281
+ listCompleted?: (since: Date, cursor?: string) => Promise<CheckoutPage>
282
+ }
283
+
284
+ // ---------------------------------------------------------------------------------------------
285
+ // Subscriptions
286
+ // ---------------------------------------------------------------------------------------------
287
+
288
+ /** A paygate subscription as the host reads it. */
289
+ export interface PaygateSubscription {
290
+ id: string
291
+ customerId?: string
292
+ metadata: Record<string, string>
293
+ status: SubscriptionStatus
294
+ /** The paygate's own status word. */
295
+ externalStatus: string
296
+ /** Collection is paused (or the subscription is). */
297
+ paused: boolean
298
+ /** The plan its item's price names (the price's lookup key, else its own plan sku). */
299
+ planSku?: string
300
+ itemId?: string
301
+ priceId?: string
302
+ unitAmountMinor?: number
303
+ quantity?: number
304
+ priceCurrency?: string
305
+ taxBehavior?: string
306
+ periodStart?: Date
307
+ periodEnd?: Date
308
+ cancelAtPeriodEnd: boolean
309
+ canceledAt?: Date
310
+ endedAt?: Date
311
+ trialEnd?: Date
312
+ latestInvoiceId?: string
313
+ currency?: string
314
+ createdAt?: Date
315
+ }
316
+
317
+ /** When an ordinary cancellation takes effect: the period end, or a later instant. */
318
+ export interface CancelSchedule {
319
+ atPeriodEnd: boolean
320
+ /** Without `atPeriodEnd`: the instant it ends (no proration). */
321
+ at?: Date
322
+ }
323
+
324
+ export interface PaygateSubscriptions {
325
+ /** A subscription by id, or `null` when the paygate no longer has it. */
326
+ retrieve: (subscriptionId: string) => Promise<PaygateSubscription | null>
327
+ /** End it at once, without proration or a final invoice. `alreadyCanceled`: it had ended. */
328
+ cancelNow: (subscriptionId: string, call: PaygateCall) => Promise<{ alreadyCanceled: boolean }>
329
+ scheduleCancel: (subscriptionId: string, when: CancelSchedule, call: PaygateCall) => Promise<void>
330
+ }
331
+
332
+ // ---------------------------------------------------------------------------------------------
333
+ // Payments
334
+ // ---------------------------------------------------------------------------------------------
335
+
336
+ export interface PaygateCharge {
337
+ id: string
338
+ paymentId?: string
339
+ invoiceId?: string
340
+ /** What the customer paid, tax included. */
341
+ amountMinor?: number
342
+ /** Refunded on it so far. */
343
+ refundedMinor?: number
344
+ }
345
+
346
+ export interface PaygateInvoice {
347
+ id: string
348
+ number?: string
349
+ /** Its first line — what a credit note corrects. */
350
+ firstLineId?: string
351
+ paymentId?: string
352
+ subscriptionId?: string
353
+ /** The customer address's country, as the paygate keeps it. */
354
+ country?: string
355
+ subtotalMinor?: number
356
+ taxMinor?: number
357
+ totalMinor?: number
358
+ currency?: string
359
+ }
360
+
361
+ export interface PaygateRefund {
362
+ id: string
363
+ /** The paygate's own status word; the host applies only a succeeded one. */
364
+ status: string
365
+ succeeded: boolean
366
+ amountMinor: number
367
+ currency: string
368
+ paymentId?: string
369
+ chargeId?: string
370
+ metadata: Record<string, string>
371
+ }
372
+
373
+ export interface PaygateDispute {
374
+ id: string
375
+ status: string
376
+ amountMinor: number
377
+ currency: string
378
+ paymentId?: string
379
+ chargeId?: string
380
+ }
381
+
382
+ export interface RefundInput {
383
+ paymentId: string
384
+ amountMinor: number
385
+ /** Kept on the refund and read back from its events (`withdrawalId`, `purchaseId`). */
386
+ metadata: Record<string, string>
387
+ }
388
+
389
+ /** One invoice line credited by an amount. */
390
+ export interface CreditNoteLine {
391
+ invoiceId: string
392
+ lineId: string
393
+ amountMinor: number
394
+ }
395
+
396
+ export interface CreditNoteInput extends CreditNoteLine {
397
+ /** The refund the credit note documents. */
398
+ refundId: string
399
+ memo: string
400
+ metadata: Record<string, string>
401
+ }
402
+
403
+ export interface PaygateCreditNote {
404
+ id: string
405
+ totalMinor: number
406
+ currency: string
407
+ }
408
+
409
+ export interface PaygateCreditNotes {
410
+ /** What a credit note of this line would total (tax included). */
411
+ preview: (line: CreditNoteLine) => Promise<{ totalMinor: number }>
412
+ create: (input: CreditNoteInput, call: PaygateCall) => Promise<PaygateCreditNote>
413
+ /** A live credit note of the invoice carrying this metadata — what a retry adopts. */
414
+ find: (invoiceId: string, metadata: Record<string, string>) => Promise<PaygateCreditNote | null>
415
+ }
416
+
417
+ export interface PaygatePayments {
418
+ /** A charge by id, or `null` when the paygate has none. */
419
+ charge: (chargeId: string) => Promise<PaygateCharge | null>
420
+ /** An invoice by id, or `null` when the paygate has none. */
421
+ invoice: (invoiceId: string) => Promise<PaygateInvoice | null>
422
+ refund: (input: RefundInput, call: PaygateCall) => Promise<PaygateRefund>
423
+ /** A live refund of the payment carrying this metadata — what a retry adopts. */
424
+ findRefund: (paymentId: string, metadata: Record<string, string>) => Promise<PaygateRefund | null>
425
+ creditNotes?: PaygateCreditNotes
426
+ }
427
+
428
+ // ---------------------------------------------------------------------------------------------
429
+ // Webhook
430
+ // ---------------------------------------------------------------------------------------------
431
+
432
+ /** A webhook delivery exactly as it arrived: the untouched raw body and the headers. */
433
+ export interface PaygateDelivery {
434
+ rawBody?: string | Buffer
435
+ headers: Record<string, string | string[] | undefined>
436
+ }
437
+
438
+ export interface EventCustomer {
439
+ id: string
440
+ email?: string
441
+ name?: string
442
+ taxId?: string
443
+ /** The saved address's country, as the paygate keeps it. */
444
+ country?: string
445
+ currency?: string
446
+ metadata: Record<string, string>
447
+ }
448
+
449
+ export interface CustomerEvent {
450
+ type: 'customer'
451
+ change: 'upserted' | 'deleted'
452
+ customer: EventCustomer
453
+ }
454
+
455
+ export interface CheckoutEvent {
456
+ type: 'checkout'
457
+ /** `completed` also for a delayed payment that succeeded; `failed` a delayed payment that failed. */
458
+ outcome: 'completed' | 'failed' | 'expired'
459
+ session: CheckoutSnapshot
460
+ }
461
+
462
+ export interface SubscriptionChangedEvent {
463
+ type: 'subscription'
464
+ eventId: string
465
+ /** When the paygate produced the event; an older payload than the stored state is not applied. */
466
+ createdAt: Date
467
+ subscription: PaygateSubscription
468
+ /** The subscription was deleted: stored as canceled, whatever its payload says. */
469
+ deleted: boolean
470
+ /** The paygate announced the trial ends soon. */
471
+ trialEnding: boolean
472
+ }
473
+
474
+ export interface EventInvoice {
475
+ id: string
476
+ subscriptionId?: string
477
+ customerId?: string
478
+ /** A renewal of the subscription's cycle. */
479
+ renewal: boolean
480
+ attempt?: number
481
+ nextAttemptAt?: Date
482
+ }
483
+
484
+ export interface InvoiceEvent {
485
+ type: 'invoice'
486
+ eventId: string
487
+ change: 'paid' | 'payment-failed' | 'action-required' | 'uncollectible' | 'voided'
488
+ invoice: EventInvoice
489
+ }
490
+
491
+ export interface RefundChangedEvent {
492
+ type: 'refund'
493
+ refund: PaygateRefund
494
+ /** The refunded charge, when the event carries it. */
495
+ charge?: PaygateCharge
496
+ }
497
+
498
+ export interface DisputeChangedEvent {
499
+ type: 'dispute'
500
+ phase: DisputePhase
501
+ dispute: PaygateDispute
502
+ }
503
+
504
+ export type PaygateEvent =
505
+ | CustomerEvent | CheckoutEvent | SubscriptionChangedEvent | InvoiceEvent | RefundChangedEvent | DisputeChangedEvent
506
+
507
+ export interface PaygateWebhook {
508
+ /**
509
+ * Verify a delivery against the owner's secrets and translate it into the events the host applies,
510
+ * in order — none for an event type the host does not process.
511
+ *
512
+ * @throws PaygateSignatureError | WebhookSetupError('secret')
513
+ */
514
+ verify: (delivery: PaygateDelivery, owner: PaygateOwner) => Promise<PaygateEvent[]>
515
+ /** Keep the deployment's endpoint at its webhook URL; the row is the host's `payment-webhook` record. */
516
+ ensureEndpoint: (deployment: PaygateDeployment, opts?: { force?: boolean }) => Promise<PaymentWebhookRecord | null>
517
+ }
518
+
519
+ // ---------------------------------------------------------------------------------------------
520
+ // Catalogue, portal, tax, FX
521
+ // ---------------------------------------------------------------------------------------------
522
+
523
+ /** A reusable price as the paygate holds it. */
524
+ export interface PaygatePrice {
525
+ id: string
526
+ currency: string
527
+ unitAmountMinor?: number
528
+ /** Further currencies it can be charged in. */
529
+ options: Record<string, { unitAmountMinor?: number }>
530
+ }
531
+
532
+ /** One plan resolved for the paygate: its lookup key, its amount in the paygate's currency and its options. */
533
+ export interface CataloguePlan {
534
+ plan: PaymentPlan
535
+ lookupKey: string
536
+ unitAmountMinor: number
537
+ currency: string
538
+ options: SyncedPriceOption[]
539
+ }
540
+
541
+ export interface CatalogueSync {
542
+ productId: string
543
+ prices: Array<{ planSku: string, priceId: string, taxBehavior?: string }>
544
+ }
545
+
546
+ export interface PaygateCatalogue {
547
+ /** The active reusable price under a lookup key. @throws ProductError('price:<lookup key>') */
548
+ price: (productSku: string, lookupKey: string) => Promise<PaygatePrice>
549
+ /**
550
+ * Bring one product and its plans' prices to what is declared; an amount plan has no reusable price
551
+ * (its stale ones are deactivated). Answers the price of every other plan.
552
+ */
553
+ syncProduct: (product: PaymentProduct, plans: CataloguePlan[], opts: { behavior: TaxBehavior | null }) => Promise<CatalogueSync>
554
+ }
555
+
556
+ export interface PortalSessionInput {
557
+ customerId: string
558
+ flow: PortalFlow
559
+ returnUrl: string
560
+ /** `Cancel`, `Update`, `Change`: the subscription acted on. */
561
+ subscriptionId?: string
562
+ /** `Change`: the subscription's item switched… */
563
+ itemId?: string
564
+ /** …to the price under this lookup key. */
565
+ lookupKey?: string
566
+ owner: string
567
+ /** The deployment, computed only when the configuration must be ensured first. */
568
+ deployment: () => PaygateDeployment
569
+ }
570
+
571
+ export interface PaygatePortal {
572
+ ensureConfiguration: (deployment: PaygateDeployment, opts?: { force?: boolean }) => Promise<string | null>
573
+ /** The portal session URL. */
574
+ createSession: (input: PortalSessionInput) => Promise<string>
575
+ }
576
+
577
+ export interface TaxInput {
578
+ currency: string
579
+ amountMinor: number
580
+ /** The line's own reference (the plan sku). */
581
+ reference: string
582
+ taxBehavior: TaxBehavior
583
+ taxCode?: string
584
+ country: string
585
+ /** The customer's tax ids of `country`. */
586
+ taxIds: Array<{ type: string, value: string }>
587
+ exemption?: 'exempt' | 'reverse'
588
+ }
589
+
590
+ export interface PaygateTax {
591
+ /** The tax at one country; `null` when the paygate cannot place the buyer (the estimate says `at-checkout`). */
592
+ calculate: (input: TaxInput) => Promise<TaxEstimate | null>
593
+ }
594
+
595
+ /** One unlocked rate: `to` units per one `from` unit. */
596
+ export interface FxQuote {
597
+ from: string
598
+ to: string
599
+ /** The fee-inclusive rate. */
600
+ exchangeRate: number
601
+ /** The rate before any fee. */
602
+ baseRate: number
603
+ /** The rate a catalogue amount is converted into the settlement currency at. */
604
+ referenceRate: number
605
+ fxFeeRate?: number
606
+ }
607
+
608
+ export interface PaygateFx {
609
+ quote: (from: string, to: string) => Promise<FxQuote | null>
610
+ }
611
+
612
+ // ---------------------------------------------------------------------------------------------
613
+ // The host's registry
614
+ // ---------------------------------------------------------------------------------------------
615
+
616
+ /** The registered payment providers of a context — a lazy service: providers register while the application is wired. */
617
+ export interface PaymentProvidersService extends LazyService {
618
+ /** Register a provider; one with the same id is replaced. */
619
+ register: (provider: PaymentProvider) => void
620
+ /** The provider of an id, or `null`. */
621
+ provider: (id: string) => PaymentProvider | null
622
+ /** Every registered provider, in registration order. */
623
+ providers: () => readonly PaymentProvider[]
624
+ }
625
+
626
+ /** The paygates of a context — each provider bound once. */
627
+ export interface PaygateRegistry {
628
+ /** The paygate of an id, or `null` when no provider of that id is registered. */
629
+ find: (id: string) => Paygate | null
630
+ /** @throws UnknownPaygate */
631
+ get: (id: string) => Paygate
632
+ /** Every registered paygate, in registration order. */
633
+ all: () => Paygate[]
634
+ /**
635
+ * The paygates a plan is for sale through, in registration order: the ones it is sold through
636
+ * (`planHelper.isSoldThrough` — absent `gateways` is any paygate) that can sell it (a recurring plan
637
+ * needs `subscriptions`), narrowed to `requested` when given.
638
+ */
639
+ forSale: (product: PaymentProduct, plan: PaymentPlan, requested?: string) => Paygate[]
640
+ }
package/src/plan/types.ts CHANGED
@@ -16,6 +16,11 @@ export interface PlanHelper {
16
16
  */
17
17
  overriddenPlan: (plan: PaymentPlan, row: Pick<PaymentSubscriptionRecord, 'overrides'> | null | undefined) => PaymentPlan
18
18
  planLookupKey: (product: PaymentProduct, plan: PaymentPlan) => string
19
- /** Whether a plan is sold through a paygate: never a free plan; otherwise its own gateways, else the product's. */
19
+ /**
20
+ * Whether a plan is sold through a paygate: never a free plan; otherwise its own `gateways`, else
21
+ * the product's — and neither declared means every paygate.
22
+ */
20
23
  isSoldThrough: (product: PaymentProduct, plan: PaymentPlan, paygate: string) => boolean
24
+ /** Whether a plan is sold at all: not free, and its gateways (its own, else the product's) are absent or not empty. */
25
+ isForSale: (product: PaymentProduct, plan: PaymentPlan) => boolean
21
26
  }
package/src/plan.ts CHANGED
@@ -32,10 +32,28 @@ export const createPlanHelper = (): PlanHelper => {
32
32
  const planLookupKey = (product: PaymentProduct, plan: PaymentPlan): string =>
33
33
  product.type === ProductType.Consumable ? `${product.sku}-consumable` : plan.sku
34
34
 
35
- const isSoldThrough = (product: PaymentProduct, plan: PaymentPlan, paygate: string): boolean =>
36
- plan.free !== true && (plan.gateways ?? product.gateways ?? []).includes(paygate)
35
+ const gatewaysOf = (product: PaymentProduct, plan: PaymentPlan): string[] | undefined =>
36
+ plan.gateways ?? product.gateways ?? undefined
37
37
 
38
- return { planRank, overriddenPlan, planLookupKey, isSoldThrough }
38
+ const isSoldThrough = (product: PaymentProduct, plan: PaymentPlan, paygate: string): boolean => {
39
+ if (plan.free === true) {
40
+ return false
41
+ }
42
+ const gateways = gatewaysOf(product, plan)
43
+
44
+ return gateways == null || gateways.includes(paygate)
45
+ }
46
+
47
+ const isForSale = (product: PaymentProduct, plan: PaymentPlan): boolean => {
48
+ if (plan.free === true) {
49
+ return false
50
+ }
51
+ const gateways = gatewaysOf(product, plan)
52
+
53
+ return gateways == null || gateways.length > 0
54
+ }
55
+
56
+ return { planRank, overriddenPlan, planLookupKey, isSoldThrough, isForSale }
39
57
  }
40
58
 
41
59
  export const planHelper = createPlanHelper()