@voltro/plugin-billing 0.89.2 → 0.90.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/CHANGELOG.md +1822 -0
- package/README.md +0 -435
- package/dist/catalog.js +175 -76
- package/dist/custodyInventory.js +101 -65
- package/dist/dunning.js +14 -6
- package/dist/eventDelivery.js +14 -5
- package/dist/historicalUsage.js +94 -0
- package/dist/index.d.ts +773 -67
- package/dist/index.js +340 -219
- package/dist/invoiceHistoryJournal.js +115 -90
- package/dist/invoiceHistoryProgress.js +49 -25
- package/dist/invoiceHistoryWork.js +162 -160
- package/dist/invoicePaymentEpoch.js +145 -0
- package/dist/invoicePaymentObservation.js +64 -0
- package/dist/invoiceSnapshot.js +52 -42
- package/dist/invoiceStore.js +122 -88
- package/dist/paidInvoiceCoverage.js +84 -0
- package/dist/plans.js +15 -2
- package/dist/providers.js +124 -67
- package/dist/provision.js +84 -73
- package/dist/rpc.d.ts +53 -13
- package/dist/rpc.js +141 -78
- package/dist/service.js +1159 -673
- package/dist/stores.js +231 -139
- package/dist/stripeInvoicePayments.js +129 -0
- package/dist/stripeInvoiceSnapshot.js +59 -40
- package/dist/stripeProvider.js +542 -173
- package/dist/subscriptionObservation.js +93 -0
- package/dist/subscriptionWriteStore.js +325 -0
- package/dist/table.js +75 -66
- package/dist/types.d.ts +415 -27
- package/dist/types.js +21 -4
- package/dist/web/index.d.ts +93 -3
- package/dist/web/index.js +76 -33
- package/dist/web/writeRequest.js +65 -0
- package/package.json +8 -8
package/dist/stores.js
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
import { BillingError as e } from "./types.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
2
|
+
import { dataStoreSubscriptionWriteStore as t } from "./subscriptionWriteStore.js";
|
|
3
|
+
import { dataStoreDunningNoticeStore as n } from "./dunning.js";
|
|
4
|
+
import { dataStoreCheckoutOperationStore as r } from "./checkoutStore.js";
|
|
5
|
+
import { dataStoreBillingCloseStore as i } from "./closeStore.js";
|
|
6
|
+
import { recordDataStoreUsageEvent as a } from "./usageEvents.js";
|
|
7
|
+
import { dataStoreInvoiceStore as o } from "./invoiceStore.js";
|
|
8
|
+
import { dataStoreCustomerStore as s } from "./customerStore.js";
|
|
9
|
+
import { dataStoreInvoiceHistoryJournal as c } from "./invoiceHistoryJournal.js";
|
|
10
|
+
import { dataStoreInvoiceHistoryWorkStore as l } from "./invoiceHistoryWork.js";
|
|
11
|
+
import { dataStoreInvoiceHistoryProgressStore as u } from "./invoiceHistoryProgress.js";
|
|
12
|
+
import { BILLING_SUBSCRIPTIONS_TABLE as d, BILLING_USAGE_TABLE as f, billingTables as p } from "./table.js";
|
|
13
|
+
import { captureSubscriptionObservation as m } from "./subscriptionObservation.js";
|
|
14
|
+
import { Effect as h } from "effect";
|
|
15
|
+
import { _voltroIdempotencyTable as g, and as _, ensureTableRegistered as v, eq as y, lt as b } from "@voltro/database";
|
|
16
|
+
import { InMemoryDataStore as x } from "@voltro/runtime";
|
|
17
|
+
import { createLogger as S } from "@voltro/logger";
|
|
16
18
|
// src/stores.ts
|
|
17
|
-
var
|
|
19
|
+
var C = (e) => `${e}_${Math.random().toString(36).slice(2)}${Date.now().toString(36)}`, w = S({ scope: "@voltro/plugin-billing" }), T = (t, n) => h.tryPromise({
|
|
18
20
|
try: t,
|
|
19
|
-
catch: (t) => (
|
|
21
|
+
catch: (t) => t instanceof e ? t : (w.warn("billing store operation failed", {
|
|
20
22
|
source: n,
|
|
21
23
|
detail: String(t?.message ?? t)
|
|
22
24
|
}), new e({
|
|
@@ -24,37 +26,52 @@ var b = (e) => `${e}_${Math.random().toString(36).slice(2)}${Date.now().toString
|
|
|
24
26
|
message: `${n} failed`,
|
|
25
27
|
transient: !0
|
|
26
28
|
}))
|
|
27
|
-
}),
|
|
29
|
+
}), E = () => new e({
|
|
30
|
+
source: "subscriptionStore",
|
|
31
|
+
message: "Subscription observation changed before exact patch.",
|
|
32
|
+
transient: !0
|
|
33
|
+
}), D = (e) => ({
|
|
28
34
|
table: e.table,
|
|
29
35
|
predicate: e.predicate,
|
|
30
36
|
order: e.order ?? [],
|
|
31
37
|
take: e.take,
|
|
32
38
|
skip: void 0,
|
|
33
39
|
projection: void 0
|
|
34
|
-
}),
|
|
40
|
+
}), O = (e, t) => e === null || t === null ? e === t : Number.isFinite(e.getTime()) && Number.isFinite(t.getTime()) && e.getTime() === t.getTime(), k = (e) => e === "canceled", A = /* @__PURE__ */ new Set([
|
|
35
41
|
"active",
|
|
36
42
|
"trialing",
|
|
37
43
|
"pastDue",
|
|
38
44
|
"incomplete"
|
|
39
|
-
]),
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
45
|
+
]), j = (e) => A.has(e), M = (e) => e.find((e) => j(e.status)) ?? e[0] ?? null, N = (e, t) => e === null ? !1 : t === null || t.getTime() < e.getTime(), P = (e, t) => {
|
|
46
|
+
try {
|
|
47
|
+
return m(e.providerObservation, {
|
|
48
|
+
...e,
|
|
49
|
+
...t
|
|
50
|
+
});
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}, F = (e) => {
|
|
55
|
+
let t = {
|
|
56
|
+
tenantId: String(e.tenantId),
|
|
57
|
+
provider: String(e.provider),
|
|
58
|
+
providerSubscriptionId: String(e.providerSubscriptionId),
|
|
59
|
+
plan: String(e.plan),
|
|
60
|
+
status: String(e.status),
|
|
61
|
+
quantity: e.quantity == null ? 1 : Number(e.quantity),
|
|
62
|
+
currentPeriodStart: e.currentPeriodStart == null ? null : new Date(e.currentPeriodStart),
|
|
63
|
+
currentPeriodEnd: e.currentPeriodEnd == null ? null : new Date(e.currentPeriodEnd),
|
|
64
|
+
cancelAt: e.cancelAt == null ? null : new Date(e.cancelAt),
|
|
65
|
+
pastDueSince: e.pastDueSince == null ? null : new Date(e.pastDueSince),
|
|
66
|
+
statusEventAt: e.statusEventAt == null ? null : new Date(e.statusEventAt)
|
|
67
|
+
};
|
|
68
|
+
return {
|
|
69
|
+
...t,
|
|
70
|
+
providerObservation: m(e.providerObservation, t)
|
|
71
|
+
};
|
|
72
|
+
}, I = async (e, t) => (await e.query(D({
|
|
73
|
+
table: d,
|
|
74
|
+
predicate: y("tenantId", t),
|
|
58
75
|
order: [{
|
|
59
76
|
column: "updatedAt",
|
|
60
77
|
direction: "desc"
|
|
@@ -62,105 +79,169 @@ var b = (e) => `${e}_${Math.random().toString(36).slice(2)}${Date.now().toString
|
|
|
62
79
|
column: "id",
|
|
63
80
|
direction: "desc"
|
|
64
81
|
}]
|
|
65
|
-
}))).map(
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
82
|
+
}))).map(F), L = () => /* @__PURE__ */ Error("The subscription observation write is unconfirmed."), R = (e, t) => ({
|
|
83
|
+
tenantId: e.tenantId,
|
|
84
|
+
provider: e.provider,
|
|
85
|
+
plan: e.plan,
|
|
86
|
+
status: e.status,
|
|
87
|
+
quantity: e.quantity,
|
|
88
|
+
currentPeriodStart: e.currentPeriodStart,
|
|
89
|
+
currentPeriodEnd: e.currentPeriodEnd,
|
|
90
|
+
cancelAt: e.cancelAt,
|
|
91
|
+
statusEventAt: e.statusEventAt,
|
|
92
|
+
providerObservation: t
|
|
93
|
+
}), z = (e, t, n) => {
|
|
94
|
+
if (e === void 0) throw L();
|
|
95
|
+
let r;
|
|
96
|
+
try {
|
|
97
|
+
r = F(e);
|
|
98
|
+
} catch {
|
|
99
|
+
throw L();
|
|
100
|
+
}
|
|
101
|
+
if (r.providerSubscriptionId !== t.providerSubscriptionId || r.tenantId !== t.tenantId || r.provider !== t.provider || r.plan !== t.plan || r.status !== t.status || r.quantity !== t.quantity || !O(r.currentPeriodStart, t.currentPeriodStart) || !O(r.currentPeriodEnd, t.currentPeriodEnd) || !O(r.cancelAt, t.cancelAt) || !O(r.statusEventAt, t.statusEventAt) || JSON.stringify(r.providerObservation ?? null) !== JSON.stringify(n)) throw L();
|
|
102
|
+
}, B = (e) => ({
|
|
103
|
+
...D({
|
|
104
|
+
table: d,
|
|
105
|
+
predicate: y("providerSubscriptionId", e)
|
|
106
|
+
}),
|
|
107
|
+
lock: "update"
|
|
108
|
+
}), V = async (e, t) => {
|
|
109
|
+
let n = await e.query(B(t));
|
|
110
|
+
if (n.length > 1) throw L();
|
|
111
|
+
return n[0] ?? null;
|
|
112
|
+
}, H = [
|
|
113
|
+
"plan",
|
|
114
|
+
"quantity",
|
|
115
|
+
"status",
|
|
116
|
+
"currentPeriodStart",
|
|
117
|
+
"currentPeriodEnd",
|
|
118
|
+
"pastDueSince",
|
|
119
|
+
"statusEventAt"
|
|
120
|
+
], U = (e) => {
|
|
121
|
+
let t = {};
|
|
122
|
+
for (let n of H) e[n] !== void 0 && (t[n] = e[n]);
|
|
123
|
+
return t;
|
|
124
|
+
}, W = (e, t) => H.every((n) => {
|
|
125
|
+
let r = t[n];
|
|
126
|
+
if (r === void 0) return !0;
|
|
127
|
+
let i = e[n];
|
|
128
|
+
return r instanceof Date || i instanceof Date ? r instanceof Date && i instanceof Date && O(i, r) : i === r;
|
|
129
|
+
}), G = async (e, t, n, r) => {
|
|
130
|
+
let i = String(t.providerSubscriptionId);
|
|
131
|
+
if (await e.update("_voltro_billing_subscriptions", String(t.id), {
|
|
132
|
+
...n,
|
|
133
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
134
|
+
}) === null) throw L();
|
|
135
|
+
let a = await e.query(B(i)), o = a[0];
|
|
136
|
+
if (a.length !== 1 || o === void 0 || o.id !== t.id || o.tenantId !== t.tenantId || o.providerSubscriptionId !== i) throw L();
|
|
137
|
+
let s;
|
|
138
|
+
try {
|
|
139
|
+
s = F(o);
|
|
140
|
+
} catch {
|
|
141
|
+
throw L();
|
|
142
|
+
}
|
|
143
|
+
if (!r(s)) throw L();
|
|
144
|
+
}, K = (e, t, n) => e.transactional(async (e) => {
|
|
145
|
+
let r = await V(e, t);
|
|
146
|
+
r !== null && await G(e, r, {
|
|
147
|
+
status: n,
|
|
148
|
+
providerObservation: r.status === n ? r.providerObservation ?? null : null
|
|
149
|
+
}, (e) => e.status === n);
|
|
150
|
+
}), q = (t) => ({
|
|
151
|
+
upsert: (n) => T(async () => {
|
|
152
|
+
let r = structuredClone(n), i = m(r.providerObservation, r), a = /* @__PURE__ */ new Date(), o = C("bsub"), s = await t.insertIgnoreWithOutcome(d, {
|
|
153
|
+
id: o,
|
|
154
|
+
providerSubscriptionId: r.providerSubscriptionId,
|
|
155
|
+
...R(r, i),
|
|
93
156
|
pastDueSince: null,
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
157
|
+
createdAt: a,
|
|
158
|
+
updatedAt: a
|
|
159
|
+
}, { conflictColumns: ["providerSubscriptionId"] });
|
|
160
|
+
if (s.outcome === "inserted") {
|
|
161
|
+
if (s.row.id !== o) throw L();
|
|
162
|
+
return z(s.row, r, i);
|
|
163
|
+
}
|
|
164
|
+
await t.transactional(async (t) => {
|
|
165
|
+
let n = B(r.providerSubscriptionId), o = await t.query(n), s = o[0];
|
|
166
|
+
if (o.length !== 1 || s === void 0) throw L();
|
|
167
|
+
if (s.tenantId !== r.tenantId || s.provider !== r.provider) throw new e({
|
|
168
|
+
source: "subscriptionStore",
|
|
169
|
+
message: "The provider subscription belongs to different native custody.",
|
|
170
|
+
transient: !1
|
|
171
|
+
});
|
|
172
|
+
if (N(s.statusEventAt == null ? null : new Date(s.statusEventAt), r.statusEventAt) || k(String(s.status)) && !k(r.status)) return;
|
|
173
|
+
if (await t.update("_voltro_billing_subscriptions", String(s.id), {
|
|
174
|
+
...R(r, i),
|
|
175
|
+
updatedAt: a
|
|
176
|
+
}) === null) throw L();
|
|
177
|
+
let c = await t.query(n);
|
|
178
|
+
if (c.length !== 1 || c[0].id !== s.id) throw L();
|
|
179
|
+
z(c[0], r, i);
|
|
97
180
|
});
|
|
98
181
|
}, "subscriptionStore"),
|
|
99
|
-
getByTenant: (
|
|
100
|
-
listByTenant: (
|
|
101
|
-
listByStatus: (
|
|
102
|
-
table:
|
|
103
|
-
predicate:
|
|
182
|
+
getByTenant: (e) => T(async () => M(await I(t, e)), "subscriptionStore"),
|
|
183
|
+
listByTenant: (e) => T(() => I(t, e), "subscriptionStore"),
|
|
184
|
+
listByStatus: (e) => T(async () => (await t.query(D({
|
|
185
|
+
table: d,
|
|
186
|
+
predicate: y("status", e),
|
|
104
187
|
order: [{
|
|
105
188
|
column: "updatedAt",
|
|
106
189
|
direction: "asc"
|
|
107
190
|
}]
|
|
108
|
-
}))).map(
|
|
109
|
-
markCanceled: (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
...
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
134
|
-
});
|
|
135
|
-
}, "subscriptionStore")
|
|
136
|
-
}), N = (e) => ({
|
|
191
|
+
}))).map(F), "subscriptionStore"),
|
|
192
|
+
markCanceled: (e) => T(() => K(t, e, "canceled"), "subscriptionStore"),
|
|
193
|
+
patchByTenant: (e, n) => T(() => t.transactional(async (t) => {
|
|
194
|
+
let r = M(await I(t, e));
|
|
195
|
+
if (r === null) return;
|
|
196
|
+
let i = await V(t, r.providerSubscriptionId);
|
|
197
|
+
if (i === null || i.tenantId !== e || M(await I(t, e))?.providerSubscriptionId !== r.providerSubscriptionId) throw L();
|
|
198
|
+
await G(t, i, {
|
|
199
|
+
...U(n),
|
|
200
|
+
providerObservation: P(F(i), n)
|
|
201
|
+
}, (e) => W(e, n));
|
|
202
|
+
}), "subscriptionStore"),
|
|
203
|
+
patchBySubscription: (e, n, r, i) => T(() => t.transactional(async (t) => {
|
|
204
|
+
let a = await V(t, n), o = a === null ? null : F(a);
|
|
205
|
+
if (a === null || o === null || o.tenantId !== e || i !== void 0 && !O(o.statusEventAt, i) || k(o.status) && r.status !== void 0 && !k(r.status)) throw E();
|
|
206
|
+
await G(t, a, {
|
|
207
|
+
...U(r),
|
|
208
|
+
providerObservation: P(o, r)
|
|
209
|
+
}, (t) => t.tenantId === e && W(t, r));
|
|
210
|
+
}), "subscriptionStore"),
|
|
211
|
+
setStatus: (e, n) => T(() => K(t, e, n), "subscriptionStore")
|
|
212
|
+
}), J = {
|
|
213
|
+
meter: "meterDispatchedThrough",
|
|
214
|
+
invoice: "invoiceDispatchedThrough"
|
|
215
|
+
}, Y = (e) => ({
|
|
137
216
|
tenantId: String(e.tenantId),
|
|
138
217
|
entitlementKey: String(e.entitlementKey),
|
|
139
218
|
period: String(e.period),
|
|
140
219
|
used: Number(e.used ?? 0),
|
|
141
|
-
reportedToProvider: Number(e.reportedToProvider ?? 0)
|
|
142
|
-
|
|
220
|
+
reportedToProvider: Number(e.reportedToProvider ?? 0),
|
|
221
|
+
meterDispatchedThrough: Number(e.meterDispatchedThrough ?? 0),
|
|
222
|
+
invoiceDispatchedThrough: Number(e.invoiceDispatchedThrough ?? 0)
|
|
223
|
+
}), X = async (e, t, n, r) => (await e.query(D({
|
|
143
224
|
table: "_voltro_billing_usage",
|
|
144
|
-
predicate:
|
|
225
|
+
predicate: _(y("tenantId", t), y("entitlementKey", n), y("period", r)),
|
|
145
226
|
take: 1
|
|
146
|
-
})))[0] ?? null,
|
|
147
|
-
recordEvent: (n) =>
|
|
148
|
-
let r =
|
|
227
|
+
})))[0] ?? null, Z = (t) => ({
|
|
228
|
+
recordEvent: (n) => a(t, n, async (t, n) => {
|
|
229
|
+
let r = Z(t), i = await h.runPromise(r.used(n.tenantId, n.entitlementKey, n.period));
|
|
149
230
|
if (!Number.isSafeInteger(i + n.quantity)) throw new e({
|
|
150
231
|
source: "usageEvent",
|
|
151
232
|
message: "Usage total exceeds exact integer storage.",
|
|
152
233
|
transient: !1
|
|
153
234
|
});
|
|
154
|
-
return
|
|
235
|
+
return h.runPromise(r.increment(n.tenantId, n.entitlementKey, n.period, n.quantity));
|
|
155
236
|
}),
|
|
156
|
-
used: (e, n, r) =>
|
|
157
|
-
let i = await
|
|
237
|
+
used: (e, n, r) => T(async () => {
|
|
238
|
+
let i = await X(t, e, n, r);
|
|
158
239
|
return i ? Number(i.used ?? 0) : 0;
|
|
159
240
|
}, "usageStore"),
|
|
160
|
-
consume: (e, n, r, i, a) =>
|
|
241
|
+
consume: (e, n, r, i, a) => T(async () => {
|
|
161
242
|
for (let o = 0; o < 32; o++) {
|
|
162
243
|
o > 0 && await new Promise((e) => setTimeout(e, o));
|
|
163
|
-
let s = await
|
|
244
|
+
let s = await X(t, e, n, r), c = s ? Number(s.used ?? 0) : 0;
|
|
164
245
|
if (c + i > a) return {
|
|
165
246
|
allowed: !1,
|
|
166
247
|
entitlement: n,
|
|
@@ -172,7 +253,7 @@ var b = (e) => `${e}_${Math.random().toString(36).slice(2)}${Date.now().toString
|
|
|
172
253
|
if (await t.updateMany("_voltro_billing_usage", {
|
|
173
254
|
used: c + i,
|
|
174
255
|
updatedAt: /* @__PURE__ */ new Date()
|
|
175
|
-
}, { where:
|
|
256
|
+
}, { where: _(y("id", String(s.id)), y("used", c)) }) === 1) return {
|
|
176
257
|
allowed: !0,
|
|
177
258
|
entitlement: n,
|
|
178
259
|
limit: a,
|
|
@@ -180,7 +261,7 @@ var b = (e) => `${e}_${Math.random().toString(36).slice(2)}${Date.now().toString
|
|
|
180
261
|
cost: i
|
|
181
262
|
};
|
|
182
263
|
} else {
|
|
183
|
-
let o =
|
|
264
|
+
let o = C("buse"), s = await t.insertIgnore(f, {
|
|
184
265
|
id: o,
|
|
185
266
|
tenantId: e,
|
|
186
267
|
entitlementKey: n,
|
|
@@ -204,18 +285,18 @@ var b = (e) => `${e}_${Math.random().toString(36).slice(2)}${Date.now().toString
|
|
|
204
285
|
}
|
|
205
286
|
throw Error(`billing usage consume: contention on (${e}, ${n}, ${r}) exceeded 32 attempts`);
|
|
206
287
|
}, "usageStore"),
|
|
207
|
-
increment: (e, n, r, i) =>
|
|
288
|
+
increment: (e, n, r, i) => T(async () => {
|
|
208
289
|
for (let a = 0; a < 32; a++) {
|
|
209
290
|
a > 0 && await new Promise((e) => setTimeout(e, a));
|
|
210
|
-
let o = await
|
|
291
|
+
let o = await X(t, e, n, r);
|
|
211
292
|
if (o) {
|
|
212
293
|
let e = Number(o.used ?? 0);
|
|
213
294
|
if (await t.updateMany("_voltro_billing_usage", {
|
|
214
295
|
used: e + i,
|
|
215
296
|
updatedAt: /* @__PURE__ */ new Date()
|
|
216
|
-
}, { where:
|
|
297
|
+
}, { where: _(y("id", String(o.id)), y("used", e)) }) === 1) return e + i;
|
|
217
298
|
} else {
|
|
218
|
-
let a =
|
|
299
|
+
let a = C("buse"), o = await t.insertIgnore(f, {
|
|
219
300
|
id: a,
|
|
220
301
|
tenantId: e,
|
|
221
302
|
entitlementKey: n,
|
|
@@ -233,27 +314,38 @@ var b = (e) => `${e}_${Math.random().toString(36).slice(2)}${Date.now().toString
|
|
|
233
314
|
}
|
|
234
315
|
throw Error(`billing usage increment: contention on (${e}, ${n}, ${r}) exceeded 32 attempts`);
|
|
235
316
|
}, "usageStore"),
|
|
236
|
-
pending: () =>
|
|
237
|
-
markReported: (e, n, r, i) =>
|
|
238
|
-
let a = await
|
|
239
|
-
a && await t.
|
|
317
|
+
pending: () => T(async () => (await t.query(D({ table: f }))).map(Y).filter((e) => e.used > e.reportedToProvider), "usageStore"),
|
|
318
|
+
markReported: (e, n, r, i) => T(async () => {
|
|
319
|
+
let a = await X(t, e, n, r);
|
|
320
|
+
a && await t.updateMany(f, {
|
|
240
321
|
reportedToProvider: i,
|
|
241
322
|
updatedAt: /* @__PURE__ */ new Date()
|
|
242
|
-
});
|
|
323
|
+
}, { where: _(y("id", String(a.id)), b("reportedToProvider", i)) });
|
|
324
|
+
}, "usageStore"),
|
|
325
|
+
claimDispatch: (e, n, r, i, { expected: a, from: o, through: s }) => T(async () => {
|
|
326
|
+
if (s <= o) return !1;
|
|
327
|
+
let c = await X(t, e, n, r);
|
|
328
|
+
if (!c) return !1;
|
|
329
|
+
let l = J[i];
|
|
330
|
+
return await t.updateMany(f, {
|
|
331
|
+
[l]: s,
|
|
332
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
333
|
+
}, { where: _(y("id", String(c.id)), y(l, a), y("reportedToProvider", o)) }) === 1;
|
|
243
334
|
}, "usageStore")
|
|
244
|
-
}),
|
|
245
|
-
for (let e of [...
|
|
246
|
-
return
|
|
247
|
-
},
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
335
|
+
}), Q = () => {
|
|
336
|
+
for (let e of [...p(), g]) v(e);
|
|
337
|
+
return $(new x());
|
|
338
|
+
}, $ = (e) => ({
|
|
339
|
+
subscriptionWrites: t(e),
|
|
340
|
+
closures: i(e),
|
|
341
|
+
checkouts: r(e),
|
|
342
|
+
customers: s(e),
|
|
343
|
+
subscriptions: q(e),
|
|
344
|
+
invoices: o(e),
|
|
345
|
+
historyJournal: c(e),
|
|
346
|
+
historyWork: l(e),
|
|
347
|
+
historyProgress: u(e),
|
|
348
|
+
usage: Z(e),
|
|
349
|
+
dunningNotices: n(e)
|
|
258
350
|
});
|
|
259
|
-
export {
|
|
351
|
+
export { $ as dataStoreStores, q as dataStoreSubscriptionStore, Z as dataStoreUsageStore, j as isLive, N as isStaleStatusEvent, k as isTerminal, Q as memoryStores, M as selectCurrentSubscription };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { BillingError as e } from "./types.js";
|
|
2
|
+
import { assertInvoicePaymentObservation as t } from "./invoicePaymentObservation.js";
|
|
3
|
+
import { invoiceSnapshotError as n } from "./invoiceSnapshot.js";
|
|
4
|
+
import { stripeCall as r } from "./stripeClient.js";
|
|
5
|
+
import { Effect as i } from "effect";
|
|
6
|
+
// src/stripeInvoicePayments.ts
|
|
7
|
+
var a = (e) => typeof e == "string" ? e : e?.id ?? null, o = () => {
|
|
8
|
+
throw n("The invoice payment graph could not be confirmed.");
|
|
9
|
+
}, s = (e) => {
|
|
10
|
+
if (e === null) return null;
|
|
11
|
+
if (!Number.isSafeInteger(e) || e < 0 || !Number.isSafeInteger(e * 1e3)) return o();
|
|
12
|
+
let t = /* @__PURE__ */ new Date(e * 1e3);
|
|
13
|
+
return Number.isFinite(t.getTime()) ? t : o();
|
|
14
|
+
}, c = (e, o) => {
|
|
15
|
+
let c = Object.freeze({ ...o });
|
|
16
|
+
return i.gen(function* () {
|
|
17
|
+
let o = [], l = /* @__PURE__ */ new Set(), f, p = !1;
|
|
18
|
+
for (let t = 0; t < 100; t++) {
|
|
19
|
+
let t = yield* r("stripe.invoicePayments.list", () => e.invoicePayments.list({
|
|
20
|
+
invoice: c.providerInvoiceId,
|
|
21
|
+
limit: 100,
|
|
22
|
+
...f === void 0 ? {} : { starting_after: f }
|
|
23
|
+
}));
|
|
24
|
+
if (!Array.isArray(t.data) || t.data.length > 100 || typeof t.has_more != "boolean" || (t.has_more || f !== void 0) && t.data.length === 0) return yield* i.fail(n("The invoice payment page is incomplete."));
|
|
25
|
+
for (let f of t.data) {
|
|
26
|
+
if (!f.id || l.has(f.id) || a(f.invoice) !== c.providerInvoiceId || f.livemode !== c.livemode || f.currency !== c.currency || ![
|
|
27
|
+
"open",
|
|
28
|
+
"paid",
|
|
29
|
+
"canceled"
|
|
30
|
+
].includes(f.status)) return yield* i.fail(n("The invoice payment belongs to a different source or has an invalid state."));
|
|
31
|
+
l.add(f.id);
|
|
32
|
+
let t = f.payment;
|
|
33
|
+
if (!t || typeof t.type != "string" || !t.type) return yield* i.fail(n("The invoice payment source is unavailable."));
|
|
34
|
+
let p = t.type === "payment_intent" ? a(t.payment_intent) : t.type === "charge" ? a(t.charge) : t.type === "payment_record" ? a(t.payment_record) : null;
|
|
35
|
+
if ([
|
|
36
|
+
"payment_intent",
|
|
37
|
+
"charge",
|
|
38
|
+
"payment_record"
|
|
39
|
+
].includes(t.type) && p === null) return yield* i.fail(n("The invoice payment source identity is missing."));
|
|
40
|
+
let m = null;
|
|
41
|
+
if (f.status === "paid" && t.type === "payment_intent" && p !== null) {
|
|
42
|
+
let t = yield* r("stripe.paymentIntents.retrieve", () => e.paymentIntents.retrieve(p));
|
|
43
|
+
if (t.id !== p || a(t.customer) !== c.providerCustomerId || t.currency !== c.currency || t.livemode !== c.livemode) return yield* i.fail(n("The payment intent belongs to a different customer or mode."));
|
|
44
|
+
let o = a(t.latest_charge);
|
|
45
|
+
if (o !== null) {
|
|
46
|
+
m = {
|
|
47
|
+
...yield* u(e, c, o, p),
|
|
48
|
+
paymentIntentStatus: t.status,
|
|
49
|
+
paymentIntentReceivedMinor: t.amount_received
|
|
50
|
+
};
|
|
51
|
+
let s = yield* r("stripe.paymentIntents.retrieve", () => e.paymentIntents.retrieve(p));
|
|
52
|
+
if (s.id !== t.id || a(s.customer) !== a(t.customer) || s.currency !== t.currency || s.livemode !== t.livemode || s.status !== t.status || s.amount_received !== t.amount_received || a(s.latest_charge) !== o) return yield* i.fail(n("The payment intent changed during invoice observation.", !0));
|
|
53
|
+
}
|
|
54
|
+
} else f.status === "paid" && t.type === "charge" && p !== null && (m = yield* u(e, c, p, null));
|
|
55
|
+
let h = yield* d(() => s(f.status_transitions.paid_at));
|
|
56
|
+
o.push({
|
|
57
|
+
providerInvoicePaymentId: f.id,
|
|
58
|
+
sourceType: t.type,
|
|
59
|
+
sourceId: p,
|
|
60
|
+
status: f.status,
|
|
61
|
+
amountPaidMinor: f.amount_paid,
|
|
62
|
+
paidAt: h,
|
|
63
|
+
cash: m
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (!t.has_more) {
|
|
67
|
+
p = !0;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
f = t.data.at(-1)?.id;
|
|
71
|
+
}
|
|
72
|
+
if (!p) return yield* i.fail(n("The complete payment graph exceeds its observation bound."));
|
|
73
|
+
let m = {
|
|
74
|
+
protocol: "voltro.invoice-payments.v1",
|
|
75
|
+
...c,
|
|
76
|
+
observedAt: /* @__PURE__ */ new Date(),
|
|
77
|
+
payments: o
|
|
78
|
+
};
|
|
79
|
+
return yield* d(() => t(m)), m;
|
|
80
|
+
});
|
|
81
|
+
}, l = (e, t, n, r) => e.id !== n || a(e.customer) !== t.providerCustomerId || e.currency !== t.currency || e.livemode !== t.livemode || a(e.payment_intent) !== r ? o() : {
|
|
82
|
+
providerChargeId: e.id,
|
|
83
|
+
providerPaymentIntentId: r,
|
|
84
|
+
providerCustomerId: t.providerCustomerId,
|
|
85
|
+
livemode: e.livemode,
|
|
86
|
+
currency: e.currency,
|
|
87
|
+
amountMinor: e.amount,
|
|
88
|
+
capturedMinor: e.amount_captured,
|
|
89
|
+
refundedMinor: e.amount_refunded,
|
|
90
|
+
paid: e.paid,
|
|
91
|
+
captured: e.captured,
|
|
92
|
+
disputed: e.disputed
|
|
93
|
+
}, u = (e, t, o, s) => i.gen(function* () {
|
|
94
|
+
let c = yield* r("stripe.charges.retrieve", () => e.charges.retrieve(o)), u = yield* d(() => l(c, t, o, s)), f = [], p = /* @__PURE__ */ new Set(), m, h = !1;
|
|
95
|
+
for (let s = 0; s < 100; s++) {
|
|
96
|
+
let s = yield* r("stripe.refunds.list", () => e.refunds.list({
|
|
97
|
+
charge: o,
|
|
98
|
+
limit: 100,
|
|
99
|
+
...m === void 0 ? {} : { starting_after: m }
|
|
100
|
+
}));
|
|
101
|
+
if (!Array.isArray(s.data) || s.data.length > 100 || typeof s.has_more != "boolean" || (s.has_more || m !== void 0) && s.data.length === 0) return yield* i.fail(n("The refund page is incomplete."));
|
|
102
|
+
for (let e of s.data) {
|
|
103
|
+
if (!e.id || p.has(e.id) || a(e.charge) !== o || e.currency !== t.currency) return yield* i.fail(n("The refund belongs to a different charge or repeats its identity."));
|
|
104
|
+
p.add(e.id), f.push({
|
|
105
|
+
providerRefundId: e.id,
|
|
106
|
+
amountMinor: e.amount,
|
|
107
|
+
currency: e.currency,
|
|
108
|
+
status: e.status ?? null
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
if (!s.has_more) {
|
|
112
|
+
h = !0;
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
m = s.data.at(-1)?.id;
|
|
116
|
+
}
|
|
117
|
+
if (!h) return yield* i.fail(n("The complete payment graph exceeds its observation bound."));
|
|
118
|
+
let g = yield* r("stripe.charges.retrieve", () => e.charges.retrieve(o)), _ = yield* d(() => l(g, t, o, s));
|
|
119
|
+
return JSON.stringify(u) === JSON.stringify(_) ? {
|
|
120
|
+
..._,
|
|
121
|
+
refunds: f,
|
|
122
|
+
paymentIntentStatus: null,
|
|
123
|
+
paymentIntentReceivedMinor: null
|
|
124
|
+
} : yield* i.fail(n("The payment changed during observation; retry the complete invoice read.", !0));
|
|
125
|
+
}), d = (t) => i.try({
|
|
126
|
+
try: t,
|
|
127
|
+
catch: (t) => t instanceof e ? t : n("The invoice payment observation is invalid.")
|
|
128
|
+
});
|
|
129
|
+
export { c as stripeInvoicePayments };
|