@fantastic.dev/better-stripe 0.46.0 → 0.47.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 +25 -0
- package/README.md +1 -1
- package/dist/client/connect/payoutExecutionTestHelpers.d.ts +16 -16
- package/dist/client/connect/payoutLargeAdoptionTestHelpers.d.ts +32 -32
- package/dist/client/index.d.ts +24 -1
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +5 -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/balanceFunding/mutations.d.ts +27 -27
- package/dist/component/balanceFunding/workflowValidators.d.ts +16 -16
- package/dist/component/connect/validators.js +1 -1
- package/dist/component/connect/validators.js.map +1 -1
- package/dist/react/workflows/PlanPricingEditor.js +2 -2
- package/dist/react/workflows/PlanPricingEditor.js.map +1 -1
- package/docs/react-workflows.md +6 -2
- package/docs/refund-context.md +28 -0
- package/docs/releasing.md +11 -0
- package/package.json +2 -2
- package/src/client/index.ts +15 -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/connect/validators.ts +1 -1
- package/src/react/workflows/PlanPricingEditor.currency.test.tsx +1 -1
- package/src/react/workflows/PlanPricingEditor.tsx +2 -2
- package/src/react/workflows/PlanPricingEditor.zero.test.tsx +100 -0
|
@@ -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
|
+
>;
|
|
@@ -2,7 +2,7 @@ import { refundCommandFields } from "../refundHistory/commandFields.js";
|
|
|
2
2
|
import { commissionExposureValidator } from "../settlement/clawbackSchema.js";
|
|
3
3
|
import { type Infer, v } from "convex/values";
|
|
4
4
|
|
|
5
|
-
import { feeRoutingFields, splitRecipientRoleValidator } from "../lib/fees";
|
|
5
|
+
import { feeRoutingFields, splitRecipientRoleValidator } from "../lib/fees.js";
|
|
6
6
|
|
|
7
7
|
export const paymentStatusValidator = v.union(
|
|
8
8
|
v.literal("succeeded"),
|
|
@@ -65,9 +65,9 @@ export function PlanPricingEditor({
|
|
|
65
65
|
fields.monthlyPrice.trim(),
|
|
66
66
|
currency,
|
|
67
67
|
);
|
|
68
|
-
if (monthlyAmount === null
|
|
68
|
+
if (monthlyAmount === null)
|
|
69
69
|
throw new Error(
|
|
70
|
-
"Enter a
|
|
70
|
+
"Enter a nonnegative monthly price in supported currency units.",
|
|
71
71
|
);
|
|
72
72
|
const feeBasisPoints =
|
|
73
73
|
fields.saleFee.trim() === ""
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import "@testing-library/jest-dom/vitest";
|
|
3
|
+
import { cleanup, render, screen } from "@testing-library/react";
|
|
4
|
+
import userEvent from "@testing-library/user-event";
|
|
5
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
6
|
+
import { parseChargeAmount } from "../../client/charge-currency.js";
|
|
7
|
+
import { PlanPricingEditor } from "./PlanPricingEditor.js";
|
|
8
|
+
|
|
9
|
+
afterEach(cleanup);
|
|
10
|
+
const plan = {
|
|
11
|
+
id: "original-plan",
|
|
12
|
+
name: "Original",
|
|
13
|
+
monthlyAmount: 0,
|
|
14
|
+
feeBasisPoints: null,
|
|
15
|
+
affiliateBasisPoints: 2500,
|
|
16
|
+
version: 7,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
it.each([
|
|
20
|
+
["jpy", "0", 0],
|
|
21
|
+
["usd", "0.00", 0],
|
|
22
|
+
["jpy", "100", 100],
|
|
23
|
+
])(
|
|
24
|
+
"saves the exact original %s price %s through the native form",
|
|
25
|
+
async (currency, display, amount) => {
|
|
26
|
+
const onSave = vi.fn().mockResolvedValue(undefined);
|
|
27
|
+
render(
|
|
28
|
+
<PlanPricingEditor
|
|
29
|
+
plan={{ ...plan, currency, monthlyAmount: amount }}
|
|
30
|
+
onSave={onSave}
|
|
31
|
+
/>,
|
|
32
|
+
);
|
|
33
|
+
expect(parseChargeAmount(display, currency)).toBe(amount);
|
|
34
|
+
expect(
|
|
35
|
+
screen.getByLabelText(`Monthly price (${currency.toUpperCase()})`),
|
|
36
|
+
).toHaveValue(display);
|
|
37
|
+
await userEvent.click(screen.getByRole("button", { name: "Save pricing" }));
|
|
38
|
+
expect(onSave).toHaveBeenCalledExactlyOnceWith({
|
|
39
|
+
monthlyAmount: amount,
|
|
40
|
+
feeBasisPoints: null,
|
|
41
|
+
affiliateBasisPoints: 2500,
|
|
42
|
+
expectedVersion: 7,
|
|
43
|
+
});
|
|
44
|
+
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
|
45
|
+
expect(screen.getByRole("status")).toHaveTextContent("Pricing saved");
|
|
46
|
+
},
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
it.each(["jpy", "usd"])(
|
|
50
|
+
"saves edited zero %s through the render-prop controller with an explicit zero fee",
|
|
51
|
+
async (currency) => {
|
|
52
|
+
const onSave = vi.fn().mockResolvedValue(undefined);
|
|
53
|
+
render(
|
|
54
|
+
<PlanPricingEditor
|
|
55
|
+
plan={{ ...plan, currency, monthlyAmount: 100, feeBasisPoints: 0 }}
|
|
56
|
+
onSave={onSave}
|
|
57
|
+
>
|
|
58
|
+
{(state) => (
|
|
59
|
+
<>
|
|
60
|
+
<button onClick={() => state.setMonthlyPrice("0")}>Zero</button>
|
|
61
|
+
<button onClick={() => void state.save()}>Save</button>
|
|
62
|
+
<output>{state.monthlyPrice}</output>
|
|
63
|
+
{state.error && <p role="alert">{state.error}</p>}
|
|
64
|
+
</>
|
|
65
|
+
)}
|
|
66
|
+
</PlanPricingEditor>,
|
|
67
|
+
);
|
|
68
|
+
await userEvent.click(screen.getByRole("button", { name: "Zero" }));
|
|
69
|
+
expect(screen.getByRole("status")).toHaveTextContent("0");
|
|
70
|
+
await userEvent.click(screen.getByRole("button", { name: "Save" }));
|
|
71
|
+
expect(onSave).toHaveBeenCalledExactlyOnceWith({
|
|
72
|
+
monthlyAmount: 0,
|
|
73
|
+
feeBasisPoints: 0,
|
|
74
|
+
affiliateBasisPoints: 2500,
|
|
75
|
+
expectedVersion: 7,
|
|
76
|
+
});
|
|
77
|
+
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
|
78
|
+
},
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
it("retries zero with the original version after a failed save", async () => {
|
|
82
|
+
const onSave = vi
|
|
83
|
+
.fn()
|
|
84
|
+
.mockRejectedValueOnce(new Error("Retry"))
|
|
85
|
+
.mockResolvedValue(undefined);
|
|
86
|
+
render(
|
|
87
|
+
<PlanPricingEditor plan={{ ...plan, currency: "jpy" }} onSave={onSave} />,
|
|
88
|
+
);
|
|
89
|
+
await userEvent.click(screen.getByRole("button", { name: "Save pricing" }));
|
|
90
|
+
expect(screen.getByRole("alert")).toHaveTextContent("Retry");
|
|
91
|
+
await userEvent.click(screen.getByRole("button", { name: "Save pricing" }));
|
|
92
|
+
expect(onSave).toHaveBeenCalledTimes(2);
|
|
93
|
+
expect(onSave).toHaveBeenNthCalledWith(2, {
|
|
94
|
+
monthlyAmount: 0,
|
|
95
|
+
feeBasisPoints: null,
|
|
96
|
+
affiliateBasisPoints: 2500,
|
|
97
|
+
expectedVersion: 7,
|
|
98
|
+
});
|
|
99
|
+
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
|
100
|
+
});
|