@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.
Files changed (296) hide show
  1. package/README.md +52 -54
  2. package/agent-meta/manifest.json +2 -2
  3. package/agent-meta/skills/server-payment/SKILL.md +272 -330
  4. package/agent-meta/skills/server-payment/parts/reference.md +3 -3
  5. package/build/access/types.d.ts +7 -16
  6. package/build/access/types.d.ts.map +1 -1
  7. package/build/access.d.ts.map +1 -1
  8. package/build/access.js +6 -8
  9. package/build/access.js.map +1 -1
  10. package/build/actions/resync.d.ts +3 -3
  11. package/build/actions/resync.js +5 -5
  12. package/build/actions/resync.js.map +1 -1
  13. package/build/actions/webhook.d.ts +4 -1
  14. package/build/actions/webhook.d.ts.map +1 -1
  15. package/build/actions/webhook.js +8 -4
  16. package/build/actions/webhook.js.map +1 -1
  17. package/build/bootstrap/types.d.ts +13 -7
  18. package/build/bootstrap/types.d.ts.map +1 -1
  19. package/build/bootstrap.d.ts +4 -4
  20. package/build/bootstrap.d.ts.map +1 -1
  21. package/build/bootstrap.js +19 -10
  22. package/build/bootstrap.js.map +1 -1
  23. package/build/catalogue/types.d.ts +3 -3
  24. package/build/catalogue/types.d.ts.map +1 -1
  25. package/build/catalogue.d.ts.map +1 -1
  26. package/build/catalogue.js +4 -8
  27. package/build/catalogue.js.map +1 -1
  28. package/build/checkout/types.d.ts +21 -0
  29. package/build/checkout/types.d.ts.map +1 -0
  30. package/build/{plugins/stripe → checkout}/types.js.map +1 -1
  31. package/build/checkout.d.ts +6 -0
  32. package/build/checkout.d.ts.map +1 -0
  33. package/build/checkout.js +374 -0
  34. package/build/checkout.js.map +1 -0
  35. package/build/config.d.ts +10 -16
  36. package/build/config.d.ts.map +1 -1
  37. package/build/config.js +18 -59
  38. package/build/config.js.map +1 -1
  39. package/build/consts.d.ts +3 -49
  40. package/build/consts.d.ts.map +1 -1
  41. package/build/consts.js +3 -61
  42. package/build/consts.js.map +1 -1
  43. package/build/consumer/cancellation/types.d.ts +6 -4
  44. package/build/consumer/cancellation/types.d.ts.map +1 -1
  45. package/build/consumer/cancellation.d.ts.map +1 -1
  46. package/build/consumer/cancellation.js +11 -5
  47. package/build/consumer/cancellation.js.map +1 -1
  48. package/build/consumer/capture/types.d.ts +8 -12
  49. package/build/consumer/capture/types.d.ts.map +1 -1
  50. package/build/consumer/capture.d.ts.map +1 -1
  51. package/build/consumer/capture.js +48 -53
  52. package/build/consumer/capture.js.map +1 -1
  53. package/build/consumer/index.d.ts +4 -0
  54. package/build/consumer/index.d.ts.map +1 -1
  55. package/build/consumer/index.js +2 -0
  56. package/build/consumer/index.js.map +1 -1
  57. package/build/consumer/reconcile.d.ts.map +1 -1
  58. package/build/consumer/reconcile.js +82 -72
  59. package/build/consumer/reconcile.js.map +1 -1
  60. package/build/consumer/records/types.d.ts +4 -4
  61. package/build/consumer/records/types.d.ts.map +1 -1
  62. package/build/consumer/records.d.ts.map +1 -1
  63. package/build/consumer/records.js +16 -13
  64. package/build/consumer/records.js.map +1 -1
  65. package/build/consumer/service.d.ts +2 -2
  66. package/build/consumer/service.d.ts.map +1 -1
  67. package/build/consumer/service.js +28 -21
  68. package/build/consumer/service.js.map +1 -1
  69. package/build/consumer/types.d.ts +1 -4
  70. package/build/consumer/types.d.ts.map +1 -1
  71. package/build/consumer/types.local.d.ts +1 -9
  72. package/build/consumer/types.local.d.ts.map +1 -1
  73. package/build/consumer/withdrawal/types.d.ts +14 -12
  74. package/build/consumer/withdrawal/types.d.ts.map +1 -1
  75. package/build/consumer/withdrawal.d.ts.map +1 -1
  76. package/build/consumer/withdrawal.js +55 -71
  77. package/build/consumer/withdrawal.js.map +1 -1
  78. package/build/index.d.ts +12 -6
  79. package/build/index.d.ts.map +1 -1
  80. package/build/index.js +7 -4
  81. package/build/index.js.map +1 -1
  82. package/build/paygate/providers.d.ts +13 -0
  83. package/build/paygate/providers.d.ts.map +1 -0
  84. package/build/paygate/providers.js +38 -0
  85. package/build/paygate/providers.js.map +1 -0
  86. package/build/paygate/registry.d.ts +6 -0
  87. package/build/paygate/registry.d.ts.map +1 -0
  88. package/build/paygate/registry.js +37 -0
  89. package/build/paygate/registry.js.map +1 -0
  90. package/build/paygate/types.d.ts +577 -0
  91. package/build/paygate/types.d.ts.map +1 -0
  92. package/build/{plugins/session → paygate}/types.js.map +1 -1
  93. package/build/plan/types.d.ts +6 -1
  94. package/build/plan/types.d.ts.map +1 -1
  95. package/build/plan.d.ts.map +1 -1
  96. package/build/plan.js +16 -2
  97. package/build/plan.js.map +1 -1
  98. package/build/plugins/checkout-policy/types.d.ts +5 -3
  99. package/build/plugins/checkout-policy/types.d.ts.map +1 -1
  100. package/build/plugins/checkout-policy.d.ts.map +1 -1
  101. package/build/plugins/checkout-policy.js +5 -3
  102. package/build/plugins/checkout-policy.js.map +1 -1
  103. package/build/plugins/consts.local.d.ts +8 -10
  104. package/build/plugins/consts.local.d.ts.map +1 -1
  105. package/build/plugins/consts.local.js +2 -19
  106. package/build/plugins/consts.local.js.map +1 -1
  107. package/build/plugins/customer-lock/types.d.ts +2 -2
  108. package/build/plugins/customer-lock/types.d.ts.map +1 -1
  109. package/build/plugins/customer-lock.js +2 -2
  110. package/build/plugins/customer-lock.js.map +1 -1
  111. package/build/plugins/estimate/cache.js +1 -1
  112. package/build/plugins/estimate/cache.js.map +1 -1
  113. package/build/plugins/estimate/types.d.ts +10 -10
  114. package/build/plugins/estimate/types.d.ts.map +1 -1
  115. package/build/plugins/estimate.d.ts.map +1 -1
  116. package/build/plugins/estimate.js +48 -122
  117. package/build/plugins/estimate.js.map +1 -1
  118. package/build/plugins/events/types.d.ts +17 -10
  119. package/build/plugins/events/types.d.ts.map +1 -1
  120. package/build/plugins/events.d.ts +6 -5
  121. package/build/plugins/events.d.ts.map +1 -1
  122. package/build/plugins/events.js +162 -173
  123. package/build/plugins/events.js.map +1 -1
  124. package/build/plugins/fx/types.d.ts +10 -21
  125. package/build/plugins/fx/types.d.ts.map +1 -1
  126. package/build/plugins/fx.d.ts +1 -1
  127. package/build/plugins/fx.d.ts.map +1 -1
  128. package/build/plugins/fx.js +17 -54
  129. package/build/plugins/fx.js.map +1 -1
  130. package/build/plugins/portal/types.d.ts +8 -37
  131. package/build/plugins/portal/types.d.ts.map +1 -1
  132. package/build/plugins/portal.d.ts.map +1 -1
  133. package/build/plugins/portal.js +38 -208
  134. package/build/plugins/portal.js.map +1 -1
  135. package/build/plugins/refunds/types.d.ts +11 -11
  136. package/build/plugins/refunds/types.d.ts.map +1 -1
  137. package/build/plugins/refunds.d.ts.map +1 -1
  138. package/build/plugins/refunds.js +14 -39
  139. package/build/plugins/refunds.js.map +1 -1
  140. package/build/plugins/snapshot/types.d.ts +11 -0
  141. package/build/plugins/snapshot/types.d.ts.map +1 -0
  142. package/build/plugins/snapshot/types.js +2 -0
  143. package/build/plugins/snapshot/types.js.map +1 -0
  144. package/build/plugins/snapshot.d.ts +4 -0
  145. package/build/plugins/snapshot.d.ts.map +1 -0
  146. package/build/plugins/snapshot.js +19 -0
  147. package/build/plugins/snapshot.js.map +1 -0
  148. package/build/plugins/subscriptions/types.d.ts +17 -17
  149. package/build/plugins/subscriptions/types.d.ts.map +1 -1
  150. package/build/plugins/subscriptions.d.ts +4 -4
  151. package/build/plugins/subscriptions.d.ts.map +1 -1
  152. package/build/plugins/subscriptions.js +69 -77
  153. package/build/plugins/subscriptions.js.map +1 -1
  154. package/build/plugins/types.local.d.ts +9 -43
  155. package/build/plugins/types.local.d.ts.map +1 -1
  156. package/build/plugins/webhook-manager/types.d.ts +15 -43
  157. package/build/plugins/webhook-manager/types.d.ts.map +1 -1
  158. package/build/plugins/webhook-manager.d.ts +3 -3
  159. package/build/plugins/webhook-manager.d.ts.map +1 -1
  160. package/build/plugins/webhook-manager.js +21 -199
  161. package/build/plugins/webhook-manager.js.map +1 -1
  162. package/build/protocol.d.ts +1 -1
  163. package/build/protocol.d.ts.map +1 -1
  164. package/build/protocol.js +1 -1
  165. package/build/protocol.js.map +1 -1
  166. package/build/service.d.ts +11 -11
  167. package/build/service.d.ts.map +1 -1
  168. package/build/service.js +59 -23
  169. package/build/service.js.map +1 -1
  170. package/build/sync/types.d.ts +17 -13
  171. package/build/sync/types.d.ts.map +1 -1
  172. package/build/sync.d.ts.map +1 -1
  173. package/build/sync.js +22 -122
  174. package/build/sync.js.map +1 -1
  175. package/build/types.d.ts +46 -101
  176. package/build/types.d.ts.map +1 -1
  177. package/build/utils/types.d.ts +0 -8
  178. package/build/utils/types.d.ts.map +1 -1
  179. package/build/utils.d.ts +1 -4
  180. package/build/utils.d.ts.map +1 -1
  181. package/build/utils.js +1 -15
  182. package/build/utils.js.map +1 -1
  183. package/package.json +20 -22
  184. package/src/access/types.ts +7 -16
  185. package/src/access.ts +7 -15
  186. package/src/actions/resync.ts +5 -5
  187. package/src/actions/webhook.ts +8 -4
  188. package/src/bootstrap/types.ts +13 -7
  189. package/src/bootstrap.ts +23 -13
  190. package/src/catalogue/types.ts +3 -3
  191. package/src/catalogue.ts +4 -8
  192. package/src/checkout/types.ts +21 -0
  193. package/src/checkout.ts +414 -0
  194. package/src/config.ts +19 -66
  195. package/src/consts.ts +3 -69
  196. package/src/consumer/cancellation/types.ts +6 -4
  197. package/src/consumer/cancellation.ts +13 -7
  198. package/src/consumer/capture/types.ts +8 -12
  199. package/src/consumer/capture.ts +50 -59
  200. package/src/consumer/index.ts +4 -0
  201. package/src/consumer/reconcile.ts +69 -68
  202. package/src/consumer/records/types.ts +4 -4
  203. package/src/consumer/records.ts +19 -14
  204. package/src/consumer/service.ts +35 -22
  205. package/src/consumer/types.local.ts +1 -8
  206. package/src/consumer/types.ts +1 -4
  207. package/src/consumer/withdrawal/types.ts +14 -12
  208. package/src/consumer/withdrawal.ts +61 -77
  209. package/src/index.ts +12 -6
  210. package/src/paygate/providers.ts +48 -0
  211. package/src/paygate/registry.ts +53 -0
  212. package/src/paygate/types.ts +640 -0
  213. package/src/plan/types.ts +6 -1
  214. package/src/plan.ts +21 -3
  215. package/src/plugins/checkout-policy/types.ts +5 -3
  216. package/src/plugins/checkout-policy.ts +6 -5
  217. package/src/plugins/consts.local.ts +2 -26
  218. package/src/plugins/customer-lock/types.ts +2 -2
  219. package/src/plugins/customer-lock.ts +2 -2
  220. package/src/plugins/estimate/cache.ts +1 -1
  221. package/src/plugins/estimate/types.ts +10 -10
  222. package/src/plugins/estimate.ts +52 -128
  223. package/src/plugins/events/types.ts +17 -9
  224. package/src/plugins/events.ts +172 -190
  225. package/src/plugins/fx/types.ts +10 -23
  226. package/src/plugins/fx.ts +28 -64
  227. package/src/plugins/portal/types.ts +8 -38
  228. package/src/plugins/portal.ts +48 -229
  229. package/src/plugins/refunds/types.ts +11 -11
  230. package/src/plugins/refunds.ts +16 -40
  231. package/src/plugins/snapshot/types.ts +11 -0
  232. package/src/plugins/snapshot.ts +22 -0
  233. package/src/plugins/subscriptions/types.ts +17 -17
  234. package/src/plugins/subscriptions.ts +81 -81
  235. package/src/plugins/types.local.ts +9 -33
  236. package/src/plugins/webhook-manager/types.ts +15 -43
  237. package/src/plugins/webhook-manager.ts +24 -220
  238. package/src/protocol.ts +1 -1
  239. package/src/service.ts +66 -30
  240. package/src/sync/types.ts +17 -13
  241. package/src/sync.ts +25 -143
  242. package/src/types.ts +47 -97
  243. package/src/utils/types.ts +0 -9
  244. package/src/utils.ts +2 -20
  245. package/tests/capabilities.spec.ts +219 -0
  246. package/tests/entitlement.spec.ts +4 -4
  247. package/tests/gate-handlers.spec.ts +6 -6
  248. package/tests/gate.spec.ts +2 -2
  249. package/tests/grants.spec.ts +2 -2
  250. package/tests/internal-plan.spec.ts +2 -2
  251. package/tests/limit-gate.spec.ts +2 -2
  252. package/tests/paygates.spec.ts +132 -0
  253. package/tests/protocol.spec.ts +4 -5
  254. package/tests/usage.spec.ts +2 -2
  255. package/tests/webhook.spec.ts +137 -0
  256. package/build/plugins/session/types.d.ts +0 -34
  257. package/build/plugins/session/types.d.ts.map +0 -1
  258. package/build/plugins/session.d.ts +0 -4
  259. package/build/plugins/session.d.ts.map +0 -1
  260. package/build/plugins/session.js +0 -71
  261. package/build/plugins/session.js.map +0 -1
  262. package/build/plugins/status.d.ts +0 -8
  263. package/build/plugins/status.d.ts.map +0 -1
  264. package/build/plugins/status.js +0 -21
  265. package/build/plugins/status.js.map +0 -1
  266. package/build/plugins/stripe/types.d.ts +0 -20
  267. package/build/plugins/stripe/types.d.ts.map +0 -1
  268. package/build/plugins/stripe.d.ts +0 -6
  269. package/build/plugins/stripe.d.ts.map +0 -1
  270. package/build/plugins/stripe.js +0 -518
  271. package/build/plugins/stripe.js.map +0 -1
  272. package/src/plugins/session/types.ts +0 -30
  273. package/src/plugins/session.ts +0 -86
  274. package/src/plugins/status.ts +0 -22
  275. package/src/plugins/stripe/types.ts +0 -20
  276. package/src/plugins/stripe.ts +0 -571
  277. package/tests/checkout-consumer.spec.ts +0 -366
  278. package/tests/checkout-locks.spec.ts +0 -161
  279. package/tests/checkout-plugins.spec.ts +0 -164
  280. package/tests/checkout.spec.ts +0 -313
  281. package/tests/consumer-events.spec.ts +0 -215
  282. package/tests/consumer-fixtures.ts +0 -141
  283. package/tests/consumer-ops.spec.ts +0 -361
  284. package/tests/consumer-rights.integration.spec.ts +0 -147
  285. package/tests/consumer-rights.spec.ts +0 -606
  286. package/tests/context.ts +0 -110
  287. package/tests/estimate.spec.ts +0 -350
  288. package/tests/events.spec.ts +0 -367
  289. package/tests/fake-stripe.ts +0 -1004
  290. package/tests/gateway-options.spec.ts +0 -180
  291. package/tests/portal.spec.ts +0 -202
  292. package/tests/sync.spec.ts +0 -114
  293. package/tests/usage.integration.spec.ts +0 -165
  294. package/tests/webhook-manager.spec.ts +0 -149
  295. /package/build/{plugins/session → checkout}/types.js +0 -0
  296. /package/build/{plugins/stripe → paygate}/types.js +0 -0
