@voltro/plugin-billing 0.90.0 → 0.91.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/CHANGELOG.md +131 -0
- package/THIRD-PARTY-NOTICES.md +22 -646
- package/dist/catalog.js +52 -52
- package/dist/index.d.ts +4 -1
- package/dist/index.js +251 -251
- package/dist/invoiceHistoryJournal.js +108 -108
- package/dist/invoiceHistoryProgress.js +107 -107
- package/dist/invoiceHistoryWork.js +103 -103
- package/dist/invoiceSnapshot.js +11 -11
- package/dist/invoiceStore.js +43 -43
- package/dist/paidInvoiceCoverage.js +13 -13
- package/dist/providers.js +35 -35
- package/dist/service.js +592 -592
- package/dist/stores.js +89 -89
- package/dist/stripeIdentity.js +17 -17
- package/dist/stripeInvoiceHistory.js +21 -21
- package/dist/stripeInvoicePayments.js +41 -41
- package/dist/stripeInvoiceSnapshot.js +17 -17
- package/dist/stripeProvider.js +74 -74
- package/dist/stripeRest.js +10 -10
- package/package.json +9 -9
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { dataStoreInvoiceStore as
|
|
3
|
-
import { captureProviderAccount as
|
|
4
|
-
import {
|
|
1
|
+
import { BILLING_INVOICE_HISTORY_REFERENCES_TABLE as e, invoiceHistoryJournalInTransaction as t } from "./invoiceHistoryJournal.js";
|
|
2
|
+
import { dataStoreInvoiceStore as n } from "./invoiceStore.js";
|
|
3
|
+
import { captureProviderAccount as r, exactProviderIdentifier as i, sameProviderAccount as a } from "./providerIdentity.js";
|
|
4
|
+
import { BillingError as o } from "./types.js";
|
|
5
5
|
import { Effect as s, Schema as c } from "effect";
|
|
6
6
|
import { and as l, eq as u, gt as d, id as f, inSet as p, integer as m, lte as h, notInSubquery as g, table as _, text as v, timestamp as y } from "@voltro/database";
|
|
7
7
|
import { dataStoreIdempotencyStore as b } from "@voltro/runtime";
|
|
@@ -32,12 +32,12 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
32
32
|
"walkId",
|
|
33
33
|
"state",
|
|
34
34
|
"nextAttemptAt"
|
|
35
|
-
]).index("byBillingInvoiceHistoryWorkTenant", ["tenantId"]), k = 2147483647, A = 6e4, j = 864e5, M = E("billing.invoice-history-work", []), N = (
|
|
35
|
+
]).index("byBillingInvoiceHistoryWorkTenant", ["tenantId"]), k = 2147483647, A = 6e4, j = 864e5, M = E("billing.invoice-history-work", []), N = (e = "Invoice reference work remains unconfirmed and requires reconciliation.", t = !0) => new o({
|
|
36
36
|
source: "invoiceHistoryWork",
|
|
37
|
-
message:
|
|
38
|
-
transient:
|
|
37
|
+
message: e,
|
|
38
|
+
transient: t
|
|
39
39
|
}), P = (e, t = 191) => {
|
|
40
|
-
if (!
|
|
40
|
+
if (!i(e) || e.length > t || !e.isWellFormed()) throw N();
|
|
41
41
|
return e;
|
|
42
42
|
}, F = (e, t = 0) => {
|
|
43
43
|
if (typeof e != "number" || !Number.isInteger(e) || e < t || e > k) throw N();
|
|
@@ -46,9 +46,9 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
46
46
|
let t = e instanceof Date ? new Date(e) : new Date(typeof e == "string" ? e : NaN);
|
|
47
47
|
if (!Number.isFinite(t.getTime())) throw N();
|
|
48
48
|
return t;
|
|
49
|
-
}, L = (e, t) => I(new Date(e.getTime() + t)), R = (
|
|
50
|
-
try:
|
|
51
|
-
catch: (
|
|
49
|
+
}, L = (e, t) => I(new Date(e.getTime() + t)), R = (e) => s.tryPromise({
|
|
50
|
+
try: e,
|
|
51
|
+
catch: (e) => e instanceof o ? e : N()
|
|
52
52
|
}), z = (e, t = !1) => ({
|
|
53
53
|
table: D,
|
|
54
54
|
predicate: u("referenceId", e),
|
|
@@ -75,20 +75,20 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
75
75
|
providerInvoiceId: e.providerInvoiceId
|
|
76
76
|
}), H = (e, t) => {
|
|
77
77
|
if (e.livemode !== 0 && e.livemode !== 1) throw N();
|
|
78
|
-
let
|
|
78
|
+
let n = r({
|
|
79
79
|
providerAccountId: P(e.providerAccountId),
|
|
80
80
|
livemode: e.livemode === 1
|
|
81
81
|
});
|
|
82
|
-
if (e.tenantId !== t.tenantId || e.provider !== t.provider || e.providerCustomerId !== t.providerCustomerId || e.custodyId !== t.custodyId || e.walkId !== t.walkId || e.referenceId !== t.id || e.providerInvoiceId !== t.providerInvoiceId || !
|
|
83
|
-
let
|
|
84
|
-
if (
|
|
82
|
+
if (e.tenantId !== t.tenantId || e.provider !== t.provider || e.providerCustomerId !== t.providerCustomerId || e.custodyId !== t.custodyId || e.walkId !== t.walkId || e.referenceId !== t.id || e.providerInvoiceId !== t.providerInvoiceId || !a(n, t.account)) throw N();
|
|
83
|
+
let i = e.state, o = e.lastOutcome, s = e.activeClaimId;
|
|
84
|
+
if (i !== "queued" && i !== "working" && i !== "retry" && i !== "deferred" && i !== "observed" || o !== "unconfirmed" && o !== "open" && o !== "observed") throw N();
|
|
85
85
|
let c = e.observationId === null ? null : P(e.observationId), l = e.observationRevision === null ? null : F(e.observationRevision, 1);
|
|
86
|
-
if (c === null != (l === null) ||
|
|
86
|
+
if (c === null != (l === null) || i === "working" != (s !== null) || (i === "observed" || i === "deferred") && c === null || i === "observed" && o !== "observed" || i === "deferred" && o !== "open") throw N();
|
|
87
87
|
let u = {
|
|
88
88
|
id: P(e.id),
|
|
89
89
|
tenantId: t.tenantId,
|
|
90
90
|
provider: t.provider,
|
|
91
|
-
account:
|
|
91
|
+
account: n,
|
|
92
92
|
providerCustomerId: t.providerCustomerId,
|
|
93
93
|
custodyId: t.custodyId,
|
|
94
94
|
walkId: t.walkId,
|
|
@@ -97,7 +97,7 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
97
97
|
revision: F(e.revision, 1),
|
|
98
98
|
attempt: F(e.attempt),
|
|
99
99
|
executions: F(e.executions),
|
|
100
|
-
state:
|
|
100
|
+
state: i,
|
|
101
101
|
activeClaimId: s === null ? null : P(s),
|
|
102
102
|
nextAttemptAt: I(e.nextAttemptAt),
|
|
103
103
|
lastOutcome: o,
|
|
@@ -105,7 +105,7 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
105
105
|
observationRevision: l,
|
|
106
106
|
updatedAt: I(e.updatedAt)
|
|
107
107
|
};
|
|
108
|
-
if (
|
|
108
|
+
if (i === "working" && (u.attempt === 0 || u.executions === 0)) throw N();
|
|
109
109
|
return u;
|
|
110
110
|
}, U = (e) => "billing.invoice-history-work.v1:" + e.walkId, W = (e, t) => e.id + ":" + t, G = async (e, t) => (await T(c.Unknown, [
|
|
111
111
|
e.id,
|
|
@@ -131,72 +131,72 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
131
131
|
work: Object.freeze(t),
|
|
132
132
|
claim: n
|
|
133
133
|
});
|
|
134
|
-
}, q = async (e,
|
|
135
|
-
let r =
|
|
134
|
+
}, q = async (e, t) => {
|
|
135
|
+
let r = n(e), i = V(t), a = await s.runPromise(r.currentPaymentEpoch(i, { forUpdate: !0 }));
|
|
136
136
|
if (a === null) return null;
|
|
137
137
|
let o = await s.runPromise(r.get(i, { forUpdate: !0 }));
|
|
138
138
|
return o?.paymentEpoch?.headId === a.headId && o.paymentEpoch.epoch === a.epoch ? o : null;
|
|
139
|
-
}, J = (
|
|
140
|
-
let
|
|
141
|
-
let
|
|
142
|
-
if (
|
|
143
|
-
return
|
|
144
|
-
},
|
|
139
|
+
}, J = (n) => {
|
|
140
|
+
let r = b(n), i = async (e, n, r) => {
|
|
141
|
+
let i = await s.runPromise(t(e).reference(n, r));
|
|
142
|
+
if (i === null) throw N("The original owned invoice history reference is missing.", !1);
|
|
143
|
+
return i;
|
|
144
|
+
}, a = async (e, t, n = !1) => {
|
|
145
145
|
let r = await e.query(z(t.id, n));
|
|
146
146
|
if (r.length > 1) throw N();
|
|
147
147
|
return r[0] ? H(r[0], t) : null;
|
|
148
|
-
},
|
|
148
|
+
}, o = async (e, t, n, r, i) => {
|
|
149
149
|
if (n.revision >= k) throw N("The invoice reference work revision is exhausted.", !1);
|
|
150
150
|
if (await e.updateMany("_voltro_billing_invoice_history_work", {
|
|
151
|
-
...
|
|
151
|
+
...r,
|
|
152
152
|
revision: n.revision + 1,
|
|
153
|
-
updatedAt:
|
|
153
|
+
updatedAt: i
|
|
154
154
|
}, { where: l(u("id", n.id), u("referenceId", t.id), u("revision", n.revision), u("custodyId", t.custodyId), u("walkId", t.walkId)) }) !== 1) throw N();
|
|
155
|
-
let o = await
|
|
156
|
-
if (o === null || o.id !== n.id || o.revision !== n.revision + 1 || o.updatedAt.getTime() !==
|
|
157
|
-
for (let [e, t] of Object.entries(
|
|
155
|
+
let o = await a(e, t, !0);
|
|
156
|
+
if (o === null || o.id !== n.id || o.revision !== n.revision + 1 || o.updatedAt.getTime() !== i.getTime()) throw N();
|
|
157
|
+
for (let [e, t] of Object.entries(r)) {
|
|
158
158
|
let n = Object.getOwnPropertyDescriptor(o, e)?.value;
|
|
159
159
|
if (t instanceof Date ? !(n instanceof Date) || n.getTime() !== t.getTime() : n !== t) throw N("The invoice work acknowledgement was not read back exactly.");
|
|
160
160
|
}
|
|
161
161
|
return o;
|
|
162
162
|
}, c = async (e, t, n) => {
|
|
163
|
-
let
|
|
164
|
-
return
|
|
163
|
+
let r = await a(e, t, !0), i = n.work;
|
|
164
|
+
return r === null || r.id !== i.id || r.referenceId !== i.referenceId || r.state !== "working" || r.attempt !== i.attempt || r.activeClaimId !== n.claim.id || n.claim.inputHash !== await G(r, r.attempt) ? null : r;
|
|
165
165
|
};
|
|
166
166
|
return {
|
|
167
|
-
pending: (
|
|
168
|
-
let f = structuredClone(
|
|
167
|
+
pending: (r, a, o, c) => R(async () => {
|
|
168
|
+
let f = structuredClone(r), m = I(o), _ = c?.limit ?? 25;
|
|
169
169
|
if (![
|
|
170
170
|
"unprepared",
|
|
171
171
|
"ready",
|
|
172
172
|
"working",
|
|
173
173
|
"observed"
|
|
174
|
-
].includes(
|
|
174
|
+
].includes(a) || !Number.isInteger(_) || _ < 1 || _ > 100) throw N();
|
|
175
175
|
let v = c?.cursor === void 0 ? null : {
|
|
176
176
|
lane: c.cursor.lane,
|
|
177
177
|
walkId: P(c.cursor.walkId),
|
|
178
178
|
afterId: P(c.cursor.afterId)
|
|
179
179
|
};
|
|
180
|
-
if (v !== null && v.lane !==
|
|
181
|
-
return
|
|
182
|
-
let
|
|
183
|
-
if (
|
|
184
|
-
let c = await s.runPromise(
|
|
185
|
-
if (c.id !==
|
|
186
|
-
let y = l(u("tenantId", c.tenantId), u("provider", c.provider), u("providerAccountId", c.account.providerAccountId), u("livemode", +!!c.account.livemode), u("providerCustomerId", c.providerCustomerId), u("custodyId", c.custodyId), u("walkId", c.id)), b =
|
|
180
|
+
if (v !== null && v.lane !== a) throw N("Invoice work cursors belong to one selection lane.", !1);
|
|
181
|
+
return n.transactional(async (n) => {
|
|
182
|
+
let r = t(n), o = await s.runPromise(r.read(f));
|
|
183
|
+
if (o === null) throw N("No retained owned invoice history walk exists.");
|
|
184
|
+
let c = await s.runPromise(r.begin(f));
|
|
185
|
+
if (c.id !== o.id || v !== null && v.walkId !== c.id) throw N("Invoice work cursor differs from the retained walk.", !1);
|
|
186
|
+
let y = l(u("tenantId", c.tenantId), u("provider", c.provider), u("providerAccountId", c.account.providerAccountId), u("livemode", +!!c.account.livemode), u("providerCustomerId", c.providerCustomerId), u("custodyId", c.custodyId), u("walkId", c.id)), b = a === "unprepared", x = a === "working" ? ["working"] : a === "observed" ? ["observed"] : [
|
|
187
187
|
"queued",
|
|
188
188
|
"retry",
|
|
189
189
|
"deferred"
|
|
190
|
-
], S = b ?
|
|
190
|
+
], S = b ? e : D;
|
|
191
191
|
if (v !== null) {
|
|
192
|
-
let
|
|
192
|
+
let e = await n.query({
|
|
193
193
|
...z(v.afterId),
|
|
194
194
|
table: S,
|
|
195
195
|
predicate: l(y, u("id", v.afterId))
|
|
196
196
|
});
|
|
197
|
-
if (
|
|
198
|
-
let
|
|
199
|
-
b || H(
|
|
197
|
+
if (e.length !== 1 || e[0]?.id !== v.afterId) throw N("Invoice work cursor is no longer retained.", !0);
|
|
198
|
+
let t = await i(n, f, b ? v.afterId : P(e[0]?.referenceId));
|
|
199
|
+
b || H(e[0], t);
|
|
200
200
|
}
|
|
201
201
|
let C = [y];
|
|
202
202
|
v !== null && C.push(d("id", v.afterId)), b ? C.push(h("pageOrdinal", c.pageOrdinal), g("id", {
|
|
@@ -205,7 +205,7 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
205
205
|
projection: ["referenceId"],
|
|
206
206
|
take: void 0
|
|
207
207
|
})) : C.push(h("nextAttemptAt", m), p("state", x));
|
|
208
|
-
let w = await
|
|
208
|
+
let w = await n.query({
|
|
209
209
|
...z(c.id),
|
|
210
210
|
table: S,
|
|
211
211
|
predicate: l(...C),
|
|
@@ -216,8 +216,8 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
216
216
|
take: _ + 1
|
|
217
217
|
});
|
|
218
218
|
if (w.length > _ + 1) throw N();
|
|
219
|
-
let T = await s.runPromise(
|
|
220
|
-
if (b && w.length > 0 && (await
|
|
219
|
+
let T = await s.runPromise(r.referencesByIds(f, w.map((e) => P(b ? e.id : e.referenceId))));
|
|
220
|
+
if (b && w.length > 0 && (await n.query({
|
|
221
221
|
...z(c.id),
|
|
222
222
|
predicate: p("referenceId", T.map((e) => e.id)),
|
|
223
223
|
take: 1
|
|
@@ -239,7 +239,7 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
239
239
|
return {
|
|
240
240
|
entries: O.slice(0, _),
|
|
241
241
|
nextCursor: w.length > _ ? {
|
|
242
|
-
lane:
|
|
242
|
+
lane: a,
|
|
243
243
|
walkId: c.id,
|
|
244
244
|
afterId: P(w[_ - 1]?.id)
|
|
245
245
|
} : null,
|
|
@@ -247,10 +247,10 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
247
247
|
};
|
|
248
248
|
});
|
|
249
249
|
}),
|
|
250
|
-
claim: (
|
|
251
|
-
let s = structuredClone(
|
|
252
|
-
return
|
|
253
|
-
let t = await
|
|
250
|
+
claim: (e, t, r) => R(async () => {
|
|
251
|
+
let s = structuredClone(e), c = P(t), l = I(r);
|
|
252
|
+
return n.transactional(async (e) => {
|
|
253
|
+
let t = await i(e, s, c);
|
|
254
254
|
await e.insertIgnore(D, {
|
|
255
255
|
id: "bihwork_" + globalThis.crypto.randomUUID(),
|
|
256
256
|
...B(t),
|
|
@@ -265,89 +265,89 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
265
265
|
observationRevision: null,
|
|
266
266
|
updatedAt: l
|
|
267
267
|
}, { conflictColumns: ["referenceId"] });
|
|
268
|
-
let
|
|
269
|
-
if (
|
|
270
|
-
if (
|
|
271
|
-
let
|
|
272
|
-
if (
|
|
268
|
+
let n = await a(e, t, !0);
|
|
269
|
+
if (n === null) throw N();
|
|
270
|
+
if (n.state === "observed") {
|
|
271
|
+
let r = await q(e, t);
|
|
272
|
+
if (r !== null && r.id === n.observationId && r.revision >= n.observationRevision && r.snapshot.status !== "open") return {
|
|
273
273
|
kind: "observed",
|
|
274
|
-
work:
|
|
274
|
+
work: n
|
|
275
275
|
};
|
|
276
|
-
|
|
276
|
+
n = await o(e, t, n, {
|
|
277
277
|
state: "queued",
|
|
278
278
|
activeClaimId: null,
|
|
279
279
|
lastOutcome: "unconfirmed",
|
|
280
280
|
nextAttemptAt: l
|
|
281
281
|
}, l);
|
|
282
282
|
}
|
|
283
|
-
if (
|
|
283
|
+
if (n.state !== "working" && n.nextAttemptAt.getTime() > l.getTime()) return {
|
|
284
284
|
kind: "busy",
|
|
285
|
-
work:
|
|
285
|
+
work: n
|
|
286
286
|
};
|
|
287
|
-
let
|
|
288
|
-
if (
|
|
289
|
-
let u = await G(
|
|
290
|
-
if (d.kind === "replay") return
|
|
291
|
-
attempt:
|
|
287
|
+
let r = n.state === "working" ? n.attempt : n.attempt + 1;
|
|
288
|
+
if (r > k || n.executions >= k) throw N("The invoice reference attempt counter is exhausted.", !1);
|
|
289
|
+
let u = await G(n, r), d = await S(b(e), U(n), W(n, r), u, j, l.getTime());
|
|
290
|
+
if (d.kind === "replay") return n = await o(e, t, n, {
|
|
291
|
+
attempt: r,
|
|
292
292
|
state: "queued",
|
|
293
293
|
activeClaimId: null,
|
|
294
294
|
nextAttemptAt: l,
|
|
295
295
|
lastOutcome: "unconfirmed"
|
|
296
296
|
}, l), {
|
|
297
297
|
kind: "busy",
|
|
298
|
-
work:
|
|
298
|
+
work: n
|
|
299
299
|
};
|
|
300
300
|
if (d.kind === "conflict") return {
|
|
301
301
|
kind: "busy",
|
|
302
|
-
work:
|
|
302
|
+
work: n
|
|
303
303
|
};
|
|
304
304
|
if (d.kind !== "fresh") throw N("The retained invoice attempt has a different input binding.", !1);
|
|
305
|
-
return
|
|
306
|
-
attempt:
|
|
307
|
-
executions:
|
|
305
|
+
return n = await o(e, t, n, {
|
|
306
|
+
attempt: r,
|
|
307
|
+
executions: n.executions + 1,
|
|
308
308
|
state: "working",
|
|
309
309
|
activeClaimId: d.claim.id,
|
|
310
310
|
nextAttemptAt: L(l, x),
|
|
311
311
|
lastOutcome: "unconfirmed"
|
|
312
312
|
}, l), {
|
|
313
313
|
kind: "claimed",
|
|
314
|
-
work:
|
|
314
|
+
work: n,
|
|
315
315
|
attempt: Object.freeze({
|
|
316
|
-
work:
|
|
316
|
+
work: n,
|
|
317
317
|
claim: d.claim
|
|
318
318
|
})
|
|
319
319
|
};
|
|
320
320
|
});
|
|
321
321
|
}),
|
|
322
|
-
observed: (t, r, a
|
|
323
|
-
let s = structuredClone(
|
|
324
|
-
return
|
|
325
|
-
let t = await
|
|
326
|
-
if (
|
|
327
|
-
let
|
|
328
|
-
if (
|
|
329
|
-
let
|
|
322
|
+
observed: (e, t, r, a) => R(async () => {
|
|
323
|
+
let s = structuredClone(e), l = K(t), u = I(a), d = P(r.id), f = F(r.revision, 1);
|
|
324
|
+
return n.transactional(async (e) => {
|
|
325
|
+
let t = await i(e, s, l.work.referenceId), n = await c(e, t, l);
|
|
326
|
+
if (n === null) return null;
|
|
327
|
+
let r = await q(e, t);
|
|
328
|
+
if (r === null || r.id !== d || r.revision < f) throw N("The qualified native invoice observation is missing or older than the offered acknowledgement.");
|
|
329
|
+
let a = r.snapshot.status === "open";
|
|
330
330
|
return await C(b(e), l.claim, {
|
|
331
|
-
status:
|
|
331
|
+
status: a ? 202 : 204,
|
|
332
332
|
body: null
|
|
333
|
-
}, u.getTime()) ?
|
|
334
|
-
state:
|
|
333
|
+
}, u.getTime()) ? o(e, t, n, {
|
|
334
|
+
state: a ? "deferred" : "observed",
|
|
335
335
|
activeClaimId: null,
|
|
336
|
-
observationId:
|
|
337
|
-
observationRevision:
|
|
338
|
-
lastOutcome:
|
|
339
|
-
nextAttemptAt:
|
|
336
|
+
observationId: r.id,
|
|
337
|
+
observationRevision: r.revision,
|
|
338
|
+
lastOutcome: a ? "open" : "observed",
|
|
339
|
+
nextAttemptAt: a ? L(u, A) : u
|
|
340
340
|
}, u) : null;
|
|
341
341
|
});
|
|
342
342
|
}),
|
|
343
|
-
failed: (t, r
|
|
344
|
-
let
|
|
345
|
-
return
|
|
346
|
-
let t = await
|
|
347
|
-
return
|
|
343
|
+
failed: (e, t, r) => R(async () => {
|
|
344
|
+
let a = structuredClone(e), s = K(t), l = I(r);
|
|
345
|
+
return n.transactional(async (e) => {
|
|
346
|
+
let t = await i(e, a, s.work.referenceId), n = await c(e, t, s);
|
|
347
|
+
return n === null || !await C(b(e), s.claim, {
|
|
348
348
|
status: 503,
|
|
349
349
|
body: null
|
|
350
|
-
}, l.getTime()) ? null :
|
|
350
|
+
}, l.getTime()) ? null : o(e, t, n, {
|
|
351
351
|
state: "retry",
|
|
352
352
|
activeClaimId: null,
|
|
353
353
|
lastOutcome: "unconfirmed",
|
|
@@ -355,9 +355,9 @@ var D = "_voltro_billing_invoice_history_work", O = _(D, {
|
|
|
355
355
|
}, l);
|
|
356
356
|
});
|
|
357
357
|
}),
|
|
358
|
-
hold: (e,
|
|
359
|
-
let
|
|
360
|
-
return w(
|
|
358
|
+
hold: (e, t) => {
|
|
359
|
+
let n = K(e);
|
|
360
|
+
return w(r, n.claim, x, t);
|
|
361
361
|
}
|
|
362
362
|
};
|
|
363
363
|
};
|
package/dist/invoiceSnapshot.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { InvoicePaymentObservationSchema as e, assertInvoicePaymentObservation as t } from "./invoicePaymentObservation.js";
|
|
2
|
+
import { BillingError as n } from "./types.js";
|
|
3
3
|
import { Schema as r } from "effect";
|
|
4
4
|
// src/invoiceSnapshot.ts
|
|
5
|
-
var i = (
|
|
5
|
+
var i = (e, t = !1) => new n({
|
|
6
6
|
source: "invoiceSnapshot",
|
|
7
|
-
message:
|
|
8
|
-
transient:
|
|
7
|
+
message: e,
|
|
8
|
+
transient: t
|
|
9
9
|
}), a = (e) => typeof e == "string" && e.length > 0 && e.trim() === e, o = (e) => e === null || a(e), s = (e) => e instanceof Date && Number.isSafeInteger(e.getTime()), c = (e) => Number.isSafeInteger(e) && e >= 0, l = r.Struct({
|
|
10
|
-
paymentObservation: r.optionalKey(r.NullOr(
|
|
10
|
+
paymentObservation: r.optionalKey(r.NullOr(e)),
|
|
11
11
|
providerInvoiceId: r.String,
|
|
12
12
|
providerCustomerId: r.String,
|
|
13
13
|
providerSubscriptionId: r.NullOr(r.String),
|
|
@@ -62,14 +62,14 @@ var i = (t, n = !1) => new e({
|
|
|
62
62
|
"void"
|
|
63
63
|
].includes(e.status) || typeof e.livemode != "boolean" || !/^[a-z]{3}$/.test(e.currency) || !c(e.amountDueMinor) || !c(e.amountPaidMinor) || !c(e.amountRemainingMinor) || !s(e.finalizedAt) || !s(e.observedAt) || e.paidAt !== null && (!s(e.paidAt) || e.paidAt < e.finalizedAt) || e.status === "paid" && e.paidAt === null || !Array.isArray(e.lines) || e.lines.length > 1e4) throw i("A complete finalized invoice observation is required.");
|
|
64
64
|
if (e.paymentObservation !== null) try {
|
|
65
|
-
if (
|
|
65
|
+
if (t(e.paymentObservation), e.paymentObservation.providerInvoiceId !== e.providerInvoiceId || e.paymentObservation.providerCustomerId !== e.providerCustomerId || e.paymentObservation.currency !== e.currency || e.paymentObservation.livemode !== e.livemode) throw TypeError("Invoice payment identity mismatch.");
|
|
66
66
|
} catch {
|
|
67
67
|
throw i("Invoice payment observations must match the complete invoice ownership.");
|
|
68
68
|
}
|
|
69
|
-
let
|
|
70
|
-
for (let
|
|
71
|
-
if (!a(
|
|
72
|
-
|
|
69
|
+
let n = /* @__PURE__ */ new Set();
|
|
70
|
+
for (let t of e.lines) {
|
|
71
|
+
if (!a(t.providerLineId) || n.has(t.providerLineId) || !o(t.providerSubscriptionId) || !o(t.providerSubscriptionItemId) || !o(t.providerPriceId) || !Number.isSafeInteger(t.amountMinor) || t.currency !== e.currency || t.quantityDecimal !== null && (typeof t.quantityDecimal != "string" || !/^\d+(?:\.\d{1,12})?$/.test(t.quantityDecimal)) || !s(t.periodStart) || !s(t.periodEnd) || t.periodEnd < t.periodStart || t.proration !== null && typeof t.proration != "boolean") throw i("Invoice lines must retain exact identities, money and periods.");
|
|
72
|
+
n.add(t.providerLineId);
|
|
73
73
|
}
|
|
74
74
|
};
|
|
75
75
|
export { p as assertInvoiceSnapshot, f as decodeInvoiceSnapshot, d as encodeInvoiceSnapshot, i as invoiceSnapshotError };
|
package/dist/invoiceStore.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { captureInvoicePaymentEpoch as e, currentInvoicePaymentEpoch as t, invalidateInvoicePayments as n, withInvoicePaymentEpoch as r } from "./invoicePaymentEpoch.js";
|
|
2
|
+
import { decodeInvoiceSnapshot as i, encodeInvoiceSnapshot as a, invoiceSnapshotError as o } from "./invoiceSnapshot.js";
|
|
3
|
+
import { BillingError as s } from "./types.js";
|
|
4
4
|
import { Effect as c } from "effect";
|
|
5
5
|
import { and as l, eq as u, id as d, integer as f, json as p, table as m, text as h, timestamp as g } from "@voltro/database";
|
|
6
6
|
import "@voltro/runtime";
|
|
@@ -42,9 +42,9 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
|
|
|
42
42
|
take: t,
|
|
43
43
|
skip: void 0,
|
|
44
44
|
projection: void 0
|
|
45
|
-
}), C = (
|
|
46
|
-
try:
|
|
47
|
-
catch: (
|
|
45
|
+
}), C = (e) => c.tryPromise({
|
|
46
|
+
try: e,
|
|
47
|
+
catch: (e) => e instanceof s ? e : o("The native invoice observation could not be persisted or read.", !0)
|
|
48
48
|
}), w = (e) => {
|
|
49
49
|
if (![
|
|
50
50
|
e.tenantId,
|
|
@@ -52,7 +52,7 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
|
|
|
52
52
|
e.providerAccountId,
|
|
53
53
|
e.providerCustomerId,
|
|
54
54
|
e.providerInvoiceId
|
|
55
|
-
].every(x) || typeof e.livemode != "boolean") throw
|
|
55
|
+
].every(x) || typeof e.livemode != "boolean") throw o("A valid customer-bound invoice identity is required.");
|
|
56
56
|
return Object.freeze({
|
|
57
57
|
tenantId: e.tenantId,
|
|
58
58
|
provider: e.provider,
|
|
@@ -71,23 +71,23 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
|
|
|
71
71
|
])));
|
|
72
72
|
return [...new Uint8Array(t)].map((e) => e.toString(16).padStart(2, "0")).join("");
|
|
73
73
|
}, E = (e, t) => e.tenantId === t.tenantId && e.provider === t.provider && e.providerAccountId === t.providerAccountId && e.livemode === t.livemode && e.providerCustomerId === t.providerCustomerId && e.providerInvoiceId === t.providerInvoiceId, D = (e, t) => {
|
|
74
|
-
if (!E(e, t)) throw
|
|
75
|
-
}, O = (
|
|
76
|
-
let n =
|
|
77
|
-
if (!x(n) || !x(r) || !x(
|
|
78
|
-
let p =
|
|
79
|
-
if (p.providerCustomerId !== l || p.providerInvoiceId !==
|
|
80
|
-
let m =
|
|
81
|
-
headId:
|
|
82
|
-
epoch:
|
|
74
|
+
if (!E(e, t)) throw o("The invoice is bound to a different tenant or billing customer.");
|
|
75
|
+
}, O = (t) => {
|
|
76
|
+
let n = t.tenantId, r = t.provider, a = t.providerInvoiceId, s = t.providerAccountId, c = t.livemode, l = t.providerCustomerId, u = t.identityKey, d = t.revision, f = t.id;
|
|
77
|
+
if (!x(n) || !x(r) || !x(a) || !x(l) || !x(f) || !x(s) || c !== 0 && c !== 1 || typeof u != "string" || !/^[0-9a-f]{64}$/.test(u) || typeof d != "number" || !Number.isInteger(d) || d < 1 || d > b) throw o("The native invoice observation identity is invalid.");
|
|
78
|
+
let p = i(t.providerSnapshot);
|
|
79
|
+
if (p.providerCustomerId !== l || p.providerInvoiceId !== a || p.livemode !== (c === 1)) throw o("The stored observation has a different customer or invoice.");
|
|
80
|
+
let m = t.paymentHeadId == null && t.paymentEpoch == null ? null : e({
|
|
81
|
+
headId: t.paymentHeadId,
|
|
82
|
+
epoch: t.paymentEpoch
|
|
83
83
|
});
|
|
84
84
|
return {
|
|
85
85
|
id: f,
|
|
86
86
|
tenantId: n,
|
|
87
87
|
provider: r,
|
|
88
|
-
providerAccountId:
|
|
88
|
+
providerAccountId: s,
|
|
89
89
|
livemode: c === 1,
|
|
90
|
-
providerInvoiceId:
|
|
90
|
+
providerInvoiceId: a,
|
|
91
91
|
providerCustomerId: l,
|
|
92
92
|
identityKey: u,
|
|
93
93
|
revision: d,
|
|
@@ -99,35 +99,35 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
|
|
|
99
99
|
...t,
|
|
100
100
|
...w(t)
|
|
101
101
|
});
|
|
102
|
-
if (i !== null && (D(i, r), !x(i.id) || !/^[0-9a-f]{64}$/.test(i.identityKey) || !Number.isInteger(i.revision) || i.revision < 1 || i.revision >= b)) throw
|
|
103
|
-
if (n.providerInvoiceId !== r.providerInvoiceId || n.providerCustomerId !== r.providerCustomerId || n.livemode !== r.livemode) throw
|
|
102
|
+
if (i !== null && (D(i, r), !x(i.id) || !/^[0-9a-f]{64}$/.test(i.identityKey) || !Number.isInteger(i.revision) || i.revision < 1 || i.revision >= b)) throw o("The expected native invoice revision is invalid or exhausted.");
|
|
103
|
+
if (n.providerInvoiceId !== r.providerInvoiceId || n.providerCustomerId !== r.providerCustomerId || n.livemode !== r.livemode) throw o("The complete invoice must match the confirmed customer binding.");
|
|
104
104
|
return {
|
|
105
105
|
owner: r,
|
|
106
106
|
previous: i,
|
|
107
|
-
providerSnapshot: structuredClone(
|
|
107
|
+
providerSnapshot: structuredClone(a(n))
|
|
108
108
|
};
|
|
109
|
-
}, A = (
|
|
110
|
-
let a = async (
|
|
111
|
-
let
|
|
112
|
-
...
|
|
109
|
+
}, A = (i) => {
|
|
110
|
+
let a = async (e, t = i, n = !1) => {
|
|
111
|
+
let r = S(u("identityKey", e), 2), a = await t.query(n ? {
|
|
112
|
+
...r,
|
|
113
113
|
lock: "update"
|
|
114
|
-
} :
|
|
115
|
-
if (a.length > 1) throw
|
|
116
|
-
let
|
|
117
|
-
if (
|
|
118
|
-
return
|
|
114
|
+
} : r);
|
|
115
|
+
if (a.length > 1) throw o("The native invoice identity is not unique.");
|
|
116
|
+
let s = a[0] ? O(a[0]) : null;
|
|
117
|
+
if (s !== null && (s.identityKey !== e || await T(s) !== e)) throw o("The native invoice identity digest could not be confirmed.");
|
|
118
|
+
return s;
|
|
119
119
|
};
|
|
120
120
|
return {
|
|
121
|
-
capturePaymentEpoch: (
|
|
122
|
-
currentPaymentEpoch: (
|
|
123
|
-
invalidatePayments: (
|
|
124
|
-
get: (
|
|
125
|
-
let
|
|
126
|
-
return
|
|
121
|
+
capturePaymentEpoch: (e) => C(() => r(i, e, !0, async (e, t) => t)),
|
|
122
|
+
currentPaymentEpoch: (e, n) => C(() => t(i, e, n?.forUpdate === !0)),
|
|
123
|
+
invalidatePayments: (e, t) => C(() => n(i, e, t)),
|
|
124
|
+
get: (e, t) => C(async () => {
|
|
125
|
+
let n = w(e), r = await a(await T(n), i, t?.forUpdate === !0);
|
|
126
|
+
return r !== null && D(r, n), r;
|
|
127
127
|
}),
|
|
128
|
-
complete: (
|
|
129
|
-
let { owner: d, previous: f, providerSnapshot: p } = k(
|
|
130
|
-
return i
|
|
128
|
+
complete: (t, n, s, c) => C(async () => {
|
|
129
|
+
let { owner: d, previous: f, providerSnapshot: p } = k(t, n, s), m = e(c), h = await T(d);
|
|
130
|
+
return r(i, d, !1, async (e, t) => {
|
|
131
131
|
if (t.headId !== m.headId || t.epoch !== m.epoch) return !1;
|
|
132
132
|
if (f === null) {
|
|
133
133
|
let t = "binvobs_" + globalThis.crypto.randomUUID();
|
|
@@ -143,10 +143,10 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
|
|
|
143
143
|
createdAt: /* @__PURE__ */ new Date()
|
|
144
144
|
}, { conflictColumns: ["identityKey"] });
|
|
145
145
|
let n = await a(h, e, !0);
|
|
146
|
-
if (n === null) throw
|
|
146
|
+
if (n === null) throw o("The persisted invoice observation could not be confirmed.", !0);
|
|
147
147
|
return D(n, d), n.id === t && (j(n, m, p), !0);
|
|
148
148
|
}
|
|
149
|
-
if (h !== f.identityKey) throw
|
|
149
|
+
if (h !== f.identityKey) throw o("The expected invoice identity digest is invalid.");
|
|
150
150
|
if (await e.updateMany("_voltro_billing_account_invoices", {
|
|
151
151
|
providerSnapshot: p,
|
|
152
152
|
revision: f.revision + 1,
|
|
@@ -154,13 +154,13 @@ var _ = "_voltro_billing_account_invoices", v = m("_voltro_billing_invoice_obser
|
|
|
154
154
|
paymentEpoch: m.epoch
|
|
155
155
|
}, { where: l(u("id", f.id), u("identityKey", h), u("revision", f.revision), u("tenantId", d.tenantId), u("provider", d.provider), u("providerAccountId", d.providerAccountId), u("livemode", +!!d.livemode), u("providerCustomerId", d.providerCustomerId), u("providerInvoiceId", d.providerInvoiceId)) }) !== 1) return !1;
|
|
156
156
|
let n = await a(h, e, !0);
|
|
157
|
-
if (!n || n.id !== f.id || n.revision !== f.revision + 1) throw
|
|
157
|
+
if (!n || n.id !== f.id || n.revision !== f.revision + 1) throw o("The updated invoice revision could not be read back.", !0);
|
|
158
158
|
return D(n, d), j(n, m, p), !0;
|
|
159
159
|
});
|
|
160
160
|
})
|
|
161
161
|
};
|
|
162
162
|
}, j = (e, t, n) => {
|
|
163
163
|
let r = e.paymentEpoch;
|
|
164
|
-
if (r === null || r.headId !== t.headId || r.epoch !== t.epoch || JSON.stringify(
|
|
164
|
+
if (r === null || r.headId !== t.headId || r.epoch !== t.epoch || JSON.stringify(a(e.snapshot)) !== JSON.stringify(n)) throw o("Invoice payment generation or exact observation could not be read back.", !0);
|
|
165
165
|
};
|
|
166
166
|
export { _ as BILLING_INVOICE_OBSERVATIONS_TABLE, y as billingInvoiceObservationsTable, v as billingRetainedInvoiceObservationsTable, A as dataStoreInvoiceStore };
|