@owlmeans/server-payment 0.1.22-rc.0 → 0.2.0-alpha.1
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.
- package/README.md +52 -54
- package/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/server-payment/SKILL.md +272 -330
- package/agent-meta/skills/server-payment/parts/reference.md +3 -3
- package/build/access/types.d.ts +7 -16
- package/build/access/types.d.ts.map +1 -1
- package/build/access.d.ts.map +1 -1
- package/build/access.js +6 -8
- package/build/access.js.map +1 -1
- package/build/actions/resync.d.ts +3 -3
- package/build/actions/resync.js +5 -5
- package/build/actions/resync.js.map +1 -1
- package/build/actions/webhook.d.ts +4 -1
- package/build/actions/webhook.d.ts.map +1 -1
- package/build/actions/webhook.js +8 -4
- package/build/actions/webhook.js.map +1 -1
- package/build/bootstrap/types.d.ts +13 -7
- package/build/bootstrap/types.d.ts.map +1 -1
- package/build/bootstrap.d.ts +4 -4
- package/build/bootstrap.d.ts.map +1 -1
- package/build/bootstrap.js +19 -10
- package/build/bootstrap.js.map +1 -1
- package/build/catalogue/types.d.ts +3 -3
- package/build/catalogue/types.d.ts.map +1 -1
- package/build/catalogue.d.ts.map +1 -1
- package/build/catalogue.js +4 -8
- package/build/catalogue.js.map +1 -1
- package/build/checkout/types.d.ts +21 -0
- package/build/checkout/types.d.ts.map +1 -0
- package/build/{plugins/stripe → checkout}/types.js.map +1 -1
- package/build/checkout.d.ts +6 -0
- package/build/checkout.d.ts.map +1 -0
- package/build/checkout.js +374 -0
- package/build/checkout.js.map +1 -0
- package/build/config.d.ts +10 -16
- package/build/config.d.ts.map +1 -1
- package/build/config.js +18 -59
- package/build/config.js.map +1 -1
- package/build/consts.d.ts +3 -49
- package/build/consts.d.ts.map +1 -1
- package/build/consts.js +3 -61
- package/build/consts.js.map +1 -1
- package/build/consumer/cancellation/types.d.ts +6 -4
- package/build/consumer/cancellation/types.d.ts.map +1 -1
- package/build/consumer/cancellation.d.ts.map +1 -1
- package/build/consumer/cancellation.js +11 -5
- package/build/consumer/cancellation.js.map +1 -1
- package/build/consumer/capture/types.d.ts +8 -12
- package/build/consumer/capture/types.d.ts.map +1 -1
- package/build/consumer/capture.d.ts.map +1 -1
- package/build/consumer/capture.js +48 -53
- package/build/consumer/capture.js.map +1 -1
- package/build/consumer/index.d.ts +4 -0
- package/build/consumer/index.d.ts.map +1 -1
- package/build/consumer/index.js +2 -0
- package/build/consumer/index.js.map +1 -1
- package/build/consumer/reconcile.d.ts.map +1 -1
- package/build/consumer/reconcile.js +82 -72
- package/build/consumer/reconcile.js.map +1 -1
- package/build/consumer/records/types.d.ts +4 -4
- package/build/consumer/records/types.d.ts.map +1 -1
- package/build/consumer/records.d.ts.map +1 -1
- package/build/consumer/records.js +16 -13
- package/build/consumer/records.js.map +1 -1
- package/build/consumer/service.d.ts +2 -2
- package/build/consumer/service.d.ts.map +1 -1
- package/build/consumer/service.js +28 -21
- package/build/consumer/service.js.map +1 -1
- package/build/consumer/types.d.ts +1 -4
- package/build/consumer/types.d.ts.map +1 -1
- package/build/consumer/types.local.d.ts +1 -9
- package/build/consumer/types.local.d.ts.map +1 -1
- package/build/consumer/withdrawal/types.d.ts +14 -12
- package/build/consumer/withdrawal/types.d.ts.map +1 -1
- package/build/consumer/withdrawal.d.ts.map +1 -1
- package/build/consumer/withdrawal.js +55 -71
- package/build/consumer/withdrawal.js.map +1 -1
- package/build/index.d.ts +12 -6
- package/build/index.d.ts.map +1 -1
- package/build/index.js +7 -4
- package/build/index.js.map +1 -1
- package/build/paygate/providers.d.ts +13 -0
- package/build/paygate/providers.d.ts.map +1 -0
- package/build/paygate/providers.js +38 -0
- package/build/paygate/providers.js.map +1 -0
- package/build/paygate/registry.d.ts +6 -0
- package/build/paygate/registry.d.ts.map +1 -0
- package/build/paygate/registry.js +37 -0
- package/build/paygate/registry.js.map +1 -0
- package/build/paygate/types.d.ts +577 -0
- package/build/paygate/types.d.ts.map +1 -0
- package/build/{plugins/session → paygate}/types.js.map +1 -1
- package/build/plan/types.d.ts +6 -1
- package/build/plan/types.d.ts.map +1 -1
- package/build/plan.d.ts.map +1 -1
- package/build/plan.js +16 -2
- package/build/plan.js.map +1 -1
- package/build/plugins/checkout-policy/types.d.ts +5 -3
- package/build/plugins/checkout-policy/types.d.ts.map +1 -1
- package/build/plugins/checkout-policy.d.ts.map +1 -1
- package/build/plugins/checkout-policy.js +5 -3
- package/build/plugins/checkout-policy.js.map +1 -1
- package/build/plugins/consts.local.d.ts +8 -10
- package/build/plugins/consts.local.d.ts.map +1 -1
- package/build/plugins/consts.local.js +2 -19
- package/build/plugins/consts.local.js.map +1 -1
- package/build/plugins/customer-lock/types.d.ts +2 -2
- package/build/plugins/customer-lock/types.d.ts.map +1 -1
- package/build/plugins/customer-lock.js +2 -2
- package/build/plugins/customer-lock.js.map +1 -1
- package/build/plugins/estimate/cache.js +1 -1
- package/build/plugins/estimate/cache.js.map +1 -1
- package/build/plugins/estimate/types.d.ts +10 -10
- package/build/plugins/estimate/types.d.ts.map +1 -1
- package/build/plugins/estimate.d.ts.map +1 -1
- package/build/plugins/estimate.js +48 -122
- package/build/plugins/estimate.js.map +1 -1
- package/build/plugins/events/types.d.ts +17 -10
- package/build/plugins/events/types.d.ts.map +1 -1
- package/build/plugins/events.d.ts +6 -5
- package/build/plugins/events.d.ts.map +1 -1
- package/build/plugins/events.js +162 -173
- package/build/plugins/events.js.map +1 -1
- package/build/plugins/fx/types.d.ts +10 -21
- package/build/plugins/fx/types.d.ts.map +1 -1
- package/build/plugins/fx.d.ts +1 -1
- package/build/plugins/fx.d.ts.map +1 -1
- package/build/plugins/fx.js +17 -54
- package/build/plugins/fx.js.map +1 -1
- package/build/plugins/portal/types.d.ts +8 -37
- package/build/plugins/portal/types.d.ts.map +1 -1
- package/build/plugins/portal.d.ts.map +1 -1
- package/build/plugins/portal.js +38 -208
- package/build/plugins/portal.js.map +1 -1
- package/build/plugins/refunds/types.d.ts +11 -11
- package/build/plugins/refunds/types.d.ts.map +1 -1
- package/build/plugins/refunds.d.ts.map +1 -1
- package/build/plugins/refunds.js +14 -39
- package/build/plugins/refunds.js.map +1 -1
- package/build/plugins/snapshot/types.d.ts +11 -0
- package/build/plugins/snapshot/types.d.ts.map +1 -0
- package/build/plugins/snapshot/types.js +2 -0
- package/build/plugins/snapshot/types.js.map +1 -0
- package/build/plugins/snapshot.d.ts +4 -0
- package/build/plugins/snapshot.d.ts.map +1 -0
- package/build/plugins/snapshot.js +19 -0
- package/build/plugins/snapshot.js.map +1 -0
- package/build/plugins/subscriptions/types.d.ts +17 -17
- package/build/plugins/subscriptions/types.d.ts.map +1 -1
- package/build/plugins/subscriptions.d.ts +4 -4
- package/build/plugins/subscriptions.d.ts.map +1 -1
- package/build/plugins/subscriptions.js +69 -77
- package/build/plugins/subscriptions.js.map +1 -1
- package/build/plugins/types.local.d.ts +9 -43
- package/build/plugins/types.local.d.ts.map +1 -1
- package/build/plugins/webhook-manager/types.d.ts +15 -43
- package/build/plugins/webhook-manager/types.d.ts.map +1 -1
- package/build/plugins/webhook-manager.d.ts +3 -3
- package/build/plugins/webhook-manager.d.ts.map +1 -1
- package/build/plugins/webhook-manager.js +21 -199
- package/build/plugins/webhook-manager.js.map +1 -1
- package/build/protocol.d.ts +1 -1
- package/build/protocol.d.ts.map +1 -1
- package/build/protocol.js +1 -1
- package/build/protocol.js.map +1 -1
- package/build/service.d.ts +11 -11
- package/build/service.d.ts.map +1 -1
- package/build/service.js +59 -23
- package/build/service.js.map +1 -1
- package/build/sync/types.d.ts +17 -13
- package/build/sync/types.d.ts.map +1 -1
- package/build/sync.d.ts.map +1 -1
- package/build/sync.js +22 -122
- package/build/sync.js.map +1 -1
- package/build/types.d.ts +46 -101
- package/build/types.d.ts.map +1 -1
- package/build/utils/types.d.ts +0 -8
- package/build/utils/types.d.ts.map +1 -1
- package/build/utils.d.ts +1 -4
- package/build/utils.d.ts.map +1 -1
- package/build/utils.js +1 -15
- package/build/utils.js.map +1 -1
- package/package.json +20 -22
- package/src/access/types.ts +7 -16
- package/src/access.ts +7 -15
- package/src/actions/resync.ts +5 -5
- package/src/actions/webhook.ts +8 -4
- package/src/bootstrap/types.ts +13 -7
- package/src/bootstrap.ts +23 -13
- package/src/catalogue/types.ts +3 -3
- package/src/catalogue.ts +4 -8
- package/src/checkout/types.ts +21 -0
- package/src/checkout.ts +414 -0
- package/src/config.ts +19 -66
- package/src/consts.ts +3 -69
- package/src/consumer/cancellation/types.ts +6 -4
- package/src/consumer/cancellation.ts +13 -7
- package/src/consumer/capture/types.ts +8 -12
- package/src/consumer/capture.ts +50 -59
- package/src/consumer/index.ts +4 -0
- package/src/consumer/reconcile.ts +69 -68
- package/src/consumer/records/types.ts +4 -4
- package/src/consumer/records.ts +19 -14
- package/src/consumer/service.ts +35 -22
- package/src/consumer/types.local.ts +1 -8
- package/src/consumer/types.ts +1 -4
- package/src/consumer/withdrawal/types.ts +14 -12
- package/src/consumer/withdrawal.ts +61 -77
- package/src/index.ts +12 -6
- package/src/paygate/providers.ts +48 -0
- package/src/paygate/registry.ts +53 -0
- package/src/paygate/types.ts +640 -0
- package/src/plan/types.ts +6 -1
- package/src/plan.ts +21 -3
- package/src/plugins/checkout-policy/types.ts +5 -3
- package/src/plugins/checkout-policy.ts +6 -5
- package/src/plugins/consts.local.ts +2 -26
- package/src/plugins/customer-lock/types.ts +2 -2
- package/src/plugins/customer-lock.ts +2 -2
- package/src/plugins/estimate/cache.ts +1 -1
- package/src/plugins/estimate/types.ts +10 -10
- package/src/plugins/estimate.ts +52 -128
- package/src/plugins/events/types.ts +17 -9
- package/src/plugins/events.ts +172 -190
- package/src/plugins/fx/types.ts +10 -23
- package/src/plugins/fx.ts +28 -64
- package/src/plugins/portal/types.ts +8 -38
- package/src/plugins/portal.ts +48 -229
- package/src/plugins/refunds/types.ts +11 -11
- package/src/plugins/refunds.ts +16 -40
- package/src/plugins/snapshot/types.ts +11 -0
- package/src/plugins/snapshot.ts +22 -0
- package/src/plugins/subscriptions/types.ts +17 -17
- package/src/plugins/subscriptions.ts +81 -81
- package/src/plugins/types.local.ts +9 -33
- package/src/plugins/webhook-manager/types.ts +15 -43
- package/src/plugins/webhook-manager.ts +24 -220
- package/src/protocol.ts +1 -1
- package/src/service.ts +66 -30
- package/src/sync/types.ts +17 -13
- package/src/sync.ts +25 -143
- package/src/types.ts +47 -97
- package/src/utils/types.ts +0 -9
- package/src/utils.ts +2 -20
- package/tests/capabilities.spec.ts +219 -0
- package/tests/entitlement.spec.ts +4 -4
- package/tests/gate-handlers.spec.ts +6 -6
- package/tests/gate.spec.ts +2 -2
- package/tests/grants.spec.ts +2 -2
- package/tests/internal-plan.spec.ts +2 -2
- package/tests/limit-gate.spec.ts +2 -2
- package/tests/paygates.spec.ts +132 -0
- package/tests/protocol.spec.ts +4 -5
- package/tests/usage.spec.ts +2 -2
- package/tests/webhook.spec.ts +137 -0
- package/build/plugins/session/types.d.ts +0 -34
- package/build/plugins/session/types.d.ts.map +0 -1
- package/build/plugins/session.d.ts +0 -4
- package/build/plugins/session.d.ts.map +0 -1
- package/build/plugins/session.js +0 -71
- package/build/plugins/session.js.map +0 -1
- package/build/plugins/status.d.ts +0 -8
- package/build/plugins/status.d.ts.map +0 -1
- package/build/plugins/status.js +0 -21
- package/build/plugins/status.js.map +0 -1
- package/build/plugins/stripe/types.d.ts +0 -20
- package/build/plugins/stripe/types.d.ts.map +0 -1
- package/build/plugins/stripe.d.ts +0 -6
- package/build/plugins/stripe.d.ts.map +0 -1
- package/build/plugins/stripe.js +0 -518
- package/build/plugins/stripe.js.map +0 -1
- package/src/plugins/session/types.ts +0 -30
- package/src/plugins/session.ts +0 -86
- package/src/plugins/status.ts +0 -22
- package/src/plugins/stripe/types.ts +0 -20
- package/src/plugins/stripe.ts +0 -571
- package/tests/checkout-consumer.spec.ts +0 -366
- package/tests/checkout-locks.spec.ts +0 -161
- package/tests/checkout-plugins.spec.ts +0 -164
- package/tests/checkout.spec.ts +0 -313
- package/tests/consumer-events.spec.ts +0 -215
- package/tests/consumer-fixtures.ts +0 -141
- package/tests/consumer-ops.spec.ts +0 -361
- package/tests/consumer-rights.integration.spec.ts +0 -147
- package/tests/consumer-rights.spec.ts +0 -606
- package/tests/context.ts +0 -110
- package/tests/estimate.spec.ts +0 -350
- package/tests/events.spec.ts +0 -367
- package/tests/fake-stripe.ts +0 -1004
- package/tests/gateway-options.spec.ts +0 -180
- package/tests/portal.spec.ts +0 -202
- package/tests/sync.spec.ts +0 -114
- package/tests/usage.integration.spec.ts +0 -165
- package/tests/webhook-manager.spec.ts +0 -149
- /package/build/{plugins/session → checkout}/types.js +0 -0
- /package/build/{plugins/stripe → paygate}/types.js +0 -0
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
import { afterAll, beforeAll, describe, expect, test } from 'bun:test'
|
|
2
|
-
import { LimitExhausted, LimitKind, LimitWindow, SUBSCRIPTION_WEEK_MS, planLimitHelper, SubscriptionStatus } from '@owlmeans/payment'
|
|
3
|
-
import { createEventHandler } from '../src/plugins/events.js'
|
|
4
|
-
import { BURST_PLAN, gate, makeSuite } from './context.js'
|
|
5
|
-
import type { Booted } from './context.js'
|
|
6
|
-
import { CREDITS_PRODUCT, eventOf, makeFakeStripe, PRO, subscriptionOf, TEAM } from './fake-stripe.js'
|
|
7
|
-
import { paymentAccessOf } from '../src/access.js'
|
|
8
|
-
import { webhookOf } from '../src/plugins/webhook-manager.js'
|
|
9
|
-
import { EntitlementGrantKind, EntitlementGrantStatus } from '../src/consts.js'
|
|
10
|
-
import type { LimitOutcome } from '../src/types.js'
|
|
11
|
-
|
|
12
|
-
const suite = makeSuite('payusage')
|
|
13
|
-
const it = gate.skip ? test.skip : test
|
|
14
|
-
|
|
15
|
-
describe('@owlmeans/server-payment — usage ledger on Mongo', () => {
|
|
16
|
-
if (gate.skip) {
|
|
17
|
-
test.skip(gate.reason ?? 'mongo gate closed', () => {})
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
let booted: Booted
|
|
22
|
-
beforeAll(async () => { booted = await suite.boot() }, 60_000)
|
|
23
|
-
afterAll(async () => { await suite.teardown() }, 60_000)
|
|
24
|
-
|
|
25
|
-
it('uses subscription weeks for admission, views, reconciliation, replay and release', async () => {
|
|
26
|
-
const { ctx } = booted
|
|
27
|
-
const access = paymentAccessOf(ctx)
|
|
28
|
-
const service = access.entitlements()
|
|
29
|
-
await access.gateway().grantInternalPlan(ctx, 'weekly-1', BURST_PLAN, { force: true })
|
|
30
|
-
const anchor = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000)
|
|
31
|
-
await access.subscriptions().collection.updateOne({ entityId: 'weekly-1', planSku: BURST_PLAN }, { $set: { createdAt: anchor } })
|
|
32
|
-
const window = planLimitHelper.windowKeyOf(LimitKind.Window, LimitWindow.SubscriptionWeek, new Date(), anchor)
|
|
33
|
-
const outcomes = await Promise.allSettled(Array.from({ length: 8 }, (_, index) =>
|
|
34
|
-
service.consume({ entityId: 'weekly-1', limitKey: 'weekly', eventKey: `weekly:${index}` })))
|
|
35
|
-
expect(outcomes.filter(row => row.status === 'fulfilled')).toHaveLength(1)
|
|
36
|
-
const winner = outcomes.find(row => row.status === 'fulfilled')!
|
|
37
|
-
if (winner.status !== 'fulfilled') throw new Error('No admitted weekly unit')
|
|
38
|
-
expect(winner.value.window).toBe(window)
|
|
39
|
-
expect(winner.value.resetsAt).toEqual(new Date(anchor.getTime() + SUBSCRIPTION_WEEK_MS))
|
|
40
|
-
expect((await service.entitlements('weekly-1')).limits.find(row => row.key === 'weekly')).toMatchObject({ used: 1, remaining: 0, windowStart: anchor })
|
|
41
|
-
await access.usageCounters().collection.updateOne({ entityId: 'weekly-1', limitKey: 'weekly', window }, { $set: { used: 9 } })
|
|
42
|
-
await service.reconcileCounters('weekly-1')
|
|
43
|
-
expect((await service.limitState('weekly-1', 'weekly')).used).toBe(1)
|
|
44
|
-
const request = { entityId: 'weekly-1', limitKey: 'weekly', eventKey: winner.value.eventKey }
|
|
45
|
-
expect((await service.consume(request)).replayed).toBe(true)
|
|
46
|
-
expect((await service.release(request)).remaining).toBe(1)
|
|
47
|
-
expect((await service.consume(request)).admitted).toBe(false)
|
|
48
|
-
const oldStart = new Date(anchor.getTime() - SUBSCRIPTION_WEEK_MS)
|
|
49
|
-
const oldWindow = planLimitHelper.windowKeyOf(LimitKind.Window, LimitWindow.SubscriptionWeek, oldStart, anchor)
|
|
50
|
-
await access.usageEvents().create({ entityId: 'weekly-1', limitKey: 'weekly', window: oldWindow,
|
|
51
|
-
delta: 1, eventKey: 'weekly:past', createdAt: oldStart, planSku: BURST_PLAN })
|
|
52
|
-
await service.reconcileCounters('weekly-1')
|
|
53
|
-
expect((await service.consume({ ...request, eventKey: 'weekly:past' })).resetsAt).toEqual(anchor)
|
|
54
|
-
await service.release({ ...request, eventKey: 'weekly:past' })
|
|
55
|
-
expect((await service.limitState('weekly-1', 'weekly')).used).toBe(0)
|
|
56
|
-
expect((await service.consume({ ...request, eventKey: 'weekly:new' })).admitted).toBe(true)
|
|
57
|
-
}, 60_000)
|
|
58
|
-
|
|
59
|
-
it('admits exactly the limit out of 20 concurrent consumes', async () => {
|
|
60
|
-
const { ctx } = booted
|
|
61
|
-
await paymentAccessOf(ctx).gateway().grantInternalPlan(ctx, 'burst-1', BURST_PLAN, { force: true })
|
|
62
|
-
const results = await Promise.allSettled(Array.from({ length: 20 }, async (_, index) =>
|
|
63
|
-
await paymentAccessOf(ctx).entitlements().consume({ entityId: 'burst-1', limitKey: 'burst', eventKey: `burst:${index}` })))
|
|
64
|
-
|
|
65
|
-
expect(results.filter(result => result.status === 'fulfilled')).toHaveLength(5)
|
|
66
|
-
const refused = results.filter((result): result is PromiseRejectedResult => result.status === 'rejected')
|
|
67
|
-
expect(refused).toHaveLength(15)
|
|
68
|
-
expect(refused.every(result => result.reason instanceof LimitExhausted)).toBe(true)
|
|
69
|
-
expect(await paymentAccessOf(ctx).usageEvents().count({ entityId: 'burst-1', limitKey: 'burst' })).toBe(5)
|
|
70
|
-
expect((await paymentAccessOf(ctx).usageCounters().load({ entityId: 'burst-1', limitKey: 'burst' }))?.used).toBe(5)
|
|
71
|
-
}, 60_000)
|
|
72
|
-
|
|
73
|
-
it('admits a concurrently repeated event key once', async () => {
|
|
74
|
-
const { ctx } = booted
|
|
75
|
-
await paymentAccessOf(ctx).gateway().grantInternalPlan(ctx, 'dup-1', TEAM, { force: true })
|
|
76
|
-
const outcomes = await Promise.all(Array.from({ length: 10 }, async () =>
|
|
77
|
-
await paymentAccessOf(ctx).entitlements().consume({ entityId: 'dup-1', limitKey: 'exports', eventKey: 'export:same', ref: 'same' })))
|
|
78
|
-
|
|
79
|
-
expect(outcomes.every(outcome => outcome.admitted)).toBe(true)
|
|
80
|
-
expect(outcomes.filter(outcome => !outcome.replayed)).toHaveLength(1)
|
|
81
|
-
expect(await paymentAccessOf(ctx).usageEvents().count({ entityId: 'dup-1', limitKey: 'exports' })).toBe(1)
|
|
82
|
-
expect((await paymentAccessOf(ctx).usageCounters().load({ entityId: 'dup-1', limitKey: 'exports' }))?.used).toBe(1)
|
|
83
|
-
expect((await paymentAccessOf(ctx).entitlements().consumptionByRef('dup-1', 'exports', 'same'))?.eventKey).toBe('export:same')
|
|
84
|
-
}, 60_000)
|
|
85
|
-
|
|
86
|
-
it('admits a grant\'s units after the plan, exactly, under concurrency — through the grant validator', async () => {
|
|
87
|
-
const { ctx } = booted
|
|
88
|
-
const access = paymentAccessOf(ctx)
|
|
89
|
-
await access.gateway().grantInternalPlan(ctx, 'grant-1', BURST_PLAN, { force: true })
|
|
90
|
-
const grant = await access.grants().grant('grant-1', {
|
|
91
|
-
kind: EntitlementGrantKind.Units, limitKey: 'burst', amount: 3, ref: 'op-1',
|
|
92
|
-
})
|
|
93
|
-
const results = await Promise.allSettled(Array.from({ length: 20 }, async (_, index) =>
|
|
94
|
-
await access.entitlements().consume({ entityId: 'grant-1', limitKey: 'burst', eventKey: `burst:${index}` })))
|
|
95
|
-
|
|
96
|
-
const admitted = results.filter((result): result is PromiseFulfilledResult<LimitOutcome> => result.status === 'fulfilled')
|
|
97
|
-
expect(admitted).toHaveLength(8)
|
|
98
|
-
expect(admitted.filter(result => result.value.window === `grant:${grant.id}`)).toHaveLength(3)
|
|
99
|
-
const refused = results.filter((result): result is PromiseRejectedResult => result.status === 'rejected')
|
|
100
|
-
expect(refused.every(result => result.reason instanceof LimitExhausted)).toBe(true)
|
|
101
|
-
expect((await access.grants().get(grant.id!))?.used).toBe(3)
|
|
102
|
-
expect((await access.usageCounters().load({ entityId: 'grant-1', limitKey: 'burst' }))?.used).toBe(5)
|
|
103
|
-
expect(await access.usageEvents().count({ entityId: 'grant-1', limitKey: 'burst' })).toBe(8)
|
|
104
|
-
expect(await access.entitlements().limitState('grant-1', 'burst')).toMatchObject({ limit: 8, used: 8, remaining: 0 })
|
|
105
|
-
|
|
106
|
-
await access.grantRecords().collection.updateOne({ entityId: 'grant-1' }, { $set: { used: 9 } })
|
|
107
|
-
await access.entitlements().reconcileCounters('grant-1')
|
|
108
|
-
expect((await access.grants().get(grant.id!))?.used).toBe(3)
|
|
109
|
-
expect(await access.usageCounters().count({ entityId: 'grant-1', window: `grant:${grant.id}` })).toBe(0)
|
|
110
|
-
|
|
111
|
-
const revoked = await access.grants().revoke(grant.id!)
|
|
112
|
-
expect(revoked).toMatchObject({ changed: true, grant: { status: EntitlementGrantStatus.Revoked, used: 3 } })
|
|
113
|
-
expect(await access.entitlements().limitState('grant-1', 'burst')).toMatchObject({ limit: 5, used: 5, remaining: 0 })
|
|
114
|
-
}, 60_000)
|
|
115
|
-
|
|
116
|
-
it('recomputes counters to the ledger sum and reconciles occupancy', async () => {
|
|
117
|
-
const { ctx } = booted
|
|
118
|
-
await paymentAccessOf(ctx).gateway().grantInternalPlan(ctx, 'rec-1', PRO, { force: true })
|
|
119
|
-
const service = paymentAccessOf(ctx).entitlements()
|
|
120
|
-
for (const key of ['a', 'b', 'c']) {
|
|
121
|
-
await service.consume({ entityId: 'rec-1', limitKey: 'reports', eventKey: `report:${key}` })
|
|
122
|
-
}
|
|
123
|
-
await service.release({ entityId: 'rec-1', limitKey: 'reports', eventKey: 'report:b' })
|
|
124
|
-
await paymentAccessOf(ctx).usageCounters().collection.updateOne({ entityId: 'rec-1', limitKey: 'reports' }, { $set: { used: 7 } })
|
|
125
|
-
|
|
126
|
-
const result = await service.reconcileCounters('rec-1')
|
|
127
|
-
expect(result.repaired).toBeGreaterThanOrEqual(1)
|
|
128
|
-
const { items } = await paymentAccessOf(ctx).usageEvents().list({ entityId: 'rec-1', limitKey: 'reports' }, { size: 0 })
|
|
129
|
-
const sum = items.reduce((total, event) => total + event.delta, 0)
|
|
130
|
-
expect(sum).toBe(2)
|
|
131
|
-
expect((await paymentAccessOf(ctx).usageCounters().load({ entityId: 'rec-1', limitKey: 'reports' }))?.used).toBe(sum)
|
|
132
|
-
|
|
133
|
-
const over = await service.reconcileOccupancy('rec-1', 'seats', 4)
|
|
134
|
-
expect(over.overSince).toBeInstanceOf(Date)
|
|
135
|
-
const within = await service.reconcileOccupancy('rec-1', 'seats', 2)
|
|
136
|
-
expect(within).toEqual({ limitKey: 'seats', used: 2, limit: 2, over: 0 })
|
|
137
|
-
expect((await paymentAccessOf(ctx).usageCounters().load({ entityId: 'rec-1', limitKey: 'seats' }))?.overSince ?? null).toBeNull()
|
|
138
|
-
expect(await paymentAccessOf(ctx).usageEvents().count({ entityId: 'rec-1', limitKey: 'seats' })).toBe(1)
|
|
139
|
-
}, 60_000)
|
|
140
|
-
|
|
141
|
-
it('stores subscriptions, fulfillments and webhook endpoints through their collection validators', async () => {
|
|
142
|
-
const { ctx } = booted
|
|
143
|
-
const { stripe } = makeFakeStripe()
|
|
144
|
-
const handler = createEventHandler(ctx, stripe)
|
|
145
|
-
await handler.process(eventOf('customer.subscription.updated', subscriptionOf({ entityId: 'sub-1' })))
|
|
146
|
-
await handler.process(eventOf('customer.subscription.updated', subscriptionOf({ entityId: 'sub-1', cancelAtPeriodEnd: true })))
|
|
147
|
-
await handler.process(eventOf('checkout.session.completed', {
|
|
148
|
-
id: 'cs_int', mode: 'payment', payment_status: 'paid', customer: 'cus_1', currency: 'usd', amount_subtotal: 1021,
|
|
149
|
-
payment_intent: 'pi_int',
|
|
150
|
-
metadata: {
|
|
151
|
-
pricingMode: 'amount', amountMinor: '1000', chargeAmountMinor: '1021', currency: 'usd',
|
|
152
|
-
entityId: 'sub-1', service: 'app', productSku: CREDITS_PRODUCT,
|
|
153
|
-
},
|
|
154
|
-
}))
|
|
155
|
-
await webhookOf(ctx).ensureWebhookEndpoint(stripe)
|
|
156
|
-
|
|
157
|
-
const row = await paymentAccessOf(ctx).subscriptions().byExternalId('sub_1', 'stripe')
|
|
158
|
-
expect(row).toEqual(expect.objectContaining({ status: SubscriptionStatus.Active, cancelAtPeriodEnd: true, rank: 10 }))
|
|
159
|
-
expect(row?.propagated?.cancelAtPeriodEnd).toBe(true)
|
|
160
|
-
expect(row?.createdAt).toBeInstanceOf(Date)
|
|
161
|
-
expect((await paymentAccessOf(ctx).entitlements().entitlements('sub-1')).plan.sku).toBe(PRO)
|
|
162
|
-
expect((await paymentAccessOf(ctx).fulfillments().byExternalId('cs_int', 'stripe'))?.fulfilledAt).toBeInstanceOf(Date)
|
|
163
|
-
expect((await paymentAccessOf(ctx).paymentWebhooks().load({ service: 'app' }))?.secret).toStartWith('whsec_')
|
|
164
|
-
}, 60_000)
|
|
165
|
-
})
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'bun:test'
|
|
2
|
-
import { WebhookSetupError } from '@owlmeans/payment'
|
|
3
|
-
import { STRIPE_SIGNATURE, WEBHOOK_EVENTS } from '../src/consts.js'
|
|
4
|
-
import { HOST, makeFakeContext, SDK_API_VERSION, SERVICE } from './fake-stripe.js'
|
|
5
|
-
import { webhookOf } from '../src/plugins/webhook-manager.js'
|
|
6
|
-
|
|
7
|
-
const URL = 'https://api.example.com/payment-gate/webhook/stripe'
|
|
8
|
-
const ours = { owlmeans: 'payment', service: SERVICE }
|
|
9
|
-
|
|
10
|
-
describe('@owlmeans/server-payment — webhook endpoint management', () => {
|
|
11
|
-
test('skips a URL Stripe cannot deliver to without calling Stripe', async () => {
|
|
12
|
-
for (const host of ['localhost', '127.0.0.1', 'api', 'payments.local']) {
|
|
13
|
-
const fake = await makeFakeContext({ host })
|
|
14
|
-
expect(await webhookOf(fake.ctx).ensureWebhookEndpoint(fake.stripe)).toBeNull()
|
|
15
|
-
expect(fake.state.calls).toEqual([])
|
|
16
|
-
}
|
|
17
|
-
const plain = await makeFakeContext()
|
|
18
|
-
;(plain.ctx.cfg as { security?: { unsecure?: boolean } }).security = { unsecure: true }
|
|
19
|
-
expect(await webhookOf(plain.ctx).ensureWebhookEndpoint(plain.stripe)).toBeNull()
|
|
20
|
-
expect(plain.state.calls).toEqual([])
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
test('creates the endpoint on the client API version, persists its secret, makes no call while nothing changed, and a forced run recreates it once deleted from outside', async () => {
|
|
24
|
-
const fake = await makeFakeContext()
|
|
25
|
-
expect(webhookOf(fake.ctx).webhookUrlOf()).toBe(URL)
|
|
26
|
-
|
|
27
|
-
const row = await webhookOf(fake.ctx).ensureWebhookEndpoint(fake.stripe)
|
|
28
|
-
expect(fake.state.calls).toEqual(['webhookEndpoints.list', 'webhookEndpoints.create'])
|
|
29
|
-
const [endpoint] = fake.state.webhookEndpoints
|
|
30
|
-
expect(endpoint).toEqual(expect.objectContaining({
|
|
31
|
-
url: URL, api_version: SDK_API_VERSION, enabled_events: [...WEBHOOK_EVENTS].sort(), metadata: ours,
|
|
32
|
-
description: `owlmeans:${SERVICE}`,
|
|
33
|
-
}))
|
|
34
|
-
expect(row).toEqual(expect.objectContaining({
|
|
35
|
-
externalId: endpoint.id, secret: endpoint.secret, apiVersion: SDK_API_VERSION, url: URL,
|
|
36
|
-
}))
|
|
37
|
-
|
|
38
|
-
fake.state.calls.length = 0
|
|
39
|
-
await webhookOf(fake.ctx).ensureWebhookEndpoint(fake.stripe)
|
|
40
|
-
expect(fake.state.calls).toEqual([])
|
|
41
|
-
|
|
42
|
-
fake.state.webhookEndpoints = []
|
|
43
|
-
await webhookOf(fake.ctx).ensureWebhookEndpoint(fake.stripe)
|
|
44
|
-
expect(fake.state.calls).toEqual([])
|
|
45
|
-
|
|
46
|
-
const healed = await webhookOf(fake.ctx).ensureWebhookEndpoint(fake.stripe, { force: true })
|
|
47
|
-
expect(fake.state.calls).toEqual(['webhookEndpoints.retrieve', 'webhookEndpoints.list', 'webhookEndpoints.create'])
|
|
48
|
-
const [recreated] = fake.state.webhookEndpoints
|
|
49
|
-
expect(recreated).toEqual(expect.objectContaining({ id: healed?.externalId, url: URL, metadata: ours }))
|
|
50
|
-
expect(fake.stores['payment-webhook'].rows).toEqual([
|
|
51
|
-
expect.objectContaining({ url: URL, externalId: recreated.id, secret: recreated.secret }),
|
|
52
|
-
])
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
test('updates changed events in place, and recreates the endpoint for a new API version', async () => {
|
|
56
|
-
const fake = await makeFakeContext()
|
|
57
|
-
await webhookOf(fake.ctx).ensureWebhookEndpoint(fake.stripe)
|
|
58
|
-
const stored = fake.stores['payment-webhook'].rows[0]
|
|
59
|
-
const firstSecret = stored.secret
|
|
60
|
-
stored.hash = 'events-of-an-older-release'
|
|
61
|
-
fake.state.calls.length = 0
|
|
62
|
-
|
|
63
|
-
await webhookOf(fake.ctx).ensureWebhookEndpoint(fake.stripe)
|
|
64
|
-
expect(fake.state.calls).toEqual(['webhookEndpoints.update'])
|
|
65
|
-
expect(fake.stores['payment-webhook'].rows[0].secret).toBe(firstSecret)
|
|
66
|
-
|
|
67
|
-
fake.state.apiVersion = '2099-01-01.future'
|
|
68
|
-
fake.state.calls.length = 0
|
|
69
|
-
await webhookOf(fake.ctx).ensureWebhookEndpoint(fake.stripe)
|
|
70
|
-
expect(fake.state.calls).toEqual(['webhookEndpoints.del', 'webhookEndpoints.list', 'webhookEndpoints.create'])
|
|
71
|
-
expect(fake.state.webhookEndpoints).toHaveLength(1)
|
|
72
|
-
expect(fake.state.webhookEndpoints[0].api_version).toBe('2099-01-01.future')
|
|
73
|
-
expect(fake.stores['payment-webhook'].rows).toHaveLength(1)
|
|
74
|
-
expect(fake.stores['payment-webhook'].rows[0].secret).not.toBe(firstSecret)
|
|
75
|
-
})
|
|
76
|
-
|
|
77
|
-
test('leaves another deployment\'s endpoint in a shared account alone, and replaces an untracked one at its own URL', async () => {
|
|
78
|
-
const STAGE_URL = 'https://stage.example.com/payment-gate/webhook/stripe'
|
|
79
|
-
const STAGING_URL = 'https://staging.example.com/payment-gate/webhook/stripe'
|
|
80
|
-
const stage = await makeFakeContext({
|
|
81
|
-
host: 'stage.example.com',
|
|
82
|
-
stripe: {
|
|
83
|
-
webhookEndpoints: [{ id: 'we_manual', url: STAGING_URL, metadata: {}, enabled_events: ['*'], secret: 'whsec_manual' }],
|
|
84
|
-
},
|
|
85
|
-
})
|
|
86
|
-
const staging = await makeFakeContext({ host: 'staging.example.com' })
|
|
87
|
-
|
|
88
|
-
const stageRow = await webhookOf(stage.ctx).ensureWebhookEndpoint(stage.stripe)
|
|
89
|
-
const stagingRow = await webhookOf(staging.ctx).ensureWebhookEndpoint(stage.stripe)
|
|
90
|
-
expect(stage.state.webhookEndpoints.map(({ id, url, metadata }) => ({ id, url, metadata }))).toEqual([
|
|
91
|
-
{ id: stageRow?.externalId, url: STAGE_URL, metadata: ours },
|
|
92
|
-
{ id: stagingRow?.externalId, url: STAGING_URL, metadata: ours },
|
|
93
|
-
])
|
|
94
|
-
|
|
95
|
-
stage.state.calls.length = 0
|
|
96
|
-
expect(await webhookOf(stage.ctx).ensureWebhookEndpoint(stage.stripe, { force: true }))
|
|
97
|
-
.toEqual(expect.objectContaining({ externalId: stageRow?.externalId, secret: stageRow?.secret }))
|
|
98
|
-
expect(stage.state.calls).toEqual(['webhookEndpoints.retrieve', 'webhookEndpoints.update'])
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
test('after a URL change, drops the endpoints its own former rows name and removes those rows', async () => {
|
|
102
|
-
const fake = await makeFakeContext({ host: 'old.example.com' })
|
|
103
|
-
const former = await webhookOf(fake.ctx).ensureWebhookEndpoint(fake.stripe)
|
|
104
|
-
fake.stores['payment-webhook'].rows.push({
|
|
105
|
-
id: 'older-row', paygate: 'stripe', service: SERVICE, url: 'https://older.example.com/payment-gate/webhook/stripe',
|
|
106
|
-
externalId: 'we_gone', secret: 'whsec_gone', apiVersion: SDK_API_VERSION, events: [], hash: 'x', createdAt: new Date(),
|
|
107
|
-
})
|
|
108
|
-
;(fake.ctx.cfg.services?.[SERVICE] as { host: string }).host = HOST
|
|
109
|
-
fake.state.calls.length = 0
|
|
110
|
-
|
|
111
|
-
const moved = await webhookOf(fake.ctx).ensureWebhookEndpoint(fake.stripe)
|
|
112
|
-
expect(moved).toEqual(expect.objectContaining({ url: URL }))
|
|
113
|
-
expect(fake.state.calls).toEqual([
|
|
114
|
-
'webhookEndpoints.list', 'webhookEndpoints.create', 'webhookEndpoints.del', 'webhookEndpoints.del',
|
|
115
|
-
])
|
|
116
|
-
expect(fake.state.webhookEndpoints.map(endpoint => endpoint.id)).toEqual([moved?.externalId])
|
|
117
|
-
expect(moved?.externalId).not.toBe(former?.externalId)
|
|
118
|
-
expect(fake.stores['payment-webhook'].rows).toEqual([expect.objectContaining({ url: URL, externalId: moved?.externalId })])
|
|
119
|
-
})
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
describe('@owlmeans/server-payment — webhook secret', () => {
|
|
123
|
-
test('resolves the configured secret first, then the stored one, else fails setup', async () => {
|
|
124
|
-
const bare = await makeFakeContext()
|
|
125
|
-
await expect(webhookOf(bare.ctx).stripeWebhookSecret()).rejects.toBeInstanceOf(WebhookSetupError)
|
|
126
|
-
|
|
127
|
-
await webhookOf(bare.ctx).ensureWebhookEndpoint(bare.stripe)
|
|
128
|
-
expect(await webhookOf(bare.ctx).stripeWebhookSecret()).toBe(bare.state.webhookEndpoints[0].secret)
|
|
129
|
-
|
|
130
|
-
const configured = await makeFakeContext({ webhookSecret: 'whsec_file' })
|
|
131
|
-
await webhookOf(configured.ctx).ensureWebhookEndpoint(configured.stripe)
|
|
132
|
-
expect(await webhookOf(configured.ctx).stripeWebhookSecret()).toBe('whsec_file')
|
|
133
|
-
})
|
|
134
|
-
|
|
135
|
-
test('verifies a delivery with either secret, and refuses a signature neither verifies', async () => {
|
|
136
|
-
const fake = await makeFakeContext({ webhookSecret: 'whsec_file' })
|
|
137
|
-
await webhookOf(fake.ctx).ensureWebhookEndpoint(fake.stripe)
|
|
138
|
-
const stored = fake.state.webhookEndpoints[0].secret
|
|
139
|
-
const body = JSON.stringify({ id: 'evt_1', type: 'customer.created', data: { object: { id: 'cus_9', metadata: { entityId: 'e9' } } } })
|
|
140
|
-
const deliver = async (signature: string) => await webhookOf(fake.ctx).handleStripeWebhook(fake.stripe, {
|
|
141
|
-
rawBody: body, headers: { [STRIPE_SIGNATURE.toLowerCase()]: signature },
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
await deliver(`sig:${stored}`)
|
|
145
|
-
await deliver('sig:whsec_file')
|
|
146
|
-
expect(fake.stores['payment-paygate-customer'].rows).toHaveLength(1)
|
|
147
|
-
await expect(deliver('sig:whsec_forged')).rejects.toThrow('signature')
|
|
148
|
-
})
|
|
149
|
-
})
|
|
File without changes
|
|
File without changes
|