@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.
- 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,164 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'bun:test'
|
|
2
|
-
import { CheckoutLimitExceeded } from '@owlmeans/payment'
|
|
3
|
-
import type { CheckoutPlugin, CheckoutSettled } from '../src/types.js'
|
|
4
|
-
import {
|
|
5
|
-
CREDITS_PRODUCT, ENTITY, makeRightsContext, paidSession, send,
|
|
6
|
-
} from './consumer-fixtures.js'
|
|
7
|
-
import type { FakeContext } from './fake-stripe.js'
|
|
8
|
-
import { paymentAccessOf } from '../src/access.js'
|
|
9
|
-
import { stripeCheckoutOf } from '../src/plugins/stripe.js'
|
|
10
|
-
|
|
11
|
-
const successUrl = 'https://app.example.com/ok'
|
|
12
|
-
const topUp = (fake: FakeContext, amountMinor = 1_000) => stripeCheckoutOf(fake.ctx).createCheckoutLink(fake.stripe, {
|
|
13
|
-
productSku: CREDITS_PRODUCT, entityId: ENTITY, service: 'app', amountMinor, successUrl, country: 'US',
|
|
14
|
-
}, paymentAccessOf(fake.ctx).gateway().checkoutPlugins())
|
|
15
|
-
|
|
16
|
-
interface Journal {
|
|
17
|
-
admitted: unknown[]
|
|
18
|
-
created: unknown[]
|
|
19
|
-
settled: CheckoutSettled[]
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const recording = (alias: string, journal: Journal, extra: Partial<CheckoutPlugin> = {}): CheckoutPlugin => ({
|
|
23
|
-
alias,
|
|
24
|
-
admit: async (_ctx, attempt) => { journal.admitted.push({ alias, ...attempt }); return { reservationId: `${alias}-hold` } },
|
|
25
|
-
created: async (_ctx, created) => { journal.created.push({ alias, ...created }) },
|
|
26
|
-
settled: async (_ctx, settled) => { journal.settled.push({ ...settled, alias } as CheckoutSettled) },
|
|
27
|
-
...extra,
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
const journal = (): Journal => ({ admitted: [], created: [], settled: [] })
|
|
31
|
-
|
|
32
|
-
describe('checkout plugins — narrowing', () => {
|
|
33
|
-
test('narrowings compose to the smallest maximum; the amount policy read equals the enforcement', async () => {
|
|
34
|
-
const fake = await makeRightsContext()
|
|
35
|
-
paymentAccessOf(fake.ctx).gateway().use({ alias: 'tier', narrow: async () => ({ maximumMinor: 2_500, reason: 'per-purchase' }) })
|
|
36
|
-
paymentAccessOf(fake.ctx).gateway().use({
|
|
37
|
-
alias: 'window', narrow: async () => ({ maximumMinor: 4_000, reason: 'window', remainingMinor: 4_000 }),
|
|
38
|
-
})
|
|
39
|
-
const view = await paymentAccessOf(fake.ctx).gateway().amountPolicy(fake.ctx, ENTITY, CREDITS_PRODUCT)
|
|
40
|
-
expect(view.limit).toEqual(expect.objectContaining({ maximumMinor: 2_500, reason: 'per-purchase', blocked: false, narrowed: true }))
|
|
41
|
-
expect(view.policy.presetsMinor).toEqual([1_000, 2_000])
|
|
42
|
-
|
|
43
|
-
await topUp(fake, 2_500)
|
|
44
|
-
const refusal = await topUp(fake, 2_600).catch(error => error)
|
|
45
|
-
expect(refusal).toBeInstanceOf(CheckoutLimitExceeded)
|
|
46
|
-
expect(refusal).toEqual(expect.objectContaining({ reason: 'per-purchase', maximumMinor: 2_500, currency: 'usd' }))
|
|
47
|
-
expect(fake.state.checkoutSessions).toHaveLength(1)
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
test('a blocked limit refuses any amount, the minimum too', async () => {
|
|
51
|
-
const fake = await makeRightsContext()
|
|
52
|
-
const resetsAt = new Date(Date.now() + 86_400_000)
|
|
53
|
-
paymentAccessOf(fake.ctx).gateway().use({ alias: 'hold', narrow: async () => ({ maximumMinor: 0, reason: 'hold', resetsAt }) })
|
|
54
|
-
const view = await paymentAccessOf(fake.ctx).gateway().amountPolicy(fake.ctx, ENTITY, CREDITS_PRODUCT)
|
|
55
|
-
expect(view.limit?.blocked).toBe(true)
|
|
56
|
-
const refusal = await topUp(fake, 500).catch(error => error)
|
|
57
|
-
expect(refusal).toBeInstanceOf(CheckoutLimitExceeded)
|
|
58
|
-
expect(refusal.resetsAt?.getTime()).toBe(Math.floor(resetsAt.getTime() / 1000) * 1000 === resetsAt.getTime() ? resetsAt.getTime() : refusal.resetsAt?.getTime())
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
test('a plugin registered twice under one alias replaces the first; a narrowing error fails closed', async () => {
|
|
62
|
-
const fake = await makeRightsContext()
|
|
63
|
-
paymentAccessOf(fake.ctx).gateway().use({ alias: 'tier', narrow: async () => ({ maximumMinor: 600, reason: 'old' }) })
|
|
64
|
-
paymentAccessOf(fake.ctx).gateway().use({ alias: 'tier', narrow: async () => ({ maximumMinor: 3_000, reason: 'new' }) })
|
|
65
|
-
expect(paymentAccessOf(fake.ctx).gateway().checkoutPlugins()).toHaveLength(1)
|
|
66
|
-
expect((await paymentAccessOf(fake.ctx).gateway().amountPolicy(fake.ctx, ENTITY, CREDITS_PRODUCT)).limit?.reason).toBe('new')
|
|
67
|
-
|
|
68
|
-
paymentAccessOf(fake.ctx).gateway().use({ alias: 'broken', narrow: async () => { throw new Error('tier store down') } })
|
|
69
|
-
await expect(topUp(fake)).rejects.toThrow('tier store down')
|
|
70
|
-
expect(fake.state.checkoutSessions).toHaveLength(0)
|
|
71
|
-
})
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
describe('checkout plugins — admission and the session', () => {
|
|
75
|
-
test('admit sees the attempt; created gets the session, the amount, the reservation and the expiry', async () => {
|
|
76
|
-
const fake = await makeRightsContext()
|
|
77
|
-
const log = journal()
|
|
78
|
-
paymentAccessOf(fake.ctx).gateway().use(recording('tier', log, { sessionTtlSeconds: 3_600 }))
|
|
79
|
-
const before = Math.floor(Date.now() / 1000)
|
|
80
|
-
await topUp(fake)
|
|
81
|
-
expect(log.admitted[0]).toEqual(expect.objectContaining({
|
|
82
|
-
alias: 'tier', entityId: ENTITY, productSku: CREDITS_PRODUCT, mode: 'amount', amountMinor: 1_000,
|
|
83
|
-
amountCurrency: 'usd', chargeMinor: 1_021, currency: 'usd', sessionTtlSeconds: 3_600,
|
|
84
|
-
}))
|
|
85
|
-
expect(log.created[0]).toEqual(expect.objectContaining({
|
|
86
|
-
alias: 'tier', sessionId: expect.stringMatching(/^cs_/), url: expect.stringContaining('https://'),
|
|
87
|
-
reservationId: 'tier-hold', amountMinor: 1_000,
|
|
88
|
-
}))
|
|
89
|
-
const expiresAt = fake.state.checkoutSessions[0].expires_at as number
|
|
90
|
-
expect(expiresAt).toBeGreaterThanOrEqual(before + 3_600)
|
|
91
|
-
expect(expiresAt).toBeLessThanOrEqual(before + 3_602)
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
test('the session lifetime is the smallest declared, clamped to Stripe’s 30 min – 24 h', async () => {
|
|
95
|
-
const fake = await makeRightsContext()
|
|
96
|
-
paymentAccessOf(fake.ctx).gateway().use({ alias: 'short', sessionTtlSeconds: 60 })
|
|
97
|
-
paymentAccessOf(fake.ctx).gateway().use({ alias: 'long', sessionTtlSeconds: 7_200 })
|
|
98
|
-
const now = Math.floor(Date.now() / 1000)
|
|
99
|
-
await topUp(fake)
|
|
100
|
-
expect(fake.state.checkoutSessions[0].expires_at - now).toBeGreaterThanOrEqual(1_800)
|
|
101
|
-
expect(fake.state.checkoutSessions[0].expires_at - now).toBeLessThanOrEqual(1_802)
|
|
102
|
-
|
|
103
|
-
const plain = await makeRightsContext()
|
|
104
|
-
await topUp(plain)
|
|
105
|
-
expect(plain.state.checkoutSessions[0].expires_at).toBeUndefined()
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
test('a veto releases what earlier plugins admitted and creates no session', async () => {
|
|
109
|
-
const fake = await makeRightsContext()
|
|
110
|
-
const log = journal()
|
|
111
|
-
paymentAccessOf(fake.ctx).gateway().use(recording('first', log))
|
|
112
|
-
paymentAccessOf(fake.ctx).gateway().use({
|
|
113
|
-
alias: 'veto',
|
|
114
|
-
admit: async () => { throw new CheckoutLimitExceeded({ reason: 'window', maximumMinor: 0, currency: 'usd' }) },
|
|
115
|
-
})
|
|
116
|
-
await expect(topUp(fake)).rejects.toBeInstanceOf(CheckoutLimitExceeded)
|
|
117
|
-
expect(fake.state.checkoutSessions).toHaveLength(0)
|
|
118
|
-
expect(log.settled).toEqual([expect.objectContaining({ alias: 'first', outcome: 'failed', reservationId: 'first-hold' })])
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
test('a created that throws expires the fresh session and releases every hold', async () => {
|
|
122
|
-
const fake = await makeRightsContext()
|
|
123
|
-
const log = journal()
|
|
124
|
-
paymentAccessOf(fake.ctx).gateway().use(recording('first', log))
|
|
125
|
-
paymentAccessOf(fake.ctx).gateway().use(recording('second', log, { created: async () => { throw new Error('reservation lost') } }))
|
|
126
|
-
await expect(topUp(fake)).rejects.toThrow('reservation lost')
|
|
127
|
-
expect(fake.state.expiredSessions).toHaveLength(1)
|
|
128
|
-
expect(log.settled.map(settled => [settled.outcome, settled.reservationId, settled.sessionId]))
|
|
129
|
-
.toEqual([['failed', 'first-hold', fake.state.expiredSessions[0]], ['failed', 'second-hold', fake.state.expiredSessions[0]]])
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
test('Stripe refusing the session releases the holds too', async () => {
|
|
133
|
-
const fake = await makeRightsContext()
|
|
134
|
-
const log = journal()
|
|
135
|
-
paymentAccessOf(fake.ctx).gateway().use(recording('first', log))
|
|
136
|
-
fake.state.failures['checkout.sessions.create'] = 'amount_too_small'
|
|
137
|
-
await expect(topUp(fake)).rejects.toThrow('amount_too_small')
|
|
138
|
-
expect(log.settled).toEqual([expect.objectContaining({ outcome: 'failed', reservationId: 'first-hold' })])
|
|
139
|
-
})
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
describe('checkout plugins — settlement from the webhook', () => {
|
|
143
|
-
test('paid, expired and failed sessions are settled; a settling error never fails the webhook', async () => {
|
|
144
|
-
const fake = await makeRightsContext()
|
|
145
|
-
const log = journal()
|
|
146
|
-
paymentAccessOf(fake.ctx).gateway().use(recording('tier', log))
|
|
147
|
-
paymentAccessOf(fake.ctx).gateway().use({ alias: 'broken', settled: async () => { throw new Error('ignored') } })
|
|
148
|
-
|
|
149
|
-
await send(fake, 'checkout.session.completed', paidSession({ id: 'cs_paid' }))
|
|
150
|
-
await send(fake, 'checkout.session.expired', { ...paidSession({ id: 'cs_gone', payment_status: 'unpaid' }), status: 'expired' })
|
|
151
|
-
await send(fake, 'checkout.session.async_payment_failed', paidSession({ id: 'cs_failed', payment_status: 'unpaid' }))
|
|
152
|
-
expect(log.settled.map(settled => [settled.sessionId, settled.outcome])).toEqual([
|
|
153
|
-
['cs_paid', 'paid'], ['cs_gone', 'expired'], ['cs_failed', 'failed'],
|
|
154
|
-
])
|
|
155
|
-
expect(log.settled[0]).toEqual(expect.objectContaining({ entityId: ENTITY, productSku: CREDITS_PRODUCT, amountMinor: 1_000 }))
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
test('an unmanaged gateway still narrows and answers the amount policy', async () => {
|
|
159
|
-
const fake = await makeRightsContext()
|
|
160
|
-
expect(paymentAccessOf(fake.ctx).gateway().managed).toBe(false)
|
|
161
|
-
paymentAccessOf(fake.ctx).gateway().use({ alias: 'tier', narrow: async () => ({ maximumMinor: 700, reason: 'per-purchase' }) })
|
|
162
|
-
expect((await paymentAccessOf(fake.ctx).gateway().amountPolicy(fake.ctx, ENTITY, CREDITS_PRODUCT)).policy.maximumMinor).toBe(700)
|
|
163
|
-
})
|
|
164
|
-
})
|
package/tests/checkout.spec.ts
DELETED
|
@@ -1,313 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'bun:test'
|
|
2
|
-
import {
|
|
3
|
-
CheckoutPricingMode, PlanDuration, PlanStatus, ProductError, ProductType, TaxBehavior,
|
|
4
|
-
} from '@owlmeans/payment'
|
|
5
|
-
import { createEventHandler } from '../src/plugins/events.js'
|
|
6
|
-
import type { PaymentPlan, PaymentProduct } from '../src/types.js'
|
|
7
|
-
import { CREDITS_PRODUCT, FREE, makeFakeContext, PLANS_PRODUCT, PRO, TEAM } from './fake-stripe.js'
|
|
8
|
-
import type { FakeContext } from './fake-stripe.js'
|
|
9
|
-
import { stripeCheckoutOf } from '../src/plugins/stripe.js'
|
|
10
|
-
import { stripeSessionHelper } from '../src/plugins/session.js'
|
|
11
|
-
import { freeSession } from './consumer-fixtures.js'
|
|
12
|
-
|
|
13
|
-
const proPrice = { id: 'price_pro', product: PLANS_PRODUCT, lookup_key: PRO, active: true, recurring: { interval: 'month' } }
|
|
14
|
-
const successUrl = 'https://app.example.com/ok'
|
|
15
|
-
|
|
16
|
-
const product: PaymentProduct = {
|
|
17
|
-
type: ProductType.Consumable, sku: 'credits', title: 'Credits', services: ['app'],
|
|
18
|
-
}
|
|
19
|
-
const plan: PaymentPlan = {
|
|
20
|
-
productSku: product.sku, sku: 'credits-unit', title: 'Credits', status: PlanStatus.Active,
|
|
21
|
-
duration: PlanDuration.Consumable, price: 0.02, currency: 'usd',
|
|
22
|
-
pricingMode: CheckoutPricingMode.Amount,
|
|
23
|
-
amountPolicy: {
|
|
24
|
-
currency: 'usd', minimumMinor: 500, maximumMinor: 50_000, defaultMinor: 1_000,
|
|
25
|
-
presetsMinor: [1_000, 2_000, 5_000, 10_000], fixedMinor: 0, rateBps: 200,
|
|
26
|
-
},
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const paid = (overrides: Record<string, unknown> = {}) => ({
|
|
30
|
-
id: 'cs_amount', mode: 'payment', payment_status: 'paid', customer: 'cus_1',
|
|
31
|
-
currency: 'usd', amount_subtotal: 1021, payment_intent: 'pi_1', invoice: 'in_1',
|
|
32
|
-
metadata: {
|
|
33
|
-
pricingMode: 'amount', amountMinor: '1000', chargeAmountMinor: '1021', currency: 'usd',
|
|
34
|
-
entityId: 'entity-1', service: 'app', productSku: CREDITS_PRODUCT, planSku: 'app-credit-unit',
|
|
35
|
-
},
|
|
36
|
-
...overrides,
|
|
37
|
-
})
|
|
38
|
-
|
|
39
|
-
const process = async (fake: FakeContext, type: string, object: unknown) =>
|
|
40
|
-
await createEventHandler(fake.ctx, fake.stripe).process({ type, data: { object } } as never)
|
|
41
|
-
const fulfillment = (fake: FakeContext) => fake.stores['payment-fulfillment'].rows
|
|
42
|
-
|
|
43
|
-
describe('Stripe checkout', () => {
|
|
44
|
-
test('amount checkout uses one inline, tax-exclusive item and no reusable Price', () => {
|
|
45
|
-
const result = stripeSessionHelper.amountCheckoutLineItem(product, plan, 1_000)
|
|
46
|
-
expect(result.chargeMinor).toBe(1_021)
|
|
47
|
-
expect(result.lineItem).toEqual({
|
|
48
|
-
price_data: { product: 'credits', currency: 'usd', unit_amount: 1_021, tax_behavior: 'exclusive' },
|
|
49
|
-
quantity: 1,
|
|
50
|
-
})
|
|
51
|
-
expect(result.lineItem.adjustable_quantity).toBeUndefined()
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
test('quantity checkout keeps a reusable Price and an adjustable quantity', () => {
|
|
55
|
-
expect(stripeSessionHelper.quantityCheckoutLineItem({ id: 'price_1' } as never, {
|
|
56
|
-
minimum: 5, maximum: 500, default: 10,
|
|
57
|
-
})).toEqual({
|
|
58
|
-
price: 'price_1', quantity: 10,
|
|
59
|
-
adjustable_quantity: { enabled: true, minimum: 5, maximum: 500 },
|
|
60
|
-
})
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
test('a subscription checkout takes the asked plan, locale and disclosure; a free plan is never checked out', async () => {
|
|
64
|
-
const fake = await makeFakeContext({
|
|
65
|
-
stripe: { prices: [{ id: 'price_team', product: PLANS_PRODUCT, lookup_key: TEAM, active: true, recurring: { interval: 'month' } }] },
|
|
66
|
-
})
|
|
67
|
-
await stripeCheckoutOf(fake.ctx).createCheckoutLink(fake.stripe, {
|
|
68
|
-
productSku: PLANS_PRODUCT, planSku: TEAM, entityId: 'entity-1', service: 'app', successUrl: 'https://app.example.com/ok',
|
|
69
|
-
locale: 'fr', submitText: 'Comprend des crédits prépayés obligatoires.',
|
|
70
|
-
})
|
|
71
|
-
expect(fake.state.checkoutSessions[0]).toEqual(expect.objectContaining({
|
|
72
|
-
mode: 'subscription', line_items: [{ price: 'price_team', quantity: 1 }],
|
|
73
|
-
locale: 'fr', custom_text: { submit: { message: 'Comprend des crédits prépayés obligatoires.' } },
|
|
74
|
-
}))
|
|
75
|
-
expect(fake.state.checkoutSessions[0].subscription_data.metadata).toEqual(expect.objectContaining({ entityId: 'entity-1', planSku: TEAM }))
|
|
76
|
-
expect(fake.state.customers.cus_1.preferred_locales).toEqual(['fr'])
|
|
77
|
-
|
|
78
|
-
await expect(stripeCheckoutOf(fake.ctx).createCheckoutLink(fake.stripe, {
|
|
79
|
-
productSku: PLANS_PRODUCT, planSku: FREE, entityId: 'entity-1', service: 'app',
|
|
80
|
-
})).rejects.toBeInstanceOf(ProductError)
|
|
81
|
-
})
|
|
82
|
-
})
|
|
83
|
-
|
|
84
|
-
describe('Stripe checkout — pricing policy', () => {
|
|
85
|
-
test('amount promotion codes are opt-in and never change the selected credit value or base charge', async () => {
|
|
86
|
-
for (const enabled of [undefined, false, true]) {
|
|
87
|
-
const fake = await makeFakeContext({
|
|
88
|
-
pricing: {
|
|
89
|
-
tax: { automatic: true, collectTaxId: true, estimate: false }, currency: { estimate: false },
|
|
90
|
-
...(enabled != null ? { stripe: { amountPromotionCodes: enabled } } : {}),
|
|
91
|
-
},
|
|
92
|
-
})
|
|
93
|
-
await stripeCheckoutOf(fake.ctx).createCheckoutLink(fake.stripe, {
|
|
94
|
-
productSku: CREDITS_PRODUCT, entityId: 'entity-1', service: 'app', amountMinor: 1000, successUrl,
|
|
95
|
-
})
|
|
96
|
-
const session = fake.state.checkoutSessions[0]
|
|
97
|
-
expect(session.allow_promotion_codes).toBe(enabled === true)
|
|
98
|
-
expect(session.metadata.amountMinor).toBe('1000')
|
|
99
|
-
expect(session.line_items[0].price_data.unit_amount).toBe(1021)
|
|
100
|
-
}
|
|
101
|
-
})
|
|
102
|
-
|
|
103
|
-
test('an undeclared policy reproduces exactly the session hard-coded before this policy existed', async () => {
|
|
104
|
-
const fake = await makeFakeContext({ stripe: { prices: [proPrice] } })
|
|
105
|
-
await stripeCheckoutOf(fake.ctx).createCheckoutLink(fake.stripe, {
|
|
106
|
-
productSku: PLANS_PRODUCT, planSku: PRO, entityId: 'entity-1', service: 'app', successUrl,
|
|
107
|
-
})
|
|
108
|
-
const session = fake.state.checkoutSessions[0]
|
|
109
|
-
expect(session).toEqual(expect.objectContaining({
|
|
110
|
-
automatic_tax: { enabled: true }, billing_address_collection: 'required',
|
|
111
|
-
tax_id_collection: { enabled: true }, customer_update: { address: 'auto', name: 'auto' },
|
|
112
|
-
}))
|
|
113
|
-
expect(session.adaptive_pricing).toEqual({ enabled: false })
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
test('tax.automatic and tax.collectTaxId are independent switches', async () => {
|
|
117
|
-
const fake = await makeFakeContext({
|
|
118
|
-
pricing: { tax: { automatic: false, collectTaxId: true, estimate: false }, currency: { estimate: false } },
|
|
119
|
-
stripe: { prices: [proPrice] },
|
|
120
|
-
})
|
|
121
|
-
await stripeCheckoutOf(fake.ctx).createCheckoutLink(fake.stripe, {
|
|
122
|
-
productSku: PLANS_PRODUCT, planSku: PRO, entityId: 'entity-1', service: 'app', successUrl,
|
|
123
|
-
})
|
|
124
|
-
const session = fake.state.checkoutSessions[0]
|
|
125
|
-
expect(session.automatic_tax).toBeUndefined()
|
|
126
|
-
expect(session.billing_address_collection).toBeUndefined()
|
|
127
|
-
expect(session.tax_id_collection).toEqual({ enabled: true })
|
|
128
|
-
// only the concern that needs it contributes a `customer_update` field
|
|
129
|
-
expect(session.customer_update).toEqual({ name: 'auto' })
|
|
130
|
-
})
|
|
131
|
-
|
|
132
|
-
test('adaptive_pricing is enabled when currency.adaptive is declared', async () => {
|
|
133
|
-
const fake = await makeFakeContext({
|
|
134
|
-
pricing: { tax: { automatic: true, collectTaxId: true, estimate: false }, currency: { adaptive: true, estimate: false } },
|
|
135
|
-
stripe: { prices: [proPrice] },
|
|
136
|
-
})
|
|
137
|
-
await stripeCheckoutOf(fake.ctx).createCheckoutLink(fake.stripe, {
|
|
138
|
-
productSku: PLANS_PRODUCT, planSku: PRO, entityId: 'entity-1', service: 'app', successUrl,
|
|
139
|
-
})
|
|
140
|
-
expect(fake.state.checkoutSessions[0].adaptive_pricing).toEqual({ enabled: true })
|
|
141
|
-
})
|
|
142
|
-
|
|
143
|
-
test('subscription checkout can explicitly offer recurring-capable payment methods', async () => {
|
|
144
|
-
const fake = await makeFakeContext({
|
|
145
|
-
pricing: {
|
|
146
|
-
tax: { automatic: true, collectTaxId: true, estimate: false }, currency: { estimate: false },
|
|
147
|
-
stripe: { subscriptionPaymentMethodTypes: ['CARD', 'link', 'klarna'] },
|
|
148
|
-
},
|
|
149
|
-
stripe: { prices: [proPrice] },
|
|
150
|
-
})
|
|
151
|
-
await stripeCheckoutOf(fake.ctx).createCheckoutLink(fake.stripe, {
|
|
152
|
-
productSku: PLANS_PRODUCT, planSku: PRO, entityId: 'entity-1', service: 'app', successUrl,
|
|
153
|
-
})
|
|
154
|
-
expect(fake.state.checkoutSessions[0].payment_method_types).toEqual(['card', 'link', 'klarna'])
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
test('currency-specific subscription methods reject invalid currencies and method lists', async () => {
|
|
158
|
-
for (const methods of [
|
|
159
|
-
{ US: ['card'] }, { USD: [] }, { USD: ['CARD', 'card'] },
|
|
160
|
-
{ USD: ['not a method'] }, { USD: ['card'], usd: ['link'] },
|
|
161
|
-
]) {
|
|
162
|
-
await expect(makeFakeContext({ pricing: {
|
|
163
|
-
tax: { automatic: false, collectTaxId: false, estimate: false }, currency: { estimate: false },
|
|
164
|
-
stripe: { subscriptionPaymentMethodTypesByCurrency: methods },
|
|
165
|
-
} })).rejects.toThrow('pricing-policy:subscription-payment-methods')
|
|
166
|
-
}
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
test('a declared behavior is set on the inline amount line item', async () => {
|
|
170
|
-
const fake = await makeFakeContext({
|
|
171
|
-
pricing: { tax: { automatic: true, collectTaxId: true, estimate: false, behavior: TaxBehavior.Inclusive }, currency: { estimate: false } },
|
|
172
|
-
})
|
|
173
|
-
await stripeCheckoutOf(fake.ctx).createCheckoutLink(fake.stripe, {
|
|
174
|
-
productSku: CREDITS_PRODUCT, entityId: 'entity-1', service: 'app', amountMinor: 1_000, successUrl,
|
|
175
|
-
})
|
|
176
|
-
expect(fake.state.checkoutSessions[0].line_items[0].price_data.tax_behavior).toBe('inclusive')
|
|
177
|
-
})
|
|
178
|
-
|
|
179
|
-
test('an amount checkout converts the USD catalogue charge to the settlement currency', async () => {
|
|
180
|
-
const fake = await makeFakeContext({
|
|
181
|
-
pricing: {
|
|
182
|
-
tax: { automatic: true, collectTaxId: true, estimate: false },
|
|
183
|
-
currency: { adaptive: true, estimate: false }, stripe: { settlementCurrency: 'EUR' },
|
|
184
|
-
},
|
|
185
|
-
stripe: { fxRates: { usd: { exchangeRate: 0.853568, referenceRate: 0.8726 } } },
|
|
186
|
-
})
|
|
187
|
-
await stripeCheckoutOf(fake.ctx).createCheckoutLink(fake.stripe, {
|
|
188
|
-
productSku: CREDITS_PRODUCT, entityId: 'entity-1', service: 'app', amountMinor: 1_000, successUrl,
|
|
189
|
-
})
|
|
190
|
-
const session = fake.state.checkoutSessions[0]
|
|
191
|
-
expect(session.line_items[0].price_data).toEqual(expect.objectContaining({ currency: 'eur', unit_amount: 891 }))
|
|
192
|
-
expect(session.metadata).toEqual(expect.objectContaining({
|
|
193
|
-
amountMinor: '1000', sourceChargeAmountMinor: '1021', amountCurrency: 'usd',
|
|
194
|
-
chargeAmountMinor: '891', currency: 'eur',
|
|
195
|
-
}))
|
|
196
|
-
expect(fake.state.rawRequests[0]).toEqual(expect.objectContaining({
|
|
197
|
-
params: { to_currency: 'eur', 'from_currencies[]': 'usd', lock_duration: 'none' },
|
|
198
|
-
}))
|
|
199
|
-
})
|
|
200
|
-
})
|
|
201
|
-
|
|
202
|
-
describe('Stripe checkout fulfillment', () => {
|
|
203
|
-
test('percentage and fixed discounts grant the full selected credit value once', async () => {
|
|
204
|
-
for (const discount of [511, 100]) {
|
|
205
|
-
const fake = await makeFakeContext()
|
|
206
|
-
const session = paid({
|
|
207
|
-
allow_promotion_codes: true, amount_total: 1021 - discount,
|
|
208
|
-
total_details: { amount_tax: 0, amount_discount: discount },
|
|
209
|
-
})
|
|
210
|
-
await process(fake, 'checkout.session.completed', session)
|
|
211
|
-
await process(fake, 'checkout.session.completed', session)
|
|
212
|
-
expect(fake.observed.topUp).toEqual([expect.objectContaining({ amountMinor: 1000, chargeAmountMinor: 1021 })])
|
|
213
|
-
expect(fulfillment(fake)[0].amountTotalMinor).toBe(1021 - discount)
|
|
214
|
-
}
|
|
215
|
-
})
|
|
216
|
-
|
|
217
|
-
test('a fully discounted completion without a payment intent retries the observer and fulfills once', async () => {
|
|
218
|
-
for (const payment_status of ['no_payment_required', 'paid']) {
|
|
219
|
-
const fake = await makeFakeContext()
|
|
220
|
-
const session = freeSession({ payment_status })
|
|
221
|
-
fake.observed.failTopUp = 1
|
|
222
|
-
await expect(process(fake, 'checkout.session.completed', session)).rejects.toThrow('ledger unavailable')
|
|
223
|
-
await process(fake, 'checkout.session.completed', session)
|
|
224
|
-
await process(fake, 'checkout.session.completed', session)
|
|
225
|
-
expect(fake.observed.topUp).toEqual([expect.objectContaining({ amountMinor: 1000, chargeAmountMinor: 919 })])
|
|
226
|
-
expect(fulfillment(fake)[0]).toEqual(expect.objectContaining({ amountTotalMinor: 0, fulfilledAt: expect.any(Date) }))
|
|
227
|
-
expect(fulfillment(fake)[0].paymentIntentId).toBeUndefined()
|
|
228
|
-
}
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
test('free fulfillment requires a completed amount session and a full authorized discount', async () => {
|
|
232
|
-
const fake = await makeFakeContext()
|
|
233
|
-
for (const patch of [
|
|
234
|
-
{ status: 'open' }, { payment_status: 'unpaid' }, { amount_total: 1 },
|
|
235
|
-
{ allow_promotion_codes: false }, { metadata: { pricingMode: 'quantity' } },
|
|
236
|
-
{ total_details: { amount_discount: 918, amount_tax: 0 } },
|
|
237
|
-
{ total_details: { amount_discount: 919, amount_tax: 1 } },
|
|
238
|
-
]) {
|
|
239
|
-
await process(fake, 'checkout.session.completed', freeSession(patch))
|
|
240
|
-
}
|
|
241
|
-
expect(fake.observed.topUp).toHaveLength(0)
|
|
242
|
-
await expect(process(fake, 'checkout.session.completed', freeSession({ metadata: { currency: 'usd' } }))).rejects.toThrow('amount-mismatch')
|
|
243
|
-
await expect(process(fake, 'checkout.session.completed', freeSession({ metadata: { chargeAmountMinor: '918' } }))).rejects.toThrow('amount-mismatch')
|
|
244
|
-
})
|
|
245
|
-
|
|
246
|
-
test('fulfills a paid amount once even when the session carries Adaptive Pricing presentment details', async () => {
|
|
247
|
-
const fake = await makeFakeContext()
|
|
248
|
-
await process(fake, 'checkout.session.completed', paid({
|
|
249
|
-
presentment_details: { presentment_amount: 4_613, presentment_currency: 'eur' },
|
|
250
|
-
}))
|
|
251
|
-
expect(fake.observed.topUp).toEqual([expect.objectContaining({ amountMinor: 1_000, chargeAmountMinor: 1_021 })])
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
test('fulfills a paid amount once, without crediting the adjustment, and records the payment intent', async () => {
|
|
255
|
-
const fake = await makeFakeContext()
|
|
256
|
-
await process(fake, 'checkout.session.completed', paid())
|
|
257
|
-
await process(fake, 'checkout.session.completed', paid())
|
|
258
|
-
expect(fake.observed.topUp).toEqual([expect.objectContaining({
|
|
259
|
-
mode: 'amount', amountMinor: 1_000, chargeAmountMinor: 1_021, externalId: 'cs_amount', entityId: 'entity-1',
|
|
260
|
-
})])
|
|
261
|
-
expect(fulfillment(fake)[0]).toEqual(expect.objectContaining({
|
|
262
|
-
externalId: 'cs_amount', paymentIntentId: 'pi_1', invoiceId: 'in_1', mode: 'amount',
|
|
263
|
-
}))
|
|
264
|
-
expect(fulfillment(fake)[0].fulfilledAt).toBeInstanceOf(Date)
|
|
265
|
-
expect(fake.stores['payment-subscription'].rows).toHaveLength(0)
|
|
266
|
-
})
|
|
267
|
-
|
|
268
|
-
test('fulfills the USD credit value from an EUR settlement charge', async () => {
|
|
269
|
-
const fake = await makeFakeContext()
|
|
270
|
-
await process(fake, 'checkout.session.completed', paid({
|
|
271
|
-
currency: 'eur', amount_subtotal: 891,
|
|
272
|
-
metadata: {
|
|
273
|
-
pricingMode: 'amount', amountMinor: '1000', sourceChargeAmountMinor: '1021', amountCurrency: 'usd',
|
|
274
|
-
chargeAmountMinor: '891', currency: 'eur', entityId: 'entity-1', service: 'app',
|
|
275
|
-
productSku: CREDITS_PRODUCT, planSku: 'app-credit-unit',
|
|
276
|
-
},
|
|
277
|
-
}))
|
|
278
|
-
expect(fake.observed.topUp[0]).toEqual(expect.objectContaining({
|
|
279
|
-
amountMinor: 1_000, sourceChargeAmountMinor: 1_021, amountCurrency: 'usd',
|
|
280
|
-
chargeAmountMinor: 891, currency: 'eur',
|
|
281
|
-
}))
|
|
282
|
-
expect(fulfillment(fake)[0]).toEqual(expect.objectContaining({ amountCurrency: 'usd', currency: 'eur' }))
|
|
283
|
-
})
|
|
284
|
-
|
|
285
|
-
test('grants nothing for an unpaid completion; the asynchronous success does', async () => {
|
|
286
|
-
const fake = await makeFakeContext()
|
|
287
|
-
await process(fake, 'checkout.session.completed', paid({ payment_status: 'unpaid' }))
|
|
288
|
-
expect(fake.observed.topUp).toHaveLength(0)
|
|
289
|
-
await process(fake, 'checkout.session.async_payment_succeeded', paid())
|
|
290
|
-
expect(fake.observed.topUp).toHaveLength(1)
|
|
291
|
-
})
|
|
292
|
-
|
|
293
|
-
test('rejects a currency or subtotal mismatch; retries after a failed observer', async () => {
|
|
294
|
-
const fake = await makeFakeContext()
|
|
295
|
-
await expect(process(fake, 'checkout.session.completed', paid({ amount_subtotal: 1020 }))).rejects.toThrow()
|
|
296
|
-
|
|
297
|
-
fake.observed.failTopUp = 1
|
|
298
|
-
await expect(process(fake, 'checkout.session.completed', paid())).rejects.toThrow('ledger unavailable')
|
|
299
|
-
expect(fulfillment(fake)[0]?.fulfilledAt).toBeUndefined()
|
|
300
|
-
await process(fake, 'checkout.session.completed', paid())
|
|
301
|
-
expect(fake.observed.topUp).toHaveLength(1)
|
|
302
|
-
expect(fulfillment(fake)[0].fulfilledAt).toBeInstanceOf(Date)
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
test('a legacy session without pricing metadata fulfills by quantity', async () => {
|
|
306
|
-
const fake = await makeFakeContext()
|
|
307
|
-
await process(fake, 'checkout.session.completed', paid({
|
|
308
|
-
id: 'cs_legacy', metadata: { entityId: 'entity-1', service: 'app', productSku: CREDITS_PRODUCT, planSku: 'app-credit-unit' },
|
|
309
|
-
}))
|
|
310
|
-
expect(fake.observed.topUp[0]).toEqual(expect.objectContaining({ mode: 'quantity', units: 7 }))
|
|
311
|
-
expect(fulfillment(fake)[0]).toEqual(expect.objectContaining({ mode: 'quantity', units: 7 }))
|
|
312
|
-
})
|
|
313
|
-
})
|