@voltro/plugin-billing 0.89.2 → 0.91.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.
Files changed (40) hide show
  1. package/CHANGELOG.md +1943 -0
  2. package/README.md +0 -435
  3. package/THIRD-PARTY-NOTICES.md +22 -646
  4. package/dist/catalog.js +169 -70
  5. package/dist/custodyInventory.js +101 -65
  6. package/dist/dunning.js +14 -6
  7. package/dist/eventDelivery.js +14 -5
  8. package/dist/historicalUsage.js +94 -0
  9. package/dist/index.d.ts +777 -68
  10. package/dist/index.js +354 -233
  11. package/dist/invoiceHistoryJournal.js +204 -179
  12. package/dist/invoiceHistoryProgress.js +130 -106
  13. package/dist/invoiceHistoryWork.js +188 -186
  14. package/dist/invoicePaymentEpoch.js +145 -0
  15. package/dist/invoicePaymentObservation.js +64 -0
  16. package/dist/invoiceSnapshot.js +56 -46
  17. package/dist/invoiceStore.js +124 -90
  18. package/dist/paidInvoiceCoverage.js +84 -0
  19. package/dist/plans.js +15 -2
  20. package/dist/providers.js +149 -92
  21. package/dist/provision.js +84 -73
  22. package/dist/rpc.d.ts +53 -13
  23. package/dist/rpc.js +141 -78
  24. package/dist/service.js +1208 -722
  25. package/dist/stores.js +240 -148
  26. package/dist/stripeIdentity.js +17 -17
  27. package/dist/stripeInvoiceHistory.js +21 -21
  28. package/dist/stripeInvoicePayments.js +129 -0
  29. package/dist/stripeInvoiceSnapshot.js +65 -46
  30. package/dist/stripeProvider.js +570 -201
  31. package/dist/stripeRest.js +10 -10
  32. package/dist/subscriptionObservation.js +93 -0
  33. package/dist/subscriptionWriteStore.js +325 -0
  34. package/dist/table.js +75 -66
  35. package/dist/types.d.ts +415 -27
  36. package/dist/types.js +21 -4
  37. package/dist/web/index.d.ts +93 -3
  38. package/dist/web/index.js +76 -33
  39. package/dist/web/writeRequest.js +65 -0
  40. package/package.json +9 -9
