@fantastic.dev/better-stripe 0.45.2 → 0.46.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 +20 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts +46 -0
- package/dist/client/connect/payoutAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +23 -0
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +46 -0
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutSyncTestHelpers.d.ts +23 -0
- package/dist/client/connect/payoutSyncTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/refundSync.d.ts +32 -0
- package/dist/client/connect/refundSync.d.ts.map +1 -0
- package/dist/client/connect/refundSync.js +61 -0
- package/dist/client/connect/refundSync.js.map +1 -0
- package/dist/client/connect/refundSyncValidation.d.ts +16 -0
- package/dist/client/connect/refundSyncValidation.d.ts.map +1 -0
- package/dist/client/connect/refundSyncValidation.js +87 -0
- package/dist/client/connect/refundSyncValidation.js.map +1 -0
- package/dist/client/index.d.ts +30 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +5 -0
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/api.d.ts +12 -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 +67 -0
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/connect/mutations.d.ts.map +1 -1
- package/dist/component/connect/mutations.js +2 -53
- package/dist/component/connect/mutations.js.map +1 -1
- package/dist/component/connect/refundWrites.d.ts +6 -0
- package/dist/component/connect/refundWrites.d.ts.map +1 -0
- package/dist/component/connect/refundWrites.js +56 -0
- package/dist/component/connect/refundWrites.js.map +1 -0
- package/dist/component/earnings/triggers.d.ts +46 -0
- package/dist/component/earnings/triggers.d.ts.map +1 -1
- package/dist/component/processingCostReconciliation/rollback.d.ts +1 -1
- package/dist/component/refundHistory/originalSeller.d.ts +1 -1
- package/dist/component/refundHistory/originalSeller.d.ts.map +1 -1
- package/dist/component/refundHistory/originalSeller.js.map +1 -1
- package/dist/component/refundSync/helpers.d.ts +43 -0
- package/dist/component/refundSync/helpers.d.ts.map +1 -0
- package/dist/component/refundSync/helpers.js +71 -0
- package/dist/component/refundSync/helpers.js.map +1 -0
- package/dist/component/refundSync/mutations.d.ts +56 -0
- package/dist/component/refundSync/mutations.d.ts.map +1 -0
- package/dist/component/refundSync/mutations.js +113 -0
- package/dist/component/refundSync/mutations.js.map +1 -0
- package/dist/component/refundSync/queries.d.ts +12 -0
- package/dist/component/refundSync/queries.d.ts.map +1 -0
- package/dist/component/refundSync/queries.js +10 -0
- package/dist/component/refundSync/queries.js.map +1 -0
- package/dist/component/refundSync/routing.d.ts +8 -0
- package/dist/component/refundSync/routing.d.ts.map +1 -0
- package/dist/component/refundSync/routing.js +42 -0
- package/dist/component/refundSync/routing.js.map +1 -0
- package/dist/component/refundSync/schema.d.ts +25 -0
- package/dist/component/refundSync/schema.d.ts.map +1 -0
- package/dist/component/refundSync/schema.js +15 -0
- package/dist/component/refundSync/schema.js.map +1 -0
- package/dist/component/refundSync/validators.d.ts +164 -0
- package/dist/component/refundSync/validators.d.ts.map +1 -0
- package/dist/component/refundSync/validators.js +32 -0
- package/dist/component/refundSync/validators.js.map +1 -0
- package/dist/component/schema.d.ts +23 -0
- package/dist/component/schema.js +2 -0
- package/dist/component/schema.js.map +1 -1
- package/docs/refund-context.md +54 -0
- package/package.json +1 -1
- package/src/client/connect/refundSync.test.ts +239 -0
- package/src/client/connect/refundSync.transport.test.ts +396 -0
- package/src/client/connect/refundSync.ts +125 -0
- package/src/client/connect/refundSyncValidation.ts +120 -0
- package/src/client/index.ts +19 -0
- package/src/component/_generated/api.ts +12 -0
- package/src/component/_generated/component.ts +100 -0
- package/src/component/connect/mutations.ts +2 -67
- package/src/component/connect/refundWrites.ts +76 -0
- package/src/component/refundHistory/originalSeller.ts +4 -1
- package/src/component/refundSync/helpers.ts +99 -0
- package/src/component/refundSync/mutations.ts +131 -0
- package/src/component/refundSync/queries.ts +10 -0
- package/src/component/refundSync/refund-sync.test.ts +255 -0
- package/src/component/refundSync/routing.ts +55 -0
- package/src/component/refundSync/schema.ts +19 -0
- package/src/component/refundSync/validators.ts +35 -0
- package/src/component/schema.ts +2 -0
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { mutation } from "../earnings/triggers.js";
|
|
3
|
+
import { writeRefund } from "../connect/refundWrites.js";
|
|
4
|
+
import {
|
|
5
|
+
claimByRefund,
|
|
6
|
+
refundById,
|
|
7
|
+
validateIdentity,
|
|
8
|
+
validateSnapshot,
|
|
9
|
+
assertOriginal,
|
|
10
|
+
fingerprint,
|
|
11
|
+
} from "./helpers.js";
|
|
12
|
+
import { refundRouting } from "./routing.js";
|
|
13
|
+
import { identity, prepared, result } from "./validators.js";
|
|
14
|
+
|
|
15
|
+
/** Failed first reads cannot permanently claim an unverified provider API namespace. */
|
|
16
|
+
export const begin = mutation({
|
|
17
|
+
args: identity,
|
|
18
|
+
returns: v.number(),
|
|
19
|
+
handler: async (ctx, args) => {
|
|
20
|
+
validateIdentity(args);
|
|
21
|
+
const claim = await claimByRefund(ctx, args.stripeRefundId);
|
|
22
|
+
await refundById(ctx, args.stripeRefundId);
|
|
23
|
+
if (
|
|
24
|
+
claim?.acceptedContext !== undefined &&
|
|
25
|
+
claim.acceptedContext !== args.apiContext
|
|
26
|
+
)
|
|
27
|
+
throw new Error("Accepted refund API context does not match.");
|
|
28
|
+
const generation = (claim?.generation ?? 0) + 1;
|
|
29
|
+
if (!Number.isSafeInteger(generation) || generation <= 0)
|
|
30
|
+
throw new Error("Refund synchronization generation exhausted.");
|
|
31
|
+
const fields = {
|
|
32
|
+
generation,
|
|
33
|
+
requestedContext: args.apiContext,
|
|
34
|
+
status: "pending" as const,
|
|
35
|
+
startedAt: Date.now(),
|
|
36
|
+
};
|
|
37
|
+
if (claim) await ctx.db.patch("refundSyncClaims", claim._id, fields);
|
|
38
|
+
else
|
|
39
|
+
await ctx.db.insert("refundSyncClaims", {
|
|
40
|
+
stripeRefundId: args.stripeRefundId,
|
|
41
|
+
...fields,
|
|
42
|
+
});
|
|
43
|
+
return generation;
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
/** Accept the qualified observation and unchanged financial writer atomically. */
|
|
48
|
+
export const finish = mutation({
|
|
49
|
+
args: prepared,
|
|
50
|
+
returns: result,
|
|
51
|
+
handler: async (ctx, args) => {
|
|
52
|
+
validateIdentity(args);
|
|
53
|
+
if (!Number.isSafeInteger(args.generation) || args.generation <= 0)
|
|
54
|
+
throw new Error("Invalid refund synchronization generation.");
|
|
55
|
+
const claim = await claimByRefund(ctx, args.stripeRefundId);
|
|
56
|
+
if (!claim) throw new Error("Refund synchronization claim is missing.");
|
|
57
|
+
if (claim.generation !== args.generation)
|
|
58
|
+
return { status: "superseded" as const, generation: args.generation };
|
|
59
|
+
if (
|
|
60
|
+
claim.requestedContext !== args.apiContext ||
|
|
61
|
+
(claim.acceptedContext !== undefined &&
|
|
62
|
+
claim.acceptedContext !== args.apiContext)
|
|
63
|
+
)
|
|
64
|
+
throw new Error("Refund synchronization API context does not match.");
|
|
65
|
+
validateSnapshot(args.refund);
|
|
66
|
+
if (args.refund.stripeRefundId !== args.stripeRefundId)
|
|
67
|
+
throw new Error("Refund synchronization identity does not match.");
|
|
68
|
+
const routing = await refundRouting(ctx, args.refund);
|
|
69
|
+
if (
|
|
70
|
+
routing.status === "held" ||
|
|
71
|
+
(routing.merchantAccount !== null &&
|
|
72
|
+
routing.merchantAccount !== args.refund.accountId) ||
|
|
73
|
+
(routing.requiresChargeProof && args.refund.accountId === undefined)
|
|
74
|
+
)
|
|
75
|
+
throw new Error(
|
|
76
|
+
"Original refund merchant is unavailable or contradictory.",
|
|
77
|
+
);
|
|
78
|
+
const existing = await refundById(ctx, args.stripeRefundId);
|
|
79
|
+
assertOriginal(existing, args.refund);
|
|
80
|
+
const nextFingerprint = await fingerprint(args.refund);
|
|
81
|
+
const changed =
|
|
82
|
+
!existing || (await fingerprint(existing)) !== nextFingerprint;
|
|
83
|
+
if (claim.status === "accepted") {
|
|
84
|
+
if (claim.acceptedFingerprint !== nextFingerprint || changed || !existing)
|
|
85
|
+
throw new Error(
|
|
86
|
+
"Accepted refund observation changed outside its generation.",
|
|
87
|
+
);
|
|
88
|
+
return {
|
|
89
|
+
status: "accepted" as const,
|
|
90
|
+
generation: args.generation,
|
|
91
|
+
changed: false,
|
|
92
|
+
refund: existing,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
await writeRefund(ctx, {
|
|
96
|
+
...args.refund,
|
|
97
|
+
reason: args.refund.reason,
|
|
98
|
+
failureReason: args.refund.failureReason,
|
|
99
|
+
});
|
|
100
|
+
const refund = await refundById(ctx, args.stripeRefundId);
|
|
101
|
+
if (!refund) throw new Error("Accepted refund was not persisted.");
|
|
102
|
+
await ctx.db.patch("refundSyncClaims", claim._id, {
|
|
103
|
+
status: "accepted",
|
|
104
|
+
acceptedContext: args.apiContext,
|
|
105
|
+
acceptedGeneration: args.generation,
|
|
106
|
+
acceptedAt: Date.now(),
|
|
107
|
+
acceptedFingerprint: nextFingerprint,
|
|
108
|
+
});
|
|
109
|
+
return {
|
|
110
|
+
status: "accepted" as const,
|
|
111
|
+
generation: args.generation,
|
|
112
|
+
changed,
|
|
113
|
+
refund,
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
export const markUnknown = mutation({
|
|
118
|
+
args: { ...identity, generation: v.number() },
|
|
119
|
+
returns: v.null(),
|
|
120
|
+
handler: async (ctx, args) => {
|
|
121
|
+
validateIdentity(args);
|
|
122
|
+
const claim = await claimByRefund(ctx, args.stripeRefundId);
|
|
123
|
+
if (
|
|
124
|
+
claim?.generation === args.generation &&
|
|
125
|
+
claim.requestedContext === args.apiContext &&
|
|
126
|
+
claim.status === "pending"
|
|
127
|
+
)
|
|
128
|
+
await ctx.db.patch("refundSyncClaims", claim._id, { status: "unknown" });
|
|
129
|
+
return null;
|
|
130
|
+
},
|
|
131
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { query } from "../_generated/server.js";
|
|
2
|
+
import { routingInput, routingResult } from "./validators.js";
|
|
3
|
+
import { refundRouting } from "./routing.js";
|
|
4
|
+
|
|
5
|
+
/** Trusted-host routing proof only; this does not establish seller dispatch authority. */
|
|
6
|
+
export const routing = query({
|
|
7
|
+
args: routingInput,
|
|
8
|
+
returns: routingResult,
|
|
9
|
+
handler: refundRouting,
|
|
10
|
+
});
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
import { convexTest } from "convex-test";
|
|
3
|
+
import { makeFunctionReference } from "convex/server";
|
|
4
|
+
import { expect, it, vi } from "vitest";
|
|
5
|
+
import { schema, modules } from "../../test.js";
|
|
6
|
+
import { api } from "../_generated/api.js";
|
|
7
|
+
|
|
8
|
+
const identity = { stripeRefundId: "re_original", apiContext: "platform" };
|
|
9
|
+
const refund = {
|
|
10
|
+
stripeRefundId: "re_original",
|
|
11
|
+
stripePaymentIntentId: "pi_original",
|
|
12
|
+
stripeChargeId: "ch_original",
|
|
13
|
+
amount: 250,
|
|
14
|
+
currency: "usd",
|
|
15
|
+
status: "succeeded" as const,
|
|
16
|
+
metadata: { betterStripeRefundCommand: "original-command" },
|
|
17
|
+
};
|
|
18
|
+
const operation = (name: string) =>
|
|
19
|
+
makeFunctionReference<"mutation">(`refundSync/mutations:${name}`);
|
|
20
|
+
async function fixture() {
|
|
21
|
+
const t = convexTest(schema, modules);
|
|
22
|
+
const generation = await t.mutation(operation("begin"), identity);
|
|
23
|
+
return { t, args: { ...identity, generation, refund } };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
it("replays the same committed generation without changing observed time or its row", async () => {
|
|
27
|
+
vi.useFakeTimers();
|
|
28
|
+
try {
|
|
29
|
+
vi.setSystemTime(1000);
|
|
30
|
+
const { t, args } = await fixture();
|
|
31
|
+
await t.mutation(operation("finish"), args);
|
|
32
|
+
const first = await t.run((ctx) => ctx.db.query("refunds").unique());
|
|
33
|
+
vi.setSystemTime(2000);
|
|
34
|
+
expect(await t.mutation(operation("finish"), args)).toMatchObject({
|
|
35
|
+
status: "accepted",
|
|
36
|
+
changed: false,
|
|
37
|
+
});
|
|
38
|
+
expect(await t.run((ctx) => ctx.db.query("refunds").unique())).toEqual(
|
|
39
|
+
first,
|
|
40
|
+
);
|
|
41
|
+
expect(
|
|
42
|
+
await t.run((ctx) => ctx.db.query("refundSyncClaims").unique()),
|
|
43
|
+
).toMatchObject({ acceptedAt: 1000, status: "accepted" });
|
|
44
|
+
await expect(
|
|
45
|
+
t.mutation(operation("finish"), {
|
|
46
|
+
...args,
|
|
47
|
+
refund: { ...refund, status: "pending" },
|
|
48
|
+
}),
|
|
49
|
+
).rejects.toThrow("Accepted refund observation changed");
|
|
50
|
+
} finally {
|
|
51
|
+
vi.useRealTimers();
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("rejects original money and payment identity changes atomically", async () => {
|
|
56
|
+
const { t, args } = await fixture();
|
|
57
|
+
await t.mutation(operation("finish"), args);
|
|
58
|
+
const before = await t.run((ctx) => ctx.db.query("refunds").unique());
|
|
59
|
+
for (const patch of [
|
|
60
|
+
{ amount: 251 },
|
|
61
|
+
{ currency: "jpy" },
|
|
62
|
+
{ stripePaymentIntentId: "pi_foreign" },
|
|
63
|
+
{ stripeChargeId: "ch_foreign" },
|
|
64
|
+
]) {
|
|
65
|
+
const generation = await t.mutation(operation("begin"), identity);
|
|
66
|
+
await expect(
|
|
67
|
+
t.mutation(operation("finish"), {
|
|
68
|
+
...args,
|
|
69
|
+
generation,
|
|
70
|
+
refund: { ...refund, ...patch },
|
|
71
|
+
}),
|
|
72
|
+
).rejects.toThrow("Original refund identity");
|
|
73
|
+
expect(await t.run((ctx) => ctx.db.query("refunds").unique())).toEqual(
|
|
74
|
+
before,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("preserves first command normalization and permanently latches changed or removed keys", async () => {
|
|
80
|
+
const { t, args } = await fixture();
|
|
81
|
+
await t.run((ctx) =>
|
|
82
|
+
ctx.db.insert("refunds", {
|
|
83
|
+
...refund,
|
|
84
|
+
metadata: { betterStripeRefundCommand: "first-original-key" },
|
|
85
|
+
}),
|
|
86
|
+
);
|
|
87
|
+
await t.mutation(operation("finish"), args);
|
|
88
|
+
expect(await t.run((ctx) => ctx.db.query("refunds").unique())).toMatchObject({
|
|
89
|
+
refundCommandKey: "first-original-key",
|
|
90
|
+
refundCommandConflict: true,
|
|
91
|
+
refundCommandNormalizationVersion: 1,
|
|
92
|
+
});
|
|
93
|
+
const observed = (await t.run((ctx) => ctx.db.query("refunds").unique()))!
|
|
94
|
+
.refundCommandObservedAt;
|
|
95
|
+
const generation = await t.mutation(operation("begin"), identity);
|
|
96
|
+
await t.mutation(operation("finish"), {
|
|
97
|
+
...args,
|
|
98
|
+
generation,
|
|
99
|
+
refund: { ...refund, metadata: {} },
|
|
100
|
+
});
|
|
101
|
+
expect(await t.run((ctx) => ctx.db.query("refunds").unique())).toMatchObject({
|
|
102
|
+
refundCommandKey: "first-original-key",
|
|
103
|
+
refundCommandConflict: true,
|
|
104
|
+
refundCommandObservedAt: observed,
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("keeps omitted legacy metadata distinct from an explicit replacement", async () => {
|
|
109
|
+
const t = convexTest(schema, modules);
|
|
110
|
+
await t.mutation(api.connect.mutations.upsertRefund, refund);
|
|
111
|
+
const { metadata: _metadata, ...partial } = refund;
|
|
112
|
+
await t.mutation(api.connect.mutations.upsertRefund, {
|
|
113
|
+
...partial,
|
|
114
|
+
status: "pending",
|
|
115
|
+
});
|
|
116
|
+
expect(await t.run((ctx) => ctx.db.query("refunds").unique())).toMatchObject({
|
|
117
|
+
refundCommandKey: "original-command",
|
|
118
|
+
refundCommandConflict: false,
|
|
119
|
+
});
|
|
120
|
+
await t.mutation(api.connect.mutations.upsertRefund, {
|
|
121
|
+
...refund,
|
|
122
|
+
metadata: {},
|
|
123
|
+
});
|
|
124
|
+
expect(await t.run((ctx) => ctx.db.query("refunds").unique())).toMatchObject({
|
|
125
|
+
refundCommandConflict: true,
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("retains all 1005 failed/canceled rows while summing only the complete live refund contribution", async () => {
|
|
130
|
+
const { t, args } = await fixture();
|
|
131
|
+
await t.run(async (ctx) => {
|
|
132
|
+
for (let i = 0; i < 1005; i++)
|
|
133
|
+
await ctx.db.insert("refunds", {
|
|
134
|
+
...refund,
|
|
135
|
+
stripeRefundId: `re_excluded_${i}`,
|
|
136
|
+
amount: 10000,
|
|
137
|
+
status: i % 2 === 0 ? "failed" : "canceled",
|
|
138
|
+
});
|
|
139
|
+
await ctx.db.insert("payments", {
|
|
140
|
+
stripePaymentIntentId: "pi_original",
|
|
141
|
+
userId: "buyer",
|
|
142
|
+
amount: 1000,
|
|
143
|
+
currency: "usd",
|
|
144
|
+
status: "succeeded",
|
|
145
|
+
});
|
|
146
|
+
for (const [status, amount] of [
|
|
147
|
+
["pending", 50],
|
|
148
|
+
["requires_action", 100],
|
|
149
|
+
["succeeded", 200],
|
|
150
|
+
] as const)
|
|
151
|
+
await ctx.db.insert("refunds", {
|
|
152
|
+
...refund,
|
|
153
|
+
stripeRefundId: `re_live_${status}`,
|
|
154
|
+
status,
|
|
155
|
+
amount,
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
await t.mutation(operation("finish"), args);
|
|
159
|
+
expect(await t.run((ctx) => ctx.db.query("payments").unique())).toMatchObject(
|
|
160
|
+
{ refundedAmount: 600, refundStatus: "partially_refunded" },
|
|
161
|
+
);
|
|
162
|
+
expect(await t.run((ctx) => ctx.db.query("refunds").collect())).toHaveLength(
|
|
163
|
+
1009,
|
|
164
|
+
);
|
|
165
|
+
const generation = await t.mutation(operation("begin"), identity);
|
|
166
|
+
await t.mutation(operation("finish"), {
|
|
167
|
+
...args,
|
|
168
|
+
generation,
|
|
169
|
+
refund: { ...refund, status: "canceled" },
|
|
170
|
+
});
|
|
171
|
+
expect(await t.run((ctx) => ctx.db.query("payments").unique())).toMatchObject(
|
|
172
|
+
{ refundedAmount: 350, refundStatus: "partially_refunded" },
|
|
173
|
+
);
|
|
174
|
+
// The legacy entry point still uses exactly the same full cumulative writer.
|
|
175
|
+
await t.mutation(api.connect.mutations.upsertRefund, {
|
|
176
|
+
...refund,
|
|
177
|
+
stripeRefundId: "re_final",
|
|
178
|
+
amount: 650,
|
|
179
|
+
});
|
|
180
|
+
expect(await t.run((ctx) => ctx.db.query("payments").unique())).toMatchObject(
|
|
181
|
+
{ refundedAmount: 1000, refundStatus: "fully_refunded" },
|
|
182
|
+
);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it("holds a new refund whose provider currency contradicts the registered original payment", async () => {
|
|
186
|
+
const { t, args } = await fixture();
|
|
187
|
+
await t.run((ctx) =>
|
|
188
|
+
ctx.db.insert("payments", {
|
|
189
|
+
stripePaymentIntentId: "pi_original",
|
|
190
|
+
userId: "buyer",
|
|
191
|
+
amount: 1000,
|
|
192
|
+
currency: "usd",
|
|
193
|
+
status: "succeeded",
|
|
194
|
+
}),
|
|
195
|
+
);
|
|
196
|
+
await expect(
|
|
197
|
+
t.mutation(operation("finish"), {
|
|
198
|
+
...args,
|
|
199
|
+
refund: { ...refund, currency: "jpy" },
|
|
200
|
+
}),
|
|
201
|
+
).rejects.toThrow("Original refund merchant");
|
|
202
|
+
expect(await t.run((ctx) => ctx.db.query("refunds").collect())).toEqual([]);
|
|
203
|
+
expect(
|
|
204
|
+
await t.run((ctx) => ctx.db.query("payments").unique()),
|
|
205
|
+
).not.toHaveProperty("refundedAmount");
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("rejects duplicate refund identities or claim rows before mutating either projection", async () => {
|
|
209
|
+
const t = convexTest(schema, modules);
|
|
210
|
+
await t.run(async (ctx) => {
|
|
211
|
+
await ctx.db.insert("refunds", refund);
|
|
212
|
+
await ctx.db.insert("refunds", refund);
|
|
213
|
+
});
|
|
214
|
+
await expect(t.mutation(operation("begin"), identity)).rejects.toThrow(
|
|
215
|
+
"Refund identity is ambiguous",
|
|
216
|
+
);
|
|
217
|
+
expect(
|
|
218
|
+
await t.run((ctx) => ctx.db.query("refundSyncClaims").collect()),
|
|
219
|
+
).toEqual([]);
|
|
220
|
+
const s = convexTest(schema, modules);
|
|
221
|
+
await s.run(async (ctx) => {
|
|
222
|
+
for (let i = 0; i < 2; i++)
|
|
223
|
+
await ctx.db.insert("refundSyncClaims", {
|
|
224
|
+
stripeRefundId: identity.stripeRefundId,
|
|
225
|
+
requestedContext: "platform",
|
|
226
|
+
generation: 1,
|
|
227
|
+
status: "pending",
|
|
228
|
+
startedAt: 0,
|
|
229
|
+
});
|
|
230
|
+
});
|
|
231
|
+
await expect(s.mutation(operation("begin"), identity)).rejects.toThrow(
|
|
232
|
+
"Refund synchronization is ambiguous",
|
|
233
|
+
);
|
|
234
|
+
expect(
|
|
235
|
+
await s.run((ctx) => ctx.db.query("refundSyncClaims").collect()),
|
|
236
|
+
).toHaveLength(2);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("holds duplicate original payment identities before the cumulative writer selects a row", async () => {
|
|
240
|
+
const { t, args } = await fixture();
|
|
241
|
+
await t.run(async (ctx) => {
|
|
242
|
+
for (const userId of ["buyer", "foreign"])
|
|
243
|
+
await ctx.db.insert("payments", {
|
|
244
|
+
stripePaymentIntentId: "pi_original",
|
|
245
|
+
userId,
|
|
246
|
+
amount: 1000,
|
|
247
|
+
currency: "usd",
|
|
248
|
+
status: "succeeded",
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
await expect(t.mutation(operation("finish"), args)).rejects.toThrow(
|
|
252
|
+
"Original refund merchant",
|
|
253
|
+
);
|
|
254
|
+
expect(await t.run((ctx) => ctx.db.query("refunds").collect())).toEqual([]);
|
|
255
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Infer } from "convex/values";
|
|
2
|
+
import type { QueryCtx, MutationCtx } from "../_generated/server.js";
|
|
3
|
+
import { originalRefundSeller } from "../refundHistory/originalSeller.js";
|
|
4
|
+
import { refundById } from "./helpers.js";
|
|
5
|
+
import type { routingInput, routingResult } from "./validators.js";
|
|
6
|
+
|
|
7
|
+
/** Unknown attribution cannot silently inherit a mutable payment's current destination. */
|
|
8
|
+
export async function refundRouting(
|
|
9
|
+
ctx: QueryCtx | MutationCtx,
|
|
10
|
+
input: { [K in keyof typeof routingInput]: Infer<(typeof routingInput)[K]> },
|
|
11
|
+
): Promise<Infer<typeof routingResult>> {
|
|
12
|
+
const existing = await refundById(ctx, input.stripeRefundId);
|
|
13
|
+
const allocations = await ctx.db
|
|
14
|
+
.query("settlementAllocations")
|
|
15
|
+
.withIndex("by_source_charge", (q) =>
|
|
16
|
+
q.eq("sourceChargeId", input.stripeChargeId),
|
|
17
|
+
)
|
|
18
|
+
.take(2);
|
|
19
|
+
const merchant = await originalRefundSeller(ctx, input);
|
|
20
|
+
const payments = await ctx.db
|
|
21
|
+
.query("payments")
|
|
22
|
+
.withIndex("by_stripe_payment_intent_id", (q) =>
|
|
23
|
+
q.eq("stripePaymentIntentId", input.stripePaymentIntentId),
|
|
24
|
+
)
|
|
25
|
+
.take(2);
|
|
26
|
+
if (
|
|
27
|
+
payments.length > 1 ||
|
|
28
|
+
(payments[0] !== undefined && payments[0].currency !== input.currency) ||
|
|
29
|
+
(allocations.length > 0 && !merchant) ||
|
|
30
|
+
(existing?.accountId !== undefined &&
|
|
31
|
+
!/^acct_[A-Za-z0-9_]{1,250}$/.test(existing.accountId)) ||
|
|
32
|
+
(merchant &&
|
|
33
|
+
existing?.accountId !== undefined &&
|
|
34
|
+
merchant !== existing.accountId)
|
|
35
|
+
)
|
|
36
|
+
return {
|
|
37
|
+
status: "held",
|
|
38
|
+
merchantAccount: null,
|
|
39
|
+
requiresChargeProof: false,
|
|
40
|
+
};
|
|
41
|
+
if (merchant)
|
|
42
|
+
return {
|
|
43
|
+
status: "known",
|
|
44
|
+
merchantAccount: merchant,
|
|
45
|
+
requiresChargeProof: false,
|
|
46
|
+
};
|
|
47
|
+
const payment = payments[0];
|
|
48
|
+
const fallback = payment?.destinationAccountId;
|
|
49
|
+
return {
|
|
50
|
+
status: "unknown",
|
|
51
|
+
merchantAccount: null,
|
|
52
|
+
requiresChargeProof:
|
|
53
|
+
existing?.accountId !== undefined || fallback !== undefined,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
/** Observation claims only; refunds and payment aggregates remain in existing tables. */
|
|
5
|
+
export const claimsTable = defineTable({
|
|
6
|
+
stripeRefundId: v.string(),
|
|
7
|
+
requestedContext: v.string(),
|
|
8
|
+
generation: v.number(),
|
|
9
|
+
status: v.union(
|
|
10
|
+
v.literal("pending"),
|
|
11
|
+
v.literal("accepted"),
|
|
12
|
+
v.literal("unknown"),
|
|
13
|
+
),
|
|
14
|
+
startedAt: v.number(),
|
|
15
|
+
acceptedContext: v.optional(v.string()),
|
|
16
|
+
acceptedGeneration: v.optional(v.number()),
|
|
17
|
+
acceptedAt: v.optional(v.number()),
|
|
18
|
+
acceptedFingerprint: v.optional(v.string()),
|
|
19
|
+
}).index("by_stripe_refund_id", ["stripeRefundId"]);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
import { refundFields, refundDocValidator } from "../connect/validators.js";
|
|
3
|
+
|
|
4
|
+
export const identity = { stripeRefundId: v.string(), apiContext: v.string() };
|
|
5
|
+
export const snapshot = v.object({
|
|
6
|
+
...refundFields,
|
|
7
|
+
stripePaymentIntentId: v.string(),
|
|
8
|
+
stripeChargeId: v.string(),
|
|
9
|
+
metadata: v.optional(v.record(v.string(), v.string())),
|
|
10
|
+
});
|
|
11
|
+
export const prepared = {
|
|
12
|
+
...identity,
|
|
13
|
+
generation: v.number(),
|
|
14
|
+
refund: snapshot,
|
|
15
|
+
};
|
|
16
|
+
export const result = v.union(
|
|
17
|
+
v.object({
|
|
18
|
+
status: v.literal("accepted"),
|
|
19
|
+
generation: v.number(),
|
|
20
|
+
changed: v.boolean(),
|
|
21
|
+
refund: refundDocValidator,
|
|
22
|
+
}),
|
|
23
|
+
v.object({ status: v.literal("superseded"), generation: v.number() }),
|
|
24
|
+
);
|
|
25
|
+
export const routingInput = {
|
|
26
|
+
stripeRefundId: v.string(),
|
|
27
|
+
stripePaymentIntentId: v.string(),
|
|
28
|
+
stripeChargeId: v.string(),
|
|
29
|
+
currency: v.string(),
|
|
30
|
+
};
|
|
31
|
+
export const routingResult = v.object({
|
|
32
|
+
status: v.union(v.literal("known"), v.literal("unknown"), v.literal("held")),
|
|
33
|
+
merchantAccount: v.union(v.string(), v.null()),
|
|
34
|
+
requiresChargeProof: v.boolean(),
|
|
35
|
+
});
|
package/src/component/schema.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { claimsTable as refundSyncClaimsTable } from "./refundSync/schema.js";
|
|
1
2
|
import { operationsTable as processingCostReconciliationOperations } from "./processingCostReconciliation/schema.js";
|
|
2
3
|
import {
|
|
3
4
|
operationsTable as recoveryWorkflowOperations,
|
|
@@ -88,6 +89,7 @@ import { pricesTable, productsTable } from "./products/schema";
|
|
|
88
89
|
import { webhookEventsTable } from "./webhooks/schema";
|
|
89
90
|
|
|
90
91
|
export default defineSchema({
|
|
92
|
+
refundSyncClaims: refundSyncClaimsTable,
|
|
91
93
|
processingCostReconciliationOperations,
|
|
92
94
|
recoveryWorkflowOperations,
|
|
93
95
|
recoveryWorkflowRequests,
|