@owlmeans/server-payment 0.1.20-rc.1 → 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,219 @@
1
+ import { describe, expect, test } from 'bun:test'
2
+ import {
3
+ ConsumerRightsError, PortalFlow, PortalUnavailable, ProductError, PurchaseKind, SubscriptionStatus,
4
+ TaxEstimateStatus, WithdrawalStatus,
5
+ } from '@owlmeans/payment'
6
+ import {
7
+ CREDITS_PRODUCT, future, makeFakeContext, makeFakePaygate, past, PLANS_PRODUCT, PRO,
8
+ type FakeContextOptions, type FakePaygate, type FakePaygateOptions,
9
+ } from '@owlmeans/test-payment'
10
+ import {
11
+ estimateOf, makeEstimateCache, paymentAccessOf, productSyncOf, type CheckoutAttempt, type CheckoutPlugin,
12
+ type ConsumerRightsDef, type UsageMeter,
13
+ } from '../src/index.js'
14
+
15
+ const ENTITY = 'entity-1'
16
+ const successUrl = 'https://app.example.com/ok'
17
+ const MANAGED = { manage: true, bootstrap: false }
18
+
19
+ const managed = async (paygate: FakePaygateOptions = {}, opts: FakeContextOptions = {}) => {
20
+ const fake = makeFakePaygate(paygate)
21
+ const context = await makeFakeContext({ providers: [fake.provider], gateway: MANAGED, ...opts })
22
+ return { ...context, fake }
23
+ }
24
+ const methods = (fake: FakePaygate) => fake.state.calls.map(call => call.method)
25
+
26
+ const RIGHTS: ConsumerRightsDef = {
27
+ textVersion: 'terms-v1',
28
+ links: { en: { billingTerms: 'https://legal.example.com/billing', withdrawalInformation: 'https://legal.example.com/withdrawal' } },
29
+ mechanisms: { withdrawal: true, automaticRefunds: true },
30
+ trader: { name: 'Example', legalName: 'Example Ltd', address: '1 Example Street', email: 'support@example.com' },
31
+ }
32
+ const meter: UsageMeter = { used: async () => ({ granted: 1_000, used: 0, usedAfter: 0 }) }
33
+
34
+ /** A paid, in-scope purchase of the fake paygate with an open window. */
35
+ const purchaseRow = (patch: Record<string, unknown> = {}) => ({
36
+ purchaseId: 'fake:cs_1', contractRef: 'CR-261102-ABCDEF', entityId: ENTITY, kind: PurchaseKind.TopUp, paygate: 'fake',
37
+ sessionId: 'cs_1', paymentIntentId: 'pay_1', productSku: CREDITS_PRODUCT, inScope: true, language: 'en', currency: 'usd',
38
+ amountSubtotalMinor: 1_000, amountTaxMinor: 0, amountTotalMinor: 1_000, purchasedAt: past(1), deadline: future(13),
39
+ createdAt: past(1), ...patch,
40
+ })
41
+
42
+ describe('@owlmeans/server-payment — the checkout a paygate is asked for', () => {
43
+ test('an amount checkout: the inline line, the policy\'s options and the metadata the events read back', async () => {
44
+ const { ctx, fake } = await managed()
45
+ const url = await paymentAccessOf(ctx).gateway().createLink(ctx, {
46
+ productSku: CREDITS_PRODUCT, entityId: ENTITY, profileId: 'profile-1', service: 'app', amountMinor: 1_000,
47
+ locale: 'de', submitText: 'Top up', successUrl,
48
+ })
49
+
50
+ expect(url).toStartWith('https://pay.example.test/')
51
+ expect(fake.state.sessions).toEqual([{
52
+ mode: 'amount', entityId: ENTITY, productSku: CREDITS_PRODUCT, planSku: 'app-credit-unit', customerId: 'cus_1',
53
+ line: { kind: 'amount', productSku: CREDITS_PRODUCT, currency: 'usd', unitAmountMinor: 1_021 },
54
+ currency: 'usd', forceCurrency: false, successUrl, cancelUrl: successUrl, locale: 'de',
55
+ automaticTax: true, collectTaxId: true, keepAddress: false, adaptiveCurrency: false, collectTerms: false,
56
+ texts: { submit: 'Top up' },
57
+ metadata: {
58
+ pricingMode: 'amount', currency: 'usd', entityId: ENTITY, profileId: 'profile-1', service: 'app',
59
+ productSku: CREDITS_PRODUCT, planSku: 'app-credit-unit', amountCurrency: 'usd', amountMinor: '1000',
60
+ sourceChargeAmountMinor: '1021', chargeAmountMinor: '1021',
61
+ },
62
+ }])
63
+ expect(methods(fake)).toEqual(['customers.ensure', 'checkout.createSession'])
64
+ })
65
+
66
+ test('a plugin\'s lifetime is clamped into the paygate\'s bounds; a paygate without bounds takes none', async () => {
67
+ const attempts: CheckoutAttempt[] = []
68
+ const plugin: CheckoutPlugin = { alias: 'hold', sessionTtlSeconds: 60, admit: async (_ctx, attempt) => { attempts.push(attempt) } }
69
+ const bounded = await managed()
70
+ paymentAccessOf(bounded.ctx).gateway().use(plugin)
71
+ await paymentAccessOf(bounded.ctx).gateway().createLink(bounded.ctx, { productSku: CREDITS_PRODUCT, entityId: ENTITY, service: 'app', amountMinor: 1_000 })
72
+ expect(attempts[0].sessionTtlSeconds).toBe(30 * 60)
73
+ expect(bounded.fake.state.sessions[0].expiresAt).toBeInstanceOf(Date)
74
+
75
+ const open = await managed({ features: { sessionTtl: undefined } })
76
+ paymentAccessOf(open.ctx).gateway().use(plugin)
77
+ await paymentAccessOf(open.ctx).gateway().createLink(open.ctx, { productSku: CREDITS_PRODUCT, entityId: ENTITY, service: 'app', amountMinor: 1_000 })
78
+ expect(attempts[1].sessionTtlSeconds).toBeUndefined()
79
+ expect(open.fake.state.sessions[0].expiresAt).toBeUndefined()
80
+ })
81
+
82
+ test('a text longer than the paygate shows is refused before any session', async () => {
83
+ const { ctx, fake } = await managed({ features: { customTextMax: 5 } })
84
+ await expect(paymentAccessOf(ctx).gateway().createLink(ctx, {
85
+ productSku: CREDITS_PRODUCT, entityId: ENTITY, service: 'app', amountMinor: 1_000, submitText: 'far too long',
86
+ })).rejects.toBeInstanceOf(ConsumerRightsError)
87
+ expect(methods(fake)).not.toContain('checkout.createSession')
88
+ })
89
+
90
+ test('a created session a plugin refuses is expired and every hold released; a session without a URL is refused', async () => {
91
+ const settled: string[] = []
92
+ const { ctx, fake } = await managed()
93
+ paymentAccessOf(ctx).gateway().use({ alias: 'hold', admit: async () => ({ reservationId: 'r1' }), settled: async (_ctx, done) => { settled.push(done.outcome) } })
94
+ paymentAccessOf(ctx).gateway().use({ alias: 'veto', created: async () => { throw new Error('veto') } })
95
+ await expect(paymentAccessOf(ctx).gateway().createLink(ctx, { productSku: CREDITS_PRODUCT, entityId: ENTITY, service: 'app', amountMinor: 1_000 }))
96
+ .rejects.toThrow('veto')
97
+ expect(fake.state.expired).toEqual(['cs_2'])
98
+ expect(settled).toEqual(['failed'])
99
+
100
+ fake.state.sessionUrl = null
101
+ paymentAccessOf(ctx).gateway().use({ alias: 'veto' })
102
+ await expect(paymentAccessOf(ctx).gateway().createLink(ctx, { productSku: CREDITS_PRODUCT, entityId: ENTITY, service: 'app', amountMinor: 1_000 }))
103
+ .rejects.toThrow('paygate:session')
104
+ })
105
+
106
+ test('the customer e-mail lock requires a usable e-mail before any paygate call, and pins it', async () => {
107
+ const { ctx, fake } = await managed({ state: { locks: { email: true, country: false } } })
108
+ await expect(paymentAccessOf(ctx).gateway().createLink(ctx, { productSku: CREDITS_PRODUCT, entityId: ENTITY, service: 'app', amountMinor: 1_000 }))
109
+ .rejects.toThrow('paygate:customer-email')
110
+ expect(fake.state.calls).toEqual([])
111
+
112
+ await paymentAccessOf(ctx).gateway().createLink(ctx, {
113
+ productSku: CREDITS_PRODUCT, entityId: ENTITY, service: 'app', amountMinor: 1_000, email: ' owner@shop.eu ',
114
+ })
115
+ expect(fake.state.calls[0]).toEqual({ method: 'customers.ensure', args: [{ entityId: ENTITY, service: 'app', email: 'owner@shop.eu' }] })
116
+ })
117
+ })
118
+
119
+ describe('@owlmeans/server-payment — a capability the paygate lacks', () => {
120
+ test('subscriptions: a recurring plan is not for sale through it, resync skips its rows', async () => {
121
+ const { ctx, fake } = await managed({ without: ['subscriptions'] }, {
122
+ seed: { 'payment-subscription': [{ id: 'row-1', entityId: ENTITY, planSku: PRO, productSku: PLANS_PRODUCT, service: 'app', paygate: 'fake', externalId: 'sub_1', status: SubscriptionStatus.Active, rank: 10, createdAt: past(5) }] },
123
+ })
124
+ await expect(paymentAccessOf(ctx).gateway().createLink(ctx, { productSku: PLANS_PRODUCT, planSku: PRO, entityId: ENTITY, service: 'app' }))
125
+ .rejects.toThrow(`plan:${PRO}`)
126
+ expect(await paymentAccessOf(ctx).gateway().resyncAll(ctx)).toEqual({ scanned: 0, updated: 0 })
127
+ expect(fake.state.calls).toEqual([])
128
+ })
129
+
130
+ test('subscriptions, kept: a subscription the paygate no longer has is canceled by resync', async () => {
131
+ const { ctx, observed } = await managed({}, {
132
+ seed: { 'payment-subscription': [{ id: 'row-1', entityId: ENTITY, planSku: PRO, productSku: PLANS_PRODUCT, service: 'app', paygate: 'fake', externalId: 'sub_gone', status: SubscriptionStatus.Active, rank: 10, createdAt: past(5), propagated: { planSku: PRO, rank: 10, status: SubscriptionStatus.Active } }] },
133
+ })
134
+ expect(await paymentAccessOf(ctx).gateway().resyncAll(ctx)).toEqual({ scanned: 1, updated: 1 })
135
+ expect(observed.subscription.map(event => event.change)).toEqual(['canceled'])
136
+ })
137
+
138
+ test('catalogue: a subscription checkout has no price to sell, and the product sync skips the paygate', async () => {
139
+ const { ctx, fake } = await managed({ without: ['catalogue'] })
140
+ await expect(paymentAccessOf(ctx).gateway().createLink(ctx, { productSku: PLANS_PRODUCT, planSku: PRO, entityId: ENTITY, service: 'app' }))
141
+ .rejects.toBeInstanceOf(ProductError)
142
+ await productSyncOf(ctx).syncPaymentProducts()
143
+ expect(methods(fake)).not.toContain('catalogue.syncProduct')
144
+
145
+ const sold = await managed({ state: { prices: { [PRO]: { id: 'price_pro', currency: 'usd', unitAmountMinor: 2_000, options: {} } } } })
146
+ await paymentAccessOf(sold.ctx).gateway().createLink(sold.ctx, { productSku: PLANS_PRODUCT, planSku: PRO, entityId: ENTITY, service: 'app' })
147
+ expect(sold.fake.state.sessions[0]).toEqual(expect.objectContaining({
148
+ mode: 'subscription', line: { kind: 'subscription', priceId: 'price_pro' }, currency: 'usd', forceCurrency: false,
149
+ subscriptionMetadata: { pricingMode: 'quantity', entityId: ENTITY, service: 'app', productSku: PLANS_PRODUCT, planSku: PRO },
150
+ }))
151
+ })
152
+
153
+ test('fx: a charge in the settlement currency is refused without a rate; with one it is converted, rounded up', async () => {
154
+ const plain = await managed({ without: ['fx'], state: { settlementCurrency: 'eur' } })
155
+ await expect(paymentAccessOf(plain.ctx).gateway().createLink(plain.ctx, { productSku: CREDITS_PRODUCT, entityId: ENTITY, service: 'app', amountMinor: 1_000 }))
156
+ .rejects.toThrow('paygate:fx-rate:usd:eur')
157
+
158
+ const quoted = await managed({ state: { settlementCurrency: 'eur', quotes: { 'usd:eur': { from: 'usd', to: 'eur', exchangeRate: 0.88, baseRate: 0.9, referenceRate: 0.9 } } } })
159
+ await paymentAccessOf(quoted.ctx).gateway().createLink(quoted.ctx, { productSku: CREDITS_PRODUCT, entityId: ENTITY, service: 'app', amountMinor: 1_000 })
160
+ expect(quoted.fake.state.sessions[0].line).toEqual({ kind: 'amount', productSku: CREDITS_PRODUCT, currency: 'eur', unitAmountMinor: 919 })
161
+ })
162
+
163
+ test('portal: refused for the paygate; kept: the session names the entitling subscription', async () => {
164
+ const seed = {
165
+ 'payment-paygate-customer': [{ id: 'customer-1', paygate: 'fake', externalId: 'cus_1', entityId: ENTITY }],
166
+ 'payment-subscription': [{ id: 'row-1', entityId: ENTITY, planSku: PRO, productSku: PLANS_PRODUCT, service: 'app', paygate: 'fake', externalId: 'sub_1', itemId: 'item_1', status: SubscriptionStatus.Active, rank: 10, createdAt: past(5) }],
167
+ }
168
+ const plain = await managed({ without: ['portal'] }, { seed })
169
+ await expect(paymentAccessOf(plain.ctx).gateway().portalLink(plain.ctx, ENTITY, { flow: PortalFlow.Manage, returnUrl: successUrl }))
170
+ .rejects.toBeInstanceOf(PortalUnavailable)
171
+
172
+ const kept = await managed({}, { seed })
173
+ await paymentAccessOf(kept.ctx).gateway().portalLink(kept.ctx, ENTITY, { flow: PortalFlow.Cancel, returnUrl: successUrl })
174
+ expect(kept.fake.state.portalSessions).toEqual([expect.objectContaining({
175
+ customerId: 'cus_1', flow: PortalFlow.Cancel, subscriptionId: 'sub_1', owner: 'app', returnUrl: successUrl,
176
+ })])
177
+ await expect(paymentAccessOf(kept.ctx).gateway().portalLink(kept.ctx, 'nobody', { flow: PortalFlow.Manage, returnUrl: successUrl }))
178
+ .rejects.toThrow('portal-unavailable:customer')
179
+ })
180
+
181
+ test('tax: an estimate says at-checkout without a calculation', async () => {
182
+ const plain = await managed({ without: ['tax'] })
183
+ const estimate = await estimateOf(plain.ctx).estimatePrice({ productSku: CREDITS_PRODUCT, entityId: ENTITY, country: 'PL' }, makeEstimateCache())
184
+ expect(estimate).toEqual(expect.objectContaining({ country: 'PL', source: 'request', currency: 'usd' }))
185
+ expect(estimate.tax.status).toBe(TaxEstimateStatus.AtCheckout)
186
+ expect(methods(plain.fake)).toEqual([])
187
+ })
188
+
189
+ test('payments: every withdrawal is left to an operator; no credit notes: a proportional refund on the payment', async () => {
190
+ const opts = { consumerRights: RIGHTS, meter, seed: { 'payment-purchase': [purchaseRow()] } }
191
+ const unpaid = await managed({ without: ['payments'] }, opts)
192
+ const review = await paymentAccessOf(unpaid.ctx).consumerRights().withdraw({ entityId: ENTITY }, {
193
+ purchaseId: 'fake:cs_1', name: 'Anna', email: 'anna@shop.eu', language: 'en',
194
+ })
195
+ expect(review.status).toBe(WithdrawalStatus.Review)
196
+ expect(methods(unpaid.fake)).toEqual([])
197
+
198
+ const plain = await managed({ without: ['creditNotes'] }, {
199
+ ...opts, seed: { 'payment-purchase': [purchaseRow({ invoiceId: 'inv_1', invoiceLineId: 'line_1' })] },
200
+ })
201
+ const refunded = await paymentAccessOf(plain.ctx).consumerRights().withdraw({ entityId: ENTITY }, {
202
+ purchaseId: 'fake:cs_1', name: 'Anna', email: 'anna@shop.eu', language: 'en',
203
+ })
204
+ expect(refunded).toEqual(expect.objectContaining({ status: WithdrawalStatus.Refunded, refundMinor: 1_000 }))
205
+ expect(plain.fake.state.calls).toEqual([{
206
+ method: 'payments.refund',
207
+ args: [
208
+ { paymentId: 'pay_1', amountMinor: 1_000, metadata: { withdrawalId: expect.any(String), purchaseId: 'fake:cs_1' } },
209
+ { idempotencyKey: expect.stringMatching(/^withdrawal:.+:refund$/) },
210
+ ],
211
+ }])
212
+ })
213
+
214
+ test('listCompleted: the consumer-rights reconcile backfills nothing from the paygate', async () => {
215
+ const plain = await managed({ without: ['listCompleted'] }, { consumerRights: RIGHTS })
216
+ expect((await paymentAccessOf(plain.ctx).consumerRights().reconcile()).backfilled).toBe(0)
217
+ expect(methods(plain.fake)).toEqual([])
218
+ })
219
+ })
@@ -3,14 +3,14 @@ import { INTERNAL_PAYGATE, LimitExhausted, LimitKind, PlanRequired, Subscription
3
3
  import type { PaymentSubscriptionRecord } from '../src/types.js'
4
4
  import {
5
5
  CAP_PREVIEW, CAP_WHITELABEL, FREE, future, makeFakeContext, past, PLANS_PRODUCT, PRO, TEAM,
6
- } from './fake-stripe.js'
7
- import type { FakeContext } from './fake-stripe.js'
6
+ } from '@owlmeans/test-payment'
7
+ import type { FakeContext } from '@owlmeans/test-payment'
8
8
  import { paymentAccessOf } from '../src/access.js'
