@fantastic.dev/better-stripe 0.94.0 → 0.95.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 +12 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalAttach/internal/operationJournal.d.ts +6 -0
- package/dist/client/billing/renewalAttach/internal/operationJournal.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/operationJournal.js +10 -0
- package/dist/client/billing/renewalAttach/internal/operationJournal.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts +5 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts.map +1 -1
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js +40 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js.map +1 -1
- package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts +13 -0
- package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js +43 -0
- package/dist/client/billing/renewalAttach/internal/producer/invocationProducer.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts +32 -0
- package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js +102 -0
- package/dist/client/billing/renewalAttach/internal/producer/lineageAuthority.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/ownedAwaitFactory.d.ts +3 -0
- package/dist/client/billing/renewalAttach/internal/producer/ownedAwaitFactory.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/ownedAwaitFactory.js +59 -0
- package/dist/client/billing/renewalAttach/internal/producer/ownedAwaitFactory.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/ownerFactory.d.ts +11 -0
- package/dist/client/billing/renewalAttach/internal/producer/ownerFactory.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/ownerFactory.js +132 -0
- package/dist/client/billing/renewalAttach/internal/producer/ownerFactory.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.d.ts +6 -0
- package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.js +16 -0
- package/dist/client/billing/renewalAttach/internal/producer/stockDelegateFactory.js.map +1 -0
- package/dist/client/billing/renewalObservation/evidence-invoice-lines.d.ts +27 -0
- package/dist/client/billing/renewalObservation/evidence-invoice-lines.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/evidence-invoice-lines.js +164 -0
- package/dist/client/billing/renewalObservation/evidence-invoice-lines.js.map +1 -0
- package/dist/client/billing/renewalObservation/evidence-invoice.d.ts +4 -0
- package/dist/client/billing/renewalObservation/evidence-invoice.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/evidence-invoice.js +190 -0
- package/dist/client/billing/renewalObservation/evidence-invoice.js.map +1 -0
- package/dist/client/billing/renewalObservation/evidence-material.d.ts +26 -0
- package/dist/client/billing/renewalObservation/evidence-material.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/evidence-material.js +253 -0
- package/dist/client/billing/renewalObservation/evidence-material.js.map +1 -0
- package/dist/client/billing/renewalObservation/evidence-v1-types.d.ts +156 -0
- package/dist/client/billing/renewalObservation/evidence-v1-types.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/evidence-v1-types.js +2 -0
- package/dist/client/billing/renewalObservation/evidence-v1-types.js.map +1 -0
- package/dist/client/billing/renewalObservation/evidence-v1.d.ts +4 -0
- package/dist/client/billing/renewalObservation/evidence-v1.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/evidence-v1.js +263 -0
- package/dist/client/billing/renewalObservation/evidence-v1.js.map +1 -0
- package/dist/client/billing/renewalObservation/terminal-evidence.d.ts +17 -0
- package/dist/client/billing/renewalObservation/terminal-evidence.d.ts.map +1 -0
- package/dist/client/billing/renewalObservation/terminal-evidence.js +289 -0
- package/dist/client/billing/renewalObservation/terminal-evidence.js.map +1 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +16 -16
- package/dist/component/balanceFunding/mutations.d.ts +43 -43
- package/dist/component/balanceFunding/queries.d.ts +1 -1
- package/dist/component/balanceFunding/stateHandlers.d.ts +8 -8
- package/dist/component/earnings/triggers.d.ts +16 -16
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/docs/renewal-context-reads.md +28 -0
- package/docs/renewal-material-observation.md +50 -0
- package/package.json +1 -1
- package/src/client/billing/renewalAttach/internal/operationJournal.ts +12 -0
- package/src/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.ts +59 -0
- package/src/client/billing/renewalAttach/internal/producer/invocationProducer.ts +58 -0
- package/src/client/billing/renewalAttach/internal/producer/lineageAuthority.ts +158 -0
- package/src/client/billing/renewalAttach/internal/producer/ownedAwaitFactory.ts +54 -0
- package/src/client/billing/renewalAttach/internal/producer/ownerFactory.ts +136 -0
- package/src/client/billing/renewalAttach/internal/producer/producerClock.integration.test.ts +175 -0
- package/src/client/billing/renewalAttach/internal/producer/producerOwner.integration.test.ts +449 -0
- package/src/client/billing/renewalAttach/internal/producer/stockDelegateFactory.ts +23 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialDeclarations.ts +5 -0
- package/src/client/billing/renewalAttach/internal/test-fixtures/producerInitialOperation.ts +142 -0
- package/src/client/billing/renewalObservation/evidence-admission.test.ts +219 -0
- package/src/client/billing/renewalObservation/evidence-history.test.ts +187 -0
- package/src/client/billing/renewalObservation/evidence-invoice-controls.test.ts +293 -0
- package/src/client/billing/renewalObservation/evidence-invoice-lines.ts +238 -0
- package/src/client/billing/renewalObservation/evidence-invoice.test.ts +177 -0
- package/src/client/billing/renewalObservation/evidence-invoice.ts +271 -0
- package/src/client/billing/renewalObservation/evidence-material.test.ts +289 -0
- package/src/client/billing/renewalObservation/evidence-material.ts +313 -0
- package/src/client/billing/renewalObservation/evidence-v1-types.ts +186 -0
- package/src/client/billing/renewalObservation/evidence-v1.test.ts +371 -0
- package/src/client/billing/renewalObservation/evidence-v1.ts +286 -0
- package/src/client/billing/renewalObservation/terminal-evidence.ts +379 -0
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
InvoiceLineMatchV1,
|
|
3
|
+
Reason,
|
|
4
|
+
Period,
|
|
5
|
+
} from "./evidence-v1-types.js";
|
|
6
|
+
import { equal, type O } from "./common.js";
|
|
7
|
+
|
|
8
|
+
export class InvoiceEvidenceStopV1 extends Error {
|
|
9
|
+
constructor(
|
|
10
|
+
readonly reason: Reason,
|
|
11
|
+
readonly predicate: "unknown" | "conflict" = "unknown",
|
|
12
|
+
) {
|
|
13
|
+
super(reason);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
export function invoiceRecordV1(value: unknown): O {
|
|
17
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
18
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
19
|
+
return value as O;
|
|
20
|
+
}
|
|
21
|
+
/** Expanded references need their complete separately correlated counterpart. */
|
|
22
|
+
export function invoiceReferenceV1(
|
|
23
|
+
raw: unknown,
|
|
24
|
+
expected: string,
|
|
25
|
+
kind: string,
|
|
26
|
+
known?: O,
|
|
27
|
+
): string {
|
|
28
|
+
if (raw === null || raw === undefined)
|
|
29
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
30
|
+
if (typeof raw === "string") {
|
|
31
|
+
if (raw !== expected)
|
|
32
|
+
throw new InvoiceEvidenceStopV1("invoice_relation_mismatch", "conflict");
|
|
33
|
+
return raw;
|
|
34
|
+
}
|
|
35
|
+
const expanded = invoiceRecordV1(raw);
|
|
36
|
+
if (
|
|
37
|
+
expanded.id !== expected ||
|
|
38
|
+
expanded.object !== kind ||
|
|
39
|
+
expanded.deleted === true
|
|
40
|
+
)
|
|
41
|
+
throw new InvoiceEvidenceStopV1("invoice_relation_mismatch", "conflict");
|
|
42
|
+
for (const field of ["livemode", "customer_account"])
|
|
43
|
+
if (
|
|
44
|
+
Object.prototype.hasOwnProperty.call(expanded, field) &&
|
|
45
|
+
(!known || expanded[field] !== known[field])
|
|
46
|
+
)
|
|
47
|
+
throw new InvoiceEvidenceStopV1("invoice_relation_mismatch", "conflict");
|
|
48
|
+
const minimal = Object.keys(expanded).every((key) =>
|
|
49
|
+
["id", "object", "deleted", "livemode", "customer_account"].includes(key),
|
|
50
|
+
);
|
|
51
|
+
if (!minimal && (!known || !equal(expanded, known)))
|
|
52
|
+
throw new InvoiceEvidenceStopV1("invoice_relation_mismatch", "conflict");
|
|
53
|
+
return expected;
|
|
54
|
+
}
|
|
55
|
+
function decimal(raw: unknown) {
|
|
56
|
+
if (typeof raw !== "string" || !/^\d+(?:\.\d{1,12})?$/.test(raw))
|
|
57
|
+
throw new InvoiceEvidenceStopV1("unsupported_profile");
|
|
58
|
+
const [integer, fraction = ""] = raw.split(".");
|
|
59
|
+
return {
|
|
60
|
+
coefficient: BigInt(integer + fraction),
|
|
61
|
+
scale: fraction.length,
|
|
62
|
+
raw,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export function sameInvoiceDecimalV1(left: unknown, right: unknown): boolean {
|
|
66
|
+
const a = decimal(left),
|
|
67
|
+
b = decimal(right),
|
|
68
|
+
scale = Math.max(a.scale, b.scale);
|
|
69
|
+
return (
|
|
70
|
+
a.coefficient * 10n ** BigInt(scale - a.scale) ===
|
|
71
|
+
b.coefficient * 10n ** BigInt(scale - b.scale)
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
type LineContext = {
|
|
75
|
+
invoiceId: string;
|
|
76
|
+
invoiceStatus: InvoiceLineMatchV1["invoiceStatus"];
|
|
77
|
+
subscriptionId: string;
|
|
78
|
+
subscription: O;
|
|
79
|
+
itemId: string;
|
|
80
|
+
period: Period;
|
|
81
|
+
price: O;
|
|
82
|
+
productId: string;
|
|
83
|
+
quantity: number;
|
|
84
|
+
livemode: boolean;
|
|
85
|
+
};
|
|
86
|
+
function material(condition: unknown): asserts condition {
|
|
87
|
+
if (!condition)
|
|
88
|
+
throw new InvoiceEvidenceStopV1("invoice_material_mismatch", "conflict");
|
|
89
|
+
}
|
|
90
|
+
function oneLine(
|
|
91
|
+
raw: unknown,
|
|
92
|
+
context: LineContext,
|
|
93
|
+
): InvoiceLineMatchV1 | null {
|
|
94
|
+
const line = invoiceRecordV1(raw);
|
|
95
|
+
if (
|
|
96
|
+
line.object !== "line_item" ||
|
|
97
|
+
typeof line.id !== "string" ||
|
|
98
|
+
!/^il_[A-Za-z0-9_]+$/.test(line.id)
|
|
99
|
+
)
|
|
100
|
+
throw new InvoiceEvidenceStopV1("unsupported_profile");
|
|
101
|
+
invoiceReferenceV1(line.invoice, context.invoiceId, "invoice");
|
|
102
|
+
if (typeof line.livemode !== "boolean" || typeof line.currency !== "string")
|
|
103
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
104
|
+
if (line.livemode !== context.livemode)
|
|
105
|
+
throw new InvoiceEvidenceStopV1("invoice_relation_mismatch", "conflict");
|
|
106
|
+
material(line.currency === context.price.currency);
|
|
107
|
+
const parent = invoiceRecordV1(line.parent);
|
|
108
|
+
if (parent.type === "invoice_item_details") {
|
|
109
|
+
invoiceRecordV1(parent.invoice_item_details);
|
|
110
|
+
if (parent.subscription_item_details !== null)
|
|
111
|
+
throw new InvoiceEvidenceStopV1("unsupported_profile");
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
if (parent.type !== "subscription_item_details")
|
|
115
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
116
|
+
const details = invoiceRecordV1(parent.subscription_item_details);
|
|
117
|
+
invoiceReferenceV1(
|
|
118
|
+
details.subscription,
|
|
119
|
+
context.subscriptionId,
|
|
120
|
+
"subscription",
|
|
121
|
+
context.subscription,
|
|
122
|
+
);
|
|
123
|
+
invoiceReferenceV1(
|
|
124
|
+
details.subscription_item,
|
|
125
|
+
context.itemId,
|
|
126
|
+
"subscription_item",
|
|
127
|
+
);
|
|
128
|
+
if (details.proration !== false)
|
|
129
|
+
throw new InvoiceEvidenceStopV1("unsupported_profile");
|
|
130
|
+
if (line.subscription !== null && line.subscription !== undefined)
|
|
131
|
+
invoiceReferenceV1(
|
|
132
|
+
line.subscription,
|
|
133
|
+
context.subscriptionId,
|
|
134
|
+
"subscription",
|
|
135
|
+
context.subscription,
|
|
136
|
+
);
|
|
137
|
+
const pricing = invoiceRecordV1(line.pricing);
|
|
138
|
+
if (pricing.type === null || pricing.type === undefined)
|
|
139
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
140
|
+
if (pricing.type !== "price_details")
|
|
141
|
+
throw new InvoiceEvidenceStopV1("unsupported_profile");
|
|
142
|
+
const price = invoiceRecordV1(pricing.price_details);
|
|
143
|
+
if (typeof price.price !== "string" || typeof price.product !== "string")
|
|
144
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
145
|
+
material(
|
|
146
|
+
price.price === context.price.id && price.product === context.productId,
|
|
147
|
+
);
|
|
148
|
+
const period = invoiceRecordV1(line.period);
|
|
149
|
+
if (
|
|
150
|
+
typeof period.start !== "number" ||
|
|
151
|
+
typeof period.end !== "number" ||
|
|
152
|
+
!Number.isSafeInteger(period.start) ||
|
|
153
|
+
!Number.isSafeInteger(period.end)
|
|
154
|
+
)
|
|
155
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
156
|
+
material(
|
|
157
|
+
period.start === context.period.start && period.end === context.period.end,
|
|
158
|
+
);
|
|
159
|
+
if (
|
|
160
|
+
line.quantity_decimal === null ||
|
|
161
|
+
line.quantity_decimal === undefined ||
|
|
162
|
+
pricing.unit_amount_decimal === null ||
|
|
163
|
+
pricing.unit_amount_decimal === undefined
|
|
164
|
+
)
|
|
165
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
166
|
+
const quantity = decimal(line.quantity_decimal);
|
|
167
|
+
material(sameInvoiceDecimalV1(quantity.raw, String(context.quantity)));
|
|
168
|
+
material(
|
|
169
|
+
sameInvoiceDecimalV1(
|
|
170
|
+
pricing.unit_amount_decimal,
|
|
171
|
+
context.price.unit_amount_decimal,
|
|
172
|
+
),
|
|
173
|
+
);
|
|
174
|
+
if (line.quantity !== null && line.quantity !== undefined) {
|
|
175
|
+
if (
|
|
176
|
+
typeof line.quantity !== "number" ||
|
|
177
|
+
!Number.isSafeInteger(line.quantity) ||
|
|
178
|
+
line.quantity < 0
|
|
179
|
+
)
|
|
180
|
+
throw new InvoiceEvidenceStopV1("unsupported_profile");
|
|
181
|
+
material(
|
|
182
|
+
BigInt(line.quantity) ===
|
|
183
|
+
quantity.coefficient / 10n ** BigInt(quantity.scale),
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
invoiceId: context.invoiceId,
|
|
188
|
+
lineId: line.id,
|
|
189
|
+
subscriptionItemId: context.itemId,
|
|
190
|
+
nativePeriod: { start: context.period.start, end: context.period.end },
|
|
191
|
+
priceId: price.price,
|
|
192
|
+
productId: price.product,
|
|
193
|
+
currency: String(line.currency),
|
|
194
|
+
quantityDecimal: quantity.raw,
|
|
195
|
+
unitAmountDecimal: decimal(pricing.unit_amount_decimal).raw,
|
|
196
|
+
invoiceStatus: context.invoiceStatus,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/** Complete bounded page DATA only, never cursor/request or provider provenance. */
|
|
200
|
+
export function inspectInvoiceLinesV1(
|
|
201
|
+
raw: unknown,
|
|
202
|
+
context: LineContext,
|
|
203
|
+
): InvoiceLineMatchV1 {
|
|
204
|
+
if (!Array.isArray(raw) || raw.length < 1 || raw.length > 2)
|
|
205
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
206
|
+
const seen = new Set<string>(),
|
|
207
|
+
candidates: InvoiceLineMatchV1[] = [];
|
|
208
|
+
for (let index = 0; index < raw.length; index++) {
|
|
209
|
+
const page = invoiceRecordV1(raw[index]);
|
|
210
|
+
if (
|
|
211
|
+
page.object !== "list" ||
|
|
212
|
+
typeof page.has_more !== "boolean" ||
|
|
213
|
+
!Array.isArray(page.data)
|
|
214
|
+
)
|
|
215
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
216
|
+
if (typeof page.url !== "string")
|
|
217
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
218
|
+
if (page.url !== `/v1/invoices/${context.invoiceId}/lines`)
|
|
219
|
+
throw new InvoiceEvidenceStopV1("invoice_relation_mismatch", "conflict");
|
|
220
|
+
if (
|
|
221
|
+
page.data.length > 100 ||
|
|
222
|
+
(page.data.length === 0 && (raw.length > 1 || page.has_more)) ||
|
|
223
|
+
page.has_more !== index < raw.length - 1
|
|
224
|
+
)
|
|
225
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
226
|
+
for (const row of page.data) {
|
|
227
|
+
const line = invoiceRecordV1(row);
|
|
228
|
+
if (typeof line.id !== "string" || seen.has(line.id))
|
|
229
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
230
|
+
seen.add(line.id);
|
|
231
|
+
const candidate = oneLine(line, context);
|
|
232
|
+
if (candidate) candidates.push(candidate);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (candidates.length !== 1)
|
|
236
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
237
|
+
return candidates[0];
|
|
238
|
+
}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
2
|
+
import { inspectRenewalEvidenceV1 } from "./evidence-v1.js";
|
|
3
|
+
import { object, get } from "./common.js";
|
|
4
|
+
import {
|
|
5
|
+
invoiceRecipe,
|
|
6
|
+
invoiceMatch,
|
|
7
|
+
currentTarget,
|
|
8
|
+
evidence,
|
|
9
|
+
activeLifecycle,
|
|
10
|
+
prerequisites,
|
|
11
|
+
bounded,
|
|
12
|
+
set,
|
|
13
|
+
type InvoiceStatus,
|
|
14
|
+
} from "../../../../tests/renewalObservation/evidenceFixtures.js";
|
|
15
|
+
|
|
16
|
+
afterEach(() => vi.unstubAllGlobals());
|
|
17
|
+
const statuses: InvoiceStatus[] = [
|
|
18
|
+
"draft",
|
|
19
|
+
"open",
|
|
20
|
+
"paid",
|
|
21
|
+
"uncollectible",
|
|
22
|
+
"void",
|
|
23
|
+
null,
|
|
24
|
+
];
|
|
25
|
+
const positives = statuses.flatMap((status) =>
|
|
26
|
+
(["subscription_create", "subscription_cycle"] as const).map(
|
|
27
|
+
(originalReason) => ({ status, originalReason }),
|
|
28
|
+
),
|
|
29
|
+
);
|
|
30
|
+
it.each(positives)(
|
|
31
|
+
"unsigned invoice correlation I0=$originalReason status=$status",
|
|
32
|
+
async ({ status, originalReason }) => {
|
|
33
|
+
const f = invoiceRecipe(originalReason, status);
|
|
34
|
+
const expected = {
|
|
35
|
+
...evidence(f.h.input, activeLifecycle, currentTarget(f.h.input)),
|
|
36
|
+
invoiceEvidence: invoiceMatch(f),
|
|
37
|
+
};
|
|
38
|
+
let fetchCalls = 0;
|
|
39
|
+
vi.stubGlobal("fetch", () => {
|
|
40
|
+
fetchCalls++;
|
|
41
|
+
throw Error("UNEXPECTED_FETCH");
|
|
42
|
+
});
|
|
43
|
+
await prerequisites(f.h);
|
|
44
|
+
// Independent raw pointer/SI/native-period joins; no paid-status inference.
|
|
45
|
+
expect(
|
|
46
|
+
object(f.h.input.configuration.preparation.subscription).latest_invoice,
|
|
47
|
+
).toBe(f.originalInvoice.id);
|
|
48
|
+
expect(object(f.h.input.reads.subscription).latest_invoice).toBe(
|
|
49
|
+
f.currentInvoice.id,
|
|
50
|
+
);
|
|
51
|
+
expect(
|
|
52
|
+
f.originalLine.parent.subscription_item_details.subscription_item,
|
|
53
|
+
).toBe("si_original");
|
|
54
|
+
expect(
|
|
55
|
+
f.currentLine.parent.subscription_item_details.subscription_item,
|
|
56
|
+
).toBe("si_successor");
|
|
57
|
+
expect(f.currentLine.period).toEqual(expected.currentMaterial.nativePeriod);
|
|
58
|
+
expect(f.originalLine.period).toEqual(
|
|
59
|
+
expected.identity?.originalNativePeriod,
|
|
60
|
+
);
|
|
61
|
+
const before = bounded(f.h.input);
|
|
62
|
+
let targetFailed = false;
|
|
63
|
+
try {
|
|
64
|
+
const actual = await inspectRenewalEvidenceV1(f.h.input);
|
|
65
|
+
expect(actual).toEqual(expected);
|
|
66
|
+
expect(Reflect.ownKeys(actual)).toHaveLength(12);
|
|
67
|
+
expect(actual.acknowledgementEstablished).toBe(false);
|
|
68
|
+
expect(actual.authenticationEstablished).toBe(false);
|
|
69
|
+
expect(actual.operationalAuthority).toBe(false);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
targetFailed = true;
|
|
72
|
+
throw error;
|
|
73
|
+
} finally {
|
|
74
|
+
try {
|
|
75
|
+
expect(f.h.input).toEqual(before);
|
|
76
|
+
expect(fetchCalls).toBe(0);
|
|
77
|
+
console.info(
|
|
78
|
+
"EVIDENCE_INVOICE_SETTLEMENT",
|
|
79
|
+
JSON.stringify({
|
|
80
|
+
originalReason,
|
|
81
|
+
status,
|
|
82
|
+
targetReached: true,
|
|
83
|
+
inputUnchanged: true,
|
|
84
|
+
fetchCalls,
|
|
85
|
+
}),
|
|
86
|
+
);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (!targetFailed) throw error;
|
|
89
|
+
console.info("EVIDENCE_INVOICE_INSPECTION_FAILED");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
);
|
|
94
|
+
it.each(["decimal-text", "complete-page-two", "large-decimal"] as const)(
|
|
95
|
+
"unsigned invoice correlation %s",
|
|
96
|
+
async (control) => {
|
|
97
|
+
const f = invoiceRecipe(
|
|
98
|
+
"subscription_create",
|
|
99
|
+
"open",
|
|
100
|
+
control === "large-decimal" ? "large-decimal" : "standard",
|
|
101
|
+
);
|
|
102
|
+
if (control === "decimal-text") {
|
|
103
|
+
set(f.originalLine, "quantity_decimal", "3.000");
|
|
104
|
+
set(f.currentLine, "quantity_decimal", "3.0");
|
|
105
|
+
set(f.originalLine.pricing, "unit_amount_decimal", "1000.000");
|
|
106
|
+
set(f.currentLine.pricing, "unit_amount_decimal", "2000.00");
|
|
107
|
+
} else if (control === "complete-page-two") {
|
|
108
|
+
const extra = structuredClone(f.currentLine);
|
|
109
|
+
set(extra, "id", "il_non_subscription");
|
|
110
|
+
set(extra, "subscription", null);
|
|
111
|
+
set(extra, "parent", {
|
|
112
|
+
type: "invoice_item_details",
|
|
113
|
+
subscription_item_details: null,
|
|
114
|
+
invoice_item_details: {
|
|
115
|
+
invoice_item: "ii_declared_extra",
|
|
116
|
+
proration: false,
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
set(f.currentPages[0], "data", [extra]);
|
|
120
|
+
set(f.currentPages[0], "has_more", true);
|
|
121
|
+
f.currentPages.push({
|
|
122
|
+
object: "list",
|
|
123
|
+
url: f.currentPages[0].url,
|
|
124
|
+
has_more: false,
|
|
125
|
+
data: [f.currentLine],
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const expected = {
|
|
129
|
+
...evidence(f.h.input, activeLifecycle, currentTarget(f.h.input)),
|
|
130
|
+
invoiceEvidence: invoiceMatch(f),
|
|
131
|
+
};
|
|
132
|
+
let fetchCalls = 0;
|
|
133
|
+
vi.stubGlobal("fetch", () => {
|
|
134
|
+
fetchCalls++;
|
|
135
|
+
throw Error("UNEXPECTED_FETCH");
|
|
136
|
+
});
|
|
137
|
+
await prerequisites(f.h);
|
|
138
|
+
expect(get(object(f.h.input.reads.subscription), "items").has_more).toBe(
|
|
139
|
+
false,
|
|
140
|
+
);
|
|
141
|
+
expect(f.currentPages[f.currentPages.length - 1]?.has_more).toBe(false);
|
|
142
|
+
const before = bounded(f.h.input);
|
|
143
|
+
let targetFailed = false;
|
|
144
|
+
try {
|
|
145
|
+
const actual = await inspectRenewalEvidenceV1(f.h.input);
|
|
146
|
+
expect(actual).toEqual(expected);
|
|
147
|
+
if (actual.invoiceEvidence.predicate === "invoice_lines_correlated") {
|
|
148
|
+
expect(actual.invoiceEvidence.original.quantityDecimal).toBe(
|
|
149
|
+
f.originalLine.quantity_decimal,
|
|
150
|
+
);
|
|
151
|
+
expect(actual.invoiceEvidence.current.unitAmountDecimal).toBe(
|
|
152
|
+
f.currentLine.pricing.unit_amount_decimal,
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
} catch (error) {
|
|
156
|
+
targetFailed = true;
|
|
157
|
+
throw error;
|
|
158
|
+
} finally {
|
|
159
|
+
try {
|
|
160
|
+
expect(f.h.input).toEqual(before);
|
|
161
|
+
expect(fetchCalls).toBe(0);
|
|
162
|
+
console.info(
|
|
163
|
+
"EVIDENCE_INVOICE_SETTLEMENT",
|
|
164
|
+
JSON.stringify({
|
|
165
|
+
control,
|
|
166
|
+
targetReached: true,
|
|
167
|
+
inputUnchanged: true,
|
|
168
|
+
fetchCalls,
|
|
169
|
+
}),
|
|
170
|
+
);
|
|
171
|
+
} catch (error) {
|
|
172
|
+
if (!targetFailed) throw error;
|
|
173
|
+
console.info("EVIDENCE_INVOICE_INSPECTION_FAILED");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
},
|
|
177
|
+
);
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
InvoiceEvidenceV1,
|
|
3
|
+
InvoiceLineMatchV1,
|
|
4
|
+
} from "./evidence-v1-types.js";
|
|
5
|
+
import {
|
|
6
|
+
reconstructConfigurationV2,
|
|
7
|
+
type ProjectRenewalConfigurationV2Options,
|
|
8
|
+
} from "../renewalV2/index.js";
|
|
9
|
+
import { object, array, get, equal, type O } from "./common.js";
|
|
10
|
+
import {
|
|
11
|
+
inspectInvoiceLinesV1,
|
|
12
|
+
invoiceRecordV1,
|
|
13
|
+
invoiceReferenceV1,
|
|
14
|
+
InvoiceEvidenceStopV1,
|
|
15
|
+
} from "./evidence-invoice-lines.js";
|
|
16
|
+
|
|
17
|
+
function incomplete(): never {
|
|
18
|
+
throw new InvoiceEvidenceStopV1("incomplete_invoice_lines");
|
|
19
|
+
}
|
|
20
|
+
function relation(value: unknown): asserts value {
|
|
21
|
+
if (!value)
|
|
22
|
+
throw new InvoiceEvidenceStopV1("invoice_relation_mismatch", "conflict");
|
|
23
|
+
}
|
|
24
|
+
function material(value: unknown): asserts value {
|
|
25
|
+
if (!value)
|
|
26
|
+
throw new InvoiceEvidenceStopV1("invoice_material_mismatch", "conflict");
|
|
27
|
+
}
|
|
28
|
+
function closed(value: unknown, keys: string[]) {
|
|
29
|
+
const data = invoiceRecordV1(value),
|
|
30
|
+
actual = Object.keys(data);
|
|
31
|
+
if (
|
|
32
|
+
actual.length !== keys.length ||
|
|
33
|
+
keys.some((key) => !actual.includes(key))
|
|
34
|
+
)
|
|
35
|
+
incomplete();
|
|
36
|
+
return data;
|
|
37
|
+
}
|
|
38
|
+
function invoiceId(value: unknown) {
|
|
39
|
+
const id =
|
|
40
|
+
typeof value === "string"
|
|
41
|
+
? value
|
|
42
|
+
: value === null || value === undefined
|
|
43
|
+
? null
|
|
44
|
+
: invoiceRecordV1(value).id;
|
|
45
|
+
if (typeof id !== "string" || !/^in_[A-Za-z0-9_]+$/.test(id)) incomplete();
|
|
46
|
+
return id;
|
|
47
|
+
}
|
|
48
|
+
function status(value: unknown): InvoiceLineMatchV1["invoiceStatus"] {
|
|
49
|
+
if (
|
|
50
|
+
value === null ||
|
|
51
|
+
value === "draft" ||
|
|
52
|
+
value === "open" ||
|
|
53
|
+
value === "paid" ||
|
|
54
|
+
value === "uncollectible" ||
|
|
55
|
+
value === "void"
|
|
56
|
+
)
|
|
57
|
+
return value;
|
|
58
|
+
throw new InvoiceEvidenceStopV1("unsupported_profile");
|
|
59
|
+
}
|
|
60
|
+
function period(value: O, startKey: string, endKey: string) {
|
|
61
|
+
const start = value[startKey],
|
|
62
|
+
end = value[endKey];
|
|
63
|
+
if (
|
|
64
|
+
typeof start !== "number" ||
|
|
65
|
+
typeof end !== "number" ||
|
|
66
|
+
!Number.isSafeInteger(start) ||
|
|
67
|
+
!Number.isSafeInteger(end) ||
|
|
68
|
+
start < 0 ||
|
|
69
|
+
end <= start
|
|
70
|
+
)
|
|
71
|
+
incomplete();
|
|
72
|
+
return { start, end };
|
|
73
|
+
}
|
|
74
|
+
/** Exact paired Endive billing facts only. No payment or exclusive-cause proof. */
|
|
75
|
+
export async function inspectInvoiceEvidenceV1(
|
|
76
|
+
input: unknown,
|
|
77
|
+
): Promise<InvoiceEvidenceV1> {
|
|
78
|
+
try {
|
|
79
|
+
const raw = invoiceRecordV1(input);
|
|
80
|
+
const inventory = closed(raw.invoiceContext, ["original", "current"]);
|
|
81
|
+
const reconstruction = await reconstructConfigurationV2(
|
|
82
|
+
raw.configuration as ProjectRenewalConfigurationV2Options,
|
|
83
|
+
);
|
|
84
|
+
if (!reconstruction.material) incomplete();
|
|
85
|
+
const { material: owned } = reconstruction;
|
|
86
|
+
const expected = owned.prepared.expected;
|
|
87
|
+
const originalSub = get(
|
|
88
|
+
object(owned.prepared.providerSnapshot),
|
|
89
|
+
"subscription",
|
|
90
|
+
);
|
|
91
|
+
const originalItem = object(array(get(originalSub, "items").data)[0]);
|
|
92
|
+
const originalPrice = get(originalItem, "price"),
|
|
93
|
+
targetPrice = object(owned.args.targetPrice);
|
|
94
|
+
const knownCustomer = object(owned.args.billingCustomer),
|
|
95
|
+
customerId = knownCustomer.id;
|
|
96
|
+
if (typeof customerId !== "string") incomplete();
|
|
97
|
+
const reads = closed(raw.reads, [
|
|
98
|
+
"schedule",
|
|
99
|
+
"subscription",
|
|
100
|
+
"targetPrice",
|
|
101
|
+
"billingCustomer",
|
|
102
|
+
"coupons",
|
|
103
|
+
]);
|
|
104
|
+
const sub = invoiceRecordV1(reads.subscription),
|
|
105
|
+
schedule = invoiceRecordV1(reads.schedule);
|
|
106
|
+
const customer = invoiceRecordV1(reads.billingCustomer);
|
|
107
|
+
relation(
|
|
108
|
+
sub.object === "subscription" &&
|
|
109
|
+
sub.id === expected.subscriptionId &&
|
|
110
|
+
sub.customer_account === expected.customerAccountId &&
|
|
111
|
+
sub.livemode === expected.livemode,
|
|
112
|
+
);
|
|
113
|
+
relation(
|
|
114
|
+
schedule.object === "subscription_schedule" &&
|
|
115
|
+
schedule.id === owned.receipt.scheduleId &&
|
|
116
|
+
schedule.customer_account === expected.customerAccountId &&
|
|
117
|
+
schedule.livemode === expected.livemode,
|
|
118
|
+
);
|
|
119
|
+
relation(
|
|
120
|
+
customer.object === "customer" &&
|
|
121
|
+
customer.id === customerId &&
|
|
122
|
+
customer.customer_account === expected.customerAccountId &&
|
|
123
|
+
customer.livemode === expected.livemode,
|
|
124
|
+
);
|
|
125
|
+
invoiceReferenceV1(sub.customer, customerId, "customer", customer);
|
|
126
|
+
invoiceReferenceV1(schedule.customer, customerId, "customer", customer);
|
|
127
|
+
invoiceReferenceV1(
|
|
128
|
+
sub.schedule,
|
|
129
|
+
owned.receipt.scheduleId,
|
|
130
|
+
"subscription_schedule",
|
|
131
|
+
schedule,
|
|
132
|
+
);
|
|
133
|
+
invoiceReferenceV1(
|
|
134
|
+
schedule.subscription,
|
|
135
|
+
expected.subscriptionId,
|
|
136
|
+
"subscription",
|
|
137
|
+
sub,
|
|
138
|
+
);
|
|
139
|
+
if (schedule.status !== "active")
|
|
140
|
+
throw new InvoiceEvidenceStopV1("unsupported_profile");
|
|
141
|
+
const items = invoiceRecordV1(sub.items);
|
|
142
|
+
if (
|
|
143
|
+
items.has_more !== false ||
|
|
144
|
+
!Array.isArray(items.data) ||
|
|
145
|
+
items.data.length !== 1
|
|
146
|
+
)
|
|
147
|
+
incomplete();
|
|
148
|
+
const item = invoiceRecordV1(items.data[0]);
|
|
149
|
+
const observedItemId = item.id;
|
|
150
|
+
if (
|
|
151
|
+
item.object !== "subscription_item" ||
|
|
152
|
+
typeof observedItemId !== "string" ||
|
|
153
|
+
!/^si_[A-Za-z0-9_]+$/.test(observedItemId)
|
|
154
|
+
)
|
|
155
|
+
incomplete();
|
|
156
|
+
invoiceReferenceV1(
|
|
157
|
+
item.subscription,
|
|
158
|
+
expected.subscriptionId,
|
|
159
|
+
"subscription",
|
|
160
|
+
sub,
|
|
161
|
+
);
|
|
162
|
+
invoiceReferenceV1(
|
|
163
|
+
item.price,
|
|
164
|
+
expected.targetPriceId,
|
|
165
|
+
"price",
|
|
166
|
+
targetPrice,
|
|
167
|
+
);
|
|
168
|
+
material(equal(reads.targetPrice, targetPrice));
|
|
169
|
+
material(item.quantity === owned.prepared.quantity);
|
|
170
|
+
const native = period(item, "current_period_start", "current_period_end");
|
|
171
|
+
const phase = closed(schedule.current_phase, ["start_date", "end_date"]);
|
|
172
|
+
const active = period(phase, "start_date", "end_date");
|
|
173
|
+
material(active.start === expected.periodEnd && equal(active, native));
|
|
174
|
+
const phases = array(schedule.phases);
|
|
175
|
+
if (phases.length !== 2) incomplete();
|
|
176
|
+
const targetPhase = invoiceRecordV1(phases[1]);
|
|
177
|
+
material(
|
|
178
|
+
targetPhase.start_date === native.start &&
|
|
179
|
+
targetPhase.end_date === native.end,
|
|
180
|
+
);
|
|
181
|
+
const originalNative = {
|
|
182
|
+
start: expected.periodStart,
|
|
183
|
+
end: expected.periodEnd,
|
|
184
|
+
};
|
|
185
|
+
const originalInvoiceId = invoiceId(originalSub.latest_invoice),
|
|
186
|
+
currentInvoiceId = invoiceId(sub.latest_invoice);
|
|
187
|
+
const examine = (role: "original" | "current") => {
|
|
188
|
+
const row = closed(inventory[role], ["invoice", "linePages"]);
|
|
189
|
+
const invoice = invoiceRecordV1(row.invoice);
|
|
190
|
+
const id = role === "original" ? originalInvoiceId : currentInvoiceId;
|
|
191
|
+
if (invoice.object !== "invoice" || typeof invoice.id !== "string")
|
|
192
|
+
incomplete();
|
|
193
|
+
if (
|
|
194
|
+
typeof invoice.customer_account !== "string" ||
|
|
195
|
+
typeof invoice.livemode !== "boolean"
|
|
196
|
+
)
|
|
197
|
+
incomplete();
|
|
198
|
+
relation(
|
|
199
|
+
invoice.id === id &&
|
|
200
|
+
invoice.customer_account === expected.customerAccountId &&
|
|
201
|
+
invoice.livemode === expected.livemode,
|
|
202
|
+
);
|
|
203
|
+
invoiceReferenceV1(
|
|
204
|
+
invoice.customer,
|
|
205
|
+
customerId,
|
|
206
|
+
"customer",
|
|
207
|
+
role === "original" ? knownCustomer : customer,
|
|
208
|
+
);
|
|
209
|
+
invoiceReferenceV1(
|
|
210
|
+
role === "original" ? originalSub.latest_invoice : sub.latest_invoice,
|
|
211
|
+
id,
|
|
212
|
+
"invoice",
|
|
213
|
+
invoice,
|
|
214
|
+
);
|
|
215
|
+
const parent = invoiceRecordV1(invoice.parent);
|
|
216
|
+
if (parent.type !== "subscription_details") incomplete();
|
|
217
|
+
const details = invoiceRecordV1(parent.subscription_details);
|
|
218
|
+
invoiceReferenceV1(
|
|
219
|
+
details.subscription,
|
|
220
|
+
expected.subscriptionId,
|
|
221
|
+
"subscription",
|
|
222
|
+
role === "original" ? originalSub : sub,
|
|
223
|
+
);
|
|
224
|
+
if (
|
|
225
|
+
role === "original"
|
|
226
|
+
? invoice.billing_reason !== "subscription_create" &&
|
|
227
|
+
invoice.billing_reason !== "subscription_cycle"
|
|
228
|
+
: invoice.billing_reason !== "subscription_cycle"
|
|
229
|
+
)
|
|
230
|
+
throw new InvoiceEvidenceStopV1("unsupported_invoice_reason");
|
|
231
|
+
const price = role === "original" ? originalPrice : targetPrice;
|
|
232
|
+
if (typeof invoice.currency !== "string") incomplete();
|
|
233
|
+
material(invoice.currency === price.currency);
|
|
234
|
+
if (invoice.status === undefined) incomplete();
|
|
235
|
+
return inspectInvoiceLinesV1(row.linePages, {
|
|
236
|
+
invoiceId: id,
|
|
237
|
+
invoiceStatus: status(invoice.status),
|
|
238
|
+
subscriptionId: expected.subscriptionId,
|
|
239
|
+
subscription: role === "original" ? originalSub : sub,
|
|
240
|
+
itemId: role === "original" ? expected.itemId : observedItemId,
|
|
241
|
+
period: role === "original" ? originalNative : native,
|
|
242
|
+
price,
|
|
243
|
+
productId:
|
|
244
|
+
role === "original"
|
|
245
|
+
? expected.currentProductId
|
|
246
|
+
: expected.targetProductId,
|
|
247
|
+
quantity: owned.prepared.quantity,
|
|
248
|
+
livemode: expected.livemode,
|
|
249
|
+
});
|
|
250
|
+
};
|
|
251
|
+
const original = examine("original"),
|
|
252
|
+
current = examine("current");
|
|
253
|
+
return {
|
|
254
|
+
predicate: "invoice_lines_correlated",
|
|
255
|
+
original,
|
|
256
|
+
current,
|
|
257
|
+
reason: null,
|
|
258
|
+
};
|
|
259
|
+
} catch (error) {
|
|
260
|
+
return {
|
|
261
|
+
predicate:
|
|
262
|
+
error instanceof InvoiceEvidenceStopV1 ? error.predicate : "unknown",
|
|
263
|
+
original: null,
|
|
264
|
+
current: null,
|
|
265
|
+
reason:
|
|
266
|
+
error instanceof InvoiceEvidenceStopV1
|
|
267
|
+
? error.reason
|
|
268
|
+
: "incomplete_invoice_lines",
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
}
|