@fantastic.dev/better-stripe 0.45.2 → 0.47.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 +36 -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 +39 -16
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts.map +1 -1
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +78 -32
- 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 +54 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +10 -3
- package/dist/client/index.js.map +1 -1
- package/dist/client/types/triggers.d.ts +3 -2
- package/dist/client/types/triggers.d.ts.map +1 -1
- package/dist/client/webhooks/helpers.d.ts.map +1 -1
- package/dist/client/webhooks/helpers.js +1 -0
- package/dist/client/webhooks/helpers.js.map +1 -1
- package/dist/client/webhooks/refundReconciliation.d.ts +10 -0
- package/dist/client/webhooks/refundReconciliation.d.ts.map +1 -0
- package/dist/client/webhooks/refundReconciliation.js +41 -0
- package/dist/client/webhooks/refundReconciliation.js.map +1 -0
- package/dist/client/webhooks/refundReconciliationValidators.d.ts +68 -0
- package/dist/client/webhooks/refundReconciliationValidators.d.ts.map +1 -0
- package/dist/client/webhooks/refundReconciliationValidators.js +11 -0
- package/dist/client/webhooks/refundReconciliationValidators.js.map +1 -0
- 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/balanceFunding/mutations.d.ts +27 -27
- package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
- 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 +82 -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 +34 -6
- package/src/client/types/triggers.ts +3 -1
- package/src/client/webhooks/helpers.ts +1 -0
- package/src/client/webhooks/refund-reconciliation.test.ts +320 -0
- package/src/client/webhooks/refundReconciliation.ts +80 -0
- package/src/client/webhooks/refundReconciliationValidators.ts +15 -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
package/src/client/index.ts
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
import { refundReconciliationResult } from "./webhooks/refundReconciliationValidators.js";
|
|
2
|
+
import type { RefundReconciliationResult } from "./webhooks/refundReconciliationValidators.js";
|
|
3
|
+
import { reconcileRefundAndTrigger } from "./webhooks/refundReconciliation.js";
|
|
4
|
+
import * as refundSyncImpl from "./connect/refundSync.js";
|
|
5
|
+
export type {
|
|
6
|
+
RefundSyncOptions,
|
|
7
|
+
RefundSyncResult,
|
|
8
|
+
} from "./connect/refundSync.js";
|
|
9
|
+
export type { RefundSyncScope } from "./connect/refundSyncValidation.js";
|
|
1
10
|
import * as personalAccountMappingImpl from "./core/personalAccountMapping.js";
|
|
2
11
|
export type * from "./publicTypes.js";
|
|
3
12
|
export * from "./financialOutcomes/index.js";
|
|
@@ -1190,6 +1199,19 @@ export class BetterStripe extends PaymentSourceClient {
|
|
|
1190
1199
|
);
|
|
1191
1200
|
}
|
|
1192
1201
|
|
|
1202
|
+
/** Host must authorize the explicit API scope; this never creates a refund. */
|
|
1203
|
+
async syncRefundFromStripe(
|
|
1204
|
+
ctx: RunCtx,
|
|
1205
|
+
opts: refundSyncImpl.RefundSyncOptions,
|
|
1206
|
+
) {
|
|
1207
|
+
return refundSyncImpl.syncRefundFromStripe(
|
|
1208
|
+
this.stripe(),
|
|
1209
|
+
this.component,
|
|
1210
|
+
ctx,
|
|
1211
|
+
opts,
|
|
1212
|
+
);
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1193
1215
|
// REFUND METHODS
|
|
1194
1216
|
|
|
1195
1217
|
/**
|
|
@@ -1511,7 +1533,10 @@ export class BetterStripe extends PaymentSourceClient {
|
|
|
1511
1533
|
*/
|
|
1512
1534
|
const SYNC: Record<
|
|
1513
1535
|
TriggerDispatcherName,
|
|
1514
|
-
(
|
|
1536
|
+
(
|
|
1537
|
+
ctx: SyncTriggerCtx,
|
|
1538
|
+
record: Record<string, unknown>,
|
|
1539
|
+
) => Promise<void | RefundReconciliationResult>
|
|
1515
1540
|
> = {
|
|
1516
1541
|
accountUpserted: (ctx, record) =>
|
|
1517
1542
|
runUpsertAndTrigger<StripeComponentAccount>(
|
|
@@ -1677,6 +1702,8 @@ export class BetterStripe extends PaymentSourceClient {
|
|
|
1677
1702
|
),
|
|
1678
1703
|
record,
|
|
1679
1704
|
),
|
|
1705
|
+
refundReconciled: (ctx, record) =>
|
|
1706
|
+
reconcileRefundAndTrigger(component, ctx, record, triggers?.refund),
|
|
1680
1707
|
refundUpserted: (ctx, record) =>
|
|
1681
1708
|
runUpsertAndTrigger<StripeComponentRefund>(
|
|
1682
1709
|
ctx,
|
|
@@ -1715,17 +1742,18 @@ export class BetterStripe extends PaymentSourceClient {
|
|
|
1715
1742
|
|
|
1716
1743
|
const syncWebhook = internalMutationGeneric({
|
|
1717
1744
|
args: { dispatcher: v.string(), data: v.any() },
|
|
1718
|
-
returns: v.null(),
|
|
1745
|
+
returns: v.union(v.null(), refundReconciliationResult),
|
|
1719
1746
|
handler: async (ctx, { dispatcher, data }) => {
|
|
1720
1747
|
const run = SYNC[dispatcher as TriggerDispatcherName];
|
|
1721
1748
|
if (!run) {
|
|
1722
1749
|
throw new Error(`[better-stripe] unknown dispatcher: ${dispatcher}`);
|
|
1723
1750
|
}
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1751
|
+
return (
|
|
1752
|
+
(await run(
|
|
1753
|
+
ctx as unknown as SyncTriggerCtx,
|
|
1754
|
+
data as Record<string, unknown>,
|
|
1755
|
+
)) ?? null
|
|
1727
1756
|
);
|
|
1728
|
-
return null;
|
|
1729
1757
|
},
|
|
1730
1758
|
});
|
|
1731
1759
|
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { RefundReconciliationResult } from "../webhooks/refundReconciliationValidators.js";
|
|
1
2
|
import type {
|
|
2
3
|
FunctionReference,
|
|
3
4
|
FunctionReturnType,
|
|
@@ -244,6 +245,7 @@ export type TriggerDispatcherName =
|
|
|
244
245
|
| "paymentUpserted"
|
|
245
246
|
| "payoutUpserted"
|
|
246
247
|
| "payoutReconciled"
|
|
248
|
+
| "refundReconciled"
|
|
247
249
|
| "refundUpserted"
|
|
248
250
|
| "disputeUpserted";
|
|
249
251
|
|
|
@@ -283,7 +285,7 @@ export type WebhookHandlerRefs = {
|
|
|
283
285
|
"mutation",
|
|
284
286
|
"internal",
|
|
285
287
|
{ dispatcher: string; data: Record<string, unknown> },
|
|
286
|
-
null
|
|
288
|
+
null | RefundReconciliationResult
|
|
287
289
|
>;
|
|
288
290
|
asyncWebhook: FunctionReference<
|
|
289
291
|
"action",
|
|
@@ -209,6 +209,7 @@ export const DISPATCHER_UPSERT_PATHS: Record<TriggerDispatcherName, string> = {
|
|
|
209
209
|
payoutUpserted: "connect/mutations/upsertPayout",
|
|
210
210
|
payoutReconciled: "payoutSync/mutations/finish",
|
|
211
211
|
refundUpserted: "connect/mutations/upsertRefund",
|
|
212
|
+
refundReconciled: "refundSync/mutations/finish",
|
|
212
213
|
disputeUpserted: "connect/mutations/upsertDispute",
|
|
213
214
|
};
|
|
214
215
|
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
// @vitest-environment edge-runtime
|
|
2
|
+
import { expect, it, vi } from "vitest";
|
|
3
|
+
import { convexTest } from "convex-test";
|
|
4
|
+
import {
|
|
5
|
+
componentsGeneric,
|
|
6
|
+
defineSchema,
|
|
7
|
+
defineTable,
|
|
8
|
+
internalMutationGeneric,
|
|
9
|
+
makeFunctionReference,
|
|
10
|
+
} from "convex/server";
|
|
11
|
+
import { v } from "convex/values";
|
|
12
|
+
import schema from "../../component/schema.js";
|
|
13
|
+
import { BetterStripe } from "../index.js";
|
|
14
|
+
import type { Component } from "../helpers.js";
|
|
15
|
+
import type { WebhookHandlerRefs } from "../types.js";
|
|
16
|
+
import { dispatchRefundReconciliation } from "./refundReconciliation.js";
|
|
17
|
+
import type { WebhookContext } from "./helpers.js";
|
|
18
|
+
import type { PreparedRefundSync } from "../connect/refundSync.js";
|
|
19
|
+
|
|
20
|
+
const component = componentsGeneric().betterStripe as unknown as Component;
|
|
21
|
+
const modules = import.meta.glob("../../component/**/*.*s");
|
|
22
|
+
const observationRef = makeFunctionReference<
|
|
23
|
+
"mutation",
|
|
24
|
+
{ status: string; oldStatus?: string },
|
|
25
|
+
null
|
|
26
|
+
>("refundHost:observe");
|
|
27
|
+
|
|
28
|
+
function fixture() {
|
|
29
|
+
let isFailing = false;
|
|
30
|
+
const handlers = new BetterStripe(component, {
|
|
31
|
+
triggers: {
|
|
32
|
+
refund: {
|
|
33
|
+
onCreate: async (ctx, doc) => {
|
|
34
|
+
await ctx.runMutation(observationRef, { status: doc.status });
|
|
35
|
+
if (isFailing) throw new Error("Injected callback failure");
|
|
36
|
+
},
|
|
37
|
+
onUpdate: async (ctx, doc, old) => {
|
|
38
|
+
await ctx.runMutation(observationRef, {
|
|
39
|
+
status: doc.status,
|
|
40
|
+
oldStatus: old.status,
|
|
41
|
+
});
|
|
42
|
+
if (isFailing) throw new Error("Injected callback failure");
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
}).webhookHandlers();
|
|
47
|
+
const t = convexTest(
|
|
48
|
+
defineSchema({
|
|
49
|
+
observations: defineTable({
|
|
50
|
+
status: v.string(),
|
|
51
|
+
oldStatus: v.optional(v.string()),
|
|
52
|
+
}),
|
|
53
|
+
}),
|
|
54
|
+
{
|
|
55
|
+
"./_generated/server.ts": () =>
|
|
56
|
+
import("../../component/_generated/server.js"),
|
|
57
|
+
"./refundHost.ts": async () => ({
|
|
58
|
+
...handlers,
|
|
59
|
+
observe: internalMutationGeneric({
|
|
60
|
+
args: { status: v.string(), oldStatus: v.optional(v.string()) },
|
|
61
|
+
returns: v.null(),
|
|
62
|
+
handler: async (ctx, args) => {
|
|
63
|
+
await ctx.db.insert("observations", args);
|
|
64
|
+
return null;
|
|
65
|
+
},
|
|
66
|
+
}),
|
|
67
|
+
}),
|
|
68
|
+
},
|
|
69
|
+
);
|
|
70
|
+
t.registerComponent("betterStripe", schema, modules);
|
|
71
|
+
const syncWebhook = makeFunctionReference<"mutation">(
|
|
72
|
+
"refundHost:syncWebhook",
|
|
73
|
+
) as unknown as WebhookHandlerRefs["syncWebhook"];
|
|
74
|
+
const refund = {
|
|
75
|
+
stripeRefundId: "re_original",
|
|
76
|
+
stripePaymentIntentId: "pi_original",
|
|
77
|
+
stripeChargeId: "ch_original",
|
|
78
|
+
amount: 250,
|
|
79
|
+
currency: "usd",
|
|
80
|
+
status: "succeeded" as const,
|
|
81
|
+
metadata: { betterStripeRefundCommand: "original-command" },
|
|
82
|
+
};
|
|
83
|
+
const prepare = async (
|
|
84
|
+
status: PreparedRefundSync["refund"]["status"] = "succeeded",
|
|
85
|
+
): Promise<PreparedRefundSync> => ({
|
|
86
|
+
stripeRefundId: refund.stripeRefundId,
|
|
87
|
+
apiContext: "platform",
|
|
88
|
+
generation: await t.mutation(component.refundSync.mutations.begin, {
|
|
89
|
+
stripeRefundId: refund.stripeRefundId,
|
|
90
|
+
apiContext: "platform",
|
|
91
|
+
}),
|
|
92
|
+
refund: { ...refund, status },
|
|
93
|
+
});
|
|
94
|
+
const accept = (data: Awaited<ReturnType<typeof prepare>>) =>
|
|
95
|
+
t.mutation(syncWebhook, { dispatcher: "refundReconciled", data });
|
|
96
|
+
const observations = () =>
|
|
97
|
+
t.run((ctx) => ctx.db.query("observations").collect());
|
|
98
|
+
return {
|
|
99
|
+
handlers,
|
|
100
|
+
t,
|
|
101
|
+
syncWebhook,
|
|
102
|
+
refund,
|
|
103
|
+
prepare,
|
|
104
|
+
accept,
|
|
105
|
+
observations,
|
|
106
|
+
fail: (value: boolean) => {
|
|
107
|
+
isFailing = value;
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
it("control: retains legacy raw refund dispatcher behavior", async () => {
|
|
113
|
+
const f = fixture();
|
|
114
|
+
expect(
|
|
115
|
+
await f.t.mutation(f.syncWebhook, {
|
|
116
|
+
dispatcher: "refundUpserted",
|
|
117
|
+
data: f.refund,
|
|
118
|
+
}),
|
|
119
|
+
).toBeNull();
|
|
120
|
+
expect(
|
|
121
|
+
await f.t.run((ctx) => ctx.db.query("observations").collect()),
|
|
122
|
+
).toMatchObject([{ status: "succeeded" }]);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("returns the qualified accepting result and commits its callback together", async () => {
|
|
126
|
+
const f = fixture();
|
|
127
|
+
const generation = await f.t.mutation(component.refundSync.mutations.begin, {
|
|
128
|
+
stripeRefundId: f.refund.stripeRefundId,
|
|
129
|
+
apiContext: "platform",
|
|
130
|
+
});
|
|
131
|
+
const result = await f.t.mutation(f.syncWebhook, {
|
|
132
|
+
dispatcher: "refundReconciled",
|
|
133
|
+
data: {
|
|
134
|
+
stripeRefundId: f.refund.stripeRefundId,
|
|
135
|
+
apiContext: "platform",
|
|
136
|
+
generation,
|
|
137
|
+
refund: f.refund,
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
expect(result).toMatchObject({
|
|
141
|
+
status: "accepted",
|
|
142
|
+
apiContext: "platform",
|
|
143
|
+
generation,
|
|
144
|
+
changed: true,
|
|
145
|
+
refund: { stripeRefundId: "re_original", status: "succeeded" },
|
|
146
|
+
});
|
|
147
|
+
expect(
|
|
148
|
+
await f.t.run((ctx) => ctx.db.query("observations").collect()),
|
|
149
|
+
).toMatchObject([{ status: "succeeded" }]);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("rolls back callback, accepting claim, mirrored refund and cumulative payment together", async () => {
|
|
153
|
+
const f = fixture();
|
|
154
|
+
await f.t.mutation(component.connect.mutations.upsertPayment, {
|
|
155
|
+
stripePaymentIntentId: "pi_original",
|
|
156
|
+
userId: "buyer",
|
|
157
|
+
amount: 1000,
|
|
158
|
+
currency: "usd",
|
|
159
|
+
status: "succeeded",
|
|
160
|
+
});
|
|
161
|
+
await f.t.mutation(component.connect.mutations.upsertRefund, {
|
|
162
|
+
...f.refund,
|
|
163
|
+
status: "failed",
|
|
164
|
+
});
|
|
165
|
+
const prepared = await f.prepare();
|
|
166
|
+
f.fail(true);
|
|
167
|
+
await expect(f.accept(prepared)).rejects.toThrow("Injected callback failure");
|
|
168
|
+
expect(await f.observations()).toEqual([]);
|
|
169
|
+
expect(
|
|
170
|
+
await f.t.query(component.connect.queries.getRefundByStripeId, {
|
|
171
|
+
stripeRefundId: "re_original",
|
|
172
|
+
}),
|
|
173
|
+
).toMatchObject({ status: "failed" });
|
|
174
|
+
expect(
|
|
175
|
+
await f.t.query(component.connect.queries.getPaymentByStripeId, {
|
|
176
|
+
stripePaymentIntentId: "pi_original",
|
|
177
|
+
}),
|
|
178
|
+
).toMatchObject({ refundedAmount: 0 });
|
|
179
|
+
f.fail(false);
|
|
180
|
+
// A rolled-back accepting claim must still permit its first changed acceptance.
|
|
181
|
+
expect(await f.accept(prepared)).toMatchObject({
|
|
182
|
+
status: "accepted",
|
|
183
|
+
changed: true,
|
|
184
|
+
});
|
|
185
|
+
expect(await f.observations()).toMatchObject([
|
|
186
|
+
{ status: "succeeded", oldStatus: "failed" },
|
|
187
|
+
]);
|
|
188
|
+
expect(
|
|
189
|
+
await f.t.query(component.connect.queries.getPaymentByStripeId, {
|
|
190
|
+
stripePaymentIntentId: "pi_original",
|
|
191
|
+
}),
|
|
192
|
+
).toMatchObject({ refundedAmount: 250 });
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("returns unchanged accepted observations without repeating callbacks", async () => {
|
|
196
|
+
const f = fixture();
|
|
197
|
+
const prepared = await f.prepare();
|
|
198
|
+
await f.accept(prepared);
|
|
199
|
+
expect(await f.accept(prepared)).toMatchObject({
|
|
200
|
+
status: "accepted",
|
|
201
|
+
changed: false,
|
|
202
|
+
});
|
|
203
|
+
expect(await f.accept(await f.prepare())).toMatchObject({
|
|
204
|
+
status: "accepted",
|
|
205
|
+
changed: false,
|
|
206
|
+
});
|
|
207
|
+
expect(await f.observations()).toHaveLength(1);
|
|
208
|
+
expect(await f.accept(await f.prepare("requires_action"))).toMatchObject({
|
|
209
|
+
status: "accepted",
|
|
210
|
+
changed: true,
|
|
211
|
+
});
|
|
212
|
+
expect(await f.observations()).toMatchObject([
|
|
213
|
+
{ status: "succeeded" },
|
|
214
|
+
{ status: "requires_action", oldStatus: "succeeded" },
|
|
215
|
+
]);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it("rejects superseded callbacks even when the newer observation has not committed", async () => {
|
|
219
|
+
const f = fixture();
|
|
220
|
+
const old = await f.prepare();
|
|
221
|
+
const current = await f.prepare("requires_action");
|
|
222
|
+
await expect(f.accept(old)).rejects.toThrow(
|
|
223
|
+
"Current refund observation is unavailable",
|
|
224
|
+
);
|
|
225
|
+
expect(await f.observations()).toEqual([]);
|
|
226
|
+
await f.accept(current);
|
|
227
|
+
await expect(f.accept(old)).rejects.toThrow(
|
|
228
|
+
"Current refund observation is unavailable",
|
|
229
|
+
);
|
|
230
|
+
expect(await f.observations()).toMatchObject([{ status: "requires_action" }]);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("preserves accepting results and superseded failure in the direct fallback", async () => {
|
|
234
|
+
const f = fixture();
|
|
235
|
+
const context = {
|
|
236
|
+
component,
|
|
237
|
+
ctx: { runMutation: f.t.mutation, runQuery: f.t.query },
|
|
238
|
+
} as unknown as WebhookContext;
|
|
239
|
+
const old = await f.prepare();
|
|
240
|
+
const current = await f.prepare();
|
|
241
|
+
await expect(dispatchRefundReconciliation(context, old)).rejects.toThrow(
|
|
242
|
+
"Current refund observation is unavailable",
|
|
243
|
+
);
|
|
244
|
+
expect(await dispatchRefundReconciliation(context, current)).toMatchObject({
|
|
245
|
+
status: "accepted",
|
|
246
|
+
changed: true,
|
|
247
|
+
});
|
|
248
|
+
expect(await dispatchRefundReconciliation(context, current)).toMatchObject({
|
|
249
|
+
status: "accepted",
|
|
250
|
+
changed: false,
|
|
251
|
+
});
|
|
252
|
+
expect(await f.observations()).toEqual([]);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("returns the actual registered host result without a later raw upsert", async () => {
|
|
256
|
+
const f = fixture();
|
|
257
|
+
const runMutation = vi.fn(f.t.mutation);
|
|
258
|
+
const context = {
|
|
259
|
+
component,
|
|
260
|
+
ctx: { runMutation, runQuery: f.t.query },
|
|
261
|
+
config: { webhooks: { syncWebhook: f.syncWebhook } },
|
|
262
|
+
} as unknown as WebhookContext;
|
|
263
|
+
const prepared = await f.prepare();
|
|
264
|
+
expect(await dispatchRefundReconciliation(context, prepared)).toMatchObject({
|
|
265
|
+
status: "accepted",
|
|
266
|
+
changed: true,
|
|
267
|
+
});
|
|
268
|
+
expect(runMutation).toHaveBeenCalledTimes(1);
|
|
269
|
+
expect(await f.observations()).toMatchObject([{ status: "succeeded" }]);
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("holds a legacy null-returning host instead of silently using a raw fallback", async () => {
|
|
273
|
+
const f = fixture();
|
|
274
|
+
const runMutation = vi.fn(async () => null);
|
|
275
|
+
const context = {
|
|
276
|
+
component,
|
|
277
|
+
ctx: { runMutation },
|
|
278
|
+
config: { webhooks: { syncWebhook: f.syncWebhook } },
|
|
279
|
+
} as unknown as WebhookContext;
|
|
280
|
+
await expect(
|
|
281
|
+
dispatchRefundReconciliation(context, await f.prepare()),
|
|
282
|
+
).rejects.toThrow("Current refund observation is unavailable");
|
|
283
|
+
expect(runMutation).toHaveBeenCalledTimes(1);
|
|
284
|
+
expect(
|
|
285
|
+
await f.t.query(component.connect.queries.getRefundByStripeId, {
|
|
286
|
+
stripeRefundId: "re_original",
|
|
287
|
+
}),
|
|
288
|
+
).toBeNull();
|
|
289
|
+
expect(await f.observations()).toEqual([]);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("holds mismatched API context without a callback or accepting write", async () => {
|
|
293
|
+
const f = fixture();
|
|
294
|
+
const prepared = await f.prepare();
|
|
295
|
+
await expect(
|
|
296
|
+
f.accept({ ...prepared, apiContext: "acct_foreign" }),
|
|
297
|
+
).rejects.toThrow("API context does not match");
|
|
298
|
+
expect(await f.observations()).toEqual([]);
|
|
299
|
+
expect(
|
|
300
|
+
await f.t.query(component.connect.queries.getRefundByStripeId, {
|
|
301
|
+
stripeRefundId: "re_original",
|
|
302
|
+
}),
|
|
303
|
+
).toBeNull();
|
|
304
|
+
expect(await f.accept(prepared)).toMatchObject({
|
|
305
|
+
status: "accepted",
|
|
306
|
+
changed: true,
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("exports a host receipt schema without component-local table ID validators", () => {
|
|
311
|
+
const { handlers } = fixture();
|
|
312
|
+
const registration = handlers.syncWebhook as typeof handlers.syncWebhook & {
|
|
313
|
+
exportReturns: () => string;
|
|
314
|
+
};
|
|
315
|
+
const schema = registration.exportReturns();
|
|
316
|
+
// Native host validation must not interpret a foreign component ID as a host table ID.
|
|
317
|
+
expect(schema).not.toContain('"tableName":"refunds"');
|
|
318
|
+
expect(schema).toContain('"apiContext"');
|
|
319
|
+
expect(schema).toContain('"refundCommandNormalizationVersion"');
|
|
320
|
+
});
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import type { Component } from "../helpers.js";
|
|
2
|
+
import type {
|
|
3
|
+
SyncTriggerCtx,
|
|
4
|
+
SyncTriggers,
|
|
5
|
+
StripeComponentRefund,
|
|
6
|
+
} from "../types.js";
|
|
7
|
+
import type {
|
|
8
|
+
PreparedRefundSync,
|
|
9
|
+
RefundSyncResult,
|
|
10
|
+
} from "../connect/refundSync.js";
|
|
11
|
+
import { validateIdentity } from "../../component/refundSync/helpers.js";
|
|
12
|
+
import { componentRef, type WebhookContext } from "./helpers.js";
|
|
13
|
+
|
|
14
|
+
import type { RefundReconciliationResult } from "./refundReconciliationValidators.js";
|
|
15
|
+
|
|
16
|
+
function accepted(
|
|
17
|
+
result: RefundSyncResult | RefundReconciliationResult | null,
|
|
18
|
+
prepared: PreparedRefundSync,
|
|
19
|
+
): RefundReconciliationResult {
|
|
20
|
+
if (
|
|
21
|
+
result?.status !== "accepted" ||
|
|
22
|
+
result.generation !== prepared.generation ||
|
|
23
|
+
result.refund.stripeRefundId !== prepared.stripeRefundId ||
|
|
24
|
+
("apiContext" in result && result.apiContext !== prepared.apiContext)
|
|
25
|
+
)
|
|
26
|
+
throw new Error(
|
|
27
|
+
"Current refund observation is unavailable; retry reconciliation.",
|
|
28
|
+
);
|
|
29
|
+
return { ...result, apiContext: prepared.apiContext };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Called inside the existing host mutation: callback failure rolls back finish. */
|
|
33
|
+
export async function reconcileRefundAndTrigger(
|
|
34
|
+
component: Component,
|
|
35
|
+
ctx: SyncTriggerCtx,
|
|
36
|
+
record: Record<string, unknown>,
|
|
37
|
+
triggers: SyncTriggers["refund"],
|
|
38
|
+
): Promise<RefundReconciliationResult> {
|
|
39
|
+
const prepared = record as PreparedRefundSync;
|
|
40
|
+
validateIdentity(prepared);
|
|
41
|
+
const old = triggers
|
|
42
|
+
? ((await ctx.runQuery(
|
|
43
|
+
componentRef(component, "connect/queries/getRefundByStripeId"),
|
|
44
|
+
{ stripeRefundId: prepared.stripeRefundId },
|
|
45
|
+
)) as StripeComponentRefund | null)
|
|
46
|
+
: null;
|
|
47
|
+
const result = (await ctx.runMutation(
|
|
48
|
+
componentRef(component, "refundSync/mutations/finish"),
|
|
49
|
+
prepared,
|
|
50
|
+
)) as RefundSyncResult;
|
|
51
|
+
const receipt = accepted(result, prepared);
|
|
52
|
+
// Callbacks receive the actual accepted component row, not the host DTO.
|
|
53
|
+
if (result.status === "accepted" && result.changed) {
|
|
54
|
+
if (old) await triggers?.onUpdate?.(ctx, result.refund, old);
|
|
55
|
+
else await triggers?.onCreate?.(ctx, result.refund);
|
|
56
|
+
}
|
|
57
|
+
return receipt;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Internal result-bearing seam; signed notification activation is separate. */
|
|
61
|
+
export async function dispatchRefundReconciliation(
|
|
62
|
+
whCtx: WebhookContext,
|
|
63
|
+
prepared: PreparedRefundSync,
|
|
64
|
+
): Promise<RefundReconciliationResult> {
|
|
65
|
+
validateIdentity(prepared);
|
|
66
|
+
const refs = whCtx.config?.webhooks;
|
|
67
|
+
const result = refs
|
|
68
|
+
? await whCtx.ctx.runMutation(refs.syncWebhook, {
|
|
69
|
+
dispatcher: "refundReconciled",
|
|
70
|
+
data: prepared,
|
|
71
|
+
})
|
|
72
|
+
: await whCtx.ctx.runMutation(
|
|
73
|
+
componentRef(whCtx.component, "refundSync/mutations/finish"),
|
|
74
|
+
prepared,
|
|
75
|
+
);
|
|
76
|
+
return accepted(
|
|
77
|
+
result as RefundSyncResult | RefundReconciliationResult | null,
|
|
78
|
+
prepared,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { v, type Infer } from "convex/values";
|
|
2
|
+
import { refundDocValidator } from "../../component/connect/validators.js";
|
|
3
|
+
|
|
4
|
+
/** Component document IDs are opaque strings in the host's table namespace. */
|
|
5
|
+
export const refundReconciliationResult = v.object({
|
|
6
|
+
status: v.literal("accepted"),
|
|
7
|
+
apiContext: v.string(),
|
|
8
|
+
generation: v.number(),
|
|
9
|
+
changed: v.boolean(),
|
|
10
|
+
refund: v.object({ ...refundDocValidator.fields, _id: v.string() }),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export type RefundReconciliationResult = Infer<
|
|
14
|
+
typeof refundReconciliationResult
|
|
15
|
+
>;
|
|
@@ -41,6 +41,7 @@ import type * as billing_validators from "../billing/validators.js";
|
|
|
41
41
|
import type * as connect_mutations from "../connect/mutations.js";
|
|
42
42
|
import type * as connect_payoutWrites from "../connect/payoutWrites.js";
|
|
43
43
|
import type * as connect_queries from "../connect/queries.js";
|
|
44
|
+
import type * as connect_refundWrites from "../connect/refundWrites.js";
|
|
44
45
|
import type * as connect_reversalClaims from "../connect/reversalClaims.js";
|
|
45
46
|
import type * as connect_reversalDiagnostics from "../connect/reversalDiagnostics.js";
|
|
46
47
|
import type * as connect_reversalReceipts from "../connect/reversalReceipts.js";
|
|
@@ -150,6 +151,11 @@ import type * as refundHistory_commandFields from "../refundHistory/commandField
|
|
|
150
151
|
import type * as refundHistory_mutations from "../refundHistory/mutations.js";
|
|
151
152
|
import type * as refundHistory_originalSeller from "../refundHistory/originalSeller.js";
|
|
152
153
|
import type * as refundHistory_queries from "../refundHistory/queries.js";
|
|
154
|
+
import type * as refundSync_helpers from "../refundSync/helpers.js";
|
|
155
|
+
import type * as refundSync_mutations from "../refundSync/mutations.js";
|
|
156
|
+
import type * as refundSync_queries from "../refundSync/queries.js";
|
|
157
|
+
import type * as refundSync_routing from "../refundSync/routing.js";
|
|
158
|
+
import type * as refundSync_validators from "../refundSync/validators.js";
|
|
153
159
|
import type * as settlement_clawbackSchema from "../settlement/clawbackSchema.js";
|
|
154
160
|
import type * as settlement_clawbacks from "../settlement/clawbacks.js";
|
|
155
161
|
import type * as settlement_mutations from "../settlement/mutations.js";
|
|
@@ -199,6 +205,7 @@ const fullApi: ApiFromModules<{
|
|
|
199
205
|
"connect/mutations": typeof connect_mutations;
|
|
200
206
|
"connect/payoutWrites": typeof connect_payoutWrites;
|
|
201
207
|
"connect/queries": typeof connect_queries;
|
|
208
|
+
"connect/refundWrites": typeof connect_refundWrites;
|
|
202
209
|
"connect/reversalClaims": typeof connect_reversalClaims;
|
|
203
210
|
"connect/reversalDiagnostics": typeof connect_reversalDiagnostics;
|
|
204
211
|
"connect/reversalReceipts": typeof connect_reversalReceipts;
|
|
@@ -308,6 +315,11 @@ const fullApi: ApiFromModules<{
|
|
|
308
315
|
"refundHistory/mutations": typeof refundHistory_mutations;
|
|
309
316
|
"refundHistory/originalSeller": typeof refundHistory_originalSeller;
|
|
310
317
|
"refundHistory/queries": typeof refundHistory_queries;
|
|
318
|
+
"refundSync/helpers": typeof refundSync_helpers;
|
|
319
|
+
"refundSync/mutations": typeof refundSync_mutations;
|
|
320
|
+
"refundSync/queries": typeof refundSync_queries;
|
|
321
|
+
"refundSync/routing": typeof refundSync_routing;
|
|
322
|
+
"refundSync/validators": typeof refundSync_validators;
|
|
311
323
|
"settlement/clawbackSchema": typeof settlement_clawbackSchema;
|
|
312
324
|
"settlement/clawbacks": typeof settlement_clawbacks;
|
|
313
325
|
"settlement/mutations": typeof settlement_mutations;
|
|
@@ -7857,6 +7857,106 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
|
7857
7857
|
>;
|
|
7858
7858
|
};
|
|
7859
7859
|
};
|
|
7860
|
+
refundSync: {
|
|
7861
|
+
mutations: {
|
|
7862
|
+
begin: FunctionReference<
|
|
7863
|
+
"mutation",
|
|
7864
|
+
"internal",
|
|
7865
|
+
{ apiContext: string; stripeRefundId: string },
|
|
7866
|
+
number,
|
|
7867
|
+
Name
|
|
7868
|
+
>;
|
|
7869
|
+
finish: FunctionReference<
|
|
7870
|
+
"mutation",
|
|
7871
|
+
"internal",
|
|
7872
|
+
{
|
|
7873
|
+
apiContext: string;
|
|
7874
|
+
generation: number;
|
|
7875
|
+
refund: {
|
|
7876
|
+
accountId?: string;
|
|
7877
|
+
amount: number;
|
|
7878
|
+
currency: string;
|
|
7879
|
+
failureReason?: string;
|
|
7880
|
+
metadata?: Record<string, string>;
|
|
7881
|
+
reason?:
|
|
7882
|
+
| "duplicate"
|
|
7883
|
+
| "fraudulent"
|
|
7884
|
+
| "requested_by_customer"
|
|
7885
|
+
| "expired_uncaptured_charge";
|
|
7886
|
+
status:
|
|
7887
|
+
| "pending"
|
|
7888
|
+
| "requires_action"
|
|
7889
|
+
| "succeeded"
|
|
7890
|
+
| "failed"
|
|
7891
|
+
| "canceled";
|
|
7892
|
+
stripeChargeId: string;
|
|
7893
|
+
stripePaymentIntentId: string;
|
|
7894
|
+
stripeRefundId: string;
|
|
7895
|
+
};
|
|
7896
|
+
stripeRefundId: string;
|
|
7897
|
+
},
|
|
7898
|
+
| {
|
|
7899
|
+
changed: boolean;
|
|
7900
|
+
generation: number;
|
|
7901
|
+
refund: {
|
|
7902
|
+
_creationTime: number;
|
|
7903
|
+
_id: string;
|
|
7904
|
+
accountId?: string;
|
|
7905
|
+
amount: number;
|
|
7906
|
+
currency: string;
|
|
7907
|
+
failureReason?: string;
|
|
7908
|
+
metadata?: any;
|
|
7909
|
+
reason?:
|
|
7910
|
+
| "duplicate"
|
|
7911
|
+
| "fraudulent"
|
|
7912
|
+
| "requested_by_customer"
|
|
7913
|
+
| "expired_uncaptured_charge";
|
|
7914
|
+
refundCommandConflict?: boolean;
|
|
7915
|
+
refundCommandKey?: string;
|
|
7916
|
+
refundCommandNormalizationVersion?: 1;
|
|
7917
|
+
refundCommandObservedAt?: number;
|
|
7918
|
+
status:
|
|
7919
|
+
| "pending"
|
|
7920
|
+
| "requires_action"
|
|
7921
|
+
| "succeeded"
|
|
7922
|
+
| "failed"
|
|
7923
|
+
| "canceled";
|
|
7924
|
+
stripeChargeId?: string;
|
|
7925
|
+
stripePaymentIntentId?: string;
|
|
7926
|
+
stripeRefundId: string;
|
|
7927
|
+
};
|
|
7928
|
+
status: "accepted";
|
|
7929
|
+
}
|
|
7930
|
+
| { generation: number; status: "superseded" },
|
|
7931
|
+
Name
|
|
7932
|
+
>;
|
|
7933
|
+
markUnknown: FunctionReference<
|
|
7934
|
+
"mutation",
|
|
7935
|
+
"internal",
|
|
7936
|
+
{ apiContext: string; generation: number; stripeRefundId: string },
|
|
7937
|
+
null,
|
|
7938
|
+
Name
|
|
7939
|
+
>;
|
|
7940
|
+
};
|
|
7941
|
+
queries: {
|
|
7942
|
+
routing: FunctionReference<
|
|
7943
|
+
"query",
|
|
7944
|
+
"internal",
|
|
7945
|
+
{
|
|
7946
|
+
currency: string;
|
|
7947
|
+
stripeChargeId: string;
|
|
7948
|
+
stripePaymentIntentId: string;
|
|
7949
|
+
stripeRefundId: string;
|
|
7950
|
+
},
|
|
7951
|
+
{
|
|
7952
|
+
merchantAccount: string | null;
|
|
7953
|
+
requiresChargeProof: boolean;
|
|
7954
|
+
status: "known" | "unknown" | "held";
|
|
7955
|
+
},
|
|
7956
|
+
Name
|
|
7957
|
+
>;
|
|
7958
|
+
};
|
|
7959
|
+
};
|
|
7860
7960
|
settlement: {
|
|
7861
7961
|
mutations: {
|
|
7862
7962
|
claimCommissionInvoice: FunctionReference<
|