@voltro/plugin-billing 0.89.1 → 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.
@@ -1,85 +1,104 @@
1
1
  import { BillingError as e } from "./types.js";
2
2
  import { assertInvoiceSnapshot as t, invoiceSnapshotError as n } from "./invoiceSnapshot.js";
3
3
  import { stripeCall as r } from "./stripeClient.js";
4
- import { Effect as i } from "effect";
4
+ import { stripeInvoicePayments as i } from "./stripeInvoicePayments.js";
5
+ import { Effect as a } from "effect";
5
6
  // src/stripeInvoiceSnapshot.ts
6
- var a = (e) => typeof e == "string" ? e : e?.id ?? null, o = (e) => {
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
- }, s = (t) => i.try({
12
+ }, c = (t) => a.try({
12
13
  try: t,
13
14
  catch: (t) => t instanceof e ? t : n("The provider invoice observation could not be confirmed.")
14
- }), c = (e, t) => {
15
- if (e.id !== t.providerInvoiceId || a(e.customer) !== t.providerCustomerId) throw n("The invoice does not belong to the linked billing customer.");
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: a(e.parent?.subscription_details?.subscription ?? null),
22
- providerRevisionId: a(e.latest_revision),
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
- finalizedAt: o(e.status_transitions.finalized_at),
30
- paidAt: e.status_transitions.paid_at === null ? null : o(e.status_transitions.paid_at)
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
- }, l = (e, t) => {
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, s = r?.type === "invoice_item_details" ? r.invoice_item_details : null;
35
- if (r?.type === "subscription_item_details" && i === null || r?.type === "invoice_item_details" && s === null) throw n("The invoice line origin could not be confirmed.");
36
- let c = a(e.subscription), l = i?.subscription ?? s?.subscription ?? null;
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" ? a(e.pricing.price_details?.price ?? null) : null,
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: o(e.period.start),
47
- periodEnd: o(e.period.end),
48
- proration: i?.proration ?? s?.proration ?? null
52
+ periodStart: s(e.period.start),
53
+ periodEnd: s(e.period.end),
54
+ proration: i?.proration ?? a?.proration ?? null
49
55
  };
50
- }, u = (e, a) => {
51
- let o = Object.freeze({
52
- providerCustomerId: a.providerCustomerId,
53
- providerInvoiceId: a.providerInvoiceId
56
+ }, d = (e, o) => {
57
+ let s = Object.freeze({
58
+ providerCustomerId: o.providerCustomerId,
59
+ providerInvoiceId: o.providerInvoiceId
54
60
  });
55
- return [o.providerCustomerId, o.providerInvoiceId].every((e) => typeof e == "string" && e.length > 0 && e.trim() === e) ? i.gen(function* () {
56
- let a = yield* r("stripe.invoices.retrieve", () => e.invoices.retrieve(o.providerInvoiceId)), u = yield* s(() => c(a, o)), d = [], f = /* @__PURE__ */ new Set(), p, m = !1;
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", () => e.invoices.retrieve(s.providerInvoiceId)), d = yield* c(() => l(o, s)), f = [], p = /* @__PURE__ */ new Set(), m, h = !1;
57
63
  for (let t = 0; t < 100; t++) {
58
- let t = yield* r("stripe.invoices.listLineItems", () => e.invoices.listLineItems(o.providerInvoiceId, {
64
+ let t = yield* r("stripe.invoices.listLineItems", () => e.invoices.listLineItems(s.providerInvoiceId, {
59
65
  limit: 100,
60
- ...p === void 0 ? {} : { starting_after: p }
66
+ ...m === void 0 ? {} : { starting_after: m }
61
67
  }));
62
- if (yield* s(() => {
63
- if (!Array.isArray(t.data) || t.data.length > 100 || typeof t.has_more != "boolean" || (t.has_more || p !== void 0) && t.data.length === 0) throw n("The invoice line page is incomplete.");
68
+ if (yield* c(() => {
69
+ if (!Array.isArray(t.data) || t.data.length > 100 || typeof t.has_more != "boolean" || (t.has_more || m !== void 0) && t.data.length === 0) throw n("The invoice line page is incomplete.");
64
70
  for (let e of t.data) {
65
- if (typeof e.id != "string" || e.id.length === 0 || f.has(e.id)) throw n("The invoice line cursor repeated an identity.", !0);
66
- f.add(e.id), d.push(l(e, o.providerInvoiceId));
71
+ if (typeof e.id != "string" || e.id.length === 0 || p.has(e.id)) throw n("The invoice line cursor repeated an identity.", !0);
72
+ p.add(e.id), f.push(u(e, s.providerInvoiceId));
67
73
  }
68
74
  }), !t.has_more) {
69
- m = !0;
75
+ h = !0;
70
76
  break;
71
77
  }
72
- p = t.data[t.data.length - 1]?.id;
78
+ m = t.data[t.data.length - 1]?.id;
73
79
  }
74
- if (!m) return yield* i.fail(n("The invoice exceeds the complete observation bound."));
75
- let h = yield* r("stripe.invoices.retrieve", () => e.invoices.retrieve(o.providerInvoiceId)), g = yield* s(() => c(h, o));
76
- if (JSON.stringify(u) !== JSON.stringify(g)) return yield* i.fail(n("The invoice changed during observation; retry the complete read.", !0));
77
- let _ = {
78
- ...g,
79
- lines: d,
80
+ if (!h) return yield* a.fail(n("The invoice exceeds the complete observation bound."));
81
+ let g = yield* i(e, {
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", () => e.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* s(() => t(_)), _;
83
- }) : i.fail(n("An exact billing customer and invoice identity are required."));
101
+ return yield* c(() => t(T)), T;
102
+ }) : a.fail(n("An exact billing customer and invoice identity are required."));
84
103
  };
85
- export { u as stripeInvoiceSnapshot };
104
+ export { d as stripeInvoiceSnapshot };