@fantastic.dev/better-stripe 0.35.0 → 0.37.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 +98 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +178 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +89 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +178 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +89 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/operationEvidence.d.ts +37 -0
- package/dist/client/operationEvidence.d.ts.map +1 -0
- package/dist/client/operationEvidence.js +44 -0
- package/dist/client/operationEvidence.js.map +1 -0
- package/dist/client/publicTypes.d.ts +1 -0
- package/dist/client/publicTypes.d.ts.map +1 -1
- package/dist/client/readContracts.d.ts +2 -0
- package/dist/client/readContracts.d.ts.map +1 -1
- package/dist/client/readContracts.js +4 -0
- package/dist/client/readContracts.js.map +1 -1
- package/dist/component/_generated/component.d.ts +181 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/earnings/pairedAdjustment.d.ts +12 -0
- package/dist/component/earnings/pairedAdjustment.d.ts.map +1 -0
- package/dist/component/earnings/pairedAdjustment.js +46 -0
- package/dist/component/earnings/pairedAdjustment.js.map +1 -0
- package/dist/component/earnings/triggers.d.ts +120 -0
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/earnings/triggers.js +10 -2
- package/dist/component/earnings/triggers.js.map +1 -1
- package/dist/component/operationEvidence/canonical.d.ts +4 -0
- package/dist/component/operationEvidence/canonical.d.ts.map +1 -0
- package/dist/component/operationEvidence/canonical.js +8 -0
- package/dist/component/operationEvidence/canonical.js.map +1 -0
- package/dist/component/operationEvidence/helpers.d.ts +87 -0
- package/dist/component/operationEvidence/helpers.d.ts.map +1 -0
- package/dist/component/operationEvidence/helpers.js +49 -0
- package/dist/component/operationEvidence/helpers.js.map +1 -0
- package/dist/component/operationEvidence/maintenance.d.ts +88 -0
- package/dist/component/operationEvidence/maintenance.d.ts.map +1 -0
- package/dist/component/operationEvidence/maintenance.js +59 -0
- package/dist/component/operationEvidence/maintenance.js.map +1 -0
- package/dist/component/operationEvidence/mutations.d.ts +97 -0
- package/dist/component/operationEvidence/mutations.d.ts.map +1 -0
- package/dist/component/operationEvidence/mutations.js +14 -0
- package/dist/component/operationEvidence/mutations.js.map +1 -0
- package/dist/component/operationEvidence/queries.d.ts +205 -0
- package/dist/component/operationEvidence/queries.d.ts.map +1 -0
- package/dist/component/operationEvidence/queries.js +75 -0
- package/dist/component/operationEvidence/queries.js.map +1 -0
- package/dist/component/operationEvidence/schema.d.ts +536 -0
- package/dist/component/operationEvidence/schema.d.ts.map +1 -0
- package/dist/component/operationEvidence/schema.js +53 -0
- package/dist/component/operationEvidence/schema.js.map +1 -0
- package/dist/component/payoutRelease/queries.d.ts +2 -2
- package/dist/component/payoutRelease/queries.d.ts.map +1 -1
- package/dist/component/refundHistory/commandEvidence.d.ts +1 -1
- package/dist/component/refundHistory/commandEvidence.d.ts.map +1 -1
- package/dist/component/refundHistory/commandEvidence.js +6 -2
- package/dist/component/refundHistory/commandEvidence.js.map +1 -1
- package/dist/component/refundHistory/originalSeller.d.ts +5 -0
- package/dist/component/refundHistory/originalSeller.d.ts.map +1 -0
- package/dist/component/refundHistory/originalSeller.js +35 -0
- package/dist/component/refundHistory/originalSeller.js.map +1 -0
- package/dist/component/refundHistory/queries.d.ts +1 -1
- package/dist/component/schema.d.ts +89 -0
- package/dist/component/schema.js +3 -0
- package/dist/component/schema.js.map +1 -1
- package/docs/financial-observations.md +85 -0
- package/docs/refund-command-evidence.md +6 -0
- package/package.json +1 -1
- package/src/client/operationEvidence.test.ts +110 -0
- package/src/client/operationEvidence.ts +92 -0
- package/src/client/publicTypes.ts +8 -0
- package/src/client/readContracts.ts +4 -0
- package/src/component/_generated/api.ts +10 -0
- package/src/component/_generated/component.ts +207 -0
- package/src/component/earnings/paired-adjustment.test.ts +258 -0
- package/src/component/earnings/pairedAdjustment.ts +77 -0
- package/src/component/earnings/triggers.ts +14 -10
- package/src/component/operationEvidence/canonical.ts +10 -0
- package/src/component/operationEvidence/helpers.ts +65 -0
- package/src/component/operationEvidence/integrity.test.ts +257 -0
- package/src/component/operationEvidence/maintenance.test.ts +152 -0
- package/src/component/operationEvidence/maintenance.ts +89 -0
- package/src/component/operationEvidence/mutations.ts +14 -0
- package/src/component/operationEvidence/queries.ts +108 -0
- package/src/component/operationEvidence/reads.test.ts +140 -0
- package/src/component/operationEvidence/schema.ts +62 -0
- package/src/component/refundHistory/commandEvidence.ts +7 -2
- package/src/component/refundHistory/original-seller-boundaries.test.ts +138 -0
- package/src/component/refundHistory/original-seller.test.ts +175 -0
- package/src/component/refundHistory/originalSeller.ts +48 -0
- package/src/component/schema.ts +6 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import { makeFunctionReference } from "convex/server";
|
|
3
|
+
import { expect, it } from "vitest";
|
|
4
|
+
import { modules, schema } from "../../test.js";
|
|
5
|
+
import type { Doc } from "../_generated/dataModel.js";
|
|
6
|
+
const capture = makeFunctionReference<"mutation">(
|
|
7
|
+
"operationEvidence/mutations:capture",
|
|
8
|
+
);
|
|
9
|
+
const resolve = makeFunctionReference<"query">(
|
|
10
|
+
"operationEvidence/queries:resolve",
|
|
11
|
+
);
|
|
12
|
+
const headers = makeFunctionReference<"query">(
|
|
13
|
+
"operationEvidence/queries:headers",
|
|
14
|
+
);
|
|
15
|
+
const original = makeFunctionReference<"query">(
|
|
16
|
+
"operationEvidence/queries:original",
|
|
17
|
+
);
|
|
18
|
+
const latest = makeFunctionReference<"query">(
|
|
19
|
+
"operationEvidence/queries:latest",
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
it("pages compact references without hydrating absent payloads and reports current generation", async () => {
|
|
23
|
+
const t = convexTest(schema, modules);
|
|
24
|
+
const ids = await t.run(async (ctx) => {
|
|
25
|
+
const result = [];
|
|
26
|
+
for (let i = 0; i < 3; i++)
|
|
27
|
+
result.push(
|
|
28
|
+
await ctx.db.insert("transferReversalOps", {
|
|
29
|
+
operationId: `operation_${i}`,
|
|
30
|
+
sourceChargeId: `charge_${i}`,
|
|
31
|
+
slices: [],
|
|
32
|
+
}),
|
|
33
|
+
);
|
|
34
|
+
return result;
|
|
35
|
+
});
|
|
36
|
+
const evidence: Array<{ header: Doc<"operationEvidenceHeaders"> }> = [];
|
|
37
|
+
for (const sourceId of ids)
|
|
38
|
+
evidence.push(await t.mutation(capture, { sourceId }));
|
|
39
|
+
await t.run(async (ctx) => {
|
|
40
|
+
for (const item of evidence) {
|
|
41
|
+
await ctx.db.delete(
|
|
42
|
+
"operationEvidencePayloads",
|
|
43
|
+
item.header.originalPayloadId,
|
|
44
|
+
);
|
|
45
|
+
await ctx.db.delete(
|
|
46
|
+
"operationEvidencePayloads",
|
|
47
|
+
item.header.latestPayloadId,
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
const first = await t.query(headers, {
|
|
52
|
+
paginationOpts: { numItems: 2, cursor: null },
|
|
53
|
+
});
|
|
54
|
+
expect(first).toMatchObject({
|
|
55
|
+
coverage: "unknown",
|
|
56
|
+
evidenceGeneration: 3,
|
|
57
|
+
isDone: false,
|
|
58
|
+
});
|
|
59
|
+
expect(first.page).toHaveLength(2);
|
|
60
|
+
expect(first.page[0]).not.toHaveProperty("slices");
|
|
61
|
+
expect(first.page[0]).not.toHaveProperty("operationId");
|
|
62
|
+
const next = await t.query(headers, {
|
|
63
|
+
paginationOpts: { numItems: 2, cursor: first.continueCursor },
|
|
64
|
+
});
|
|
65
|
+
expect([...first.page, ...next.page]).toHaveLength(3);
|
|
66
|
+
expect(
|
|
67
|
+
new Set([...first.page, ...next.page].map((row) => row._id)).size,
|
|
68
|
+
).toBe(3);
|
|
69
|
+
expect(
|
|
70
|
+
(await t.query(original, { headerId: evidence[0].header._id })).payload,
|
|
71
|
+
).toBeNull();
|
|
72
|
+
expect(
|
|
73
|
+
(await t.query(latest, { headerId: evidence[0].header._id })).payload,
|
|
74
|
+
).toBeNull();
|
|
75
|
+
for (const numItems of [0, 101, 1.5])
|
|
76
|
+
await expect(
|
|
77
|
+
t.query(headers, { paginationOpts: { numItems, cursor: null } }),
|
|
78
|
+
).rejects.toThrow();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("resolves an admitted large selector exactly and rejects reuse and forced unequal digest collision", async () => {
|
|
82
|
+
const t = convexTest(schema, modules);
|
|
83
|
+
const large = "\u0000".repeat(5000);
|
|
84
|
+
const ids = await t.run(async (ctx) => {
|
|
85
|
+
const one = await ctx.db.insert("transferReversalOps", {
|
|
86
|
+
operationId: large,
|
|
87
|
+
sourceChargeId: "charge",
|
|
88
|
+
slices: [],
|
|
89
|
+
});
|
|
90
|
+
const two = await ctx.db.insert("transferReversalOps", {
|
|
91
|
+
operationId: "different",
|
|
92
|
+
sourceChargeId: "other_charge",
|
|
93
|
+
slices: [],
|
|
94
|
+
});
|
|
95
|
+
return { one, two };
|
|
96
|
+
});
|
|
97
|
+
const one = await t.mutation(capture, { sourceId: ids.one });
|
|
98
|
+
const two = await t.mutation(capture, { sourceId: ids.two });
|
|
99
|
+
expect(
|
|
100
|
+
await t.query(resolve, { selector: "operationId", value: large }),
|
|
101
|
+
).toMatchObject({
|
|
102
|
+
coverage: "unknown",
|
|
103
|
+
evidenceGeneration: 2,
|
|
104
|
+
header: one.header,
|
|
105
|
+
});
|
|
106
|
+
expect(
|
|
107
|
+
await t.query(resolve, { selector: "sourceChargeId", value: "charge" }),
|
|
108
|
+
).toMatchObject({ header: one.header });
|
|
109
|
+
expect(
|
|
110
|
+
await t.query(resolve, { selector: "operationId", value: "missing" }),
|
|
111
|
+
).toMatchObject({ header: null });
|
|
112
|
+
await t.run((ctx) =>
|
|
113
|
+
ctx.db.patch("operationEvidenceHeaders", one.header._id, {
|
|
114
|
+
operationDigest: two.header.operationDigest,
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
117
|
+
expect(
|
|
118
|
+
await t.query(resolve, { selector: "operationId", value: "different" }),
|
|
119
|
+
).toMatchObject({ header: null });
|
|
120
|
+
await t.run((ctx) =>
|
|
121
|
+
ctx.db.delete("operationEvidenceHeaders", two.header._id),
|
|
122
|
+
);
|
|
123
|
+
expect(
|
|
124
|
+
await t.query(resolve, { selector: "operationId", value: "different" }),
|
|
125
|
+
).toMatchObject({ header: null });
|
|
126
|
+
expect(
|
|
127
|
+
(await t.query(original, { headerId: one.header._id })).payload.operationId,
|
|
128
|
+
).toBe(large);
|
|
129
|
+
const duplicateSource = await t.run((ctx) =>
|
|
130
|
+
ctx.db.insert("transferReversalOps", {
|
|
131
|
+
operationId: large,
|
|
132
|
+
sourceChargeId: "charge",
|
|
133
|
+
slices: [],
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
136
|
+
await t.mutation(capture, { sourceId: duplicateSource });
|
|
137
|
+
expect(
|
|
138
|
+
await t.query(resolve, { selector: "sourceChargeId", value: "charge" }),
|
|
139
|
+
).toMatchObject({ header: null });
|
|
140
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
import { transferReversalOpFields } from "../connect/validators.js";
|
|
4
|
+
|
|
5
|
+
// Deliberately EXACT source fields. Adding even small annotations can exceed 1 MiB.
|
|
6
|
+
export const payloadsTable = defineTable(transferReversalOpFields);
|
|
7
|
+
export const payloadDoc = v.object({
|
|
8
|
+
...transferReversalOpFields,
|
|
9
|
+
_id: v.id("operationEvidencePayloads"),
|
|
10
|
+
_creationTime: v.number(),
|
|
11
|
+
});
|
|
12
|
+
export const headerFields = {
|
|
13
|
+
sourceId: v.id("transferReversalOps"),
|
|
14
|
+
originalPayloadId: v.id("operationEvidencePayloads"),
|
|
15
|
+
latestPayloadId: v.id("operationEvidencePayloads"),
|
|
16
|
+
producer: v.literal("trusted_host_maintenance"),
|
|
17
|
+
producerVersion: v.literal(1),
|
|
18
|
+
firstObservedAt: v.number(),
|
|
19
|
+
sourceCreatedAt: v.number(),
|
|
20
|
+
originalFingerprint: v.string(),
|
|
21
|
+
latestFingerprint: v.string(),
|
|
22
|
+
originalIdentityFingerprint: v.string(),
|
|
23
|
+
operationDigest: v.string(),
|
|
24
|
+
sourceChargeDigest: v.string(),
|
|
25
|
+
generation: v.number(),
|
|
26
|
+
isContradictory: v.boolean(),
|
|
27
|
+
};
|
|
28
|
+
export const headerDoc = v.object({
|
|
29
|
+
...headerFields,
|
|
30
|
+
_id: v.id("operationEvidenceHeaders"),
|
|
31
|
+
_creationTime: v.number(),
|
|
32
|
+
});
|
|
33
|
+
export const headersTable = defineTable(headerFields)
|
|
34
|
+
.index("by_source_id", ["sourceId"])
|
|
35
|
+
.index("by_operation_digest", ["operationDigest"])
|
|
36
|
+
.index("by_source_charge_digest", ["sourceChargeDigest"]);
|
|
37
|
+
export const readFields = {
|
|
38
|
+
coverage: v.literal("unknown"),
|
|
39
|
+
evidenceGeneration: v.union(v.number(), v.null()),
|
|
40
|
+
};
|
|
41
|
+
export const headerResult = v.object({
|
|
42
|
+
...readFields,
|
|
43
|
+
header: v.union(headerDoc, v.null()),
|
|
44
|
+
});
|
|
45
|
+
export const payloadResult = v.object({
|
|
46
|
+
...headerResult.fields,
|
|
47
|
+
payload: v.union(payloadDoc, v.null()),
|
|
48
|
+
});
|
|
49
|
+
export const captureResult = v.object({
|
|
50
|
+
...headerResult.fields,
|
|
51
|
+
status: v.union(
|
|
52
|
+
v.literal("captured"),
|
|
53
|
+
v.literal("updated"),
|
|
54
|
+
v.literal("unchanged"),
|
|
55
|
+
v.literal("unavailable"),
|
|
56
|
+
v.literal("source_unavailable"),
|
|
57
|
+
),
|
|
58
|
+
});
|
|
59
|
+
export const selectorValidator = v.union(
|
|
60
|
+
v.literal("operationId"),
|
|
61
|
+
v.literal("sourceChargeId"),
|
|
62
|
+
);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { originalRefundSeller } from "./originalSeller.js";
|
|
1
2
|
import type { QueryCtx } from "../_generated/server.js";
|
|
2
3
|
import {
|
|
3
4
|
assertRefundCommandScope,
|
|
@@ -37,8 +38,12 @@ export async function readRefundCommandEvidence(
|
|
|
37
38
|
status: "unavailable" as const,
|
|
38
39
|
reason: "correlation_conflict" as const,
|
|
39
40
|
};
|
|
41
|
+
const accountId =
|
|
42
|
+
row.accountId === undefined
|
|
43
|
+
? await originalRefundSeller(ctx, scope)
|
|
44
|
+
: row.accountId;
|
|
40
45
|
if (
|
|
41
|
-
|
|
46
|
+
accountId !== scope.accountId ||
|
|
42
47
|
row.stripePaymentIntentId !== scope.stripePaymentIntentId ||
|
|
43
48
|
row.stripeChargeId !== scope.stripeChargeId ||
|
|
44
49
|
row.currency !== scope.currency
|
|
@@ -67,5 +72,5 @@ export async function readRefundCommandEvidence(
|
|
|
67
72
|
.take(2);
|
|
68
73
|
if (identity.length !== 1 || identity[0]?._id !== row._id)
|
|
69
74
|
return { status: "unavailable" as const, reason: "ambiguous" as const };
|
|
70
|
-
return { status: "observed" as const, refund: row };
|
|
75
|
+
return { status: "observed" as const, refund: { ...row, accountId } };
|
|
71
76
|
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import { expect, it } from "vitest";
|
|
3
|
+
import { api } from "../_generated/api.js";
|
|
4
|
+
import { schema, modules } from "../../test.js";
|
|
5
|
+
|
|
6
|
+
const scope = {
|
|
7
|
+
commandKey: "dojo-refund:original_store_gap:v1",
|
|
8
|
+
accountId: "acct_original_store",
|
|
9
|
+
stripePaymentIntentId: "pi_original_split",
|
|
10
|
+
stripeChargeId: "ch_original_split",
|
|
11
|
+
currency: "usd",
|
|
12
|
+
};
|
|
13
|
+
const refund = {
|
|
14
|
+
stripeRefundId: "re_original_split",
|
|
15
|
+
stripePaymentIntentId: scope.stripePaymentIntentId,
|
|
16
|
+
stripeChargeId: scope.stripeChargeId,
|
|
17
|
+
amount: 50,
|
|
18
|
+
currency: "usd",
|
|
19
|
+
status: "pending" as const,
|
|
20
|
+
metadata: { betterStripeRefundCommand: scope.commandKey },
|
|
21
|
+
};
|
|
22
|
+
const payment = {
|
|
23
|
+
stripePaymentIntentId: scope.stripePaymentIntentId,
|
|
24
|
+
userId: "original_buyer",
|
|
25
|
+
accountId: "acct_original_buyer",
|
|
26
|
+
amount: 1000,
|
|
27
|
+
currency: "usd",
|
|
28
|
+
status: "succeeded" as const,
|
|
29
|
+
chargeType: "separate" as const,
|
|
30
|
+
splitRecipients: [
|
|
31
|
+
{
|
|
32
|
+
destinationAccountId: scope.accountId,
|
|
33
|
+
role: "store" as const,
|
|
34
|
+
amount: 700,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
destinationAccountId: "acct_affiliate",
|
|
38
|
+
role: "affiliate" as const,
|
|
39
|
+
amount: 300,
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
const read = api.refundHistory.queries.getRefundCommandEvidence;
|
|
44
|
+
|
|
45
|
+
const allocation = {
|
|
46
|
+
sourceChargeId: scope.stripeChargeId,
|
|
47
|
+
paymentIntentId: scope.stripePaymentIntentId,
|
|
48
|
+
currency: scope.currency,
|
|
49
|
+
gross: 1000,
|
|
50
|
+
processingFee: 0,
|
|
51
|
+
platformNet: 0,
|
|
52
|
+
transfers: payment.splitRecipients,
|
|
53
|
+
};
|
|
54
|
+
it.each([
|
|
55
|
+
{ paymentIntentId: undefined },
|
|
56
|
+
{ paymentIntentId: "pi_foreign" },
|
|
57
|
+
{ sourceChargeId: "ch_foreign" },
|
|
58
|
+
{ currency: "eur" },
|
|
59
|
+
{ transfers: [] },
|
|
60
|
+
{ transfers: [payment.splitRecipients[1]!] },
|
|
61
|
+
{
|
|
62
|
+
transfers: [
|
|
63
|
+
...payment.splitRecipients,
|
|
64
|
+
{
|
|
65
|
+
destinationAccountId: "acct_second_store",
|
|
66
|
+
role: "store" as const,
|
|
67
|
+
amount: 0,
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
transfers: [
|
|
73
|
+
...payment.splitRecipients,
|
|
74
|
+
{
|
|
75
|
+
destinationAccountId: scope.accountId,
|
|
76
|
+
role: "affiliate" as const,
|
|
77
|
+
amount: 0,
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
transfers: [
|
|
83
|
+
{ ...payment.splitRecipients[0]!, destinationAccountId: "malformed" },
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
{ transfers: [{ ...payment.splitRecipients[0]!, amount: 0.5 }] },
|
|
87
|
+
{ transfers: [{ ...payment.splitRecipients[0]!, amount: -1 }] },
|
|
88
|
+
])(
|
|
89
|
+
"holds malformed, missing or contradictory immutable allocation %#",
|
|
90
|
+
async (patch) => {
|
|
91
|
+
const t = convexTest(schema, modules);
|
|
92
|
+
await t.mutation(api.connect.mutations.upsertPayment, payment);
|
|
93
|
+
await t.mutation(api.connect.mutations.upsertRefund, refund);
|
|
94
|
+
await t.run((ctx) =>
|
|
95
|
+
ctx.db.insert("settlementAllocations", { ...allocation, ...patch }),
|
|
96
|
+
);
|
|
97
|
+
expect(await t.query(read, scope)).toMatchObject({
|
|
98
|
+
status: "unavailable",
|
|
99
|
+
reason: "scope_mismatch",
|
|
100
|
+
});
|
|
101
|
+
},
|
|
102
|
+
);
|
|
103
|
+
it("rejects duplicate exact-charge allocations before selecting any original store", async () => {
|
|
104
|
+
const t = convexTest(schema, modules);
|
|
105
|
+
await t.mutation(api.connect.mutations.upsertRefund, refund);
|
|
106
|
+
await t.run(async (ctx) => {
|
|
107
|
+
await ctx.db.insert("settlementAllocations", allocation);
|
|
108
|
+
await ctx.db.insert("settlementAllocations", {
|
|
109
|
+
...allocation,
|
|
110
|
+
transfers: [
|
|
111
|
+
{
|
|
112
|
+
destinationAccountId: "acct_other_store",
|
|
113
|
+
role: "store",
|
|
114
|
+
amount: 1000,
|
|
115
|
+
},
|
|
116
|
+
],
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
expect(await t.query(read, scope)).toMatchObject({ status: "unavailable" });
|
|
120
|
+
});
|
|
121
|
+
it.each(["stripePaymentIntentId", "stripeChargeId", "currency"] as const)(
|
|
122
|
+
"never qualifies foreign receipt %s",
|
|
123
|
+
async (field) => {
|
|
124
|
+
const t = convexTest(schema, modules);
|
|
125
|
+
await t.mutation(
|
|
126
|
+
api.settlement.mutations.reserveSettlementAllocation,
|
|
127
|
+
allocation,
|
|
128
|
+
);
|
|
129
|
+
await t.mutation(api.connect.mutations.upsertRefund, {
|
|
130
|
+
...refund,
|
|
131
|
+
[field]: field === "currency" ? "eur" : "foreign",
|
|
132
|
+
});
|
|
133
|
+
expect(await t.query(read, scope)).toMatchObject({
|
|
134
|
+
status: "unavailable",
|
|
135
|
+
reason: "scope_mismatch",
|
|
136
|
+
});
|
|
137
|
+
},
|
|
138
|
+
);
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { convexTest } from "convex-test";
|
|
2
|
+
import { expect, it } from "vitest";
|
|
3
|
+
import { api } from "../_generated/api.js";
|
|
4
|
+
import { schema, modules } from "../../test.js";
|
|
5
|
+
|
|
6
|
+
const scope = {
|
|
7
|
+
commandKey: "dojo-refund:original_store_gap:v1",
|
|
8
|
+
accountId: "acct_original_store",
|
|
9
|
+
stripePaymentIntentId: "pi_original_split",
|
|
10
|
+
stripeChargeId: "ch_original_split",
|
|
11
|
+
currency: "usd",
|
|
12
|
+
};
|
|
13
|
+
const refund = {
|
|
14
|
+
stripeRefundId: "re_original_split",
|
|
15
|
+
stripePaymentIntentId: scope.stripePaymentIntentId,
|
|
16
|
+
stripeChargeId: scope.stripeChargeId,
|
|
17
|
+
amount: 50,
|
|
18
|
+
currency: "usd",
|
|
19
|
+
status: "pending" as const,
|
|
20
|
+
metadata: { betterStripeRefundCommand: scope.commandKey },
|
|
21
|
+
};
|
|
22
|
+
const payment = {
|
|
23
|
+
stripePaymentIntentId: scope.stripePaymentIntentId,
|
|
24
|
+
userId: "original_buyer",
|
|
25
|
+
accountId: "acct_original_buyer",
|
|
26
|
+
amount: 1000,
|
|
27
|
+
currency: "usd",
|
|
28
|
+
status: "succeeded" as const,
|
|
29
|
+
chargeType: "separate" as const,
|
|
30
|
+
splitRecipients: [
|
|
31
|
+
{
|
|
32
|
+
destinationAccountId: scope.accountId,
|
|
33
|
+
role: "store" as const,
|
|
34
|
+
amount: 700,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
destinationAccountId: "acct_affiliate",
|
|
38
|
+
role: "affiliate" as const,
|
|
39
|
+
amount: 300,
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
};
|
|
43
|
+
const read = api.refundHistory.queries.getRefundCommandEvidence;
|
|
44
|
+
|
|
45
|
+
const allocation = {
|
|
46
|
+
sourceChargeId: scope.stripeChargeId,
|
|
47
|
+
paymentIntentId: scope.stripePaymentIntentId,
|
|
48
|
+
currency: scope.currency,
|
|
49
|
+
gross: 1000,
|
|
50
|
+
processingFee: 0,
|
|
51
|
+
platformNet: 0,
|
|
52
|
+
transfers: payment.splitRecipients,
|
|
53
|
+
};
|
|
54
|
+
it.each([false, true])(
|
|
55
|
+
"qualifies immutable original separate store, refund first=%s, without rewriting history",
|
|
56
|
+
async (isRefundFirst) => {
|
|
57
|
+
const t = convexTest(schema, modules);
|
|
58
|
+
if (isRefundFirst)
|
|
59
|
+
await t.mutation(api.connect.mutations.upsertRefund, refund);
|
|
60
|
+
await t.mutation(api.connect.mutations.upsertPayment, payment);
|
|
61
|
+
await t.mutation(
|
|
62
|
+
api.settlement.mutations.reserveSettlementAllocation,
|
|
63
|
+
allocation,
|
|
64
|
+
);
|
|
65
|
+
if (!isRefundFirst)
|
|
66
|
+
await t.mutation(api.connect.mutations.upsertRefund, refund);
|
|
67
|
+
const before = await t.run((ctx) => ctx.db.query("refunds").take(2));
|
|
68
|
+
expect(await t.query(read, scope)).toMatchObject({
|
|
69
|
+
status: "observed",
|
|
70
|
+
refund: { ...refund, accountId: scope.accountId },
|
|
71
|
+
});
|
|
72
|
+
expect(await t.run((ctx) => ctx.db.query("refunds").take(2))).toEqual(
|
|
73
|
+
before,
|
|
74
|
+
);
|
|
75
|
+
expect(before[0]?.accountId).toBeUndefined();
|
|
76
|
+
},
|
|
77
|
+
);
|
|
78
|
+
it("keeps original allocation authority after normal SAME-PI store reassignment and reserve replay", async () => {
|
|
79
|
+
const t = convexTest(schema, modules);
|
|
80
|
+
await t.mutation(api.connect.mutations.upsertPayment, payment);
|
|
81
|
+
const original = await t.mutation(
|
|
82
|
+
api.settlement.mutations.reserveSettlementAllocation,
|
|
83
|
+
allocation,
|
|
84
|
+
);
|
|
85
|
+
await t.mutation(api.connect.mutations.upsertRefund, refund);
|
|
86
|
+
const changed = [
|
|
87
|
+
{
|
|
88
|
+
destinationAccountId: "acct_replacement",
|
|
89
|
+
role: "store" as const,
|
|
90
|
+
amount: 700,
|
|
91
|
+
},
|
|
92
|
+
payment.splitRecipients[1]!,
|
|
93
|
+
];
|
|
94
|
+
await t.mutation(api.connect.mutations.upsertPayment, {
|
|
95
|
+
...payment,
|
|
96
|
+
splitRecipients: changed,
|
|
97
|
+
});
|
|
98
|
+
expect(
|
|
99
|
+
await t.mutation(api.settlement.mutations.reserveSettlementAllocation, {
|
|
100
|
+
...allocation,
|
|
101
|
+
transfers: changed,
|
|
102
|
+
}),
|
|
103
|
+
).toEqual(original);
|
|
104
|
+
expect(await t.query(read, scope)).toMatchObject({
|
|
105
|
+
status: "observed",
|
|
106
|
+
refund: { accountId: scope.accountId },
|
|
107
|
+
});
|
|
108
|
+
expect(
|
|
109
|
+
await t.query(read, { ...scope, accountId: "acct_replacement" }),
|
|
110
|
+
).toMatchObject({ status: "unavailable" });
|
|
111
|
+
expect(
|
|
112
|
+
await t.query(read, { ...scope, accountId: "acct_affiliate" }),
|
|
113
|
+
).toMatchObject({ status: "unavailable" });
|
|
114
|
+
});
|
|
115
|
+
it.each([false, true])(
|
|
116
|
+
"holds payment-only ownerless legacy provenance, destination=%s",
|
|
117
|
+
async (isDestination) => {
|
|
118
|
+
const t = convexTest(schema, modules);
|
|
119
|
+
await t.mutation(api.connect.mutations.upsertRefund, refund);
|
|
120
|
+
// Late raw history has no immutable capture allocation and no receipt owner.
|
|
121
|
+
await t.run((ctx) =>
|
|
122
|
+
ctx.db.insert(
|
|
123
|
+
"payments",
|
|
124
|
+
isDestination
|
|
125
|
+
? {
|
|
126
|
+
...payment,
|
|
127
|
+
chargeType: "destination",
|
|
128
|
+
splitRecipients: undefined,
|
|
129
|
+
destinationAccountId: scope.accountId,
|
|
130
|
+
}
|
|
131
|
+
: payment,
|
|
132
|
+
),
|
|
133
|
+
);
|
|
134
|
+
expect(await t.query(read, scope)).toMatchObject({
|
|
135
|
+
status: "unavailable",
|
|
136
|
+
reason: "scope_mismatch",
|
|
137
|
+
});
|
|
138
|
+
},
|
|
139
|
+
);
|
|
140
|
+
it("retains persisted destination-account parity", async () => {
|
|
141
|
+
const t = convexTest(schema, modules);
|
|
142
|
+
await t.mutation(api.connect.mutations.upsertPayment, {
|
|
143
|
+
...payment,
|
|
144
|
+
chargeType: "destination",
|
|
145
|
+
splitRecipients: [],
|
|
146
|
+
destinationAccountId: scope.accountId,
|
|
147
|
+
});
|
|
148
|
+
await t.mutation(api.connect.mutations.upsertRefund, refund);
|
|
149
|
+
expect(await t.query(read, scope)).toMatchObject({
|
|
150
|
+
status: "observed",
|
|
151
|
+
refund: { accountId: scope.accountId },
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
it.each(["acct_foreign", ""])(
|
|
155
|
+
"never hides persisted conflicting account %s",
|
|
156
|
+
async (accountId) => {
|
|
157
|
+
const t = convexTest(schema, modules);
|
|
158
|
+
await t.mutation(
|
|
159
|
+
api.settlement.mutations.reserveSettlementAllocation,
|
|
160
|
+
allocation,
|
|
161
|
+
);
|
|
162
|
+
await t.mutation(api.connect.mutations.upsertRefund, {
|
|
163
|
+
...refund,
|
|
164
|
+
accountId,
|
|
165
|
+
});
|
|
166
|
+
const before = await t.run((ctx) => ctx.db.query("refunds").take(1));
|
|
167
|
+
expect(await t.query(read, scope)).toMatchObject({
|
|
168
|
+
status: "unavailable",
|
|
169
|
+
reason: "scope_mismatch",
|
|
170
|
+
});
|
|
171
|
+
expect(await t.run((ctx) => ctx.db.query("refunds").take(1))).toEqual(
|
|
172
|
+
before,
|
|
173
|
+
);
|
|
174
|
+
},
|
|
175
|
+
);
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { QueryCtx } from "../_generated/server.js";
|
|
2
|
+
import type { Doc } from "../_generated/dataModel.js";
|
|
3
|
+
import type { RefundCommandScope } from "../../shared/refundCommandScope.js";
|
|
4
|
+
|
|
5
|
+
const account = (value: string) => /^acct_[A-Za-z0-9_]{1,128}$/.test(value);
|
|
6
|
+
|
|
7
|
+
/** Immutable allocation routing only. Observation does not authorize a refund. */
|
|
8
|
+
function seller(allocation: Doc<"settlementAllocations">): string | undefined {
|
|
9
|
+
const destinations = new Set<string>();
|
|
10
|
+
let store: string | undefined;
|
|
11
|
+
for (const recipient of allocation.transfers) {
|
|
12
|
+
if (
|
|
13
|
+
!account(recipient.destinationAccountId) ||
|
|
14
|
+
destinations.has(recipient.destinationAccountId) ||
|
|
15
|
+
!Number.isSafeInteger(recipient.amount) ||
|
|
16
|
+
recipient.amount < 0
|
|
17
|
+
)
|
|
18
|
+
return undefined;
|
|
19
|
+
destinations.add(recipient.destinationAccountId);
|
|
20
|
+
if (recipient.role === "store") {
|
|
21
|
+
if (store !== undefined) return undefined;
|
|
22
|
+
store = recipient.destinationAccountId;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return store;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** At most two exact-charge allocations; mutable payments never establish original ownership. */
|
|
29
|
+
export async function originalRefundSeller(
|
|
30
|
+
ctx: QueryCtx,
|
|
31
|
+
scope: RefundCommandScope,
|
|
32
|
+
): Promise<string | undefined> {
|
|
33
|
+
const allocations = await ctx.db
|
|
34
|
+
.query("settlementAllocations")
|
|
35
|
+
.withIndex("by_source_charge", (q) =>
|
|
36
|
+
q.eq("sourceChargeId", scope.stripeChargeId),
|
|
37
|
+
)
|
|
38
|
+
.take(2);
|
|
39
|
+
const allocation = allocations[0];
|
|
40
|
+
if (
|
|
41
|
+
allocations.length !== 1 ||
|
|
42
|
+
!allocation ||
|
|
43
|
+
allocation.paymentIntentId !== scope.stripePaymentIntentId ||
|
|
44
|
+
allocation.currency !== scope.currency
|
|
45
|
+
)
|
|
46
|
+
return undefined;
|
|
47
|
+
return seller(allocation);
|
|
48
|
+
}
|
package/src/component/schema.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
import {
|
|
2
|
+
payloadsTable as operationEvidencePayloads,
|
|
3
|
+
headersTable as operationEvidenceHeaders,
|
|
4
|
+
} from "./operationEvidence/schema.js";
|
|
1
5
|
import { transferBatchReferencesTable } from "./financialObservations/transferReferenceSchema.js";
|
|
2
6
|
import {
|
|
3
7
|
transferObservationsTable,
|
|
@@ -74,6 +78,8 @@ import { pricesTable, productsTable } from "./products/schema";
|
|
|
74
78
|
import { webhookEventsTable } from "./webhooks/schema";
|
|
75
79
|
|
|
76
80
|
export default defineSchema({
|
|
81
|
+
operationEvidencePayloads,
|
|
82
|
+
operationEvidenceHeaders,
|
|
77
83
|
financialTransferObservations: transferObservationsTable,
|
|
78
84
|
financialTransferBatches: transferBatchesTable,
|
|
79
85
|
financialTransferBatchReferences: transferBatchReferencesTable,
|