package/tests/context.ts DELETED
@@ -1,110 +0,0 @@
1
- import { randomNamespace, gateHelper } from '@owlmeans/test-integration'
2
- import type { IntegrationGate, MongoEnv } from '@owlmeans/test-integration'
3
- import { AppType } from '@owlmeans/context'
4
- import { appendMongo } from '@owlmeans/mongo'
5
- import type { MongoDbService } from '@owlmeans/mongo-resource'
6
- import { LimitKind, LimitWindow, PlanDuration } from '@owlmeans/payment'
7
- import { config, makeServerContext } from '@owlmeans/server-context'
8
- import type { ServerConfig } from '@owlmeans/server-context'
9
- import type { Context as ApiContext } from '@owlmeans/server-api'
10
- import type Stripe from 'stripe'
11
- import { declareConsumerRights, declarePaymentPricing } from '../src/config.js'
12
- import { appendConsumerRights } from '../src/consumer/service.js'
13
- import { appendPaymentGatewayService } from '../src/service.js'
14
- import type { Config, ConsumerRightsDef, PricingDef, UsageMeter } from '../src/types.js'
15
- import { declareTestCatalogue, HOST, PLANS_PRODUCT, SERVICE } from './fake-stripe.js'
16
-
17
- /**
18
- * Category-C harness: a real server context over a real Mongo database — the payment resources with
19
- * their validators and indexes, the entitlement service and the gates — with the gateway unmanaged
20
- * (no Stripe). One database per spec file, dropped by that file's own teardown.
21
- */
22
- export const gate: IntegrationGate<MongoEnv> = gateHelper.mongoGate()
23
-
24
- export const BURST_PLAN = 'burst-plan'
25
-
26
- export interface Booted {
27
- ctx: ApiContext
28
- mongo: MongoDbService
29
- }
30
-
31
- export interface MongoSuite {
32
- database: string
33
- boot: () => Promise<Booted>
34
- teardown: () => Promise<void>
35
- }
36
-
37
- export interface SuiteOptions {
38
- consumerRights?: ConsumerRightsDef
39
- pricing?: PricingDef
40
- /** The paygate the consumer-rights service manages (a fake); without it the service is unmanaged. */
41
- stripe?: Stripe
42
- meter?: UsageMeter
43
- }
44
-
45
- export const makeSuite = (label: string, opts: SuiteOptions = {}): MongoSuite => {
46
- const prefix = process.env.MONGO_TEST_DB_PREFIX ?? 'omt'
47
- const database = randomNamespace(`${prefix}_${label}`)
48
- const booted: MongoDbService[] = []
49
-
50
- const boot = async (): Promise<Booted> => {
51
- const parsed = new URL(gate.env.MONGO_URL as string)
52
- const cfg = config<ServerConfig>(SERVICE, {
53
- dbs: [{
54
- service: 'mongo', alias: 'mongo', schema: database,
55
- host: decodeURIComponent(parsed.hostname).replace(/^\[|\]$/g, ''),
56
- port: parsed.port !== '' ? parseInt(parsed.port, 10) : 27017,
57
- ...(parsed.username !== ''
58
- ? { user: decodeURIComponent(parsed.username), secret: decodeURIComponent(parsed.password) }
59
- : {}),
60
- }],
61
- services: { [SERVICE]: { service: SERVICE, type: AppType.Backend, host: HOST } },
62
- } as Partial<ServerConfig>)
63
- declareTestCatalogue(cfg as unknown as Config, {
64
- plans: [{
65
- productSku: PLANS_PRODUCT, sku: BURST_PLAN, duration: PlanDuration.Monthly, rank: 30, price: 90,
66
- recurring: { interval: 'month' },
67
- limits: {
68
- weekly: { kind: LimitKind.Window, window: LimitWindow.SubscriptionWeek, limit: 1 },
69
- burst: { kind: LimitKind.Window, window: LimitWindow.Day, limit: 5 },
70
- seats: { kind: LimitKind.Occupancy, limit: 1 },
71
- },
72
- }],
73
- })
74
-
75
- if (opts.pricing != null) declarePaymentPricing(cfg as unknown as Config, opts.pricing)
76
- if (opts.consumerRights != null) declareConsumerRights(cfg as unknown as Config, opts.consumerRights)
77
-
78
- const context = makeServerContext(cfg)
79
- appendMongo(context)
80
- if (opts.stripe != null) {
81
- const stripe = opts.stripe
82
- appendConsumerRights(context as never, { manage: true, stripe: async () => stripe, ...(opts.meter != null ? { usage: opts.meter } : {}) })
83
- }
84
- appendPaymentGatewayService(context as never, { manage: false })
85
- context.configure()
86
- const mongo = context.service<MongoDbService>('mongo')
87
- booted.push(mongo)
88
- await context.init()
89
-
90
- return { ctx: context as unknown as ApiContext, mongo }
91
- }
92
-
93
- const teardown = async (): Promise<void> => {
94
- if (gate.skip) {
95
- return
96
- }
97
- const [first] = booted
98
- if (first != null) {
99
- await first.db().then(async db => { await db.dropDatabase() })
100
- .catch(error => console.error(`mongo test teardown (${database}):`, error))
101
- }
102
- for (const mongo of booted) {
103
- for (const client of Object.values(mongo.clients ?? {}) as Array<{ close: () => Promise<void> }>) {
104
- await client.close().catch(() => undefined)
105
- }
106
- }
107
- }
108
-
109
- return { database, boot, teardown }
110
- }
@@ -1,350 +0,0 @@
1
- import { describe, expect, test } from 'bun:test'
2
- import { PaygateError, PlanDuration, priceEstimateHelper, TaxBehavior, TaxEstimateStatus, TaxType } from '@owlmeans/payment'
3
- import { CREDITS_PRODUCT, makeFakeContext, PLANS_PRODUCT, PRO } from './fake-stripe.js'
4
- import type { FakeContext } from './fake-stripe.js'
5
- import { EUR_SETTLEMENT, makeRightsContext } from './consumer-fixtures.js'
6
- import { paymentAccessOf } from '../src/access.js'
7
- import { estimateOf } from '../src/plugins/estimate.js'
8
- import { makeEstimateCache } from '../src/plugins/estimate/cache.js'
9
-
10
- const withCustomer = async (
11
- fake: FakeContext, entityId: string, country: string, taxIds: Array<{ type: string, value: string, country: string }> = [],
12
- taxExempt: string = 'none',
13
- ) => {
14
- await paymentAccessOf(fake.ctx).paygateCustomers().create({ paygate: 'stripe', externalId: `cus_${entityId}`, entityId })
15
- fake.state.customers[`cus_${entityId}`] = {
16
- id: `cus_${entityId}`, object: 'customer', address: { country }, tax_exempt: taxExempt,
17
- tax_ids: { object: 'list', data: taxIds },
18
- }
19
- }
20
-
21
- const estimatingPolicy = { tax: { automatic: true, collectTaxId: true, estimate: true }, currency: { adaptive: true, estimate: true } }
22
-
23
- describe('@owlmeans/server-payment — Stripe Tax price estimate', () => {
24
- test('taxes the reference amount of a recurring plan at a given country, half-cent rounded up', async () => {
25
- const fake = await makeFakeContext({
26
- pricing: estimatingPolicy, stripe: { taxRates: { PL: [{ type: 'vat', percentage: '23' }] } },
27
- })
28
- const cache = makeEstimateCache()
29
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
30
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'PL',
31
- }, cache)
32
- expect(result).toEqual(expect.objectContaining({
33
- country: 'PL', source: 'request', currency: 'usd', behavior: TaxBehavior.Exclusive,
34
- }))
35
- expect(result.tax).toEqual({
36
- status: TaxEstimateStatus.Taxed, subtotalMinor: 2_000, taxMinor: 460, totalMinor: 2_460,
37
- scalable: true, rates: [{ type: TaxType.Vat, percentage: '23', ratePpm: 230_000, country: 'PL' }],
38
- })
39
- })
40
-
41
- test('taxes an amount-priced consumable at its default preset, gross-up included', async () => {
42
- const fake = await makeFakeContext({
43
- pricing: estimatingPolicy, stripe: { taxRates: { PL: [{ type: 'vat', percentage: '23' }] } },
44
- })
45
- const cache = makeEstimateCache()
46
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
47
- entityId: 'entity-1', productSku: CREDITS_PRODUCT, country: 'PL',
48
- }, cache)
49
- // the credit unit's default preset is 1_000, grossed up to 1_021 by its own amount policy
50
- expect(result.tax.subtotalMinor).toBe(1_021)
51
- expect(result.tax.taxMinor).toBe(235)
52
- expect(result.tax.totalMinor).toBe(1_256)
53
- })
54
-
55
- test('no request country and no paygate customer: `location-required`, zero Stripe calls', async () => {
56
- const fake = await makeFakeContext({ pricing: estimatingPolicy })
57
- const cache = makeEstimateCache()
58
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
59
- entityId: 'unknown-entity', productSku: PLANS_PRODUCT, planSku: PRO,
60
- }, cache)
61
- expect(result.tax.status).toBe(TaxEstimateStatus.LocationRequired)
62
- expect(fake.state.calls).not.toContain('tax.calculations.create')
63
- })
64
-
65
- test("falls back to the entity's paygate customer address, tagged `source: 'customer'`", async () => {
66
- const fake = await makeFakeContext({
67
- pricing: estimatingPolicy, stripe: { taxRates: { DE: [{ type: 'vat', percentage: '19' }] } },
68
- })
69
- await withCustomer(fake, 'entity-1', 'DE')
70
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
71
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO,
72
- }, makeEstimateCache())
73
- expect(result.country).toBe('DE')
74
- expect(result.source).toBe('customer')
75
- expect(result.tax.status).toBe(TaxEstimateStatus.Taxed)
76
- })
77
-
78
- test('a saved tax id for a DIFFERENT country than the resolved one is never sent', async () => {
79
- const fake = await makeFakeContext({
80
- pricing: estimatingPolicy, stripe: { taxRates: { PL: [{ type: 'vat', percentage: '23' }] } },
81
- })
82
- await withCustomer(fake, 'entity-1', 'PL', [{ type: 'eu_vat', value: 'DE123456789', country: 'DE' }])
83
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
84
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO,
85
- }, makeEstimateCache())
86
- // taxed, not reverse-charged — the DE tax id never reached the calculation
87
- expect(result.tax.status).toBe(TaxEstimateStatus.Taxed)
88
- expect(fake.state.taxCalculations[0].customer_details.tax_ids).toBeUndefined()
89
- })
90
-
91
- test('a matching-country tax id triggers reverse charge, with zero tax', async () => {
92
- const fake = await makeFakeContext({
93
- pricing: estimatingPolicy, stripe: { taxRates: { DE: [{ type: 'vat', percentage: '19' }] } },
94
- })
95
- await withCustomer(fake, 'entity-1', 'DE', [{ type: 'eu_vat', value: 'DE123456789', country: 'DE' }])
96
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
97
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO,
98
- }, makeEstimateCache())
99
- expect(result.tax.status).toBe(TaxEstimateStatus.ReverseCharge)
100
- expect(result.tax.taxMinor).toBe(0)
101
- expect(result.tax.totalMinor).toBe(2_000)
102
- })
103
-
104
- test('an unconfigured country (no registration) reads `none`', async () => {
105
- const fake = await makeFakeContext({ pricing: estimatingPolicy })
106
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
107
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'FR',
108
- }, makeEstimateCache())
109
- expect(result.tax.status).toBe(TaxEstimateStatus.None)
110
- expect(result.tax.taxMinor).toBe(0)
111
- })
112
-
113
- test('a jurisdiction Stripe Tax does not cover reads `at-checkout`', async () => {
114
- const fake = await makeFakeContext({ pricing: estimatingPolicy, stripe: { taxUnsupportedCountries: ['CU'] } })
115
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
116
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'CU',
117
- }, makeEstimateCache())
118
- expect(result.tax.status).toBe(TaxEstimateStatus.AtCheckout)
119
- })
120
-
121
- test('a Stripe invalid-request error (e.g. US missing a postal code) reads `at-checkout`, and the outcome is cached', async () => {
122
- const fake = await makeFakeContext({ pricing: estimatingPolicy, stripe: { taxInvalidCountries: ['US'] } })
123
- const cache = makeEstimateCache()
124
- const first = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
125
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'US',
126
- }, cache)
127
- expect(first.tax.status).toBe(TaxEstimateStatus.AtCheckout)
128
- const callsAfterFirst = fake.state.calls.filter(name => name === 'tax.calculations.create').length
129
- await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
130
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'US',
131
- }, cache)
132
- expect(fake.state.calls.filter(name => name === 'tax.calculations.create')).toHaveLength(callsAfterFirst)
133
- })
134
-
135
- test('an identical request within the cache TTL makes exactly one Stripe Tax call; a different entity with its own tax id makes another', async () => {
136
- const fake = await makeFakeContext({ pricing: estimatingPolicy, stripe: { taxRates: { PL: [{ type: 'vat', percentage: '23' }] } } })
137
- const cache = makeEstimateCache()
138
- const params = { entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'PL' }
139
- await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, params, cache)
140
- await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, params, cache)
141
- expect(fake.state.taxCalculations).toHaveLength(1)
142
-
143
- await withCustomer(fake, 'entity-2', 'PL', [{ type: 'eu_vat', value: 'PL123456789', country: 'PL' }])
144
- await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, { ...params, entityId: 'entity-2' }, cache)
145
- expect(fake.state.taxCalculations).toHaveLength(2)
146
- })
147
-
148
- test('never caches a rate-limit or connection error', async () => {
149
- let fail = true
150
- const fake = await makeFakeContext({ pricing: estimatingPolicy, stripe: { taxRates: { PL: [{ type: 'vat', percentage: '23' }] } } })
151
- const cache = makeEstimateCache()
152
- const original = (fake.stripe as never as { tax: { calculations: { create: (...args: unknown[]) => unknown } } })
153
- .tax.calculations.create
154
- ;(fake.stripe as never as { tax: { calculations: { create: (...args: unknown[]) => unknown } } })
155
- .tax.calculations.create = async (...args: unknown[]) => {
156
- if (fail) { fail = false; throw new Error('connection reset') }
157
- return await original(...args)
158
- }
159
- const params = { entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'PL' }
160
- await expect(estimateOf(fake.ctx).estimateStripePrice(fake.stripe, params, cache)).rejects.toThrow('connection reset')
161
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, params, cache)
162
- expect(result.tax.status).toBe(TaxEstimateStatus.Taxed)
163
- })
164
-
165
- test('carries a local-currency conversion from the FX Quotes API, requested to_currency=usd from_currencies=[local]', async () => {
166
- const fake = await makeFakeContext({
167
- pricing: estimatingPolicy,
168
- stripe: { taxRates: { PL: [{ type: 'vat', percentage: '23' }] }, fxRates: { pln: { exchangeRate: 0.25, fxFeeRate: 0.02 } } },
169
- })
170
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
171
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'PL',
172
- }, makeEstimateCache())
173
- expect(result.local).toEqual({ currency: 'pln', exchangeRate: 0.25, fxFeeRate: 0.02 })
174
- expect(fake.state.rawRequests[0]).toEqual(expect.objectContaining({
175
- method: 'POST', path: '/v1/fx_quotes', params: { to_currency: 'usd', 'from_currencies[]': 'pln', lock_duration: 'none' },
176
- }))
177
- })
178
-
179
- test('estimates the same USD to EUR to local chain used by settlement and Adaptive Pricing', async () => {
180
- const fake = await makeFakeContext({
181
- pricing: { ...estimatingPolicy, stripe: { settlementCurrency: 'eur' } },
182
- stripe: {
183
- taxRates: { PL: [{ type: 'vat', percentage: '23' }] },
184
- fxRates: {
185
- usd: { exchangeRate: 0.853568, referenceRate: 0.8726 },
186
- pln: { exchangeRate: 0.22, referenceRate: 0.224, fxFeeRate: 0.02 },
187
- },
188
- },
189
- })
190
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
191
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'PL',
192
- }, makeEstimateCache())
193
- expect(result.local?.currency).toBe('pln')
194
- expect(result.local?.exchangeRate).toBeCloseTo(0.22 / 0.8726)
195
- expect(fake.state.rawRequests.map(request => request.params)).toEqual([
196
- { to_currency: 'eur', 'from_currencies[]': 'usd', lock_duration: 'none' },
197
- { to_currency: 'eur', 'from_currencies[]': 'pln', lock_duration: 'none' },
198
- ])
199
- })
200
-
201
- test('a top-up CHARGED in the settlement currency (an EU buyer) carries its local line, via USD → EUR → local', async () => {
202
- // The amount plan is estimated in its policy currency (USD) but its session is converted to the
203
- // region's EUR, which Adaptive Pricing localizes — so the estimate must show the local total too.
204
- const fake = await makeRightsContext({
205
- pricing: { ...EUR_SETTLEMENT, currency: { adaptive: true, estimate: true } },
206
- stripe: {
207
- taxRates: { PL: [{ type: 'vat', percentage: '23' }] },
208
- fxRates: {
209
- usd: { exchangeRate: 0.853568, referenceRate: 0.8726 },
210
- pln: { exchangeRate: 0.22, referenceRate: 0.224, fxFeeRate: 0.02 },
211
- },
212
- },
213
- })
214
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
215
- entityId: 'entity-1', productSku: CREDITS_PRODUCT, country: 'PL',
216
- }, makeEstimateCache())
217
- expect(result.currency).toBe('usd')
218
- expect(result.local?.currency).toBe('pln')
219
- expect(result.local?.exchangeRate).toBeCloseTo(0.22 / 0.8726)
220
- })
221
-
222
- describe('the Adaptive Pricing conversion fee (`currency.adaptiveFeeRate`)', () => {
223
- // Stripe test mode, 2026-10: 1 PLN = 0.227941 EUR before fees, 0.223382 with the FX Quotes 2% fee;
224
- // Checkout showed 1 EUR = 4.5626 PLN, "includes 4% conversion fee" — 0.227941 / 1.04.
225
- const PLN = { exchangeRate: 0.223382, baseRate: 0.227941, referenceRate: 0.228128, fxFeeRate: 0.02 }
226
- const EUR_PRO = 'pro-eur'
227
- const eurPro = {
228
- catalogue: {
229
- plans: [{
230
- productSku: PLANS_PRODUCT, sku: EUR_PRO, duration: PlanDuration.Monthly, rank: 30, price: 18, currency: 'eur',
231
- recurring: { interval: 'month' as const },
232
- }],
233
- },
234
- }
235
- const pricing = (adaptiveFeeRate?: number) => ({
236
- ...EUR_SETTLEMENT,
237
- currency: { adaptive: true, estimate: true, ...(adaptiveFeeRate != null ? { adaptiveFeeRate } : {}) },
238
- })
239
- const estimate = async (fake: FakeContext, product: string, planSku?: string) => {
240
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
241
- entityId: 'entity-1', productSku: product, ...(planSku != null ? { planSku } : {}), country: 'PL',
242
- }, makeEstimateCache())
243
- return { result, quoted: priceEstimateHelper.estimateOf(result.tax.subtotalMinor, result) }
244
- }
245
-
246
- test('a EUR subscription is estimated at the base rate plus the fee — what Checkout shows (€22.14 → 101.02 PLN)', async () => {
247
- const fake = await makeFakeContext({
248
- ...eurPro, pricing: pricing(0.04),
249
- stripe: { taxRates: { PL: [{ type: 'vat', percentage: '23' }] }, fxRates: { pln: PLN } },
250
- })
251
- const { result, quoted } = await estimate(fake, PLANS_PRODUCT, EUR_PRO)
252
- expect(result.currency).toBe('eur')
253
- expect(result.local?.exchangeRate).toBeCloseTo(0.227941 / 1.04, 9)
254
- expect(result.local?.fxFeeRate).toBe(0.04)
255
- expect(quoted.totalMinor).toBe(2_214)
256
- expect(quoted.local?.totalAmount).toBeCloseTo(101.02, 1)
257
- })
258
-
259
- test('without the fee the estimate keeps the FX Quotes rate (€22.14 → 99.11 PLN)', async () => {
260
- const fake = await makeFakeContext({
261
- ...eurPro, pricing: pricing(),
262
- stripe: { taxRates: { PL: [{ type: 'vat', percentage: '23' }] }, fxRates: { pln: PLN } },
263
- })
264
- const { result, quoted } = await estimate(fake, PLANS_PRODUCT, EUR_PRO)
265
- expect(result.local).toEqual({ currency: 'pln', exchangeRate: 0.223382, fxFeeRate: 0.02 })
266
- expect(quoted.local?.totalAmount).toBeCloseTo(99.11, 1)
267
- })
268
-
269
- test('a top-up (USD → EUR reference rate, then the fee) applies it as well', async () => {
270
- const fake = await makeRightsContext({
271
- pricing: pricing(0.04),
272
- stripe: {
273
- taxRates: { PL: [{ type: 'vat', percentage: '23' }] },
274
- fxRates: { usd: { exchangeRate: 0.853568, referenceRate: 0.8726 }, pln: PLN },
275
- },
276
- })
277
- const { result } = await estimate(fake, CREDITS_PRODUCT)
278
- expect(result.currency).toBe('usd')
279
- expect(result.local?.exchangeRate).toBeCloseTo(0.227941 / 1.04 / 0.8726, 9)
280
- expect(result.local?.fxFeeRate).toBe(0.04)
281
- })
282
-
283
- test('a buyer whose local currency IS the settlement currency pays no conversion fee', async () => {
284
- const fake = await makeRightsContext({
285
- pricing: pricing(0.04),
286
- stripe: { taxRates: { DE: [{ type: 'vat', percentage: '19' }] }, fxRates: { usd: { exchangeRate: 0.853568, referenceRate: 0.8726 } } },
287
- })
288
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
289
- entityId: 'entity-1', productSku: CREDITS_PRODUCT, country: 'DE',
290
- }, makeEstimateCache())
291
- expect(result.local?.currency).toBe('eur')
292
- expect(result.local?.exchangeRate).toBeCloseTo(1 / 0.8726, 9)
293
- expect(result.local?.fxFeeRate).toBeUndefined()
294
- })
295
- })
296
-
297
- test('a top-up charged in exact USD (outside the EU) carries no local line, with no FX call', async () => {
298
- const fake = await makeRightsContext({
299
- pricing: { ...EUR_SETTLEMENT, currency: { adaptive: true, estimate: true } },
300
- stripe: { fxRates: { gbp: { exchangeRate: 1.27, referenceRate: 1.17 } } },
301
- })
302
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
303
- entityId: 'entity-1', productSku: CREDITS_PRODUCT, country: 'GB',
304
- }, makeEstimateCache())
305
- expect(result.local).toBeUndefined()
306
- expect(fake.state.rawRequests.filter(request => request.path === '/v1/fx_quotes')).toHaveLength(0)
307
- })
308
-
309
- test('omits `local` when the FX Quotes call fails, without failing the estimate', async () => {
310
- const fake = await makeFakeContext({
311
- pricing: estimatingPolicy, stripe: { taxRates: { PL: [{ type: 'vat', percentage: '23' }] }, fxUnavailable: true },
312
- })
313
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
314
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'PL',
315
- }, makeEstimateCache())
316
- expect(result.tax.status).toBe(TaxEstimateStatus.Taxed)
317
- expect(result.local).toBeUndefined()
318
- })
319
-
320
- test('omits `local` for a US billing country (same currency as the integration currency), with no FX call', async () => {
321
- const fake = await makeFakeContext({ pricing: estimatingPolicy })
322
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
323
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'US',
324
- }, makeEstimateCache())
325
- expect(result.local).toBeUndefined()
326
- expect(fake.state.rawRequests).toHaveLength(0)
327
- })
328
-
329
- test('`currency.estimate: false` never computes a local total, even with `currency.adaptive: true`', async () => {
330
- const fake = await makeFakeContext({
331
- pricing: {
332
- tax: { automatic: true, collectTaxId: true, estimate: true },
333
- currency: { adaptive: true, estimate: false },
334
- },
335
- stripe: { taxRates: { PL: [{ type: 'vat', percentage: '23' }] }, fxRates: { pln: { exchangeRate: 0.25 } } },
336
- })
337
- const result = await estimateOf(fake.ctx).estimateStripePrice(fake.stripe, {
338
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO, country: 'PL',
339
- }, makeEstimateCache())
340
- expect(result.local).toBeUndefined()
341
- expect(fake.state.rawRequests).toHaveLength(0)
342
- })
343
-
344
- test('an unmanaged gateway throws PaygateError before ever reaching Stripe', async () => {
345
- const fake = await makeFakeContext({ pricing: estimatingPolicy })
346
- await expect(paymentAccessOf(fake.ctx).gateway().estimatePrice(fake.ctx, {
347
- entityId: 'entity-1', productSku: PLANS_PRODUCT, planSku: PRO,
348
- })).rejects.toBeInstanceOf(PaygateError)
349
- })
350
- })