@owlmeans/server-payment 0.1.22-rc.0 → 0.2.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -54
- package/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/server-payment/SKILL.md +272 -330
- package/agent-meta/skills/server-payment/parts/reference.md +3 -3
- package/build/access/types.d.ts +7 -16
- package/build/access/types.d.ts.map +1 -1
- package/build/access.d.ts.map +1 -1
- package/build/access.js +6 -8
- package/build/access.js.map +1 -1
- package/build/actions/resync.d.ts +3 -3
- package/build/actions/resync.js +5 -5
- package/build/actions/resync.js.map +1 -1
- package/build/actions/webhook.d.ts +4 -1
- package/build/actions/webhook.d.ts.map +1 -1
- package/build/actions/webhook.js +8 -4
- package/build/actions/webhook.js.map +1 -1
- package/build/bootstrap/types.d.ts +13 -7
- package/build/bootstrap/types.d.ts.map +1 -1
- package/build/bootstrap.d.ts +4 -4
- package/build/bootstrap.d.ts.map +1 -1
- package/build/bootstrap.js +19 -10
- package/build/bootstrap.js.map +1 -1
- package/build/catalogue/types.d.ts +3 -3
- package/build/catalogue/types.d.ts.map +1 -1
- package/build/catalogue.d.ts.map +1 -1
- package/build/catalogue.js +4 -8
- package/build/catalogue.js.map +1 -1
- package/build/checkout/types.d.ts +21 -0
- package/build/checkout/types.d.ts.map +1 -0
- package/build/{plugins/stripe → checkout}/types.js.map +1 -1
- package/build/checkout.d.ts +6 -0
- package/build/checkout.d.ts.map +1 -0
- package/build/checkout.js +374 -0
- package/build/checkout.js.map +1 -0
- package/build/config.d.ts +10 -16
- package/build/config.d.ts.map +1 -1
- package/build/config.js +18 -59
- package/build/config.js.map +1 -1
- package/build/consts.d.ts +3 -49
- package/build/consts.d.ts.map +1 -1
- package/build/consts.js +3 -61
- package/build/consts.js.map +1 -1
- package/build/consumer/cancellation/types.d.ts +6 -4
- package/build/consumer/cancellation/types.d.ts.map +1 -1
- package/build/consumer/cancellation.d.ts.map +1 -1
- package/build/consumer/cancellation.js +11 -5
- package/build/consumer/cancellation.js.map +1 -1
- package/build/consumer/capture/types.d.ts +8 -12
- package/build/consumer/capture/types.d.ts.map +1 -1
- package/build/consumer/capture.d.ts.map +1 -1
- package/build/consumer/capture.js +48 -53
- package/build/consumer/capture.js.map +1 -1
- package/build/consumer/index.d.ts +4 -0
- package/build/consumer/index.d.ts.map +1 -1
- package/build/consumer/index.js +2 -0
- package/build/consumer/index.js.map +1 -1
- package/build/consumer/reconcile.d.ts.map +1 -1
- package/build/consumer/reconcile.js +82 -72
- package/build/consumer/reconcile.js.map +1 -1
- package/build/consumer/records/types.d.ts +4 -4
- package/build/consumer/records/types.d.ts.map +1 -1
- package/build/consumer/records.d.ts.map +1 -1
- package/build/consumer/records.js +16 -13
- package/build/consumer/records.js.map +1 -1
- package/build/consumer/service.d.ts +2 -2
- package/build/consumer/service.d.ts.map +1 -1
- package/build/consumer/service.js +28 -21
- package/build/consumer/service.js.map +1 -1
- package/build/consumer/types.d.ts +1 -4
- package/build/consumer/types.d.ts.map +1 -1
- package/build/consumer/types.local.d.ts +1 -9
- package/build/consumer/types.local.d.ts.map +1 -1
- package/build/consumer/withdrawal/types.d.ts +14 -12
- package/build/consumer/withdrawal/types.d.ts.map +1 -1
- package/build/consumer/withdrawal.d.ts.map +1 -1
- package/build/consumer/withdrawal.js +55 -71
- package/build/consumer/withdrawal.js.map +1 -1
- package/build/index.d.ts +12 -6
- package/build/index.d.ts.map +1 -1
- package/build/index.js +7 -4
- package/build/index.js.map +1 -1
- package/build/paygate/providers.d.ts +13 -0
- package/build/paygate/providers.d.ts.map +1 -0
- package/build/paygate/providers.js +38 -0
- package/build/paygate/providers.js.map +1 -0
- package/build/paygate/registry.d.ts +6 -0
- package/build/paygate/registry.d.ts.map +1 -0
- package/build/paygate/registry.js +37 -0
- package/build/paygate/registry.js.map +1 -0
- package/build/paygate/types.d.ts +577 -0
- package/build/paygate/types.d.ts.map +1 -0
- package/build/{plugins/session → paygate}/types.js.map +1 -1
- package/build/plan/types.d.ts +6 -1
- package/build/plan/types.d.ts.map +1 -1
- package/build/plan.d.ts.map +1 -1
- package/build/plan.js +16 -2
- package/build/plan.js.map +1 -1
- package/build/plugins/checkout-policy/types.d.ts +5 -3
- package/build/plugins/checkout-policy/types.d.ts.map +1 -1
- package/build/plugins/checkout-policy.d.ts.map +1 -1
- package/build/plugins/checkout-policy.js +5 -3
- package/build/plugins/checkout-policy.js.map +1 -1
- package/build/plugins/consts.local.d.ts +8 -10
- package/build/plugins/consts.local.d.ts.map +1 -1
- package/build/plugins/consts.local.js +2 -19
- package/build/plugins/consts.local.js.map +1 -1
- package/build/plugins/customer-lock/types.d.ts +2 -2
- package/build/plugins/customer-lock/types.d.ts.map +1 -1
- package/build/plugins/customer-lock.js +2 -2
- package/build/plugins/customer-lock.js.map +1 -1
- package/build/plugins/estimate/cache.js +1 -1
- package/build/plugins/estimate/cache.js.map +1 -1
- package/build/plugins/estimate/types.d.ts +10 -10
- package/build/plugins/estimate/types.d.ts.map +1 -1
- package/build/plugins/estimate.d.ts.map +1 -1
- package/build/plugins/estimate.js +48 -122
- package/build/plugins/estimate.js.map +1 -1
- package/build/plugins/events/types.d.ts +17 -10
- package/build/plugins/events/types.d.ts.map +1 -1
- package/build/plugins/events.d.ts +6 -5
- package/build/plugins/events.d.ts.map +1 -1
- package/build/plugins/events.js +162 -173
- package/build/plugins/events.js.map +1 -1
- package/build/plugins/fx/types.d.ts +10 -21
- package/build/plugins/fx/types.d.ts.map +1 -1
- package/build/plugins/fx.d.ts +1 -1
- package/build/plugins/fx.d.ts.map +1 -1
- package/build/plugins/fx.js +17 -54
- package/build/plugins/fx.js.map +1 -1
- package/build/plugins/portal/types.d.ts +8 -37
- package/build/plugins/portal/types.d.ts.map +1 -1
- package/build/plugins/portal.d.ts.map +1 -1
- package/build/plugins/portal.js +38 -208
- package/build/plugins/portal.js.map +1 -1
- package/build/plugins/refunds/types.d.ts +11 -11
- package/build/plugins/refunds/types.d.ts.map +1 -1
- package/build/plugins/refunds.d.ts.map +1 -1
- package/build/plugins/refunds.js +14 -39
- package/build/plugins/refunds.js.map +1 -1
- package/build/plugins/snapshot/types.d.ts +11 -0
- package/build/plugins/snapshot/types.d.ts.map +1 -0
- package/build/plugins/snapshot/types.js +2 -0
- package/build/plugins/snapshot/types.js.map +1 -0
- package/build/plugins/snapshot.d.ts +4 -0
- package/build/plugins/snapshot.d.ts.map +1 -0
- package/build/plugins/snapshot.js +19 -0
- package/build/plugins/snapshot.js.map +1 -0
- package/build/plugins/subscriptions/types.d.ts +17 -17
- package/build/plugins/subscriptions/types.d.ts.map +1 -1
- package/build/plugins/subscriptions.d.ts +4 -4
- package/build/plugins/subscriptions.d.ts.map +1 -1
- package/build/plugins/subscriptions.js +69 -77
- package/build/plugins/subscriptions.js.map +1 -1
- package/build/plugins/types.local.d.ts +9 -43
- package/build/plugins/types.local.d.ts.map +1 -1
- package/build/plugins/webhook-manager/types.d.ts +15 -43
- package/build/plugins/webhook-manager/types.d.ts.map +1 -1
- package/build/plugins/webhook-manager.d.ts +3 -3
- package/build/plugins/webhook-manager.d.ts.map +1 -1
- package/build/plugins/webhook-manager.js +21 -199
- package/build/plugins/webhook-manager.js.map +1 -1
- package/build/protocol.d.ts +1 -1
- package/build/protocol.d.ts.map +1 -1
- package/build/protocol.js +1 -1
- package/build/protocol.js.map +1 -1
- package/build/service.d.ts +11 -11
- package/build/service.d.ts.map +1 -1
- package/build/service.js +59 -23
- package/build/service.js.map +1 -1
- package/build/sync/types.d.ts +17 -13
- package/build/sync/types.d.ts.map +1 -1
- package/build/sync.d.ts.map +1 -1
- package/build/sync.js +22 -122
- package/build/sync.js.map +1 -1
- package/build/types.d.ts +46 -101
- package/build/types.d.ts.map +1 -1
- package/build/utils/types.d.ts +0 -8
- package/build/utils/types.d.ts.map +1 -1
- package/build/utils.d.ts +1 -4
- package/build/utils.d.ts.map +1 -1
- package/build/utils.js +1 -15
- package/build/utils.js.map +1 -1
- package/package.json +20 -22
- package/src/access/types.ts +7 -16
- package/src/access.ts +7 -15
- package/src/actions/resync.ts +5 -5
- package/src/actions/webhook.ts +8 -4
- package/src/bootstrap/types.ts +13 -7
- package/src/bootstrap.ts +23 -13
- package/src/catalogue/types.ts +3 -3
- package/src/catalogue.ts +4 -8
- package/src/checkout/types.ts +21 -0
- package/src/checkout.ts +414 -0
- package/src/config.ts +19 -66
- package/src/consts.ts +3 -69
- package/src/consumer/cancellation/types.ts +6 -4
- package/src/consumer/cancellation.ts +13 -7
- package/src/consumer/capture/types.ts +8 -12
- package/src/consumer/capture.ts +50 -59
- package/src/consumer/index.ts +4 -0
- package/src/consumer/reconcile.ts +69 -68
- package/src/consumer/records/types.ts +4 -4
- package/src/consumer/records.ts +19 -14
- package/src/consumer/service.ts +35 -22
- package/src/consumer/types.local.ts +1 -8
- package/src/consumer/types.ts +1 -4
- package/src/consumer/withdrawal/types.ts +14 -12
- package/src/consumer/withdrawal.ts +61 -77
- package/src/index.ts +12 -6
- package/src/paygate/providers.ts +48 -0
- package/src/paygate/registry.ts +53 -0
- package/src/paygate/types.ts +640 -0
- package/src/plan/types.ts +6 -1
- package/src/plan.ts +21 -3
- package/src/plugins/checkout-policy/types.ts +5 -3
- package/src/plugins/checkout-policy.ts +6 -5
- package/src/plugins/consts.local.ts +2 -26
- package/src/plugins/customer-lock/types.ts +2 -2
- package/src/plugins/customer-lock.ts +2 -2
- package/src/plugins/estimate/cache.ts +1 -1
- package/src/plugins/estimate/types.ts +10 -10
- package/src/plugins/estimate.ts +52 -128
- package/src/plugins/events/types.ts +17 -9
- package/src/plugins/events.ts +172 -190
- package/src/plugins/fx/types.ts +10 -23
- package/src/plugins/fx.ts +28 -64
- package/src/plugins/portal/types.ts +8 -38
- package/src/plugins/portal.ts +48 -229
- package/src/plugins/refunds/types.ts +11 -11
- package/src/plugins/refunds.ts +16 -40
- package/src/plugins/snapshot/types.ts +11 -0
- package/src/plugins/snapshot.ts +22 -0
- package/src/plugins/subscriptions/types.ts +17 -17
- package/src/plugins/subscriptions.ts +81 -81
- package/src/plugins/types.local.ts +9 -33
- package/src/plugins/webhook-manager/types.ts +15 -43
- package/src/plugins/webhook-manager.ts +24 -220
- package/src/protocol.ts +1 -1
- package/src/service.ts +66 -30
- package/src/sync/types.ts +17 -13
- package/src/sync.ts +25 -143
- package/src/types.ts +47 -97
- package/src/utils/types.ts +0 -9
- package/src/utils.ts +2 -20
- package/tests/capabilities.spec.ts +219 -0
- package/tests/entitlement.spec.ts +4 -4
- package/tests/gate-handlers.spec.ts +6 -6
- package/tests/gate.spec.ts +2 -2
- package/tests/grants.spec.ts +2 -2
- package/tests/internal-plan.spec.ts +2 -2
- package/tests/limit-gate.spec.ts +2 -2
- package/tests/paygates.spec.ts +132 -0
- package/tests/protocol.spec.ts +4 -5
- package/tests/usage.spec.ts +2 -2
- package/tests/webhook.spec.ts +137 -0
- package/build/plugins/session/types.d.ts +0 -34
- package/build/plugins/session/types.d.ts.map +0 -1
- package/build/plugins/session.d.ts +0 -4
- package/build/plugins/session.d.ts.map +0 -1
- package/build/plugins/session.js +0 -71
- package/build/plugins/session.js.map +0 -1
- package/build/plugins/status.d.ts +0 -8
- package/build/plugins/status.d.ts.map +0 -1
- package/build/plugins/status.js +0 -21
- package/build/plugins/status.js.map +0 -1
- package/build/plugins/stripe/types.d.ts +0 -20
- package/build/plugins/stripe/types.d.ts.map +0 -1
- package/build/plugins/stripe.d.ts +0 -6
- package/build/plugins/stripe.d.ts.map +0 -1
- package/build/plugins/stripe.js +0 -518
- package/build/plugins/stripe.js.map +0 -1
- package/src/plugins/session/types.ts +0 -30
- package/src/plugins/session.ts +0 -86
- package/src/plugins/status.ts +0 -22
- package/src/plugins/stripe/types.ts +0 -20
- package/src/plugins/stripe.ts +0 -571
- package/tests/checkout-consumer.spec.ts +0 -366
- package/tests/checkout-locks.spec.ts +0 -161
- package/tests/checkout-plugins.spec.ts +0 -164
- package/tests/checkout.spec.ts +0 -313
- package/tests/consumer-events.spec.ts +0 -215
- package/tests/consumer-fixtures.ts +0 -141
- package/tests/consumer-ops.spec.ts +0 -361
- package/tests/consumer-rights.integration.spec.ts +0 -147
- package/tests/consumer-rights.spec.ts +0 -606
- package/tests/context.ts +0 -110
- package/tests/estimate.spec.ts +0 -350
- package/tests/events.spec.ts +0 -367
- package/tests/fake-stripe.ts +0 -1004
- package/tests/gateway-options.spec.ts +0 -180
- package/tests/portal.spec.ts +0 -202
- package/tests/sync.spec.ts +0 -114
- package/tests/usage.integration.spec.ts +0 -165
- package/tests/webhook-manager.spec.ts +0 -149
- /package/build/{plugins/session → checkout}/types.js +0 -0
- /package/build/{plugins/stripe → paygate}/types.js +0 -0
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'bun:test'
|
|
2
|
-
import { PurchaseKind, withdrawalDeadlineHelper } from '@owlmeans/payment'
|
|
3
|
-
import {
|
|
4
|
-
buyTopUp, ENTITY, invoiceOf, makeRightsContext, paidSession, PRO, requestStart, send,
|
|
5
|
-
} from './consumer-fixtures.js'
|
|
6
|
-
import { subscriptionOf, type FakeContext } from './fake-stripe.js'
|
|
7
|
-
import { paymentAccessOf } from '../src/access.js'
|
|
8
|
-
import { captureOf } from '../src/consumer/capture.js'
|
|
9
|
-
|
|
10
|
-
const proSubscription = (startRequestId?: string, overrides: Record<string, unknown> = {}) => {
|
|
11
|
-
const subscription = subscriptionOf({ id: 'sub_pro', customer: 'cus_1', latestInvoice: 'in_sub' }) as unknown as Record<string, any>
|
|
12
|
-
subscription.currency = 'eur'
|
|
13
|
-
subscription.items.data[0].price = { id: 'price_pro', lookup_key: PRO, unit_amount: 1_800, currency: 'eur', tax_behavior: 'exclusive' }
|
|
14
|
-
subscription.items.data[0].quantity = 1
|
|
15
|
-
subscription.metadata = {
|
|
16
|
-
entityId: ENTITY, planSku: PRO, productSku: 'app-plans', service: 'app', country: 'DE', language: 'de',
|
|
17
|
-
...(startRequestId != null ? { startRequestId } : {}),
|
|
18
|
-
}
|
|
19
|
-
return { ...subscription, ...overrides }
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const subscriptionSession = (overrides: Record<string, unknown> = {}) => paidSession({
|
|
23
|
-
id: 'cs_sub', mode: 'subscription', subscription: 'sub_pro', invoice: 'in_sub', payment_intent: null,
|
|
24
|
-
amount_subtotal: 1_800, amount_total: 2_142, total_details: { amount_tax: 342 },
|
|
25
|
-
customer_details: { address: { country: 'DE' }, email: 'owner@shop.eu', name: 'Anna', tax_ids: [] },
|
|
26
|
-
metadata: { pricingMode: 'quantity', planSku: PRO, productSku: 'app-plans', country: 'DE', language: 'de' },
|
|
27
|
-
...overrides,
|
|
28
|
-
})
|
|
29
|
-
|
|
30
|
-
const withSubInvoice = (fake: FakeContext) => {
|
|
31
|
-
fake.state.invoices.in_sub = invoiceOf('in_sub', {
|
|
32
|
-
number: 'INV-SUB-1', subtotal: 1_800, tax: 342, total: 2_142, payment_intent: 'pi_sub',
|
|
33
|
-
lines: { object: 'list', data: [{ id: 'il_sub', amount: 1_800 }] }, customer_address: { country: 'DE' },
|
|
34
|
-
})
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
describe('webhook capture — one-time purchases', () => {
|
|
38
|
-
test('the purchase row exists before the credits are granted, with its window and evidence', async () => {
|
|
39
|
-
const fake = await makeRightsContext()
|
|
40
|
-
const seen: unknown[] = []
|
|
41
|
-
fake.observed.onTopUp = async () => { seen.push(await paymentAccessOf(fake.ctx).purchases().load({ sessionId: 'cs_pl' })) }
|
|
42
|
-
const purchase = await buyTopUp(fake)
|
|
43
|
-
|
|
44
|
-
expect(seen[0]).toEqual(expect.objectContaining({ purchaseId: 'stripe:cs_pl' }))
|
|
45
|
-
expect(purchase).toEqual(expect.objectContaining({
|
|
46
|
-
purchaseId: 'stripe:cs_pl', kind: PurchaseKind.TopUp, entityId: ENTITY, country: 'PL', region: 'eu', inScope: true,
|
|
47
|
-
language: 'pl', currency: 'eur', amountSubtotalMinor: 919, amountTaxMinor: 211, amountTotalMinor: 1_130,
|
|
48
|
-
netAmountMinor: 1_000, amountCurrency: 'usd', paymentIntentId: 'pi_pl', invoiceId: 'in_pl',
|
|
49
|
-
invoiceNumber: 'INV-0001', invoiceLineId: 'il_pl', termsAccepted: true, ipCountry: 'PL', email: 'buyer@shop.eu',
|
|
50
|
-
textVersion: 'terms-v1',
|
|
51
|
-
}))
|
|
52
|
-
expect(purchase.contractRef).toMatch(/^CR-\d{6}-[2-9A-HJKMNP-Z]{6}$/)
|
|
53
|
-
expect(purchase.deadline).toEqual(withdrawalDeadlineHelper.withdrawalDeadlineOf(new Date(purchase.purchasedAt), { marginDays: 5 }))
|
|
54
|
-
expect((await paymentAccessOf(fake.ctx).fulfillments().byExternalId('cs_pl', 'stripe'))).toEqual(expect.objectContaining({
|
|
55
|
-
country: 'PL', email: 'buyer@shop.eu', amountTotalMinor: 1_130, amountTaxMinor: 211, termsAccepted: true,
|
|
56
|
-
purchaseId: 'stripe:cs_pl',
|
|
57
|
-
}))
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
test('the first purchase locks the country; a later one elsewhere is only a lock-mismatch event', async () => {
|
|
61
|
-
const fake = await makeRightsContext()
|
|
62
|
-
await buyTopUp(fake)
|
|
63
|
-
expect(await paymentAccessOf(fake.ctx).billingProfiles().byEntity(ENTITY)).toEqual(expect.objectContaining({
|
|
64
|
-
country: 'PL', region: 'eu', currency: 'eur', language: 'pl', source: 'checkout', ipCountry: 'PL',
|
|
65
|
-
email: 'buyer@shop.eu', sessionId: 'cs_pl',
|
|
66
|
-
}))
|
|
67
|
-
const second = await buyTopUp(fake, {
|
|
68
|
-
id: 'cs_de', invoice: 'in_de', customer_details: { address: { country: 'DE' }, email: 'buyer@shop.eu' },
|
|
69
|
-
metadata: { ipCountry: 'AT' },
|
|
70
|
-
})
|
|
71
|
-
expect(second.country).toBe('DE')
|
|
72
|
-
expect((await paymentAccessOf(fake.ctx).billingProfiles().byEntity(ENTITY))?.country).toBe('PL')
|
|
73
|
-
const mismatch = (await paymentAccessOf(fake.ctx).consumerEvents().list({ action: 'lock-mismatch' })).items
|
|
74
|
-
expect(mismatch).toHaveLength(1)
|
|
75
|
-
expect(JSON.parse(mismatch[0].detail ?? '{}')).toEqual(expect.objectContaining({ locked: 'PL', observed: 'DE', sessionId: 'cs_de' }))
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
test('the confirmation is mailed once, in the billing language, with the withdrawal information and an archive copy', async () => {
|
|
79
|
-
const fake = await makeRightsContext()
|
|
80
|
-
await buyTopUp(fake, { customer_details: { address: { country: 'PL' }, email: 'buyer@shop.eu', name: 'Jan <b>K</b>' } })
|
|
81
|
-
await send(fake, 'checkout.session.completed', paidSession())
|
|
82
|
-
const mails = fake.mails.filter(mail => mail.subject.includes('CR-'))
|
|
83
|
-
expect(mails.map(mail => mail.to)).toEqual(['buyer@shop.eu', 'archive@example.com'])
|
|
84
|
-
const [mail] = mails
|
|
85
|
-
expect(mail.from).toBe('billing@example.com')
|
|
86
|
-
expect(mail.text).toContain('Example Trading Ltd, 1 Example Street, 00-001 Example, support@example.com')
|
|
87
|
-
expect(mail.text).toContain('https://app.example.com/legal/withdraw')
|
|
88
|
-
expect(mail.text).toContain('Odstąp od umowy tutaj')
|
|
89
|
-
expect(mail.html).toContain('Jan <b>K</b>')
|
|
90
|
-
expect(mail.html).not.toContain('<b>K</b>')
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
test('concurrent deliveries in several processes mail the confirmation once', async () => {
|
|
94
|
-
const fake = await makeRightsContext()
|
|
95
|
-
fake.state.invoices.in_pl = invoiceOf()
|
|
96
|
-
const session = paidSession() as never
|
|
97
|
-
// Not through the webhook handler, whose per-customer lock serializes one process: two processes race here.
|
|
98
|
-
await Promise.all([
|
|
99
|
-
captureOf(fake.ctx).capturePaymentPurchase(fake.stripe, session), captureOf(fake.ctx).capturePaymentPurchase(fake.stripe, session),
|
|
100
|
-
])
|
|
101
|
-
await send(fake, 'checkout.session.completed', paidSession())
|
|
102
|
-
expect(fake.mails.filter(mail => mail.subject.includes('CR-')).map(mail => mail.to)).toEqual(['buyer@shop.eu', 'archive@example.com'])
|
|
103
|
-
expect(await paymentAccessOf(fake.ctx).consumerEvents().count({ action: 'mail', step: 'purchase' })).toBe(1)
|
|
104
|
-
expect((await paymentAccessOf(fake.ctx).purchases().load({ sessionId: 'cs_pl' }))?.confirmationMailAt).toBeInstanceOf(Date)
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
test('a checkout that collected no terms records no acceptance', async () => {
|
|
108
|
-
const fake = await makeRightsContext()
|
|
109
|
-
const purchase = await buyTopUp(fake, { consent: null, metadata: { termsCollected: 'false' } })
|
|
110
|
-
expect(purchase.termsAccepted).toBeUndefined()
|
|
111
|
-
expect((await paymentAccessOf(fake.ctx).fulfillments().byExternalId('cs_pl', 'stripe'))?.termsAccepted).toBeUndefined()
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
test('a purchase outside the territories has no window and no confirmation', async () => {
|
|
115
|
-
const fake = await makeRightsContext()
|
|
116
|
-
const purchase = await buyTopUp(fake, {
|
|
117
|
-
id: 'cs_us', invoice: 'in_us', currency: 'usd', amount_subtotal: 1_021, amount_total: 1_021, total_details: { amount_tax: 0 },
|
|
118
|
-
customer_details: { address: { country: 'US' }, email: 'buyer@shop.us' },
|
|
119
|
-
metadata: { country: 'US', currency: 'usd', chargeAmountMinor: '1021', language: 'en' },
|
|
120
|
-
})
|
|
121
|
-
expect(purchase).toEqual(expect.objectContaining({ inScope: false, region: 'other', country: 'US' }))
|
|
122
|
-
expect(purchase.deadline).toBeUndefined()
|
|
123
|
-
expect(fake.mails).toHaveLength(0)
|
|
124
|
-
expect((await paymentAccessOf(fake.ctx).billingProfiles().byEntity(ENTITY))?.currency).toBe('usd')
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
test('customer webhooks store the paygate country and currency', async () => {
|
|
128
|
-
const fake = await makeRightsContext()
|
|
129
|
-
await send(fake, 'customer.updated', { id: 'cus_9', email: 'a@shop.eu', address: { country: 'fr' }, currency: 'EUR', metadata: { entityId: ENTITY } })
|
|
130
|
-
expect(await paymentAccessOf(fake.ctx).paygateCustomers().loadByPgId('cus_9', 'stripe')).toEqual(expect.objectContaining({ country: 'FR', currency: 'eur' }))
|
|
131
|
-
})
|
|
132
|
-
})
|
|
133
|
-
|
|
134
|
-
describe('webhook capture — subscriptions', () => {
|
|
135
|
-
test('the first commit writes the purchase before the created observers; the checkout refines it', async () => {
|
|
136
|
-
const fake = await makeRightsContext()
|
|
137
|
-
withSubInvoice(fake)
|
|
138
|
-
const startRequestId = await requestStart(fake)
|
|
139
|
-
const seen: unknown[] = []
|
|
140
|
-
fake.observed.onSubscription = async event => {
|
|
141
|
-
if (event.change === 'created') seen.push(await paymentAccessOf(fake.ctx).purchases().byPurchaseId('stripe:sub_pro'))
|
|
142
|
-
}
|
|
143
|
-
const subscription = proSubscription(startRequestId)
|
|
144
|
-
fake.state.subscriptions.sub_pro = subscription as never
|
|
145
|
-
await send(fake, 'customer.subscription.created', subscription)
|
|
146
|
-
|
|
147
|
-
expect(seen[0]).toEqual(expect.objectContaining({
|
|
148
|
-
purchaseId: 'stripe:sub_pro', kind: PurchaseKind.Subscription, subscriptionId: 'sub_pro', invoiceId: 'in_sub',
|
|
149
|
-
invoiceNumber: 'INV-SUB-1', invoiceLineId: 'il_sub', paymentIntentId: 'pi_sub', currency: 'eur',
|
|
150
|
-
amountSubtotalMinor: 1_800, amountTaxMinor: 342, amountTotalMinor: 2_142, startRequestId, consentId: startRequestId,
|
|
151
|
-
country: 'DE', inScope: true,
|
|
152
|
-
}))
|
|
153
|
-
const stored = seen[0] as { servicesStartedAt: Date, consentedAt: Date }
|
|
154
|
-
expect(stored.servicesStartedAt).toBeInstanceOf(Date)
|
|
155
|
-
expect(stored.consentedAt).toEqual(stored.servicesStartedAt)
|
|
156
|
-
|
|
157
|
-
await send(fake, 'checkout.session.completed', subscriptionSession({ customer_details: { address: { country: 'AT' }, email: 'owner@shop.eu', name: 'Anna' } }))
|
|
158
|
-
expect(await paymentAccessOf(fake.ctx).purchases().byPurchaseId('stripe:sub_pro')).toEqual(expect.objectContaining({
|
|
159
|
-
sessionId: 'cs_sub', country: 'AT', email: 'owner@shop.eu', termsAccepted: true, amountTotalMinor: 2_142,
|
|
160
|
-
}))
|
|
161
|
-
expect(await paymentAccessOf(fake.ctx).subscriptions().byExternalId('sub_pro', 'stripe')).toEqual(expect.objectContaining({
|
|
162
|
-
checkoutSessionId: 'cs_sub', purchaseId: 'stripe:sub_pro', firstInvoiceId: 'in_sub', currency: 'eur', country: 'AT',
|
|
163
|
-
email: 'owner@shop.eu', amountTotalMinor: 2_142, amountTaxMinor: 342, termsAccepted: true, startRequestId,
|
|
164
|
-
}))
|
|
165
|
-
expect((await paymentAccessOf(fake.ctx).billingProfiles().byEntity(ENTITY))?.country).toBe('AT')
|
|
166
|
-
const confirmation = fake.mails.find(mail => mail.to === 'owner@shop.eu' && mail.subject.includes('CR-'))
|
|
167
|
-
// The order confirmation is in the billing language; the start request is repeated verbatim, as shown (en).
|
|
168
|
-
expect(confirmation?.subject).toStartWith('Bestellbestätigung')
|
|
169
|
-
expect(confirmation?.text).toContain('I expressly request and agree that Example starts the Pro services — including the AI work')
|
|
170
|
-
expect(confirmation?.text).toContain('https://app.example.com/legal/cancel')
|
|
171
|
-
})
|
|
172
|
-
|
|
173
|
-
test('a completed subscription checkout that arrives first applies the subscription itself', async () => {
|
|
174
|
-
const fake = await makeRightsContext()
|
|
175
|
-
withSubInvoice(fake)
|
|
176
|
-
fake.state.subscriptions.sub_pro = proSubscription() as never
|
|
177
|
-
await send(fake, 'checkout.session.completed', subscriptionSession())
|
|
178
|
-
expect(fake.observed.subscription.map(event => event.change)).toEqual(['created'])
|
|
179
|
-
expect(await paymentAccessOf(fake.ctx).purchases().byPurchaseId('stripe:sub_pro')).toEqual(expect.objectContaining({ sessionId: 'cs_sub', country: 'DE' }))
|
|
180
|
-
// Renewals are not purchases.
|
|
181
|
-
await send(fake, 'invoice.paid', { id: 'in_renew', object: 'invoice', subscription: 'sub_pro', customer: 'cus_1', billing_reason: 'subscription_cycle' })
|
|
182
|
-
expect(fake.stores['payment-purchase'].rows).toHaveLength(1)
|
|
183
|
-
})
|
|
184
|
-
})
|
|
185
|
-
|
|
186
|
-
describe('webhook capture — refunds', () => {
|
|
187
|
-
const charge = { id: 'ch_pl', amount: 1_130, amount_refunded: 1_130, payment_intent: 'pi_pl', invoice: null }
|
|
188
|
-
|
|
189
|
-
test('a withdrawal refund carries its id to the refund observers; a whole refund closes the window', async () => {
|
|
190
|
-
const fake = await makeRightsContext({ stripe: { charges: { ch_pl: charge } } })
|
|
191
|
-
await buyTopUp(fake)
|
|
192
|
-
await send(fake, 'refund.created', {
|
|
193
|
-
id: 're_w', object: 'refund', amount: 1_130, currency: 'eur', status: 'succeeded', payment_intent: 'pi_pl',
|
|
194
|
-
charge: 'ch_pl', metadata: { owlmeans: 'payment', withdrawalId: 'decl_1', purchaseId: 'stripe:cs_pl' },
|
|
195
|
-
})
|
|
196
|
-
expect(fake.observed.refund[0]).toEqual(expect.objectContaining({
|
|
197
|
-
withdrawalId: 'decl_1', metadata: expect.objectContaining({ withdrawalId: 'decl_1' }), partial: false,
|
|
198
|
-
}))
|
|
199
|
-
const purchase = await paymentAccessOf(fake.ctx).purchases().byPurchaseId('stripe:cs_pl')
|
|
200
|
-
expect(purchase?.refundedMinor).toBe(1_130)
|
|
201
|
-
expect(purchase?.refundedAt).toBeInstanceOf(Date)
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
test('an ordinary partial refund carries no withdrawal id and keeps the window open', async () => {
|
|
205
|
-
const fake = await makeRightsContext({ stripe: { charges: { ch_pl: { ...charge, amount_refunded: 300 } } } })
|
|
206
|
-
await buyTopUp(fake)
|
|
207
|
-
await send(fake, 'refund.created', {
|
|
208
|
-
id: 're_p', object: 'refund', amount: 300, currency: 'eur', status: 'succeeded', payment_intent: 'pi_pl', charge: 'ch_pl',
|
|
209
|
-
})
|
|
210
|
-
expect(fake.observed.refund[0].withdrawalId).toBeUndefined()
|
|
211
|
-
const purchase = await paymentAccessOf(fake.ctx).purchases().byPurchaseId('stripe:cs_pl')
|
|
212
|
-
expect(purchase?.refundedMinor).toBe(300)
|
|
213
|
-
expect(purchase?.refundedAt).toBeUndefined()
|
|
214
|
-
})
|
|
215
|
-
})
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import { ConsentKind } from '@owlmeans/payment'
|
|
2
|
-
import { createEventHandler } from '../src/plugins/events.js'
|
|
3
|
-
import type {
|
|
4
|
-
ConsumerRightsDef, PricingDef, PurchaseRecord, UsageMeter, UsageQuery, UsageReading,
|
|
5
|
-
} from '../src/types.js'
|
|
6
|
-
import { CREDIT_UNIT, CREDITS_PRODUCT, makeFakeContext, PLANS_PRODUCT, PRO } from './fake-stripe.js'
|
|
7
|
-
import type { FakeContext, FakeContextOptions } from './fake-stripe.js'
|
|
8
|
-
import { paymentAccessOf } from '../src/access.js'
|
|
9
|
-
|
|
10
|
-
type Rec = Record<string, any>
|
|
11
|
-
|
|
12
|
-
export const ENTITY = 'entity-1'
|
|
13
|
-
export const TEXT_VERSION = 'terms-v1'
|
|
14
|
-
export const epoch = (date: Date): number => Math.floor(date.getTime() / 1000)
|
|
15
|
-
|
|
16
|
-
export const LINKS = {
|
|
17
|
-
en: {
|
|
18
|
-
billingTerms: 'https://legal.example.com/en/billing',
|
|
19
|
-
withdrawalInformation: 'https://legal.example.com/en/withdrawal',
|
|
20
|
-
withdrawalForm: 'https://legal.example.com/en/withdrawal-form',
|
|
21
|
-
withdrawalFunction: 'https://app.example.com/legal/withdraw',
|
|
22
|
-
cancellation: 'https://app.example.com/legal/cancel',
|
|
23
|
-
},
|
|
24
|
-
de: { billingTerms: 'https://legal.example.com/de/billing', withdrawalInformation: 'https://legal.example.com/de/withdrawal' },
|
|
25
|
-
pl: { billingTerms: 'https://legal.example.com/pl/billing', withdrawalInformation: 'https://legal.example.com/pl/withdrawal' },
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const ALL_ON = {
|
|
29
|
-
countryLock: true, checkoutTerms: false, performanceConsent: true, subscriptionStart: true, withdrawal: true,
|
|
30
|
-
automaticRefunds: true, cancellation: true, purchaseConfirmation: true,
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export const TRADER = {
|
|
34
|
-
name: 'Example', legalName: 'Example Trading Ltd', address: '1 Example Street, 00-001 Example',
|
|
35
|
-
email: 'support@example.com',
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export const rightsOf = (patch: Partial<ConsumerRightsDef> = {}): ConsumerRightsDef => ({
|
|
39
|
-
textVersion: TEXT_VERSION,
|
|
40
|
-
links: LINKS,
|
|
41
|
-
currencies: { eu: 'eur', other: 'usd' },
|
|
42
|
-
mechanisms: ALL_ON,
|
|
43
|
-
trader: TRADER,
|
|
44
|
-
mail: { from: 'billing@example.com', bcc: ['archive@example.com'] },
|
|
45
|
-
...patch,
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
/** The pricing a EUR-settling account with USD catalogue prices declares. */
|
|
49
|
-
export const EUR_SETTLEMENT: PricingDef = {
|
|
50
|
-
tax: { automatic: true, collectTaxId: true, estimate: true },
|
|
51
|
-
currency: { adaptive: true, estimate: false },
|
|
52
|
-
stripe: { settlementCurrency: 'eur' },
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/** 1 USD = 0.9 EUR at the reference rate. */
|
|
56
|
-
export const FX = { usd: { exchangeRate: 0.88, referenceRate: 0.9 } }
|
|
57
|
-
|
|
58
|
-
export const makeRightsContext = async (opts: FakeContextOptions = {}): Promise<FakeContext> =>
|
|
59
|
-
await makeFakeContext({
|
|
60
|
-
pricing: EUR_SETTLEMENT,
|
|
61
|
-
consumerRights: rightsOf(),
|
|
62
|
-
...opts,
|
|
63
|
-
stripe: { fxRates: FX, ...opts.stripe },
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
export interface RecordingMeter extends UsageMeter {
|
|
67
|
-
queries: UsageQuery[]
|
|
68
|
-
reading: Partial<UsageReading> & { granted: number }
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/** A usage meter answering one fixed reading (mutable through `reading`). */
|
|
72
|
-
export const fixedMeter = (reading: Partial<UsageReading> & { granted: number }): RecordingMeter => {
|
|
73
|
-
const meter: RecordingMeter = {
|
|
74
|
-
queries: [],
|
|
75
|
-
reading,
|
|
76
|
-
used: async (_ctx, query) => {
|
|
77
|
-
meter.queries.push(query)
|
|
78
|
-
return { used: 0, usedAfter: 0, ...meter.reading }
|
|
79
|
-
},
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return meter
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export const send = async (fake: FakeContext, type: string, object: unknown): Promise<void> => {
|
|
86
|
-
await createEventHandler(fake.ctx, fake.stripe).process({ id: `evt_${type}_${Math.random()}`, type, created: epoch(new Date()), data: { object } } as never)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** A completed, paid amount checkout: 10.00 USD of credit charged as 9.19 EUR + 23 % VAT, bought from Poland. */
|
|
90
|
-
export const paidSession = (overrides: Rec = {}): Rec => ({
|
|
91
|
-
id: 'cs_pl', object: 'checkout.session', mode: 'payment', payment_status: 'paid', status: 'complete',
|
|
92
|
-
customer: 'cus_1', currency: 'eur', amount_subtotal: 919, amount_total: 1130, total_details: { amount_tax: 211 },
|
|
93
|
-
payment_intent: 'pi_pl', invoice: 'in_pl', created: epoch(new Date()),
|
|
94
|
-
customer_details: {
|
|
95
|
-
address: { country: 'PL' }, email: 'buyer@shop.eu', name: 'Jan Kowalski', tax_ids: [], tax_exempt: 'none',
|
|
96
|
-
},
|
|
97
|
-
consent: { terms_of_service: 'accepted' },
|
|
98
|
-
...overrides,
|
|
99
|
-
metadata: {
|
|
100
|
-
pricingMode: 'amount', amountMinor: '1000', sourceChargeAmountMinor: '1021', amountCurrency: 'usd',
|
|
101
|
-
chargeAmountMinor: '919', currency: 'eur', entityId: ENTITY, service: 'app', productSku: CREDITS_PRODUCT,
|
|
102
|
-
planSku: CREDIT_UNIT, country: 'PL', language: 'pl', termsVersion: TEXT_VERSION, ipCountry: 'PL',
|
|
103
|
-
...overrides.metadata,
|
|
104
|
-
},
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
/** A completed 100% promotion-code redemption without a PaymentIntent or invoice. */
|
|
108
|
-
export const freeSession = (overrides: Rec = {}): Rec => paidSession({
|
|
109
|
-
payment_status: 'no_payment_required', allow_promotion_codes: true,
|
|
110
|
-
amount_total: 0, total_details: { amount_tax: 0, amount_discount: 919 },
|
|
111
|
-
payment_intent: null, invoice: null, ...overrides,
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
/** The post-payment invoice of `paidSession`. */
|
|
115
|
-
export const invoiceOf = (id = 'in_pl', overrides: Rec = {}): Rec => ({
|
|
116
|
-
id, object: 'invoice', number: 'INV-0001', subtotal: 919, tax: 211, total: 1130, currency: 'eur',
|
|
117
|
-
payment_intent: 'pi_pl', lines: { object: 'list', data: [{ id: 'il_pl', amount: 919 }] },
|
|
118
|
-
customer_address: { country: 'PL' }, ...overrides,
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
/** Complete a top-up: the webhook captures the purchase (and locks the country). */
|
|
122
|
-
export const buyTopUp = async (fake: FakeContext, overrides: Rec = {}): Promise<PurchaseRecord> => {
|
|
123
|
-
const session = paidSession(overrides)
|
|
124
|
-
fake.state.invoices[session.invoice as string] = fake.state.invoices[session.invoice as string] ?? invoiceOf(session.invoice as string)
|
|
125
|
-
await send(fake, 'checkout.session.completed', session)
|
|
126
|
-
const purchase = await paymentAccessOf(fake.ctx).purchases().load({ sessionId: session.id as string })
|
|
127
|
-
if (purchase == null) throw new Error('no purchase captured')
|
|
128
|
-
|
|
129
|
-
return purchase
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
/** A start request of `ENTITY` for the pro plan. */
|
|
133
|
-
export const requestStart = async (fake: FakeContext, patch: Rec = {}): Promise<string> =>
|
|
134
|
-
(await paymentAccessOf(fake.ctx).consumerRights().recordStartRequest(
|
|
135
|
-
{ entityId: ENTITY, email: 'owner@shop.eu', name: 'Anna' },
|
|
136
|
-
{ planSku: PRO, textVersion: TEXT_VERSION, language: 'en', acknowledged: true, ...patch },
|
|
137
|
-
{ ip: '203.0.113.7' },
|
|
138
|
-
{ plan: 'Pro' },
|
|
139
|
-
)).startRequestId
|
|
140
|
-
|
|
141
|
-
export { ConsentKind, CREDITS_PRODUCT, CREDIT_UNIT, PLANS_PRODUCT, PRO }
|