9
9
 
10
10
  const seed = async (
11
11
  fake: FakeContext, planSku: string, overrides: Partial<PaymentSubscriptionRecord> = {},
12
12
  ): Promise<PaymentSubscriptionRecord> => await paymentAccessOf(fake.ctx).subscriptions().create({
13
- entityId: 'entity-1', planSku, productSku: PLANS_PRODUCT, service: 'app', paygate: 'stripe',
13
+ entityId: 'entity-1', planSku, productSku: PLANS_PRODUCT, service: 'app', paygate: 'fake',
14
14
  externalId: `sub_${planSku}_${Math.random().toString(16).slice(2, 8)}`, status: SubscriptionStatus.Active,
15
15
  rank: planSku === TEAM ? 20 : planSku === PRO ? 10 : 0, createdAt: past(5), ...overrides,
16
16
  })
@@ -95,7 +95,7 @@ describe('@owlmeans/server-payment — the entitlement view', () => {
95
95
 
96
96
  const view = await paymentAccessOf(fake.ctx).entitlements().entitlements('entity-1')
97
97
  expect(view.plan).toEqual(expect.objectContaining({
98
- sku: PRO, rank: 10, free: false, status: SubscriptionStatus.PastDue, paygate: 'stripe',
98
+ sku: PRO, rank: 10, free: false, status: SubscriptionStatus.PastDue, paygate: 'fake',
99
99
  subscriptionId: row.externalId, pastDue: true, fallbackSku: FREE, cancelAtPeriodEnd: true,
100
100
  }))
101
101
  expect(view.plan.subscribedAt?.getTime()).toBe(row.createdAt.getTime())
@@ -8,8 +8,8 @@ import { bind, bindAll } from '@owlmeans/server-entrypoint'
8
8
  import type { ServerEntrypoint } from '@owlmeans/server-entrypoint'
9
9
  import { paymentGate, paymentGateEntrypoints, paymentGateHandlers, webhookOf } from '../src/index.js'
10
10
  import type { PaygateParams, ResyncResult, ResyncSubscriptionsResult } from '../src/index.js'
11
- import { makeFakeContext } from './fake-stripe.js'
12
- import type { FakeContext } from './fake-stripe.js'
11
+ import { makeFakeContext } from '@owlmeans/test-payment'
12
+ import type { FakeContext } from '@owlmeans/test-payment'
13
13
 
14
14
  const HOOKS = 'app-hooks'
15
15
  const HOOKS_HOST = 'hooks.example.com'
@@ -99,8 +99,8 @@ describe('@owlmeans/server-payment — the payment gate handlers', () => {
99
99
 
100
100
  expect(unmanaged.ctx.hasEntrypoint(paymentGate.webhook.alias)).toBe(false)
101
101
  expect((await run(webhook!, { paygate: 'paypal' })).error).toBeInstanceOf(UnknownPaygate)
102
- // Reached this process's gateway: unmanaged here, so a Stripe delivery and a resync are refused.
103
- expect((await run(webhook!, { paygate: 'stripe' })).error).toBeInstanceOf(PaygateError)
102
+ // Reached this process's gateway: unmanaged here, so a registered paygate's delivery and a resync are refused.
103
+ expect((await run(webhook!, { paygate: 'fake' })).error).toBeInstanceOf(PaygateError)
104
104
  expect((await run(resync!)).error).toBeInstanceOf(PaygateError)
105
105
 
106
106
  const managed = await hookProcess(true)
@@ -110,10 +110,10 @@ describe('@owlmeans/server-payment — the payment gate handlers', () => {
110
110
  expect(await run(resyncSubscriptions!)).toEqual(expect.objectContaining({ value: { scanned: 0, updated: 0 } }))
111
111
  })
112
112
 
113
- test('the application\'s webhook path is the one Stripe is pointed at — a re-declaration keeps the library\'s path', async () => {
113
+ test('the application\'s webhook path is the one a paygate is pointed at — a re-declaration keeps the library\'s path', async () => {
114
114
  const hooks = await hookProcess(false)
115
115
 
116
- expect(webhookOf(hooks.ctx).webhookUrlOf()).toBe(`https://${HOOKS_HOST}/payment-gate/webhook/stripe`)
116
+ expect(webhookOf(hooks.ctx).webhookUrlOf('fake')).toBe(`https://${HOOKS_HOST}/payment-gate/webhook/fake`)
117
117
  expect([own.base.route.route.path, own.webhook.route.route.path])
118
118
  .toEqual([paymentGate.base.route.route.path, paymentGate.webhook.route.route.path])
119
119
  })
@@ -4,8 +4,8 @@ import type { Auth, PermissionSet } from '@owlmeans/auth'
4
4
  import { AuthForbidden, AuthRole } from '@owlmeans/auth'
5
5
  import { CapabilityRequired, ENTITLEMENT_GATE, SubscriptionStatus } from '@owlmeans/payment'
6
6
  import { makeCapabilityGate } from '../src/gate.js'
7
- import { CAP_BASIC, CAP_PREVIEW, CAP_WHITELABEL, makeFakeContext, past, PRO } from './fake-stripe.js'
8
- import type { FakeContext } from './fake-stripe.js'
7
+ import { CAP_BASIC, CAP_PREVIEW, CAP_WHITELABEL, makeFakeContext, past, PRO } from '@owlmeans/test-payment'
8
+ import type { FakeContext } from '@owlmeans/test-payment'
9
9
  import { paymentAccessOf } from '../src/access.js'
10
10
  import { catalogueOf } from '../src/catalogue.js'
11
11
 
@@ -7,8 +7,8 @@ import {
7
7
  import { UnsupportedArgumentError } from '@owlmeans/resource'
8
8
  import { EntitlementGrantKind, EntitlementGrantStatus } from '../src/consts.js'
9
9
  import type { EntitlementGrantSpec, PaymentGrantRecord } from '../src/types.js'
10
- import { CAP_PREVIEW, CAP_WHITELABEL, future, makeFakeContext, past, PRO } from './fake-stripe.js'
11
- import type { FakeContext } from './fake-stripe.js'
10
+ import { CAP_PREVIEW, CAP_WHITELABEL, future, makeFakeContext, past, PRO } from '@owlmeans/test-payment'
11
+ import type { FakeContext } from '@owlmeans/test-payment'
12
12
  import { paymentAccessOf } from '../src/access.js'
13
13
  import { catalogueOf } from '../src/catalogue.js'
14
14
 
@@ -1,7 +1,7 @@
1
1
  import { describe, expect, test } from 'bun:test'
2
2
  import { SubscriptionStatus } from '@owlmeans/payment'
3
- import { FREE, future, makeFakeContext, past, PRO } from './fake-stripe.js'
4
- import type { FakeContext } from './fake-stripe.js'
3
+ import { FREE, future, makeFakeContext, past, PRO } from '@owlmeans/test-payment'
4
+ import type { FakeContext } from '@owlmeans/test-payment'
5
5
  import { paymentAccessOf } from '../src/access.js'
6
6
 
7
7
  const ENTITY = 'entity-1'
@@ -3,8 +3,8 @@ import type { AbstractRequest, AbstractResponse, GateService } from '@owlmeans/e
3
3
  import type { Auth } from '@owlmeans/auth'
4
4
  import { AuthForbidden, AuthRole } from '@owlmeans/auth'
5
5
  import { LIMIT_GATE, LimitExhausted, LimitWindow, planLimitHelper } from '@owlmeans/payment'
6
- import { makeFakeContext, PRO } from './fake-stripe.js'
7
- import type { FakeContext } from './fake-stripe.js'
6
+ import { makeFakeContext, PRO } from '@owlmeans/test-payment'
7
+ import type { FakeContext } from '@owlmeans/test-payment'
8
8
  import { paymentAccessOf } from '../src/access.js'
9
9
 
10
10
  const request = (auth: Partial<Auth> | null = {}): AbstractRequest => ({
@@ -0,0 +1,132 @@
1
+ import { describe, expect, test } from 'bun:test'
2
+ import { CheckoutPricingMode, PaygateError, PlanDuration, UnknownPaygate } from '@owlmeans/payment'
3
+ import {
4
+ CREDIT_UNIT, CREDITS_PRODUCT, FREE, makeFakeContext, makeFakePaygate, PLANS_PRODUCT, PRO,
5
+ } from '@owlmeans/test-payment'
6
+ import {
7
+ ensurePaymentProviders, makePaygateRegistry, paygateBootstrapOf, paygatesOf, paymentAccessOf, PAYMENT_PROVIDERS,
8
+ type PaymentPlan, type PaymentProduct,
9
+ } from '../src/index.js'
10
+
11
+ const planOf = async (ctx: Parameters<typeof paymentAccessOf>[0], sku: string): Promise<{ product: PaymentProduct, plan: PaymentPlan }> => {
12
+ const plan = await paymentAccessOf(ctx).payment().plan(sku) as PaymentPlan
13
+ const product = await paymentAccessOf(ctx).payment().product(plan.productSku) as PaymentProduct
14
+
15
+ return { product, plan }
16
+ }
17
+
18
+ describe('@owlmeans/server-payment — payment providers and the paygate registry', () => {
19
+ test('providers keep their registration order; one registered under a taken id replaces it and is bound anew', async () => {
20
+ const one = makeFakePaygate({ id: 'one' })
21
+ const two = makeFakePaygate({ id: 'two' })
22
+ const fake = await makeFakeContext({ providers: [one.provider, two.provider] })
23
+ const registry = paygatesOf(fake.ctx)
24
+ const before = registry.get('one')
25
+
26
+ expect(registry.all().map(paygate => paygate.id)).toEqual(['one', 'two'])
27
+ expect(registry.get('one')).toBe(before)
28
+
29
+ const replacement = makeFakePaygate({ id: 'one' })
30
+ ensurePaymentProviders(fake.ctx as never).register(replacement.provider)
31
+ expect(registry.all().map(paygate => paygate.id)).toEqual(['one', 'two'])
32
+ expect(registry.get('one')).not.toBe(before)
33
+ expect(ensurePaymentProviders(fake.ctx as never).provider('one')).toBe(replacement.provider)
34
+ expect(() => ensurePaymentProviders(fake.ctx as never).register({ ...replacement.provider, id: ' ' })).toThrow(PaygateError)
35
+ })
36
+
37
+ test('without a provider — or without the providers service at all — find answers null and get throws UnknownPaygate', async () => {
38
+ const empty = await makeFakeContext({ providers: [] })
39
+ expect(paymentAccessOf(empty.ctx).paygates().find('fake')).toBeNull()
40
+ expect(() => paygatesOf(empty.ctx).get('fake')).toThrow(UnknownPaygate)
41
+ expect(empty.ctx.hasService(PAYMENT_PROVIDERS)).toBe(true)
42
+
43
+ const bare = makePaygateRegistry({ hasService: () => false } as never)
44
+ expect(bare.all()).toEqual([])
45
+ expect(() => bare.get('fake')).toThrow(UnknownPaygate)
46
+ })
47
+
48
+ test('forSale: absent gateways sell through every paygate, declared ones through those only, a recurring plan needs subscriptions', async () => {
49
+ const full = makeFakePaygate({ id: 'full' })
50
+ const once = makeFakePaygate({ id: 'once', without: ['subscriptions'] })
51
+ const fake = await makeFakeContext({
52
+ providers: [full.provider, once.provider],
53
+ catalogue: {
54
+ plans: [{
55
+ productSku: CREDITS_PRODUCT, sku: 'once-only', duration: PlanDuration.Consumable, price: 0.02, gateways: ['once'],
56
+ pricingMode: CheckoutPricingMode.Amount,
57
+ amountPolicy: { currency: 'usd', minimumMinor: 500, maximumMinor: 5_000, defaultMinor: 1_000, presetsMinor: [1_000], fixedMinor: 0, rateBps: 0 },
58
+ }],
59
+ },
60
+ })
61
+ const registry = paygatesOf(fake.ctx)
62
+ const ids = (paygates: Array<{ id: string }>) => paygates.map(paygate => paygate.id)
63
+
64
+ const pro = await planOf(fake.ctx, PRO)
65
+ const credits = await planOf(fake.ctx, CREDIT_UNIT)
66
+ const onceOnly = await planOf(fake.ctx, 'once-only')
67
+ const free = await planOf(fake.ctx, FREE)
68
+
69
+ expect(ids(registry.forSale(pro.product, pro.plan))).toEqual(['full'])
70
+ expect(ids(registry.forSale(credits.product, credits.plan))).toEqual(['full', 'once'])
71
+ expect(ids(registry.forSale(credits.product, credits.plan, 'once'))).toEqual(['once'])
72
+ expect(ids(registry.forSale(onceOnly.product, onceOnly.plan))).toEqual(['once'])
73
+ expect(ids(registry.forSale(free.product, free.plan))).toEqual([])
74
+ expect(pro.product.gateways).toBeUndefined()
75
+ expect(pro.product.sku).toBe(PLANS_PRODUCT)
76
+ })
77
+
78
+ test('boot: a declared policy feature a registered paygate lacks fails the gateway initialization', async () => {
79
+ const untaxed = makeFakePaygate({ features: { automaticTax: false } })
80
+ await expect(makeFakeContext({ providers: [untaxed.provider] })).rejects.toThrow('paygate:feature:automaticTax:fake')
81
+
82
+ const local = makeFakePaygate({ features: { adaptiveCurrency: false } })
83
+ await expect(makeFakeContext({
84
+ providers: [local.provider],
85
+ pricing: { tax: { automatic: true, collectTaxId: true, estimate: false }, currency: { adaptive: true, estimate: false } },
86
+ })).rejects.toThrow('paygate:feature:adaptiveCurrency:fake')
87
+
88
+ const plain = makeFakePaygate({ features: { termsConsent: false } })
89
+ await expect(makeFakeContext({
90
+ providers: [plain.provider],
91
+ consumerRights: {
92
+ textVersion: 'terms-v1', links: { en: { billingTerms: 'https://legal.example.com/terms' } },
93
+ mechanisms: { checkoutTerms: true }, trader: { name: 'Example', legalName: 'Example Ltd' },
94
+ },
95
+ })).rejects.toThrow('paygate:feature:termsConsent:fake')
96
+
97
+ expect(await makeFakeContext({ providers: [local.provider] })).toBeDefined()
98
+ })
99
+
100
+ test('boot: a managed, bootstrapping gateway without any provider fails; without bootstrap it only warns', async () => {
101
+ await expect(makeFakeContext({ providers: [], gateway: { manage: true } })).rejects.toThrow('paygate:provider:none')
102
+ expect(await makeFakeContext({ providers: [], gateway: { manage: true, bootstrap: false } })).toBeDefined()
103
+ expect(await makeFakeContext({ providers: [], gateway: { manage: false } })).toBeDefined()
104
+ })
105
+
106
+ test('the bootstrap brings every paygate to the declaration, skipping the capabilities a paygate lacks', async () => {
107
+ const full = makeFakePaygate({ id: 'full' })
108
+ const bare = makeFakePaygate({ id: 'bare', without: ['catalogue', 'portal', 'webhook'] })
109
+ const fake = await makeFakeContext({ providers: [full.provider, bare.provider], gateway: { manage: true, bootstrap: false } })
110
+
111
+ await paygateBootstrapOf(fake.ctx).bootstrap({ force: true })
112
+
113
+ expect(full.state.calls.map(call => call.method)).toEqual([
114
+ 'catalogue.syncProduct', 'catalogue.syncProduct', 'portal.ensureConfiguration', 'webhook.ensureEndpoint',
115
+ ])
116
+ expect(full.state.endpoints).toEqual([{ owner: 'app', webhookUrl: 'https://api.example.com/payment-gate/webhook/full' }])
117
+ expect(full.state.configurations).toEqual([{ owner: 'app', webhookUrl: 'https://api.example.com/payment-gate/webhook/full' }])
118
+ expect(full.state.calls[3].args).toEqual([{ owner: 'app', webhookUrl: 'https://api.example.com/payment-gate/webhook/full' }, { force: true }])
119
+ expect(bare.state.calls).toEqual([])
120
+ expect(fake.stores['payment-fingerprint'].rows.map(row => row.sku).sort()).toEqual([CREDITS_PRODUCT, PLANS_PRODUCT])
121
+ })
122
+
123
+ test('a deployment on a local host keeps no webhook endpoint', async () => {
124
+ const local = makeFakePaygate()
125
+ const fake = await makeFakeContext({ providers: [local.provider], host: 'localhost', gateway: { manage: true, bootstrap: false } })
126
+
127
+ await paygateBootstrapOf(fake.ctx).bootstrap()
128
+
129
+ expect(local.state.calls.map(call => call.method)).not.toContain('webhook.ensureEndpoint')
130
+ expect(local.state.calls.map(call => call.method)).toContain('portal.ensureConfiguration')
131
+ })
132
+ })
@@ -3,15 +3,15 @@ import { protocols } from '@owlmeans/entrypoint'
3
3
  import type { ParamsOf, ResponseOf } from '@owlmeans/entrypoint'
4
4
  import { GUARD_ED25519 } from '@owlmeans/server-app'
5
5
  import * as payment from '../src/index.js'
6
- import { paymentGate, paymentGateEntrypoints, WEBHOOK_EVENTS } from '../src/index.js'
6
+ import { paymentGate, paymentGateEntrypoints } from '../src/index.js'
7
7
 
8
8
  describe('server payment protocols', () => {
9
9
  test('bind webhook params and the resync responses to their declarations', () => {
10
- const params: ParamsOf<typeof paymentGate.webhook> = { paygate: 'stripe' }
10
+ const params: ParamsOf<typeof paymentGate.webhook> = { paygate: 'card-gate' }
11
11
  const response: ResponseOf<typeof paymentGate.resync> = { ok: true }
12
12
  const resynced: ResponseOf<typeof paymentGate.resyncSubscriptions> = { scanned: 3, updated: 1 }
13
13
 
14
- expect(params.paygate).toBe('stripe')
14
+ expect(params.paygate).toBe('card-gate')
15
15
  expect(response.ok).toBe(true)
16
16
  expect(resynced.updated).toBe(1)
17
17
  expect(paymentGate.webhook.route.route.parent).toBe(paymentGate.base.alias)
@@ -31,7 +31,6 @@ describe('server payment protocols', () => {
31
31
  expect(JSON.stringify(paymentGate.resyncSubscriptions)).toContain(GUARD_ED25519)
32
32
  expect(JSON.stringify(paymentGate.resync)).toContain(GUARD_ED25519)
33
33
  expect(JSON.stringify(paymentGate.webhook)).not.toContain(GUARD_ED25519)
34
- expect(Object.isFrozen(WEBHOOK_EVENTS)).toBe(true)
35
- expect(new Set(WEBHOOK_EVENTS).size).toBe(WEBHOOK_EVENTS.length)
34
+ expect(payment).not.toHaveProperty('WEBHOOK_EVENTS')
36
35
  })
37
36
  })
@@ -1,8 +1,8 @@
1
1
  import { describe, expect, test } from 'bun:test'
2
2
  import { LimitExhausted, LimitKind, LimitMisdeclared, LimitUnknown, LimitWindow, planLimitHelper } from '@owlmeans/payment'
3
3
  import { UnsupportedArgumentError } from '@owlmeans/resource'
4
- import { makeFakeContext, past, PRO } from './fake-stripe.js'
5
- import type { FakeContext } from './fake-stripe.js'
4
+ import { makeFakeContext, past, PRO } from '@owlmeans/test-payment'
5
+ import type { FakeContext } from '@owlmeans/test-payment'
6
6
  import { paymentAccessOf } from '../src/access.js'
7
7
 
8
8
  const ledger = (fake: FakeContext) => fake.stores['payment-usage'].rows