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