@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.
- package/CHANGELOG.md +1943 -0
- package/README.md +0 -435
- package/THIRD-PARTY-NOTICES.md +22 -646
- package/dist/catalog.js +169 -70
- 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 +777 -68
- package/dist/index.js +354 -233
- package/dist/invoiceHistoryJournal.js +204 -179
- package/dist/invoiceHistoryProgress.js +130 -106
- package/dist/invoiceHistoryWork.js +188 -186
- package/dist/invoicePaymentEpoch.js +145 -0
- package/dist/invoicePaymentObservation.js +64 -0
- package/dist/invoiceSnapshot.js +56 -46
- package/dist/invoiceStore.js +124 -90
- package/dist/paidInvoiceCoverage.js +84 -0
- package/dist/plans.js +15 -2
- package/dist/providers.js +149 -92
- package/dist/provision.js +84 -73
- package/dist/rpc.d.ts +53 -13
- package/dist/rpc.js +141 -78
- package/dist/service.js +1208 -722
- package/dist/stores.js +240 -148
- package/dist/stripeIdentity.js +17 -17
- package/dist/stripeInvoiceHistory.js +21 -21
- package/dist/stripeInvoicePayments.js +129 -0
- package/dist/stripeInvoiceSnapshot.js +65 -46
- package/dist/stripeProvider.js +570 -201
- package/dist/stripeRest.js +10 -10
- 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 +9 -9
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { stripeCall as e } from "./stripeClient.js";
|
|
2
|
+
import { invoiceSnapshotError as t } from "./invoiceSnapshot.js";
|
|
3
|
+
import { assertInvoicePaymentObservation as n } from "./invoicePaymentObservation.js";
|
|
4
|
+
import { BillingError as r } from "./types.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 t("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 = (r, 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 n = 0; n < 100; n++) {
|
|
19
|
+
let n = yield* e("stripe.invoicePayments.list", () => r.invoicePayments.list({
|
|
20
|
+
invoice: c.providerInvoiceId,
|
|
21
|
+
limit: 100,
|
|
22
|
+
...f === void 0 ? {} : { starting_after: f }
|
|
23
|
+
}));
|
|
24
|
+
if (!Array.isArray(n.data) || n.data.length > 100 || typeof n.has_more != "boolean" || (n.has_more || f !== void 0) && n.data.length === 0) return yield* i.fail(t("The invoice payment page is incomplete."));
|
|
25
|
+
for (let f of n.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(t("The invoice payment belongs to a different source or has an invalid state."));
|
|
31
|
+
l.add(f.id);
|
|
32
|
+
let n = f.payment;
|
|
33
|
+
if (!n || typeof n.type != "string" || !n.type) return yield* i.fail(t("The invoice payment source is unavailable."));
|
|
34
|
+
let p = n.type === "payment_intent" ? a(n.payment_intent) : n.type === "charge" ? a(n.charge) : n.type === "payment_record" ? a(n.payment_record) : null;
|
|
35
|
+
if ([
|
|
36
|
+
"payment_intent",
|
|
37
|
+
"charge",
|
|
38
|
+
"payment_record"
|
|
39
|
+
].includes(n.type) && p === null) return yield* i.fail(t("The invoice payment source identity is missing."));
|
|
40
|
+
let m = null;
|
|
41
|
+
if (f.status === "paid" && n.type === "payment_intent" && p !== null) {
|
|
42
|
+
let n = yield* e("stripe.paymentIntents.retrieve", () => r.paymentIntents.retrieve(p));
|
|
43
|
+
if (n.id !== p || a(n.customer) !== c.providerCustomerId || n.currency !== c.currency || n.livemode !== c.livemode) return yield* i.fail(t("The payment intent belongs to a different customer or mode."));
|
|
44
|
+
let o = a(n.latest_charge);
|
|
45
|
+
if (o !== null) {
|
|
46
|
+
m = {
|
|
47
|
+
...yield* u(r, c, o, p),
|
|
48
|
+
paymentIntentStatus: n.status,
|
|
49
|
+
paymentIntentReceivedMinor: n.amount_received
|
|
50
|
+
};
|
|
51
|
+
let s = yield* e("stripe.paymentIntents.retrieve", () => r.paymentIntents.retrieve(p));
|
|
52
|
+
if (s.id !== n.id || a(s.customer) !== a(n.customer) || s.currency !== n.currency || s.livemode !== n.livemode || s.status !== n.status || s.amount_received !== n.amount_received || a(s.latest_charge) !== o) return yield* i.fail(t("The payment intent changed during invoice observation.", !0));
|
|
53
|
+
}
|
|
54
|
+
} else f.status === "paid" && n.type === "charge" && p !== null && (m = yield* u(r, c, p, null));
|
|
55
|
+
let h = yield* d(() => s(f.status_transitions.paid_at));
|
|
56
|
+
o.push({
|
|
57
|
+
providerInvoicePaymentId: f.id,
|
|
58
|
+
sourceType: n.type,
|
|
59
|
+
sourceId: p,
|
|
60
|
+
status: f.status,
|
|
61
|
+
amountPaidMinor: f.amount_paid,
|
|
62
|
+
paidAt: h,
|
|
63
|
+
cash: m
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (!n.has_more) {
|
|
67
|
+
p = !0;
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
f = n.data.at(-1)?.id;
|
|
71
|
+
}
|
|
72
|
+
if (!p) return yield* i.fail(t("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(() => n(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 = (n, r, o, s) => i.gen(function* () {
|
|
94
|
+
let c = yield* e("stripe.charges.retrieve", () => n.charges.retrieve(o)), u = yield* d(() => l(c, r, o, s)), f = [], p = /* @__PURE__ */ new Set(), m, h = !1;
|
|
95
|
+
for (let s = 0; s < 100; s++) {
|
|
96
|
+
let s = yield* e("stripe.refunds.list", () => n.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(t("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 !== r.currency) return yield* i.fail(t("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(t("The complete payment graph exceeds its observation bound."));
|
|
118
|
+
let g = yield* e("stripe.charges.retrieve", () => n.charges.retrieve(o)), _ = yield* d(() => l(g, r, o, s));
|
|
119
|
+
return JSON.stringify(u) === JSON.stringify(_) ? {
|
|
120
|
+
..._,
|
|
121
|
+
refunds: f,
|
|
122
|
+
paymentIntentStatus: null,
|
|
123
|
+
paymentIntentReceivedMinor: null
|
|
124
|
+
} : yield* i.fail(t("The payment changed during observation; retry the complete invoice read.", !0));
|
|
125
|
+
}), d = (e) => i.try({
|
|
126
|
+
try: e,
|
|
127
|
+
catch: (e) => e instanceof r ? e : t("The invoice payment observation is invalid.")
|
|
128
|
+
});
|
|
129
|
+
export { c as stripeInvoicePayments };
|
|
@@ -1,85 +1,104 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { stripeInvoicePayments as e } from "./stripeInvoicePayments.js";
|
|
2
2
|
import { assertInvoiceSnapshot as t, invoiceSnapshotError as n } from "./invoiceSnapshot.js";
|
|
3
3
|
import { stripeCall as r } from "./stripeClient.js";
|
|
4
|
-
import {
|
|
4
|
+
import { BillingError as i } from "./types.js";
|
|
5
|
+
import { Effect as a } from "effect";
|
|
5
6
|
// src/stripeInvoiceSnapshot.ts
|
|
6
|
-
var
|
|
7
|
+
var o = (e) => typeof e == "string" ? e : e?.id ?? null, s = (e) => {
|
|
7
8
|
if (e === null || !Number.isSafeInteger(e) || !Number.isSafeInteger(e * 1e3)) throw n("The invoice observation contains an invalid timestamp.");
|
|
8
9
|
let t = /* @__PURE__ */ new Date(e * 1e3);
|
|
9
10
|
if (!Number.isFinite(t.getTime())) throw n("The invoice timestamp is outside the supported range.");
|
|
10
11
|
return t;
|
|
11
|
-
},
|
|
12
|
-
try:
|
|
13
|
-
catch: (
|
|
14
|
-
}),
|
|
15
|
-
if (e.id !== t.providerInvoiceId ||
|
|
12
|
+
}, c = (e) => a.try({
|
|
13
|
+
try: e,
|
|
14
|
+
catch: (e) => e instanceof i ? e : n("The provider invoice observation could not be confirmed.")
|
|
15
|
+
}), l = (e, t) => {
|
|
16
|
+
if (e.id !== t.providerInvoiceId || o(e.customer) !== t.providerCustomerId) throw n("The invoice does not belong to the linked billing customer.");
|
|
16
17
|
let r = e.status;
|
|
17
18
|
if (r !== "open" && r !== "paid" && r !== "uncollectible" && r !== "void") throw n("Detailed invoice evidence requires a finalized invoice.");
|
|
18
19
|
return {
|
|
19
20
|
providerInvoiceId: e.id,
|
|
20
21
|
providerCustomerId: t.providerCustomerId,
|
|
21
|
-
providerSubscriptionId:
|
|
22
|
-
providerRevisionId:
|
|
22
|
+
providerSubscriptionId: o(e.parent?.subscription_details?.subscription ?? null),
|
|
23
|
+
providerRevisionId: o(e.latest_revision),
|
|
23
24
|
status: r === "open" ? "open" : r === "paid" ? "paid" : r === "void" ? "void" : "uncollectible",
|
|
24
25
|
livemode: e.livemode,
|
|
25
26
|
currency: e.currency,
|
|
26
27
|
amountDueMinor: e.amount_due,
|
|
27
28
|
amountPaidMinor: e.amount_paid,
|
|
28
29
|
amountRemainingMinor: e.amount_remaining,
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
prePaymentCreditNotesMinor: e.pre_payment_credit_notes_amount,
|
|
31
|
+
postPaymentCreditNotesMinor: e.post_payment_credit_notes_amount,
|
|
32
|
+
invoiceTotalMinor: e.total,
|
|
33
|
+
startingBalanceMinor: e.starting_balance,
|
|
34
|
+
endingBalanceMinor: e.ending_balance,
|
|
35
|
+
finalizedAt: s(e.status_transitions.finalized_at),
|
|
36
|
+
paidAt: e.status_transitions.paid_at === null ? null : s(e.status_transitions.paid_at)
|
|
31
37
|
};
|
|
32
|
-
},
|
|
38
|
+
}, u = (e, t) => {
|
|
33
39
|
if (e.invoice !== t) throw n("An invoice line has a different invoice identity.");
|
|
34
|
-
let r = e.parent, i = r?.type === "subscription_item_details" ? r.subscription_item_details : null,
|
|
35
|
-
if (r?.type === "subscription_item_details" && i === null || r?.type === "invoice_item_details" &&
|
|
36
|
-
let c =
|
|
40
|
+
let r = e.parent, i = r?.type === "subscription_item_details" ? r.subscription_item_details : null, a = r?.type === "invoice_item_details" ? r.invoice_item_details : null;
|
|
41
|
+
if (r?.type === "subscription_item_details" && i === null || r?.type === "invoice_item_details" && a === null) throw n("The invoice line origin could not be confirmed.");
|
|
42
|
+
let c = o(e.subscription), l = i?.subscription ?? a?.subscription ?? null;
|
|
37
43
|
if (c !== null && l !== null && c !== l) throw n("The invoice line contains conflicting subscription identities.");
|
|
38
44
|
return {
|
|
39
45
|
providerLineId: e.id,
|
|
40
46
|
providerSubscriptionId: l ?? c,
|
|
41
47
|
providerSubscriptionItemId: i?.subscription_item ?? null,
|
|
42
|
-
providerPriceId: e.pricing?.type === "price_details" ?
|
|
48
|
+
providerPriceId: e.pricing?.type === "price_details" ? o(e.pricing.price_details?.price ?? null) : null,
|
|
43
49
|
amountMinor: e.amount,
|
|
44
50
|
currency: e.currency,
|
|
45
51
|
quantityDecimal: e.quantity_decimal?.toString() ?? null,
|
|
46
|
-
periodStart:
|
|
47
|
-
periodEnd:
|
|
48
|
-
proration: i?.proration ??
|
|
52
|
+
periodStart: s(e.period.start),
|
|
53
|
+
periodEnd: s(e.period.end),
|
|
54
|
+
proration: i?.proration ?? a?.proration ?? null
|
|
49
55
|
};
|
|
50
|
-
},
|
|
51
|
-
let
|
|
52
|
-
providerCustomerId:
|
|
53
|
-
providerInvoiceId:
|
|
56
|
+
}, d = (i, o) => {
|
|
57
|
+
let s = Object.freeze({
|
|
58
|
+
providerCustomerId: o.providerCustomerId,
|
|
59
|
+
providerInvoiceId: o.providerInvoiceId
|
|
54
60
|
});
|
|
55
|
-
return [
|
|
56
|
-
let
|
|
57
|
-
for (let
|
|
58
|
-
let
|
|
61
|
+
return [s.providerCustomerId, s.providerInvoiceId].every((e) => typeof e == "string" && e.length > 0 && e.trim() === e) ? a.gen(function* () {
|
|
62
|
+
let o = yield* r("stripe.invoices.retrieve", () => i.invoices.retrieve(s.providerInvoiceId)), d = yield* c(() => l(o, s)), f = [], p = /* @__PURE__ */ new Set(), m, h = !1;
|
|
63
|
+
for (let e = 0; e < 100; e++) {
|
|
64
|
+
let e = yield* r("stripe.invoices.listLineItems", () => i.invoices.listLineItems(s.providerInvoiceId, {
|
|
59
65
|
limit: 100,
|
|
60
|
-
...
|
|
66
|
+
...m === void 0 ? {} : { starting_after: m }
|
|
61
67
|
}));
|
|
62
|
-
if (yield*
|
|
63
|
-
if (!Array.isArray(
|
|
64
|
-
for (let
|
|
65
|
-
if (typeof
|
|
66
|
-
|
|
68
|
+
if (yield* c(() => {
|
|
69
|
+
if (!Array.isArray(e.data) || e.data.length > 100 || typeof e.has_more != "boolean" || (e.has_more || m !== void 0) && e.data.length === 0) throw n("The invoice line page is incomplete.");
|
|
70
|
+
for (let t of e.data) {
|
|
71
|
+
if (typeof t.id != "string" || t.id.length === 0 || p.has(t.id)) throw n("The invoice line cursor repeated an identity.", !0);
|
|
72
|
+
p.add(t.id), f.push(u(t, s.providerInvoiceId));
|
|
67
73
|
}
|
|
68
|
-
}), !
|
|
69
|
-
|
|
74
|
+
}), !e.has_more) {
|
|
75
|
+
h = !0;
|
|
70
76
|
break;
|
|
71
77
|
}
|
|
72
|
-
|
|
78
|
+
m = e.data[e.data.length - 1]?.id;
|
|
73
79
|
}
|
|
74
|
-
if (!
|
|
75
|
-
let
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
+
if (!h) return yield* a.fail(n("The invoice exceeds the complete observation bound."));
|
|
81
|
+
let g = yield* e(i, {
|
|
82
|
+
providerInvoiceId: d.providerInvoiceId,
|
|
83
|
+
providerCustomerId: d.providerCustomerId,
|
|
84
|
+
currency: d.currency,
|
|
85
|
+
livemode: d.livemode,
|
|
86
|
+
prePaymentCreditNotesMinor: d.prePaymentCreditNotesMinor,
|
|
87
|
+
postPaymentCreditNotesMinor: d.postPaymentCreditNotesMinor,
|
|
88
|
+
invoiceTotalMinor: d.invoiceTotalMinor,
|
|
89
|
+
startingBalanceMinor: d.startingBalanceMinor,
|
|
90
|
+
endingBalanceMinor: d.endingBalanceMinor
|
|
91
|
+
}), _ = yield* r("stripe.invoices.retrieve", () => i.invoices.retrieve(s.providerInvoiceId)), v = yield* c(() => l(_, s));
|
|
92
|
+
if (JSON.stringify(d) !== JSON.stringify(v)) return yield* a.fail(n("The invoice changed during observation; retry the complete read.", !0));
|
|
93
|
+
let { prePaymentCreditNotesMinor: y, postPaymentCreditNotesMinor: b, invoiceTotalMinor: x, startingBalanceMinor: S, endingBalanceMinor: C, ...w } = v;
|
|
94
|
+
if (y !== g.prePaymentCreditNotesMinor || b !== g.postPaymentCreditNotesMinor) return yield* a.fail(n("Invoice credit-note totals changed during payment observation.", !0));
|
|
95
|
+
let T = {
|
|
96
|
+
...w,
|
|
97
|
+
lines: f,
|
|
98
|
+
paymentObservation: g,
|
|
80
99
|
observedAt: /* @__PURE__ */ new Date()
|
|
81
100
|
};
|
|
82
|
-
return yield*
|
|
83
|
-
}) :
|
|
101
|
+
return yield* c(() => t(T)), T;
|
|
102
|
+
}) : a.fail(n("An exact billing customer and invoice identity are required."));
|
|
84
103
|
};
|
|
85
|
-
export {
|
|
104
|
+
export { d as stripeInvoiceSnapshot };
|