@fantastic.dev/better-stripe 0.92.0 → 0.94.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 +12 -0
- package/README.md +1 -1
- package/dist/client/billing/renewalAttach/internal/operationJournal.d.ts +41 -0
- package/dist/client/billing/renewalAttach/internal/operationJournal.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/operationJournal.js +378 -0
- package/dist/client/billing/renewalAttach/internal/operationJournal.js.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts +44 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.d.ts.map +1 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js +251 -0
- package/dist/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.js.map +1 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +562 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +281 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +562 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +281 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/index.d.ts +3 -2
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +16 -3
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/api.d.ts +6 -0
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +406 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/balanceFunding/mutations.d.ts +27 -27
- package/dist/component/balanceFunding/queries.d.ts +1 -1
- package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
- package/dist/component/earnings/triggers.d.ts +294 -0
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/financialObservations/settlementHelpers.d.ts +3 -3
- package/dist/component/renewalOperations/helpers.d.ts +236 -0
- package/dist/component/renewalOperations/helpers.d.ts.map +1 -0
- package/dist/component/renewalOperations/helpers.js +457 -0
- package/dist/component/renewalOperations/helpers.js.map +1 -0
- package/dist/component/renewalOperations/mutations.d.ts +252 -0
- package/dist/component/renewalOperations/mutations.d.ts.map +1 -0
- package/dist/component/renewalOperations/mutations.js +340 -0
- package/dist/component/renewalOperations/mutations.js.map +1 -0
- package/dist/component/renewalOperations/queries.d.ts +163 -0
- package/dist/component/renewalOperations/queries.d.ts.map +1 -0
- package/dist/component/renewalOperations/queries.js +167 -0
- package/dist/component/renewalOperations/queries.js.map +1 -0
- package/dist/component/renewalOperations/schema.d.ts +1988 -0
- package/dist/component/renewalOperations/schema.d.ts.map +1 -0
- package/dist/component/renewalOperations/schema.js +227 -0
- package/dist/component/renewalOperations/schema.js.map +1 -0
- package/dist/component/schema.d.ts +281 -0
- package/dist/component/schema.js +5 -0
- package/dist/component/schema.js.map +1 -1
- package/dist/component/usage/allowance.fixtures.d.ts +281 -0
- package/dist/component/usage/allowance.fixtures.d.ts.map +1 -1
- package/dist/component/usageCredits/accounting.fixtures.d.ts +297 -16
- package/dist/component/usageCredits/accounting.fixtures.d.ts.map +1 -1
- package/dist/shared/renewalOperationJournal.d.ts +43 -0
- package/dist/shared/renewalOperationJournal.d.ts.map +1 -0
- package/dist/shared/renewalOperationJournal.js +294 -0
- package/dist/shared/renewalOperationJournal.js.map +1 -0
- package/docs/renewal-context-reads.md +180 -0
- package/package.json +1 -1
- package/src/client/billing/renewalAttach/internal/operationJournal.acknowledgement.integration.test.ts +500 -0
- package/src/client/billing/renewalAttach/internal/operationJournal.c10.integration.test.ts +560 -0
- package/src/client/billing/renewalAttach/internal/operationJournal.controls.integration.test.ts +356 -0
- package/src/client/billing/renewalAttach/internal/operationJournal.integration.test.ts +560 -0
- package/src/client/billing/renewalAttach/internal/operationJournal.methods.integration.test.ts +534 -0
- package/src/client/billing/renewalAttach/internal/operationJournal.ts +561 -0
- package/src/client/billing/renewalAttach/internal/producer/classOwnedActionRoot.ts +334 -0
- package/src/client/billing/renewalAttach/internal/producer/producerGuards.integration.test.ts +323 -0
- package/src/client/billing/renewalAttach/internal/producer/producerInitial.integration.test.ts +301 -0
- package/src/client/billing/renewalAttach/internal/producer/producerIsolation.integration.test.ts +531 -0
- package/src/client/index.ts +24 -3
- package/src/component/_generated/api.ts +6 -0
- package/src/component/_generated/component.ts +450 -0
- package/src/component/renewalOperations/acknowledgement.fixtures.ts +552 -0
- package/src/component/renewalOperations/acknowledgement.test.ts +573 -0
- package/src/component/renewalOperations/acknowledgementReads.test.ts +442 -0
- package/src/component/renewalOperations/acknowledgementV2.test.ts +493 -0
- package/src/component/renewalOperations/acknowledgementV2Publication.test.ts +373 -0
- package/src/component/renewalOperations/basisBinding.test.ts +567 -0
- package/src/component/renewalOperations/basisLifecycle.test.ts +569 -0
- package/src/component/renewalOperations/boundHistory.test.ts +474 -0
- package/src/component/renewalOperations/helpers.ts +592 -0
- package/src/component/renewalOperations/historyReads.test.ts +595 -0
- package/src/component/renewalOperations/initialBasis.test.ts +578 -0
- package/src/component/renewalOperations/journal.test.ts +567 -0
- package/src/component/renewalOperations/mutations.ts +456 -0
- package/src/component/renewalOperations/payload.test.ts +589 -0
- package/src/component/renewalOperations/payloadEncoding.test.ts +592 -0
- package/src/component/renewalOperations/queries.ts +204 -0
- package/src/component/renewalOperations/schema.ts +268 -0
- package/src/component/renewalOperations/timingPolicy.fixtures.ts +193 -0
- package/src/component/renewalOperations/timingPolicy.test.ts +523 -0
- package/src/component/schema.ts +11 -0
- package/src/shared/renewalOperationJournal.ts +333 -0
|
@@ -0,0 +1,592 @@
|
|
|
1
|
+
import { renewalSnapshot } from "../../client/billing/renewalReplacementSnapshot.js";
|
|
2
|
+
import {
|
|
3
|
+
constructRenewalScheduleAttachmentReceiptV2,
|
|
4
|
+
prepareRenewalPriceReplacementV2,
|
|
5
|
+
prepareRenewalScheduleAttachmentV2,
|
|
6
|
+
} from "../../client/billing/renewalV2/index.js";
|
|
7
|
+
import { captureAttachmentReceipt } from "../../client/billing/renewalV2/receipt.js";
|
|
8
|
+
import {
|
|
9
|
+
captureAttachSource,
|
|
10
|
+
captureAttachDispatch,
|
|
11
|
+
captureAttachResponse,
|
|
12
|
+
captureAttachReceipt,
|
|
13
|
+
} from "../../client/billing/renewalAttach/internal/c10/eventCapture.js";
|
|
14
|
+
import { requiredJSONSnapshot } from "../../client/billing/renewalAttach/internal/material/requiredJson.js";
|
|
15
|
+
import type { Doc } from "../_generated/dataModel.js";
|
|
16
|
+
import type { QueryCtx } from "../_generated/server.js";
|
|
17
|
+
import {
|
|
18
|
+
canonicalJournalEvent,
|
|
19
|
+
canonicalInitialBasis,
|
|
20
|
+
canonicalJournalRecord,
|
|
21
|
+
journalReservationMaterial,
|
|
22
|
+
} from "../../shared/renewalOperationJournal.js";
|
|
23
|
+
|
|
24
|
+
type Reader = Pick<QueryCtx, "db">;
|
|
25
|
+
export async function findOperation(
|
|
26
|
+
ctx: Reader,
|
|
27
|
+
authorizationScope: string,
|
|
28
|
+
logicalOperationId: string,
|
|
29
|
+
) {
|
|
30
|
+
const rows = await ctx.db
|
|
31
|
+
.query("renewalOperations")
|
|
32
|
+
.withIndex("by_authorizationScope_and_logicalOperationId", (q) =>
|
|
33
|
+
q
|
|
34
|
+
.eq("authorizationScope", authorizationScope)
|
|
35
|
+
.eq("logicalOperationId", logicalOperationId),
|
|
36
|
+
)
|
|
37
|
+
.take(2);
|
|
38
|
+
if (rows.length > 1) throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
39
|
+
return rows[0] ?? null;
|
|
40
|
+
}
|
|
41
|
+
export async function originalRecord(
|
|
42
|
+
ctx: Reader,
|
|
43
|
+
row: Doc<"renewalOperations">,
|
|
44
|
+
) {
|
|
45
|
+
const payload = await ctx.db.get("renewalOperationPayloads", row.payloadId);
|
|
46
|
+
if (!payload || payload.codecVersion !== 1)
|
|
47
|
+
throw Error("RENEWAL_JOURNAL_UNAVAILABLE");
|
|
48
|
+
const owner = payload.owner;
|
|
49
|
+
if (!owner || owner.kind !== "operation" || owner.operationId !== row._id)
|
|
50
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
51
|
+
const full = canonicalJournalRecord(payload.json);
|
|
52
|
+
if (
|
|
53
|
+
full.json !== payload.json ||
|
|
54
|
+
full.authorizationScope !== row.authorizationScope ||
|
|
55
|
+
full.authorizationId !== row.authorizationId ||
|
|
56
|
+
full.authorizationRevision !== row.authorizationRevision ||
|
|
57
|
+
full.logicalOperationId !== row.logicalOperationId ||
|
|
58
|
+
full.requestBinding !== row.requestBinding ||
|
|
59
|
+
("recordVersion" in full ? full.recordVersion : undefined) !==
|
|
60
|
+
row.recordVersion ||
|
|
61
|
+
(row.initialBasis !== undefined &&
|
|
62
|
+
(row.recordVersion !== 2 || row.state !== "RESERVED"))
|
|
63
|
+
)
|
|
64
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
65
|
+
await validateOperationalAttachSource(full);
|
|
66
|
+
return full;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** V2 unsigned full C1/C2 consistency, never operational qualification. */
|
|
70
|
+
export async function validateOperationalAttachSource(
|
|
71
|
+
full: ReturnType<typeof canonicalJournalRecord>,
|
|
72
|
+
): Promise<void> {
|
|
73
|
+
if (!("recordVersion" in full)) return;
|
|
74
|
+
try {
|
|
75
|
+
const parsed: unknown = JSON.parse(full.json);
|
|
76
|
+
const original = jsonObject(parsed);
|
|
77
|
+
const source = captureAttachSource(original.attachRequest);
|
|
78
|
+
if (!sameJSON(original.attachRequest, source)) acknowledgementConflict();
|
|
79
|
+
const prepared = await prepareRenewalPriceReplacementV2(source.preparation);
|
|
80
|
+
const request = await prepareRenewalScheduleAttachmentV2({
|
|
81
|
+
prepared,
|
|
82
|
+
apiVersion: source.apiVersion,
|
|
83
|
+
attachKey: source.attachKey,
|
|
84
|
+
firstDispatchedAt: source.firstDispatchedAt,
|
|
85
|
+
});
|
|
86
|
+
const seven = {
|
|
87
|
+
scope: request.scope,
|
|
88
|
+
apiVersion: request.apiVersion,
|
|
89
|
+
method: request.method,
|
|
90
|
+
path: request.path,
|
|
91
|
+
payload: request.payload,
|
|
92
|
+
key: request.key,
|
|
93
|
+
firstDispatchedAt: request.firstDispatchedAt,
|
|
94
|
+
};
|
|
95
|
+
const account = jsonObject(original.account);
|
|
96
|
+
const policy = jsonObject(original.timingPolicy);
|
|
97
|
+
if (
|
|
98
|
+
original.stage !== "attach" ||
|
|
99
|
+
original.apiVersion !== request.apiVersion ||
|
|
100
|
+
!sameJSON(original.dispatch, seven) ||
|
|
101
|
+
full.requestBinding !== request.requestIdentity.binding ||
|
|
102
|
+
account.mode !== "platform" ||
|
|
103
|
+
account.stripeAccount !== null ||
|
|
104
|
+
account.livemode !== prepared.expected.livemode ||
|
|
105
|
+
policy.firstDispatchedAt !== request.firstDispatchedAt ||
|
|
106
|
+
policy.periodStart !== prepared.expected.periodStart ||
|
|
107
|
+
policy.periodEnd !== prepared.expected.periodEnd
|
|
108
|
+
)
|
|
109
|
+
acknowledgementConflict();
|
|
110
|
+
// originalBundle is immutable opaque host DATA, not a C1 input projection.
|
|
111
|
+
} catch {
|
|
112
|
+
acknowledgementConflict();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function reservationFields(
|
|
117
|
+
row: Doc<"renewalOperations">,
|
|
118
|
+
full: ReturnType<typeof canonicalJournalRecord>,
|
|
119
|
+
) {
|
|
120
|
+
const material = journalReservationMaterial(full);
|
|
121
|
+
return {
|
|
122
|
+
operationId: row._id,
|
|
123
|
+
payloadId: row.payloadId,
|
|
124
|
+
authorizationScope: full.authorizationScope,
|
|
125
|
+
authorizationId: full.authorizationId,
|
|
126
|
+
authorizationRevision: full.authorizationRevision,
|
|
127
|
+
logicalOperationId: full.logicalOperationId,
|
|
128
|
+
requestBinding: full.requestBinding,
|
|
129
|
+
...material,
|
|
130
|
+
ordinal: 1 as const,
|
|
131
|
+
preparedRevision: 0 as const,
|
|
132
|
+
committedRevision: 1 as const,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/** Validate retained DATA without recreating a local clock or Owner. */
|
|
136
|
+
export function checkedInitialBasis(
|
|
137
|
+
row: Doc<"renewalOperations">,
|
|
138
|
+
attemptId: Doc<"renewalOperationAttempts">["_id"],
|
|
139
|
+
fields: ReturnType<typeof reservationFields>,
|
|
140
|
+
) {
|
|
141
|
+
const bound = row.initialBasis;
|
|
142
|
+
if (!("recordVersion" in fields)) {
|
|
143
|
+
if (bound !== undefined) throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
if (bound === undefined) return null;
|
|
147
|
+
const normalized = canonicalInitialBasis(bound.policyJSON, bound.basisJSON);
|
|
148
|
+
if (
|
|
149
|
+
row.recordVersion !== 2 ||
|
|
150
|
+
row.state !== "RESERVED" ||
|
|
151
|
+
Object.keys(bound).length !== 6 ||
|
|
152
|
+
bound.version !== 1 ||
|
|
153
|
+
bound.attemptId !== attemptId ||
|
|
154
|
+
bound.sourcePayloadId !== fields.payloadId ||
|
|
155
|
+
bound.stage !== fields.stage ||
|
|
156
|
+
bound.policyJSON !== fields.timingPolicyJSON ||
|
|
157
|
+
normalized.policyJSON !== bound.policyJSON ||
|
|
158
|
+
normalized.basisJSON !== bound.basisJSON
|
|
159
|
+
)
|
|
160
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
161
|
+
return bound;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function attemptRows(
|
|
165
|
+
ctx: Reader,
|
|
166
|
+
operationId: Doc<"renewalOperations">["_id"],
|
|
167
|
+
) {
|
|
168
|
+
return ctx.db
|
|
169
|
+
.query("renewalOperationAttempts")
|
|
170
|
+
.withIndex("by_operationId", (q) => q.eq("operationId", operationId))
|
|
171
|
+
.take(2);
|
|
172
|
+
}
|
|
173
|
+
export async function currentReservation(
|
|
174
|
+
ctx: Reader,
|
|
175
|
+
row: Doc<"renewalOperations">,
|
|
176
|
+
full: ReturnType<typeof canonicalJournalRecord>,
|
|
177
|
+
) {
|
|
178
|
+
const attempts = await attemptRows(ctx, row._id);
|
|
179
|
+
if (row.state === "PREPARED") {
|
|
180
|
+
if (
|
|
181
|
+
row.revision !== 0 ||
|
|
182
|
+
row.activeAttemptId !== undefined ||
|
|
183
|
+
row.initialBasis !== undefined ||
|
|
184
|
+
attempts.length !== 0 ||
|
|
185
|
+
(row.recordVersion === 2 && row.knownAcknowledgement !== undefined)
|
|
186
|
+
)
|
|
187
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
if (row.revision !== 1 || !row.activeAttemptId || attempts.length !== 1)
|
|
191
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
192
|
+
const actual = attempts[0];
|
|
193
|
+
const expected = reservationFields(row, full);
|
|
194
|
+
if (
|
|
195
|
+
!actual ||
|
|
196
|
+
actual._id !== row.activeAttemptId ||
|
|
197
|
+
actual.operationId !== expected.operationId ||
|
|
198
|
+
actual.payloadId !== expected.payloadId ||
|
|
199
|
+
actual.authorizationScope !== expected.authorizationScope ||
|
|
200
|
+
actual.authorizationId !== expected.authorizationId ||
|
|
201
|
+
actual.authorizationRevision !== expected.authorizationRevision ||
|
|
202
|
+
actual.logicalOperationId !== expected.logicalOperationId ||
|
|
203
|
+
actual.requestBinding !== expected.requestBinding ||
|
|
204
|
+
actual.stage !== expected.stage ||
|
|
205
|
+
actual.key !== expected.key ||
|
|
206
|
+
actual.apiVersion !== expected.apiVersion ||
|
|
207
|
+
actual.accountJSON !== expected.accountJSON ||
|
|
208
|
+
actual.firstDispatchedAt !== expected.firstDispatchedAt ||
|
|
209
|
+
actual.ordinal !== 1 ||
|
|
210
|
+
actual.preparedRevision !== 0 ||
|
|
211
|
+
actual.committedRevision !== 1 ||
|
|
212
|
+
!Number.isInteger(actual.lastSequence) ||
|
|
213
|
+
actual.lastSequence < 0 ||
|
|
214
|
+
actual.lastSequence > 3
|
|
215
|
+
)
|
|
216
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
217
|
+
if ("recordVersion" in expected) {
|
|
218
|
+
if (
|
|
219
|
+
!("recordVersion" in actual) ||
|
|
220
|
+
actual.recordVersion !== 2 ||
|
|
221
|
+
actual.timingPolicyJSON !== expected.timingPolicyJSON
|
|
222
|
+
)
|
|
223
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
224
|
+
const bound = checkedInitialBasis(row, actual._id, expected);
|
|
225
|
+
if (bound === null) {
|
|
226
|
+
if (actual.lastSequence !== 0 || row.knownAcknowledgement !== undefined)
|
|
227
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
228
|
+
const events = await ctx.db
|
|
229
|
+
.query("renewalOperationEvents")
|
|
230
|
+
.withIndex("by_attemptId_and_sequence", (q) =>
|
|
231
|
+
q.eq("attemptId", actual._id),
|
|
232
|
+
)
|
|
233
|
+
.take(1);
|
|
234
|
+
if (events.length !== 0) throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
235
|
+
} else if (
|
|
236
|
+
row.knownAcknowledgement !== undefined &&
|
|
237
|
+
actual.lastSequence < 2
|
|
238
|
+
)
|
|
239
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
240
|
+
} else {
|
|
241
|
+
if ("recordVersion" in actual || actual.basisJSON !== expected.basisJSON)
|
|
242
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
243
|
+
checkedInitialBasis(row, actual._id, expected);
|
|
244
|
+
}
|
|
245
|
+
return { ...expected, attemptId: actual._id, recordJSON: full.json };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
type Reservation = NonNullable<Awaited<ReturnType<typeof currentReservation>>>;
|
|
249
|
+
/** All reservation fields are closed scalar DATA, including complete JSON text. */
|
|
250
|
+
export function reservationMatches(actual: Reservation, supplied: Reservation) {
|
|
251
|
+
const entries = Object.entries(supplied);
|
|
252
|
+
return (
|
|
253
|
+
Object.keys(actual).length === entries.length &&
|
|
254
|
+
Object.entries(actual).every(([key, value]) =>
|
|
255
|
+
entries.some(
|
|
256
|
+
([other, candidate]) => key === other && value === candidate,
|
|
257
|
+
),
|
|
258
|
+
)
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
export async function eventChain(ctx: Reader, reservation: Reservation) {
|
|
262
|
+
const attempt = await ctx.db.get(
|
|
263
|
+
"renewalOperationAttempts",
|
|
264
|
+
reservation.attemptId,
|
|
265
|
+
);
|
|
266
|
+
if (!attempt || attempt.operationId !== reservation.operationId)
|
|
267
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
268
|
+
const rows = await ctx.db
|
|
269
|
+
.query("renewalOperationEvents")
|
|
270
|
+
.withIndex("by_attemptId_and_sequence", (q) =>
|
|
271
|
+
q.eq("attemptId", reservation.attemptId),
|
|
272
|
+
)
|
|
273
|
+
.take(4);
|
|
274
|
+
if (rows.length > 3 || rows.length !== attempt.lastSequence)
|
|
275
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
276
|
+
const events = [];
|
|
277
|
+
for (let index = 0; index < rows.length; index++) {
|
|
278
|
+
const row = rows[index];
|
|
279
|
+
if (
|
|
280
|
+
!row ||
|
|
281
|
+
row.sequence !== index + 1 ||
|
|
282
|
+
row.operationId !== reservation.operationId ||
|
|
283
|
+
row.attemptId !== reservation.attemptId ||
|
|
284
|
+
row.stage !== reservation.stage ||
|
|
285
|
+
row.sourcePayloadId !== reservation.payloadId
|
|
286
|
+
)
|
|
287
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
288
|
+
const payload = await ctx.db.get("renewalOperationPayloads", row.payloadId);
|
|
289
|
+
if (!payload || payload.codecVersion !== 1)
|
|
290
|
+
throw Error("RENEWAL_JOURNAL_UNAVAILABLE");
|
|
291
|
+
const owner = payload.owner;
|
|
292
|
+
if (
|
|
293
|
+
!owner ||
|
|
294
|
+
owner.kind !== "event" ||
|
|
295
|
+
owner.operationId !== reservation.operationId ||
|
|
296
|
+
owner.attemptId !== reservation.attemptId ||
|
|
297
|
+
owner.stage !== reservation.stage ||
|
|
298
|
+
owner.sequence !== row.sequence
|
|
299
|
+
)
|
|
300
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
301
|
+
const full = await canonicalJournalEvent(row.sequence, payload.json);
|
|
302
|
+
if (full.json !== payload.json || full.binding !== row.binding)
|
|
303
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
304
|
+
events.push({
|
|
305
|
+
operationId: row.operationId,
|
|
306
|
+
attemptId: row.attemptId,
|
|
307
|
+
stage: row.stage,
|
|
308
|
+
sourcePayloadId: row.sourcePayloadId,
|
|
309
|
+
sequence: row.sequence,
|
|
310
|
+
binding: row.binding,
|
|
311
|
+
payloadId: row.payloadId,
|
|
312
|
+
eventId: row._id,
|
|
313
|
+
dataJSON: full.json,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
return events;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function acknowledgementConflict(): never {
|
|
320
|
+
throw Error("RENEWAL_JOURNAL_CONFLICT");
|
|
321
|
+
}
|
|
322
|
+
function isJSONObject(value: unknown): value is Record<string, unknown> {
|
|
323
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
324
|
+
}
|
|
325
|
+
function jsonObject(value: unknown) {
|
|
326
|
+
if (!isJSONObject(value)) acknowledgementConflict();
|
|
327
|
+
return value;
|
|
328
|
+
}
|
|
329
|
+
function sameJSON(left: unknown, right: unknown) {
|
|
330
|
+
return (
|
|
331
|
+
JSON.stringify(renewalSnapshot(left)) ===
|
|
332
|
+
JSON.stringify(renewalSnapshot(right))
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
type AttachReceipt = Awaited<
|
|
336
|
+
ReturnType<typeof constructRenewalScheduleAttachmentReceiptV2>
|
|
337
|
+
>;
|
|
338
|
+
|
|
339
|
+
/** Parsed JSON DATA admission only; no caller-object or provider provenance. */
|
|
340
|
+
export function admitAttachReceiptOutputDATA(
|
|
341
|
+
receiptJSON: string,
|
|
342
|
+
rebuilt: AttachReceipt,
|
|
343
|
+
) {
|
|
344
|
+
if (
|
|
345
|
+
typeof receiptJSON !== "string" ||
|
|
346
|
+
receiptJSON.length > 500000 ||
|
|
347
|
+
new TextEncoder().encode(receiptJSON).length > 500000
|
|
348
|
+
)
|
|
349
|
+
acknowledgementConflict();
|
|
350
|
+
const parsed: unknown = JSON.parse(receiptJSON);
|
|
351
|
+
if (!sameJSON(parsed, rebuilt)) acknowledgementConflict();
|
|
352
|
+
const output = renewalSnapshot({
|
|
353
|
+
status: "attach_acknowledged",
|
|
354
|
+
operationalAuthority: false,
|
|
355
|
+
receipt: parsed,
|
|
356
|
+
});
|
|
357
|
+
const slots = new Map<string, { expected: string; family: string }>([
|
|
358
|
+
[
|
|
359
|
+
JSON.stringify(["receipt", "logicalBinding"]),
|
|
360
|
+
{ expected: rebuilt.logicalBinding, family: "renewal" },
|
|
361
|
+
],
|
|
362
|
+
[
|
|
363
|
+
JSON.stringify(["receipt", "request", "logicalBinding"]),
|
|
364
|
+
{ expected: rebuilt.request.logicalBinding, family: "renewal" },
|
|
365
|
+
],
|
|
366
|
+
[
|
|
367
|
+
JSON.stringify(["receipt", "request", "requestIdentity", "binding"]),
|
|
368
|
+
{
|
|
369
|
+
expected: rebuilt.request.requestIdentity.binding,
|
|
370
|
+
family: "renewal-attach",
|
|
371
|
+
},
|
|
372
|
+
],
|
|
373
|
+
[
|
|
374
|
+
JSON.stringify(["receipt", "binding"]),
|
|
375
|
+
{ expected: rebuilt.binding, family: "renewal-attach-receipt" },
|
|
376
|
+
],
|
|
377
|
+
]);
|
|
378
|
+
const found = new Set<string>();
|
|
379
|
+
function walk(value: unknown, path: string[]) {
|
|
380
|
+
const key = JSON.stringify(path);
|
|
381
|
+
const slot = slots.get(key);
|
|
382
|
+
if (slot) {
|
|
383
|
+
if (
|
|
384
|
+
value !== slot.expected ||
|
|
385
|
+
typeof value !== "string" ||
|
|
386
|
+
!new RegExp("^" + slot.family + ":v2:sha256:[0-9a-f]{64}$").test(value)
|
|
387
|
+
)
|
|
388
|
+
acknowledgementConflict();
|
|
389
|
+
found.add(key);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
if (value === null || typeof value !== "object") {
|
|
393
|
+
requiredJSONSnapshot(value);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
for (const [name, child] of Object.entries(value)) {
|
|
397
|
+
requiredJSONSnapshot(name);
|
|
398
|
+
requiredJSONSnapshot({ [name]: child === null ? null : true });
|
|
399
|
+
walk(child, [...path, name]);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
walk(output, []);
|
|
403
|
+
if (
|
|
404
|
+
found.size !== 4 ||
|
|
405
|
+
new TextEncoder().encode(JSON.stringify(output)).length > 500000
|
|
406
|
+
)
|
|
407
|
+
acknowledgementConflict();
|
|
408
|
+
return JSON.stringify(output.receipt);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** Full stored-source/event/C3 consistency; these are unsigned DATA checks. */
|
|
412
|
+
export async function validateAttachAcknowledgement(
|
|
413
|
+
full: ReturnType<typeof canonicalJournalRecord>,
|
|
414
|
+
reservation: Reservation,
|
|
415
|
+
events: Awaited<ReturnType<typeof eventChain>>,
|
|
416
|
+
receiptJSON: string,
|
|
417
|
+
) {
|
|
418
|
+
if (
|
|
419
|
+
reservation.stage !== "attach" ||
|
|
420
|
+
(events.length !== 2 && events.length !== 3) ||
|
|
421
|
+
events[0].sequence !== 1 ||
|
|
422
|
+
events[1].sequence !== 2
|
|
423
|
+
)
|
|
424
|
+
acknowledgementConflict();
|
|
425
|
+
const parsedOriginal: unknown = JSON.parse(full.json);
|
|
426
|
+
const original = jsonObject(parsedOriginal);
|
|
427
|
+
const account = jsonObject(original.account);
|
|
428
|
+
const source = captureAttachSource(original.attachRequest);
|
|
429
|
+
if (!sameJSON(original.attachRequest, source)) acknowledgementConflict();
|
|
430
|
+
const parsedDispatch: unknown = JSON.parse(events[0].dataJSON);
|
|
431
|
+
const parsedResponse: unknown = JSON.parse(events[1].dataJSON);
|
|
432
|
+
captureAttachDispatch(parsedDispatch);
|
|
433
|
+
captureAttachResponse(parsedResponse);
|
|
434
|
+
const dispatch = jsonObject(parsedDispatch);
|
|
435
|
+
const response = jsonObject(parsedResponse);
|
|
436
|
+
const raw = captureAttachmentReceipt({
|
|
437
|
+
attachRequest: source,
|
|
438
|
+
actualDispatch: original.dispatch,
|
|
439
|
+
providerSnapshot: response.body,
|
|
440
|
+
});
|
|
441
|
+
// C3 internally rebuilds C1/C2 and compares the entire closed seven-wire input.
|
|
442
|
+
const rebuilt = await constructRenewalScheduleAttachmentReceiptV2({
|
|
443
|
+
attachRequest: source,
|
|
444
|
+
actualDispatch: raw.actualDispatch,
|
|
445
|
+
providerSnapshot: raw.providerSnapshot,
|
|
446
|
+
});
|
|
447
|
+
const request = rebuilt.request;
|
|
448
|
+
const seven = {
|
|
449
|
+
scope: request.scope,
|
|
450
|
+
apiVersion: request.apiVersion,
|
|
451
|
+
method: request.method,
|
|
452
|
+
path: request.path,
|
|
453
|
+
payload: request.payload,
|
|
454
|
+
key: request.key,
|
|
455
|
+
firstDispatchedAt: request.firstDispatchedAt,
|
|
456
|
+
};
|
|
457
|
+
if (
|
|
458
|
+
!sameJSON(original.dispatch, seven) ||
|
|
459
|
+
!sameJSON(dispatch.request, request) ||
|
|
460
|
+
!sameJSON(response.request, request) ||
|
|
461
|
+
original.stage !== "attach" ||
|
|
462
|
+
original.apiVersion !== request.apiVersion ||
|
|
463
|
+
full.requestBinding !== request.requestIdentity.binding ||
|
|
464
|
+
reservation.key !== request.key ||
|
|
465
|
+
reservation.apiVersion !== request.apiVersion ||
|
|
466
|
+
reservation.firstDispatchedAt !== request.firstDispatchedAt ||
|
|
467
|
+
account.mode !== "platform" ||
|
|
468
|
+
account.stripeAccount !== null ||
|
|
469
|
+
account.livemode !== rebuilt.livemode ||
|
|
470
|
+
!sameJSON(JSON.parse(reservation.accountJSON), account)
|
|
471
|
+
)
|
|
472
|
+
acknowledgementConflict();
|
|
473
|
+
if ("recordVersion" in reservation) {
|
|
474
|
+
if (
|
|
475
|
+
!sameJSON(JSON.parse(reservation.timingPolicyJSON), original.timingPolicy)
|
|
476
|
+
)
|
|
477
|
+
acknowledgementConflict();
|
|
478
|
+
} else if (!sameJSON(JSON.parse(reservation.basisJSON), original.basis))
|
|
479
|
+
acknowledgementConflict();
|
|
480
|
+
const admittedJSON = admitAttachReceiptOutputDATA(receiptJSON, rebuilt);
|
|
481
|
+
// Only ACTUAL published event3 is checked. A head2 retention does not model
|
|
482
|
+
// or require a prospective receipt-event envelope to pass another cap.
|
|
483
|
+
if (events.length === 3)
|
|
484
|
+
validateAttachReceiptEventDATA(events[2].dataJSON, rebuilt);
|
|
485
|
+
return {
|
|
486
|
+
receiptJSON: admittedJSON,
|
|
487
|
+
receiptBinding: rebuilt.binding,
|
|
488
|
+
receipt: rebuilt,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/** Whole typed C10 DATA and full request/receipt equality, never ACK authority. */
|
|
493
|
+
export function validateAttachReceiptEventDATA(
|
|
494
|
+
raw: string,
|
|
495
|
+
receipt: AttachReceipt,
|
|
496
|
+
) {
|
|
497
|
+
const parsed: unknown = JSON.parse(raw);
|
|
498
|
+
captureAttachReceipt(parsed);
|
|
499
|
+
if (
|
|
500
|
+
!sameJSON(parsed, {
|
|
501
|
+
event: "receipt",
|
|
502
|
+
stage: "attach",
|
|
503
|
+
request: receipt.request,
|
|
504
|
+
receipt,
|
|
505
|
+
})
|
|
506
|
+
)
|
|
507
|
+
acknowledgementConflict();
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
/** Optional stored known DATA; every reference, owner and full JSON is checked. */
|
|
511
|
+
export async function validateKnownAcknowledgement(
|
|
512
|
+
ctx: Reader,
|
|
513
|
+
row: Doc<"renewalOperations">,
|
|
514
|
+
reservation: Reservation,
|
|
515
|
+
events: Awaited<ReturnType<typeof eventChain>>,
|
|
516
|
+
admitted: Pick<
|
|
517
|
+
Awaited<ReturnType<typeof validateAttachAcknowledgement>>,
|
|
518
|
+
"receiptJSON" | "receiptBinding"
|
|
519
|
+
>,
|
|
520
|
+
) {
|
|
521
|
+
const retained = row.knownAcknowledgement;
|
|
522
|
+
if (retained === undefined) return null;
|
|
523
|
+
if (
|
|
524
|
+
events.length < 2 ||
|
|
525
|
+
retained.schemaVersion !== 1 ||
|
|
526
|
+
retained.stage !== "attach" ||
|
|
527
|
+
retained.attemptId !== reservation.attemptId ||
|
|
528
|
+
retained.sourcePayloadId !== reservation.payloadId ||
|
|
529
|
+
retained.dispatchEventId !== events[0].eventId ||
|
|
530
|
+
retained.dispatchPayloadId !== events[0].payloadId ||
|
|
531
|
+
retained.responseEventId !== events[1].eventId ||
|
|
532
|
+
retained.responsePayloadId !== events[1].payloadId ||
|
|
533
|
+
retained.receiptBinding !== admitted.receiptBinding
|
|
534
|
+
)
|
|
535
|
+
acknowledgementConflict();
|
|
536
|
+
const payload = await ctx.db.get(
|
|
537
|
+
"renewalOperationPayloads",
|
|
538
|
+
retained.receiptPayloadId,
|
|
539
|
+
);
|
|
540
|
+
const owner = payload?.owner;
|
|
541
|
+
if (
|
|
542
|
+
!payload ||
|
|
543
|
+
payload.codecVersion !== 1 ||
|
|
544
|
+
payload.json !== admitted.receiptJSON ||
|
|
545
|
+
!owner ||
|
|
546
|
+
owner.kind !== "receipt" ||
|
|
547
|
+
owner.operationId !== row._id ||
|
|
548
|
+
owner.attemptId !== reservation.attemptId ||
|
|
549
|
+
owner.stage !== "attach"
|
|
550
|
+
)
|
|
551
|
+
acknowledgementConflict();
|
|
552
|
+
return retained;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
/** Leaf over an already checked chain; never creates lineage or reloads history. */
|
|
556
|
+
export async function validateV2KnownAcknowledgement(
|
|
557
|
+
ctx: Reader,
|
|
558
|
+
row: Doc<"renewalOperations">,
|
|
559
|
+
full: ReturnType<typeof canonicalJournalRecord>,
|
|
560
|
+
reservation: Reservation,
|
|
561
|
+
events: Awaited<ReturnType<typeof eventChain>>,
|
|
562
|
+
) {
|
|
563
|
+
if (
|
|
564
|
+
!("recordVersion" in reservation) ||
|
|
565
|
+
row.knownAcknowledgement === undefined
|
|
566
|
+
)
|
|
567
|
+
return null;
|
|
568
|
+
try {
|
|
569
|
+
const payload = await ctx.db.get(
|
|
570
|
+
"renewalOperationPayloads",
|
|
571
|
+
row.knownAcknowledgement.receiptPayloadId,
|
|
572
|
+
);
|
|
573
|
+
if (!payload) acknowledgementConflict();
|
|
574
|
+
const admitted = await validateAttachAcknowledgement(
|
|
575
|
+
full,
|
|
576
|
+
reservation,
|
|
577
|
+
events,
|
|
578
|
+
payload.json,
|
|
579
|
+
);
|
|
580
|
+
const retained = await validateKnownAcknowledgement(
|
|
581
|
+
ctx,
|
|
582
|
+
row,
|
|
583
|
+
reservation,
|
|
584
|
+
events,
|
|
585
|
+
admitted,
|
|
586
|
+
);
|
|
587
|
+
if (retained === null) acknowledgementConflict();
|
|
588
|
+
return { retained, admitted };
|
|
589
|
+
} catch {
|
|
590
|
+
acknowledgementConflict();
|
|
591
|
+
}
|
|
592
|
+
}
|