@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
package/tests/events.spec.ts
DELETED
|
@@ -1,367 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'bun:test'
|
|
2
|
-
import { ProductError, SubscriptionStatus } from '@owlmeans/payment'
|
|
3
|
-
import { createEventHandler } from '../src/plugins/events.js'
|
|
4
|
-
import type { PaymentSubscriptionRecord, PropagatedState } from '../src/types.js'
|
|
5
|
-
import {
|
|
6
|
-
CAP_WHITELABEL, CREDITS_PRODUCT, eventOf, FREE, future, makeFakeContext, PRO, subscriptionOf, TEAM,
|
|
7
|
-
} from './fake-stripe.js'
|
|
8
|
-
import type { FakeContext, SubscriptionShape } from './fake-stripe.js'
|
|
9
|
-
import { paymentAccessOf } from '../src/access.js'
|
|
10
|
-
import { subscriptionHelper } from '../src/subscription.js'
|
|
11
|
-
import { stripeSubscriptionsOf } from '../src/plugins/subscriptions.js'
|
|
12
|
-
import { mapStatus } from '../src/plugins/status.js'
|
|
13
|
-
|
|
14
|
-
const send = async (fake: FakeContext, type: string, object: unknown, overrides: Record<string, unknown> = {}) => {
|
|
15
|
-
const event = eventOf(type, object, overrides)
|
|
16
|
-
await createEventHandler(fake.ctx, fake.stripe).process(event)
|
|
17
|
-
return event
|
|
18
|
-
}
|
|
19
|
-
const rows = (fake: FakeContext) => fake.stores['payment-subscription'].rows
|
|
20
|
-
const changes = (fake: FakeContext) => fake.observed.subscription.map(event => event.change)
|
|
21
|
-
const epoch = (date: Date): number => Math.floor(date.getTime() / 1000)
|
|
22
|
-
|
|
23
|
-
const subscribe = async (fake: FakeContext, shape: SubscriptionShape = {}) => {
|
|
24
|
-
const subscription = subscriptionOf(shape)
|
|
25
|
-
fake.state.subscriptions[subscription.id] = subscription as never
|
|
26
|
-
await send(fake, 'customer.subscription.updated', subscription)
|
|
27
|
-
return subscription
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const record = (status: SubscriptionStatus, overrides: Partial<PaymentSubscriptionRecord> = {}): PaymentSubscriptionRecord => ({
|
|
31
|
-
entityId: 'e', planSku: PRO, productSku: 'p', service: 's', paygate: 'stripe', externalId: 'sub_1', status,
|
|
32
|
-
rank: 10, createdAt: new Date(), ...overrides,
|
|
33
|
-
})
|
|
34
|
-
const state = (overrides: Partial<PropagatedState> = {}): PropagatedState => ({
|
|
35
|
-
planSku: PRO, rank: 10, status: SubscriptionStatus.Active, cancelAtPeriodEnd: false, ...overrides,
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
describe('@owlmeans/server-payment — subscription change classification', () => {
|
|
39
|
-
test('created once entitling; canceled; paused; resumed from a pause or a suspension', () => {
|
|
40
|
-
expect(subscriptionHelper.classifySubscriptionChange(null, record(SubscriptionStatus.Active))).toBe('created')
|
|
41
|
-
expect(subscriptionHelper.classifySubscriptionChange(null, record(SubscriptionStatus.Trial))).toBe('created')
|
|
42
|
-
expect(subscriptionHelper.classifySubscriptionChange(null, record(SubscriptionStatus.Created))).toBeNull()
|
|
43
|
-
expect(subscriptionHelper.classifySubscriptionChange(state(), record(SubscriptionStatus.Canceled))).toBe('canceled')
|
|
44
|
-
expect(subscriptionHelper.classifySubscriptionChange(state(), record(SubscriptionStatus.Ended))).toBe('canceled')
|
|
45
|
-
expect(subscriptionHelper.classifySubscriptionChange(state(), record(SubscriptionStatus.Suspended, { pausedAt: new Date() }))).toBe('paused')
|
|
46
|
-
expect(subscriptionHelper.classifySubscriptionChange(state({ status: SubscriptionStatus.Suspended, pausedAt: new Date() }), record(SubscriptionStatus.Active))).toBe('resumed')
|
|
47
|
-
expect(subscriptionHelper.classifySubscriptionChange(state({ status: SubscriptionStatus.Suspended }), record(SubscriptionStatus.Active))).toBe('resumed')
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
test('plan changes by rank, cancel scheduling, renewal once per invoice, dunning, trial end, else nothing', () => {
|
|
51
|
-
expect(subscriptionHelper.classifySubscriptionChange(state(), record(SubscriptionStatus.Active, { planSku: TEAM, rank: 20 }))).toBe('upgraded')
|
|
52
|
-
expect(subscriptionHelper.classifySubscriptionChange(state({ planSku: TEAM, rank: 20 }), record(SubscriptionStatus.Active))).toBe('downgraded')
|
|
53
|
-
expect(subscriptionHelper.classifySubscriptionChange(state(), record(SubscriptionStatus.Active, { planSku: 'pro-yearly', rank: 10 }))).toBe('upgraded')
|
|
54
|
-
expect(subscriptionHelper.classifySubscriptionChange(state(), record(SubscriptionStatus.Active, { cancelAtPeriodEnd: true }))).toBe('cancel-scheduled')
|
|
55
|
-
expect(subscriptionHelper.classifySubscriptionChange(state({ cancelAtPeriodEnd: true }), record(SubscriptionStatus.Active))).toBe('cancel-undone')
|
|
56
|
-
expect(subscriptionHelper.classifySubscriptionChange(state(), record(SubscriptionStatus.Active), { renewal: true, invoiceId: 'in_2' })).toBe('renewed')
|
|
57
|
-
expect(subscriptionHelper.classifySubscriptionChange(state({ renewedInvoiceId: 'in_2' }), record(SubscriptionStatus.Active), { renewal: true, invoiceId: 'in_2' })).toBeNull()
|
|
58
|
-
expect(subscriptionHelper.classifySubscriptionChange(state(), record(SubscriptionStatus.PastDue))).toBe('past-due')
|
|
59
|
-
expect(subscriptionHelper.classifySubscriptionChange(state(), record(SubscriptionStatus.Suspended))).toBe('suspended')
|
|
60
|
-
expect(subscriptionHelper.classifySubscriptionChange(state(), record(SubscriptionStatus.Trial), { trialEnding: true })).toBe('trial-ending')
|
|
61
|
-
expect(subscriptionHelper.classifySubscriptionChange(state(), record(SubscriptionStatus.Active))).toBeNull()
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
test('maps paygate statuses: past_due entitles, unpaid, paused and paused collection revoke', () => {
|
|
65
|
-
const map = (status: string, pause: unknown = null) => mapStatus({ status, pause_collection: pause } as never)
|
|
66
|
-
expect(map('active')).toBe(SubscriptionStatus.Active)
|
|
67
|
-
expect(map('trialing')).toBe(SubscriptionStatus.Trial)
|
|
68
|
-
expect(map('past_due')).toBe(SubscriptionStatus.PastDue)
|
|
69
|
-
expect(map('unpaid')).toBe(SubscriptionStatus.Suspended)
|
|
70
|
-
expect(map('paused')).toBe(SubscriptionStatus.Suspended)
|
|
71
|
-
expect(map('active', { behavior: 'void' })).toBe(SubscriptionStatus.Suspended)
|
|
72
|
-
expect(map('incomplete')).toBe(SubscriptionStatus.Created)
|
|
73
|
-
expect(map('incomplete_expired')).toBe(SubscriptionStatus.Ended)
|
|
74
|
-
expect(map('canceled')).toBe(SubscriptionStatus.Canceled)
|
|
75
|
-
})
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
describe('@owlmeans/server-payment — customer and checkout events', () => {
|
|
79
|
-
test('customer created and updated upsert the customer; deleted marks it', async () => {
|
|
80
|
-
const fake = await makeFakeContext()
|
|
81
|
-
await send(fake, 'customer.created', { id: 'cus_1', email: 'a@example.com', metadata: { entityId: 'entity-1' } })
|
|
82
|
-
await send(fake, 'customer.updated', { id: 'cus_1', email: 'b@example.com', metadata: {} })
|
|
83
|
-
expect(fake.stores['payment-paygate-customer'].rows).toEqual([
|
|
84
|
-
expect.objectContaining({ externalId: 'cus_1', entityId: 'entity-1', email: 'b@example.com' }),
|
|
85
|
-
])
|
|
86
|
-
await send(fake, 'customer.deleted', { id: 'cus_1', deleted: true })
|
|
87
|
-
expect(fake.stores['payment-paygate-customer'].rows[0].deletedAt).toBeInstanceOf(Date)
|
|
88
|
-
})
|
|
89
|
-
|
|
90
|
-
test('an asynchronous payment failure is recorded and observed; an expired session leaves nothing', async () => {
|
|
91
|
-
const fake = await makeFakeContext()
|
|
92
|
-
const session = {
|
|
93
|
-
id: 'cs_1', mode: 'payment', customer: 'cus_1', payment_intent: 'pi_1',
|
|
94
|
-
metadata: { entityId: 'entity-1', service: 'app', productSku: CREDITS_PRODUCT, pricingMode: 'amount' },
|
|
95
|
-
}
|
|
96
|
-
await send(fake, 'checkout.session.async_payment_failed', session)
|
|
97
|
-
expect(fake.stores['payment-fulfillment'].rows[0]).toEqual(expect.objectContaining({ externalId: 'cs_1', paymentIntentId: 'pi_1' }))
|
|
98
|
-
expect(fake.stores['payment-fulfillment'].rows[0].failedAt).toBeInstanceOf(Date)
|
|
99
|
-
expect(fake.observed.paymentFailed).toEqual([
|
|
100
|
-
expect.objectContaining({ entityId: 'entity-1', kind: 'checkout', externalId: 'cs_1', eventKey: 'payment-failed:cs_1:0' }),
|
|
101
|
-
])
|
|
102
|
-
|
|
103
|
-
await send(fake, 'checkout.session.expired', session)
|
|
104
|
-
expect(fake.stores['payment-fulfillment'].rows).toHaveLength(0)
|
|
105
|
-
})
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
describe('@owlmeans/server-payment — subscription events', () => {
|
|
109
|
-
test('observers hear `created` once the subscription entitles; a repeated or payment-method-only event says nothing', async () => {
|
|
110
|
-
const fake = await makeFakeContext()
|
|
111
|
-
await send(fake, 'customer.subscription.created', subscriptionOf({ status: 'incomplete' }))
|
|
112
|
-
expect(rows(fake)[0].status).toBe(SubscriptionStatus.Created)
|
|
113
|
-
expect(fake.observed.subscription).toHaveLength(0)
|
|
114
|
-
|
|
115
|
-
const active = subscriptionOf()
|
|
116
|
-
const event = await send(fake, 'customer.subscription.updated', active)
|
|
117
|
-
const [created] = fake.observed.subscription
|
|
118
|
-
expect(created).toEqual(expect.objectContaining({
|
|
119
|
-
change: 'created', active: true, previous: null, externalEventId: event.id,
|
|
120
|
-
eventKey: `subscription:sub_1:created:${new Date(active.created * 1000).toISOString()}`,
|
|
121
|
-
}))
|
|
122
|
-
expect(created.current).toEqual(expect.objectContaining({ planSku: PRO, rank: 10, entityId: 'entity-1', subscriptionId: 'sub_1' }))
|
|
123
|
-
expect(created.current.capabilities?.[0].permissions.whitelabel).toBe(true)
|
|
124
|
-
expect(rows(fake)[0].initialPropagatedAt).toBeInstanceOf(Date)
|
|
125
|
-
|
|
126
|
-
await createEventHandler(fake.ctx, fake.stripe).process(event)
|
|
127
|
-
await send(fake, 'customer.subscription.updated', subscriptionOf({ defaultPaymentMethod: 'pm_2' }))
|
|
128
|
-
expect(changes(fake)).toEqual(['created'])
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
test("resolves the plan from the price's `metadata.sku` when a replaced price carries no lookup key", async () => {
|
|
132
|
-
const fake = await makeFakeContext()
|
|
133
|
-
const replaced = {
|
|
134
|
-
...subscriptionOf(),
|
|
135
|
-
items: {
|
|
136
|
-
object: 'list', has_more: false, url: '',
|
|
137
|
-
data: [{ id: 'si_1', price: { id: 'price_new', lookup_key: null, metadata: { sku: PRO } } }],
|
|
138
|
-
},
|
|
139
|
-
} as never
|
|
140
|
-
await send(fake, 'customer.subscription.updated', replaced)
|
|
141
|
-
expect(rows(fake)[0]).toEqual(expect.objectContaining({ planSku: PRO, priceId: 'price_new' }))
|
|
142
|
-
})
|
|
143
|
-
|
|
144
|
-
test('plan changes and cancel scheduling are classified with stable keys', async () => {
|
|
145
|
-
const fake = await makeFakeContext()
|
|
146
|
-
await subscribe(fake)
|
|
147
|
-
await send(fake, 'customer.subscription.updated', subscriptionOf({ planSku: TEAM }))
|
|
148
|
-
await send(fake, 'customer.subscription.pending_update_applied', subscriptionOf({ planSku: PRO }))
|
|
149
|
-
await send(fake, 'customer.subscription.updated', subscriptionOf({ cancelAtPeriodEnd: true }))
|
|
150
|
-
const undo = await send(fake, 'customer.subscription.updated', subscriptionOf({ cancelAtPeriodEnd: false }))
|
|
151
|
-
|
|
152
|
-
expect(changes(fake)).toEqual(['created', 'upgraded', 'downgraded', 'cancel-scheduled', 'cancel-undone'])
|
|
153
|
-
expect(fake.observed.subscription[1].eventKey).toBe(`subscription:sub_1:upgraded:${TEAM}`)
|
|
154
|
-
expect(fake.observed.subscription[1].previous?.rank).toBe(10)
|
|
155
|
-
expect(fake.observed.subscription[1].current.rank).toBe(20)
|
|
156
|
-
expect(fake.observed.subscription[2].eventKey).toBe(`subscription:sub_1:downgraded:${PRO}`)
|
|
157
|
-
expect(fake.observed.subscription[4].eventKey).toBe(`subscription:sub_1:cancel-undone:${undo.id}`)
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
test('paused collection suspends, resume restores, trial end is announced, deletion cancels', async () => {
|
|
161
|
-
const fake = await makeFakeContext()
|
|
162
|
-
await subscribe(fake)
|
|
163
|
-
await send(fake, 'customer.subscription.updated', subscriptionOf({ pauseCollection: { behavior: 'void' } }))
|
|
164
|
-
expect(rows(fake)[0]).toEqual(expect.objectContaining({ status: SubscriptionStatus.Suspended, externalStatus: 'active' }))
|
|
165
|
-
expect(rows(fake)[0].pausedAt).toBeInstanceOf(Date)
|
|
166
|
-
await send(fake, 'customer.subscription.resumed', subscriptionOf())
|
|
167
|
-
expect(rows(fake)[0].pausedAt).toBeUndefined()
|
|
168
|
-
|
|
169
|
-
const trialEnd = future(3)
|
|
170
|
-
await send(fake, 'customer.subscription.trial_will_end', subscriptionOf({ status: 'trialing', trialEnd }))
|
|
171
|
-
await send(fake, 'customer.subscription.deleted', subscriptionOf({ status: 'canceled' }))
|
|
172
|
-
|
|
173
|
-
expect(changes(fake)).toEqual(['created', 'paused', 'resumed', 'trial-ending', 'canceled'])
|
|
174
|
-
expect(fake.observed.subscription[3].eventKey)
|
|
175
|
-
.toBe(`subscription:sub_1:trial-ending:${new Date(epoch(trialEnd) * 1000).toISOString()}`)
|
|
176
|
-
expect(rows(fake)[0].status).toBe(SubscriptionStatus.Canceled)
|
|
177
|
-
expect(rows(fake)[0].endedAt).toBeInstanceOf(Date)
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
test('a throwing observer leaves the change to be told again; an older payload is not applied', async () => {
|
|
181
|
-
const fake = await makeFakeContext()
|
|
182
|
-
await subscribe(fake)
|
|
183
|
-
fake.observed.failSubscription = 1
|
|
184
|
-
const upgrade = eventOf('customer.subscription.updated', subscriptionOf({ planSku: TEAM }))
|
|
185
|
-
await expect(createEventHandler(fake.ctx, fake.stripe).process(upgrade)).rejects.toThrow('observer unavailable')
|
|
186
|
-
expect(rows(fake)[0].planSku).toBe(TEAM)
|
|
187
|
-
await createEventHandler(fake.ctx, fake.stripe).process(upgrade)
|
|
188
|
-
expect(changes(fake)).toEqual(['created', 'upgraded'])
|
|
189
|
-
|
|
190
|
-
await send(fake, 'customer.subscription.updated', subscriptionOf({ planSku: PRO }), { created: epoch(new Date()) - 3600 })
|
|
191
|
-
expect(rows(fake)[0].planSku).toBe(TEAM)
|
|
192
|
-
})
|
|
193
|
-
})
|
|
194
|
-
|
|
195
|
-
describe('@owlmeans/server-payment — invoice events', () => {
|
|
196
|
-
const invoice = (overrides: Record<string, unknown> = {}) => ({
|
|
197
|
-
id: 'in_2', object: 'invoice', subscription: 'sub_1', customer: 'cus_1', billing_reason: 'subscription_cycle',
|
|
198
|
-
attempt_count: 1, next_payment_attempt: epoch(future(2)), ...overrides,
|
|
199
|
-
})
|
|
200
|
-
|
|
201
|
-
test('a paid cycle invoice renews once per invoice; any other paid invoice only moves the latest invoice', async () => {
|
|
202
|
-
const fake = await makeFakeContext()
|
|
203
|
-
await subscribe(fake)
|
|
204
|
-
await send(fake, 'invoice.paid', invoice())
|
|
205
|
-
await send(fake, 'invoice.paid', invoice())
|
|
206
|
-
expect(changes(fake)).toEqual(['created', 'renewed'])
|
|
207
|
-
expect(fake.observed.subscription[1]).toEqual(expect.objectContaining({
|
|
208
|
-
eventKey: 'subscription:sub_1:renewed:in_2', invoiceId: 'in_2',
|
|
209
|
-
}))
|
|
210
|
-
|
|
211
|
-
await send(fake, 'invoice.paid', invoice({ id: 'in_3', billing_reason: 'manual' }))
|
|
212
|
-
expect(rows(fake)[0].latestInvoiceId).toBe('in_3')
|
|
213
|
-
expect(changes(fake)).toEqual(['created', 'renewed'])
|
|
214
|
-
})
|
|
215
|
-
|
|
216
|
-
test('a failed payment re-reads the subscription (past-due) and reports the attempt', async () => {
|
|
217
|
-
const fake = await makeFakeContext()
|
|
218
|
-
await subscribe(fake)
|
|
219
|
-
fake.state.subscriptions.sub_1 = subscriptionOf({ status: 'past_due' }) as never
|
|
220
|
-
await send(fake, 'invoice.payment_failed', invoice({ attempt_count: 2 }))
|
|
221
|
-
await send(fake, 'invoice.payment_action_required', invoice({ attempt_count: 2, next_payment_attempt: null }))
|
|
222
|
-
|
|
223
|
-
expect(changes(fake)).toEqual(['created', 'past-due'])
|
|
224
|
-
expect(fake.observed.paymentFailed).toEqual([
|
|
225
|
-
expect.objectContaining({
|
|
226
|
-
entityId: 'entity-1', kind: 'invoice', invoiceId: 'in_2', subscriptionId: 'sub_1', attempt: 2,
|
|
227
|
-
actionRequired: false, eventKey: 'payment-failed:in_2:2',
|
|
228
|
-
}),
|
|
229
|
-
expect.objectContaining({ actionRequired: true, eventKey: 'payment-failed:in_2:2' }),
|
|
230
|
-
])
|
|
231
|
-
expect(fake.observed.paymentFailed[0].nextAttemptAt).toBeInstanceOf(Date)
|
|
232
|
-
})
|
|
233
|
-
|
|
234
|
-
test('uncollectible suspends, voided refreshes the latest invoice, upcoming does nothing', async () => {
|
|
235
|
-
const fake = await makeFakeContext()
|
|
236
|
-
await subscribe(fake, { latestInvoice: 'in_1' })
|
|
237
|
-
fake.state.calls.length = 0
|
|
238
|
-
await send(fake, 'invoice.upcoming', { object: 'invoice', subscription: 'sub_1' })
|
|
239
|
-
expect(fake.state.calls).toEqual([])
|
|
240
|
-
|
|
241
|
-
fake.state.subscriptions.sub_1 = subscriptionOf({ status: 'unpaid', latestInvoice: 'in_4' }) as never
|
|
242
|
-
await send(fake, 'invoice.marked_uncollectible', invoice())
|
|
243
|
-
expect(changes(fake)).toEqual(['created', 'suspended'])
|
|
244
|
-
|
|
245
|
-
fake.state.subscriptions.sub_1 = subscriptionOf({ status: 'unpaid', latestInvoice: 'in_5' }) as never
|
|
246
|
-
await send(fake, 'invoice.voided', invoice({ id: 'in_4' }))
|
|
247
|
-
expect(rows(fake)[0].latestInvoiceId).toBe('in_5')
|
|
248
|
-
expect(changes(fake)).toEqual(['created', 'suspended'])
|
|
249
|
-
})
|
|
250
|
-
})
|
|
251
|
-
|
|
252
|
-
describe('@owlmeans/server-payment — refund and dispute events', () => {
|
|
253
|
-
const fulfilled = async () => {
|
|
254
|
-
const fake = await makeFakeContext({
|
|
255
|
-
stripe: {
|
|
256
|
-
charges: { ch_1: { id: 'ch_1', amount: 1225, amount_refunded: 500, payment_intent: 'pi_1', invoice: null } },
|
|
257
|
-
},
|
|
258
|
-
})
|
|
259
|
-
await send(fake, 'checkout.session.completed', {
|
|
260
|
-
id: 'cs_1', mode: 'payment', payment_status: 'paid', customer: 'cus_1', currency: 'usd', amount_subtotal: 1021,
|
|
261
|
-
payment_intent: 'pi_1', invoice: 'in_9',
|
|
262
|
-
metadata: {
|
|
263
|
-
pricingMode: 'amount', amountMinor: '1000', chargeAmountMinor: '1021', currency: 'usd',
|
|
264
|
-
entityId: 'entity-1', service: 'app', productSku: CREDITS_PRODUCT, planSku: 'app-credit-unit',
|
|
265
|
-
},
|
|
266
|
-
})
|
|
267
|
-
return fake
|
|
268
|
-
}
|
|
269
|
-
const refund = (overrides: Record<string, unknown> = {}) => ({
|
|
270
|
-
id: 're_1', object: 'refund', amount: 500, currency: 'usd', status: 'succeeded', payment_intent: 'pi_1', charge: 'ch_1',
|
|
271
|
-
...overrides,
|
|
272
|
-
})
|
|
273
|
-
|
|
274
|
-
test('a succeeded refund of a fulfillment is recorded and observed with the checkout amounts', async () => {
|
|
275
|
-
const fake = await fulfilled()
|
|
276
|
-
await send(fake, 'refund.updated', refund({ status: 'pending' }))
|
|
277
|
-
await send(fake, 'refund.failed', refund({ status: 'failed' }))
|
|
278
|
-
expect(fake.observed.refund).toHaveLength(0)
|
|
279
|
-
|
|
280
|
-
await send(fake, 'refund.created', refund())
|
|
281
|
-
expect(fake.stores['payment-fulfillment'].rows[0]).toEqual(expect.objectContaining({ refundedMinor: 500, paymentIntentId: 'pi_1' }))
|
|
282
|
-
expect(fake.observed.refund).toEqual([expect.objectContaining({
|
|
283
|
-
entityId: 'entity-1', target: 'fulfillment', externalId: 'cs_1', refundId: 're_1', amountMinor: 500,
|
|
284
|
-
refundedTotalMinor: 500, paidMinor: 1225, partial: true, currency: 'usd', eventKey: 'refund:re_1',
|
|
285
|
-
netAmountMinor: 1000, chargeAmountMinor: 1021, productSku: CREDITS_PRODUCT,
|
|
286
|
-
})])
|
|
287
|
-
})
|
|
288
|
-
|
|
289
|
-
test('charge.refunded resolves a subscription invoice through the charge; an unknown payment is ignored', async () => {
|
|
290
|
-
const fake = await makeFakeContext({
|
|
291
|
-
stripe: {
|
|
292
|
-
charges: { ch_2: { id: 'ch_2', amount: 2000, amount_refunded: 2000, payment_intent: 'pi_2', invoice: 'in_2' } },
|
|
293
|
-
refunds: [{ id: 're_2', amount: 2000, currency: 'usd', status: 'succeeded', charge: 'ch_2', payment_intent: 'pi_2' }],
|
|
294
|
-
},
|
|
295
|
-
})
|
|
296
|
-
await subscribe(fake, { latestInvoice: 'in_2' })
|
|
297
|
-
await send(fake, 'charge.refunded', fake.state.charges.ch_2)
|
|
298
|
-
expect(fake.observed.refund).toEqual([expect.objectContaining({
|
|
299
|
-
target: 'subscription', externalId: 'sub_1', invoiceId: 'in_2', planSku: PRO, partial: false, eventKey: 'refund:re_2',
|
|
300
|
-
})])
|
|
301
|
-
|
|
302
|
-
await send(fake, 'refund.created', refund({ id: 're_x', payment_intent: 'pi_x', charge: null }))
|
|
303
|
-
expect(fake.observed.refund).toHaveLength(1)
|
|
304
|
-
})
|
|
305
|
-
|
|
306
|
-
test('every dispute phase marks the target and is observed once per phase', async () => {
|
|
307
|
-
const fake = await fulfilled()
|
|
308
|
-
const dispute = { id: 'dp_1', object: 'dispute', amount: 1225, currency: 'usd', charge: 'ch_1', payment_intent: null }
|
|
309
|
-
await send(fake, 'charge.dispute.created', { ...dispute, status: 'needs_response' })
|
|
310
|
-
await send(fake, 'charge.dispute.funds_withdrawn', { ...dispute, status: 'under_review' })
|
|
311
|
-
await send(fake, 'charge.dispute.funds_reinstated', { ...dispute, status: 'won' })
|
|
312
|
-
await send(fake, 'charge.dispute.closed', { ...dispute, status: 'won' })
|
|
313
|
-
|
|
314
|
-
expect(fake.observed.dispute.map(event => event.eventKey)).toEqual([
|
|
315
|
-
'dispute:dp_1:opened', 'dispute:dp_1:funds-withdrawn', 'dispute:dp_1:funds-reinstated', 'dispute:dp_1:closed',
|
|
316
|
-
])
|
|
317
|
-
expect(fake.observed.dispute[0]).toEqual(expect.objectContaining({
|
|
318
|
-
paidMinor: 1225,
|
|
319
|
-
target: 'fulfillment', externalId: 'cs_1', amountMinor: 1225, netAmountMinor: 1000,
|
|
320
|
-
}))
|
|
321
|
-
expect(fake.stores['payment-fulfillment'].rows[0]).toEqual(expect.objectContaining({ disputeStatus: 'won', chargeId: 'ch_1' }))
|
|
322
|
-
expect(fake.stores['payment-fulfillment'].rows[0].disputedAt).toBeInstanceOf(Date)
|
|
323
|
-
})
|
|
324
|
-
|
|
325
|
-
test('a discounted dispute resolved by payment intent carries the actual charge including tax', async () => {
|
|
326
|
-
const fake = await fulfilled()
|
|
327
|
-
fake.state.charges.ch_1.amount = 600
|
|
328
|
-
await send(fake, 'charge.dispute.funds_withdrawn', {
|
|
329
|
-
id: 'dp_discount', amount: 600, currency: 'usd', charge: 'ch_1', payment_intent: 'pi_1', status: 'needs_response',
|
|
330
|
-
})
|
|
331
|
-
expect(fake.observed.dispute[0]).toEqual(expect.objectContaining({ paidMinor: 600, netAmountMinor: 1000, chargeAmountMinor: 1021 }))
|
|
332
|
-
})
|
|
333
|
-
})
|
|
334
|
-
|
|
335
|
-
describe('@owlmeans/server-payment — resync and internal grants', () => {
|
|
336
|
-
test('resync applies the paygate state, cancels a vanished subscription and skips terminal rows', async () => {
|
|
337
|
-
const fake = await makeFakeContext()
|
|
338
|
-
await subscribe(fake)
|
|
339
|
-
await subscribe(fake, { id: 'sub_2', planSku: TEAM, entityId: 'entity-2' })
|
|
340
|
-
fake.state.subscriptions.sub_1 = subscriptionOf({ cancelAtPeriodEnd: true }) as never
|
|
341
|
-
delete fake.state.subscriptions.sub_2
|
|
342
|
-
|
|
343
|
-
expect(await stripeSubscriptionsOf(fake.ctx).resyncStripeSubscriptions(fake.stripe)).toEqual({ scanned: 2, updated: 2 })
|
|
344
|
-
expect(changes(fake)).toEqual(['created', 'created', 'cancel-scheduled', 'canceled'])
|
|
345
|
-
expect(rows(fake).find(row => row.externalId === 'sub_2')?.status).toBe(SubscriptionStatus.Canceled)
|
|
346
|
-
|
|
347
|
-
expect(await stripeSubscriptionsOf(fake.ctx).resyncStripeSubscriptions(fake.stripe)).toEqual({ scanned: 1, updated: 0 })
|
|
348
|
-
expect(await stripeSubscriptionsOf(fake.ctx).resyncStripeSubscription(fake.stripe, { entityId: 'entity-2' })).toBe(0)
|
|
349
|
-
})
|
|
350
|
-
|
|
351
|
-
test('an internal grant is created once, keeps its creation date and needs force for a paid plan', async () => {
|
|
352
|
-
const fake = await makeFakeContext()
|
|
353
|
-
const first = await paymentAccessOf(fake.ctx).gateway().grantInternalPlan(fake.ctx, 'entity-1', FREE)
|
|
354
|
-
const again = await paymentAccessOf(fake.ctx).gateway().grantInternalPlan(fake.ctx, 'entity-1', FREE)
|
|
355
|
-
expect(first.externalId).toBe('free:entity-1')
|
|
356
|
-
expect(again.createdAt.getTime()).toBe(first.createdAt.getTime())
|
|
357
|
-
expect(fake.observed.subscription).toEqual([expect.objectContaining({
|
|
358
|
-
change: 'created', eventKey: `subscription:free:entity-1:created:${first.createdAt.toISOString()}`,
|
|
359
|
-
})])
|
|
360
|
-
|
|
361
|
-
await expect(paymentAccessOf(fake.ctx).gateway().grantInternalPlan(fake.ctx, 'entity-1', PRO)).rejects.toBeInstanceOf(ProductError)
|
|
362
|
-
const comp = await paymentAccessOf(fake.ctx).gateway().grantInternalPlan(fake.ctx, 'entity-1', PRO, { force: true, periodEnd: future(365) })
|
|
363
|
-
expect(comp).toEqual(expect.objectContaining({ externalId: `internal:${PRO}:entity-1`, paygate: 'internal', rank: 10 }))
|
|
364
|
-
expect(fake.observed.subscription[1].current.capabilities?.some(set => set.permissions.whitelabel === true)).toBe(true)
|
|
365
|
-
expect(CAP_WHITELABEL).toBe('feature:whitelabel')
|
|
366
|
-
})
|
|
367
|
-
})
|