@@ -14,6 +14,7 @@ var f = 864e5, p = (t) => new e({
14
14
  let e = structuredClone(l);
15
15
  if (e.rejected) throw p("The provider event could not be normalized and requires reconciliation.");
16
16
  if (typeof e.id != "string" || !e.id || e.id.trim() !== e.id || e.createdAt !== null && (!(e.createdAt instanceof Date) || !Number.isFinite(e.createdAt.getTime()))) throw p("The verified provider delivery requires exact identity and time evidence.");
17
+ if (e.event === null && e.providerCustomerId === null) return "processed";
17
18
  let d = m.receipts?.store() ?? h;
18
19
  if (g === void 0) {
19
20
  let e = m.provider.fetchAccountIdentity?.bind(m.provider);
@@ -39,12 +40,20 @@ var f = 864e5, p = (t) => new e({
39
40
  if (!await d.renew(x.claim, Date.now())) throw p("The provider delivery claim changed before processing.");
40
41
  let t = e.event, n = e.providerCustomerId ?? (t !== null && "providerCustomerId" in t ? t.providerCustomerId : null);
41
42
  if (t !== null && "providerCustomerId" in t && e.providerCustomerId !== null && t.providerCustomerId !== e.providerCustomerId) throw p("The provider delivery has inconsistent customer identities.");
42
- let i = t?._tag === "invoicePaid" || t?._tag === "invoicePaymentFailed", a = n !== null && t?._tag !== "customerUnlinked" && !i, o = [];
43
- if (t !== null && (!a || t._tag !== "subscriptionUpserted" && t._tag !== "subscriptionCanceled" && t._tag !== "customerLinked") && o.push(await r.runPromise(m.service.applyEvent(t))), a) {
44
- let e = await r.runPromise(m.service.syncCustomer(n, t !== null && "tenantId" in t && typeof t.tenantId == "string" ? t.tenantId : void 0));
45
- o.push(...e.events);
43
+ let i = [];
44
+ if (t?._tag === "invoicePaymentsCorrected") i.push(...await r.runPromise(m.service.reconcilePaymentCorrection({
45
+ ...v,
46
+ providerCustomerId: t.providerCustomerId,
47
+ providerEventId: e.id
48
+ })));
49
+ else {
50
+ let e = t?._tag === "invoicePaid" || t?._tag === "invoicePaymentFailed", a = n !== null && t?._tag !== "customerUnlinked" && !e;
51
+ if (t !== null && (!a || t._tag !== "subscriptionUpserted" && t._tag !== "subscriptionCanceled" && t._tag !== "customerLinked") && i.push(await r.runPromise(m.service.applyEvent(t))), a) {
52
+ let e = await r.runPromise(m.service.syncCustomer(n, t !== null && "tenantId" in t && typeof t.tenantId == "string" ? t.tenantId : void 0));
53
+ i.push(...e.events);
54
+ }
46
55
  }
47
- for (let t of o) {
56
+ for (let t of i) {
48
57
  if (!await d.renew(x.claim, Date.now())) throw p("The provider delivery claim changed before callback dispatch.");
49
58
  let n = m.onEvent?.[t._tag];
50
59
  n !== void 0 && await r.runPromise(n(t, Object.freeze({
@@ -0,0 +1,94 @@
1
+ import { BillingError as e } from "./types.js";
2
+ import { Effect as t } from "effect";
3
+ // src/historicalUsage.ts
4
+ var n = /^(?:0|[1-9][0-9]*)(?:\.[0-9]{1,12})?$/, r = (e, t) => e !== null && n.test(e) ? e : t !== null && Number.isSafeInteger(t) && t >= 0 ? String(t) : null, i = (e) => Number(e) === 0, a = (e, t, n) => {
5
+ if (!Number.isSafeInteger(t) || !Number.isSafeInteger(n) || t < 0 || n <= t) return null;
6
+ if (e.tiers === null) {
7
+ let i = r(null, e.unitAmountMinor);
8
+ return i === null ? null : [{
9
+ quantity: n - t,
10
+ unitAmountDecimal: i
11
+ }];
12
+ }
13
+ if (e.tiersMode !== "graduated") return null;
14
+ let i = [], a = 0;
15
+ for (let o of e.tiers) {
16
+ if (o.flatAmountMinor !== null && o.flatAmountMinor !== 0) return null;
17
+ let e = o.upTo ?? Infinity;
18
+ if (e <= a) return null;
19
+ let s = Math.min(n, e) - Math.max(t, a);
20
+ if (s > 0) {
21
+ let e = r(o.unitAmountDecimal, o.unitAmountMinor);
22
+ if (e === null) return null;
23
+ i.push({
24
+ quantity: s,
25
+ unitAmountDecimal: e
26
+ });
27
+ }
28
+ if (o.upTo === null) return i;
29
+ a = e;
30
+ }
31
+ return null;
32
+ }, o = (e, t, n) => {
33
+ let r = e.find((e) => e.id === t);
34
+ if (r === void 0) return null;
35
+ let i = null;
36
+ for (let t of e) t.productId === r.productId && t.currency === r.currency && t.usageType === r.usageType && t.interval === r.interval && (t.createdAt.getTime() >= n.getTime() || (i === null || t.createdAt.getTime() > i.createdAt.getTime() || t.createdAt.getTime() === i.createdAt.getTime() && t.id > i.id) && (i = t));
37
+ return i;
38
+ }, s = (t, n = !1) => new e({
39
+ source: "historicalUsage",
40
+ message: t,
41
+ transient: n
42
+ }), c = /^[A-Za-z0-9][A-Za-z0-9_.:-]{0,179}$/, l = (e) => (n) => t.gen(function* () {
43
+ let r = n.period.start.getTime(), l = n.period.end.getTime(), u = e.now().getTime();
44
+ if (n.tenantId.length === 0 || n.priceId.length === 0 || !c.test(n.idempotencyKey) || n.description.trim().length === 0 || n.description.length > 500 || !Number.isFinite(r) || !Number.isFinite(l) || r >= l || !Number.isSafeInteger(n.fromUnits) || !Number.isSafeInteger(n.toUnits) || n.fromUnits < 0 || n.toUnits <= n.fromUnits) return yield* t.fail(s("Historical usage requires a tenant, a price, a valid closed period, a positive unit interval and a stable key."));
45
+ if (l > u) return yield* t.fail(s("Historical usage belongs to a closed period; this period has not ended yet."));
46
+ let d = e.provider.fetchPriceVersions, f = e.provider.ensureInvoiceItem;
47
+ if (d === void 0 || f === void 0) return yield* t.fail(s(`Billing provider "${e.provider.name}" cannot invoice historical usage.`));
48
+ if (yield* e.sealed(n.tenantId)) return yield* t.fail(s("Usage arrived after final billing dispatch was sealed. Financial reconciliation is required."));
49
+ let p = o(yield* d(n.priceId), n.priceId, n.period.end);
50
+ if (p === null) return yield* t.fail(s("The provider holds no version of this price that was in force during the period."));
51
+ let m = a(p, n.fromUnits, n.toUnits);
52
+ if (m === null) return yield* t.fail(s("The period's price is not a flat or graduated per-unit price; its amount cannot be applied exactly."));
53
+ let h = m.map((e, t) => ({
54
+ segment: e,
55
+ index: t
56
+ })).filter(({ segment: e }) => !i(e.unitAmountDecimal));
57
+ if (h.length === 0) return {
58
+ priceId: p.id,
59
+ currency: p.currency,
60
+ lines: []
61
+ };
62
+ let g = yield* e.customerOf(n.tenantId);
63
+ if (g === null) return yield* t.fail(s("Historical usage requires a linked provider customer.", !0));
64
+ let _ = yield* e.liveSubscriptionOf(n.tenantId), v = [];
65
+ for (let { segment: e, index: t } of h) {
66
+ let r = yield* f({
67
+ providerCustomerId: g,
68
+ ..._ === null ? {} : { providerSubscriptionId: _ },
69
+ productId: p.productId,
70
+ currency: p.currency,
71
+ quantity: e.quantity,
72
+ unitAmountDecimal: e.unitAmountDecimal,
73
+ taxBehavior: p.taxBehavior,
74
+ period: {
75
+ start: n.period.start,
76
+ end: n.period.end
77
+ },
78
+ description: n.description,
79
+ idempotencyKey: `${n.idempotencyKey}:${String(t)}`,
80
+ notBefore: n.period.end
81
+ });
82
+ v.push({
83
+ providerInvoiceItemId: r.providerInvoiceItemId,
84
+ quantity: e.quantity,
85
+ unitAmountDecimal: e.unitAmountDecimal
86
+ });
87
+ }
88
+ return {
89
+ priceId: p.id,
90
+ currency: p.currency,
91
+ lines: v
92
+ };
93
+ });
94
+ export { a as historicalUsageSegments, l as makeHistoricalUsageInvoicer, o as priceVersionInForce };