@fantastic.dev/better-stripe 0.30.1 → 0.30.3

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.
Files changed (49) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +14 -2
  3. package/docs/affiliate-clawbacks.md +5 -5
  4. package/docs/settlement.md +1 -1
  5. package/package.json +2 -2
  6. package/src/component/{affiliate-direct-payout.test.ts → affiliatePayouts/affiliate-direct-payout.test.ts} +6 -7
  7. package/src/component/{affiliate-component-pagination.test.ts → affiliateRecovery/affiliate-component-pagination.test.ts} +7 -10
  8. package/src/component/{affiliate-recovery-lifecycle.test.ts → affiliateRecovery/affiliate-recovery-lifecycle.test.ts} +6 -7
  9. package/src/component/billing/checkout.test.ts +89 -0
  10. package/src/component/billing/invoices.test.ts +294 -0
  11. package/src/component/{store-subscriptions.test.ts → billing/store-subscriptions.test.ts} +2 -3
  12. package/src/component/{subscription-scoping.test.ts → billing/subscription-scoping.test.ts} +2 -4
  13. package/src/component/billing/subscriptions.test.ts +477 -0
  14. package/src/component/{connect.test.ts → connect/connect.test.ts} +2 -4
  15. package/src/component/{fee-routing.test.ts → connect/fee-routing.test.ts} +2 -4
  16. package/src/component/connect/payments.test.ts +88 -0
  17. package/src/component/connect/payouts.test.ts +173 -0
  18. package/src/component/{reversal-races.test.ts → connect/reversal-races.test.ts} +4 -6
  19. package/src/component/{seller-case-ownership.test.ts → connect/seller-case-ownership.test.ts} +2 -3
  20. package/src/component/{transfers.test.ts → connect/transfers.test.ts} +3 -5
  21. package/src/component/core/accounts.test.ts +266 -0
  22. package/src/component/{config-queries.test.ts → core/config-queries.test.ts} +2 -4
  23. package/src/component/{payout-reset-safety.test.ts → core/payout-reset-safety.test.ts} +1 -2
  24. package/src/component/core/reset.test.ts +124 -0
  25. package/src/component/{cost-recovery.test.ts → costRecovery/cost-recovery.test.ts} +1 -2
  26. package/src/component/{currency-migration.test.ts → earnings/currency-migration.test.ts} +2 -3
  27. package/src/component/{currency-totals.test.ts → earnings/currency-totals.test.ts} +2 -3
  28. package/src/component/{earnings-totals.test.ts → earnings/earnings-totals.test.ts} +3 -5
  29. package/src/component/{summary-arithmetic.test.ts → earnings/summary-arithmetic.test.ts} +2 -3
  30. package/src/component/{payout-policy-adoption.test.ts → payoutPolicy/payout-policy-adoption.test.ts} +2 -3
  31. package/src/component/{payout-policy.test.ts → payoutPolicy/payout-policy.test.ts} +1 -2
  32. package/src/component/{payout-adoption.test.ts → payoutRelease/payout-adoption.test.ts} +2 -6
  33. package/src/component/{payout-fees.test.ts → payoutRelease/payout-fees.test.ts} +2 -3
  34. package/src/component/{payout-recovery-integration.test.ts → payoutRelease/payout-recovery-integration.test.ts} +3 -4
  35. package/src/component/{payout-release-snapshot.test.ts → payoutRelease/payout-release-snapshot.test.ts} +1 -2
  36. package/src/component/{payout-release.test.ts → payoutRelease/payout-release.test.ts} +2 -3
  37. package/src/component/{payout-revisions.test.ts → payoutSnapshots/payout-revisions.test.ts} +1 -2
  38. package/src/component/{payout-snapshots.test.ts → payoutSnapshots/payout-snapshots.test.ts} +4 -5
  39. package/src/component/{payout-sync.test.ts → payoutSync/payout-sync.test.ts} +1 -2
  40. package/src/component/{payout-component-pagination.test.ts → payoutWorker/payout-component-pagination.test.ts} +8 -9
  41. package/src/component/{payout-worker.test.ts → payoutWorker/payout-worker.test.ts} +1 -2
  42. package/src/component/products/products.test.ts +239 -0
  43. package/src/component/{read-pagination.test.ts → products/read-pagination.test.ts} +2 -3
  44. package/src/component/{refund-history.test.ts → refundHistory/refund-history.test.ts} +2 -3
  45. package/src/component/{affiliate-clawbacks.test.ts → settlement/affiliate-clawbacks.test.ts} +3 -4
  46. package/src/component/{settlement-claims.test.ts → settlement/settlement-claims.test.ts} +2 -3
  47. package/src/component/webhooks/events.test.ts +121 -0
  48. package/src/component/{triggers.test.ts → webhooks/triggers.test.ts} +2 -4
  49. package/src/component/public.test.ts +0 -1858
@@ -0,0 +1,477 @@
1
+ // @vitest-environment edge-runtime
2
+ import { convexTest } from "convex-test";
3
+ import { describe, expect, it } from "vitest";
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
6
+
7
+ describe("billing — subscription mutations and queries", () => {
8
+ it("upsertSubscription + getActiveSubscription", async () => {
9
+ const t = convexTest(schema, modules);
10
+
11
+ await t.mutation(api.billing.mutations.upsertSubscription, {
12
+ stripeSubscriptionId: "sub_001",
13
+ userId: "user_010",
14
+ status: "active",
15
+ cancelAtPeriodEnd: false,
16
+ isTrialing: false,
17
+ });
18
+
19
+ const sub = await t.query(api.billing.queries.getActiveSubscription, {
20
+ userId: "user_010",
21
+ });
22
+
23
+ expect(sub).not.toBeNull();
24
+ expect(sub!.stripeSubscriptionId).toBe("sub_001");
25
+ expect(sub!.status).toBe("active");
26
+ });
27
+
28
+ it("getTrialStatus: returns trial days remaining", async () => {
29
+ const t = convexTest(schema, modules);
30
+
31
+ const trialStart = "2030-01-01T00:00:00.000Z";
32
+ const trialEnd = "2030-01-08T00:00:00.000Z";
33
+
34
+ await t.mutation(api.billing.mutations.upsertSubscription, {
35
+ stripeSubscriptionId: "sub_trial_001",
36
+ userId: "user_trial",
37
+ status: "trialing",
38
+ cancelAtPeriodEnd: false,
39
+ isTrialing: true,
40
+ trialStart,
41
+ trialEnd,
42
+ });
43
+
44
+ const doc = await t.query(api.billing.queries.getSubscriptionByStripeId, {
45
+ stripeSubscriptionId: "sub_trial_001",
46
+ });
47
+
48
+ const trialStatus = await t.query(api.billing.queries.getTrialStatus, {
49
+ subscriptionId: doc!._id,
50
+ });
51
+
52
+ expect(trialStatus).not.toBeNull();
53
+ expect(trialStatus!.isTrialing).toBe(true);
54
+ expect(trialStatus!.daysRemaining).toBeGreaterThanOrEqual(6);
55
+ expect(trialStatus!.status).toBe("trialing");
56
+ });
57
+
58
+ // BTS-33: dunning tracks the delinquent subscription lifecycle. Stripe moves a
59
+ // subscription to `past_due` on the first failed recurring charge and to
60
+ // `unpaid` once smart retries are exhausted — both must round-trip the table.
61
+ it.each(["past_due", "unpaid"] as const)(
62
+ "persists the delinquent subscription status %s",
63
+ async (status) => {
64
+ const t = convexTest(schema, modules);
65
+
66
+ await t.mutation(api.billing.mutations.upsertSubscription, {
67
+ stripeSubscriptionId: `sub_${status}`,
68
+ userId: "user_dunning",
69
+ status,
70
+ cancelAtPeriodEnd: false,
71
+ isTrialing: false,
72
+ });
73
+
74
+ const doc = await t.query(api.billing.queries.getSubscriptionByStripeId, {
75
+ stripeSubscriptionId: `sub_${status}`,
76
+ });
77
+ expect(doc!.status).toBe(status);
78
+ },
79
+ );
80
+ });
81
+
82
+ describe("billing — listSubscriptions with filters", () => {
83
+ it("filters by status", async () => {
84
+ const t = convexTest(schema, modules);
85
+
86
+ await t.mutation(api.billing.mutations.upsertSubscription, {
87
+ stripeSubscriptionId: "sub_active",
88
+ userId: "user_a",
89
+ status: "active",
90
+ cancelAtPeriodEnd: false,
91
+ isTrialing: false,
92
+ });
93
+
94
+ await t.mutation(api.billing.mutations.upsertSubscription, {
95
+ stripeSubscriptionId: "sub_canceled",
96
+ userId: "user_b",
97
+ status: "canceled",
98
+ cancelAtPeriodEnd: false,
99
+ isTrialing: false,
100
+ });
101
+
102
+ const active = await t.query(api.billing.queries.listSubscriptions, {
103
+ status: "active",
104
+ });
105
+ const canceled = await t.query(api.billing.queries.listSubscriptions, {
106
+ status: "canceled",
107
+ });
108
+
109
+ expect(active).toHaveLength(1);
110
+ expect(active[0].stripeSubscriptionId).toBe("sub_active");
111
+ expect(canceled).toHaveLength(1);
112
+ expect(canceled[0].stripeSubscriptionId).toBe("sub_canceled");
113
+ });
114
+
115
+ it("rejects an invalid status with a validation error", async () => {
116
+ const t = convexTest(schema, modules);
117
+
118
+ await expect(
119
+ t.query(api.billing.queries.listSubscriptions, {
120
+ // @ts-expect-error — invalid status must be rejected by the validator
121
+ status: "not_a_real_status",
122
+ }),
123
+ ).rejects.toThrow(/Validator error/);
124
+ });
125
+
126
+ it("respects limit", async () => {
127
+ const t = convexTest(schema, modules);
128
+
129
+ for (let i = 0; i < 5; i++) {
130
+ await t.mutation(api.billing.mutations.upsertSubscription, {
131
+ stripeSubscriptionId: `sub_limit_${i}`,
132
+ userId: `user_limit_${i}`,
133
+ status: "active",
134
+ cancelAtPeriodEnd: false,
135
+ isTrialing: false,
136
+ });
137
+ }
138
+
139
+ const limited = await t.query(api.billing.queries.listSubscriptions, {
140
+ limit: 2,
141
+ });
142
+
143
+ expect(limited).toHaveLength(2);
144
+ });
145
+
146
+ it("filters by accountId", async () => {
147
+ const t = convexTest(schema, modules);
148
+
149
+ await t.mutation(api.billing.mutations.upsertSubscription, {
150
+ stripeSubscriptionId: "sub_acct_a1",
151
+ accountId: "acct_sub_a",
152
+ userId: "user_sub_acct_a",
153
+ status: "active",
154
+ cancelAtPeriodEnd: false,
155
+ isTrialing: false,
156
+ });
157
+
158
+ await t.mutation(api.billing.mutations.upsertSubscription, {
159
+ stripeSubscriptionId: "sub_acct_a2",
160
+ accountId: "acct_sub_a",
161
+ userId: "user_sub_acct_a",
162
+ status: "canceled",
163
+ cancelAtPeriodEnd: false,
164
+ isTrialing: false,
165
+ });
166
+
167
+ await t.mutation(api.billing.mutations.upsertSubscription, {
168
+ stripeSubscriptionId: "sub_acct_b1",
169
+ accountId: "acct_sub_b",
170
+ userId: "user_sub_acct_b",
171
+ status: "active",
172
+ cancelAtPeriodEnd: false,
173
+ isTrialing: false,
174
+ });
175
+
176
+ const subs = await t.query(api.billing.queries.listSubscriptions, {
177
+ accountId: "acct_sub_a",
178
+ });
179
+
180
+ expect(subs).toHaveLength(2);
181
+ for (const sub of subs) {
182
+ expect(sub.accountId).toBe("acct_sub_a");
183
+ }
184
+
185
+ const statusFiltered = await t.query(
186
+ api.billing.queries.listSubscriptions,
187
+ {
188
+ accountId: "acct_sub_a",
189
+ status: "active",
190
+ },
191
+ );
192
+
193
+ expect(statusFiltered).toHaveLength(1);
194
+ expect(statusFiltered[0].stripeSubscriptionId).toBe("sub_acct_a1");
195
+ });
196
+ });
197
+
198
+ describe("billing — listSubscriptionsByOrg", () => {
199
+ it("returns subscriptions for the given org", async () => {
200
+ const t = convexTest(schema, modules);
201
+
202
+ await t.mutation(api.billing.mutations.upsertSubscription, {
203
+ stripeSubscriptionId: "sub_org_a",
204
+ userId: "user_org_a",
205
+ orgId: "org_a",
206
+ status: "active",
207
+ cancelAtPeriodEnd: false,
208
+ isTrialing: false,
209
+ });
210
+
211
+ await t.mutation(api.billing.mutations.upsertSubscription, {
212
+ stripeSubscriptionId: "sub_org_b",
213
+ userId: "user_org_b",
214
+ orgId: "org_b",
215
+ status: "active",
216
+ cancelAtPeriodEnd: false,
217
+ isTrialing: false,
218
+ });
219
+
220
+ const orgASubs = await t.query(api.billing.queries.listSubscriptionsByOrg, {
221
+ orgId: "org_a",
222
+ });
223
+
224
+ expect(orgASubs).toHaveLength(1);
225
+ expect(orgASubs[0].stripeSubscriptionId).toBe("sub_org_a");
226
+ });
227
+
228
+ it("filters by status within org", async () => {
229
+ const t = convexTest(schema, modules);
230
+
231
+ await t.mutation(api.billing.mutations.upsertSubscription, {
232
+ stripeSubscriptionId: "sub_org_active",
233
+ userId: "user_x",
234
+ orgId: "org_x",
235
+ status: "active",
236
+ cancelAtPeriodEnd: false,
237
+ isTrialing: false,
238
+ });
239
+
240
+ await t.mutation(api.billing.mutations.upsertSubscription, {
241
+ stripeSubscriptionId: "sub_org_canceled",
242
+ userId: "user_x",
243
+ orgId: "org_x",
244
+ status: "canceled",
245
+ cancelAtPeriodEnd: false,
246
+ isTrialing: false,
247
+ });
248
+
249
+ const active = await t.query(api.billing.queries.listSubscriptionsByOrg, {
250
+ orgId: "org_x",
251
+ status: "active",
252
+ });
253
+
254
+ expect(active).toHaveLength(1);
255
+ expect(active[0].status).toBe("active");
256
+ });
257
+ });
258
+
259
+ describe("billing — getActiveSubscription with orgId", () => {
260
+ it("finds active subscription by orgId", async () => {
261
+ const t = convexTest(schema, modules);
262
+
263
+ await t.mutation(api.billing.mutations.upsertSubscription, {
264
+ stripeSubscriptionId: "sub_org_act",
265
+ userId: "user_oa",
266
+ orgId: "org_act",
267
+ status: "active",
268
+ cancelAtPeriodEnd: false,
269
+ isTrialing: false,
270
+ });
271
+
272
+ const sub = await t.query(api.billing.queries.getActiveSubscription, {
273
+ userId: "user_oa",
274
+ orgId: "org_act",
275
+ });
276
+
277
+ expect(sub).not.toBeNull();
278
+ expect(sub!.stripeSubscriptionId).toBe("sub_org_act");
279
+ });
280
+
281
+ it("finds trialing subscription as active", async () => {
282
+ const t = convexTest(schema, modules);
283
+
284
+ await t.mutation(api.billing.mutations.upsertSubscription, {
285
+ stripeSubscriptionId: "sub_trial_act",
286
+ userId: "user_trial_act",
287
+ status: "trialing",
288
+ cancelAtPeriodEnd: false,
289
+ isTrialing: true,
290
+ });
291
+
292
+ const sub = await t.query(api.billing.queries.getActiveSubscription, {
293
+ userId: "user_trial_act",
294
+ });
295
+
296
+ expect(sub).not.toBeNull();
297
+ expect(sub!.status).toBe("trialing");
298
+ });
299
+
300
+ it("returns null when no active subscription exists", async () => {
301
+ const t = convexTest(schema, modules);
302
+
303
+ await t.mutation(api.billing.mutations.upsertSubscription, {
304
+ stripeSubscriptionId: "sub_canceled_only",
305
+ userId: "user_canceled",
306
+ status: "canceled",
307
+ cancelAtPeriodEnd: false,
308
+ isTrialing: false,
309
+ });
310
+
311
+ const sub = await t.query(api.billing.queries.getActiveSubscription, {
312
+ userId: "user_canceled",
313
+ });
314
+
315
+ expect(sub).toBeNull();
316
+ });
317
+ });
318
+
319
+ describe("regression — listSubscriptions returns complete results beyond limit", () => {
320
+ it("returns all active subscriptions even when canceled ones fill the default limit", async () => {
321
+ const t = convexTest(schema, modules);
322
+ const accountId = "acct_regression_sub";
323
+
324
+ // Insert 55 canceled then 5 active subscriptions for the same accountId.
325
+ // Old code: take(50) → JS filter → returns 0 active. New code: compound index → 5.
326
+ for (let i = 0; i < 55; i++) {
327
+ await t.mutation(api.billing.mutations.upsertSubscription, {
328
+ stripeSubscriptionId: `sub_canceled_${i}`,
329
+ accountId,
330
+ userId: "user_regression_sub",
331
+ status: "canceled",
332
+ cancelAtPeriodEnd: false,
333
+ isTrialing: false,
334
+ });
335
+ }
336
+ for (let i = 0; i < 5; i++) {
337
+ await t.mutation(api.billing.mutations.upsertSubscription, {
338
+ stripeSubscriptionId: `sub_active_${i}`,
339
+ accountId,
340
+ userId: "user_regression_sub",
341
+ status: "active",
342
+ cancelAtPeriodEnd: false,
343
+ isTrialing: false,
344
+ });
345
+ }
346
+
347
+ const result = await t.query(api.billing.queries.listSubscriptions, {
348
+ accountId,
349
+ status: "active",
350
+ });
351
+
352
+ expect(result).toHaveLength(5);
353
+ expect(result.every((s) => s.status === "active")).toBe(true);
354
+ });
355
+
356
+ it("respects limit when filtering by status via compound index", async () => {
357
+ const t = convexTest(schema, modules);
358
+ const accountId = "acct_regression_sub_limit";
359
+
360
+ for (let i = 0; i < 60; i++) {
361
+ await t.mutation(api.billing.mutations.upsertSubscription, {
362
+ stripeSubscriptionId: `sub_lim_active_${i}`,
363
+ accountId,
364
+ userId: "user_regression_sub_limit",
365
+ status: "active",
366
+ cancelAtPeriodEnd: false,
367
+ isTrialing: false,
368
+ });
369
+ }
370
+
371
+ const result = await t.query(api.billing.queries.listSubscriptions, {
372
+ accountId,
373
+ status: "active",
374
+ limit: 10,
375
+ });
376
+
377
+ expect(result).toHaveLength(10);
378
+ });
379
+ });
380
+
381
+ describe("guard — empty userId/orgId never matches user-scoped queries", () => {
382
+ it("getActiveSubscription({ userId: '' }) returns null even when an unattributed row exists", async () => {
383
+ const t = convexTest(schema, modules);
384
+
385
+ // Insert an unattributed subscription (the sentinel row written by webhook processors).
386
+ await t.mutation(api.billing.mutations.upsertSubscription, {
387
+ stripeSubscriptionId: "sub_unattr_001",
388
+ userId: "",
389
+ status: "active",
390
+ cancelAtPeriodEnd: false,
391
+ isTrialing: false,
392
+ });
393
+
394
+ const result = await t.query(api.billing.queries.getActiveSubscription, {
395
+ userId: "",
396
+ });
397
+
398
+ expect(result).toBeNull();
399
+ });
400
+
401
+ it("listSubscriptionsByUser({ userId: '' }) returns [] even when an unattributed row exists", async () => {
402
+ const t = convexTest(schema, modules);
403
+
404
+ await t.mutation(api.billing.mutations.upsertSubscription, {
405
+ stripeSubscriptionId: "sub_unattr_002",
406
+ userId: "",
407
+ status: "active",
408
+ cancelAtPeriodEnd: false,
409
+ isTrialing: false,
410
+ });
411
+
412
+ const result = await t.query(api.billing.queries.listSubscriptionsByUser, {
413
+ userId: "",
414
+ });
415
+
416
+ expect(result).toEqual([]);
417
+ });
418
+
419
+ it("listSubscriptionsByUser({ userId: 'user_1' }) returns only the user_1 row, not the '' row", async () => {
420
+ const t = convexTest(schema, modules);
421
+
422
+ // Insert one unattributed and one attributed subscription.
423
+ await t.mutation(api.billing.mutations.upsertSubscription, {
424
+ stripeSubscriptionId: "sub_unattr_003",
425
+ userId: "",
426
+ status: "active",
427
+ cancelAtPeriodEnd: false,
428
+ isTrialing: false,
429
+ });
430
+ await t.mutation(api.billing.mutations.upsertSubscription, {
431
+ stripeSubscriptionId: "sub_user1_001",
432
+ userId: "user_1",
433
+ status: "active",
434
+ cancelAtPeriodEnd: false,
435
+ isTrialing: false,
436
+ });
437
+
438
+ const result = await t.query(api.billing.queries.listSubscriptionsByUser, {
439
+ userId: "user_1",
440
+ });
441
+
442
+ expect(result).toHaveLength(1);
443
+ expect(result[0].stripeSubscriptionId).toBe("sub_user1_001");
444
+ });
445
+
446
+ it("listInvoices({ userId: '' }) falls through to the unfiltered branch", async () => {
447
+ const t = convexTest(schema, modules);
448
+
449
+ // Insert one unattributed invoice and one attributed invoice.
450
+ await t.mutation(api.billing.mutations.upsertInvoice, {
451
+ stripeInvoiceId: "inv_unattr_001",
452
+ userId: "",
453
+ status: "paid",
454
+ currency: "usd",
455
+ amountDue: 1000,
456
+ amountPaid: 1000,
457
+ });
458
+ await t.mutation(api.billing.mutations.upsertInvoice, {
459
+ stripeInvoiceId: "inv_user2_001",
460
+ userId: "user_2",
461
+ status: "paid",
462
+ currency: "usd",
463
+ amountDue: 2000,
464
+ amountPaid: 2000,
465
+ });
466
+
467
+ // With userId: "" the guard causes fallthrough to the unfiltered take — both rows should appear.
468
+ const result = await t.query(api.billing.queries.listInvoices, {
469
+ userId: "",
470
+ });
471
+
472
+ expect(result.length).toBeGreaterThanOrEqual(2);
473
+ const ids = result.map((i) => i.stripeInvoiceId);
474
+ expect(ids).toContain("inv_unattr_001");
475
+ expect(ids).toContain("inv_user2_001");
476
+ });
477
+ });
@@ -2,10 +2,8 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { describe, expect, it } from "vitest";
4
4
 
5
- import { api } from "./_generated/api.js";
6
- import schema from "./schema.js";
7
-
8
- const modules = import.meta.glob("./**/*.*s");
5
+ import { api } from "../_generated/api.js";
6
+ import { schema, modules } from "../../test.js";
9
7
 
10
8
  describe("connect — refunds", () => {
11
9
  const seedRefund = async (
@@ -2,10 +2,8 @@
2
2
  import { convexTest } from "convex-test";
3
3
  import { describe, expect, it } from "vitest";
4
4
 
5
- import { api } from "./_generated/api.js";
6
- import schema from "./schema.js";
7
-
8
- const modules = import.meta.glob("./**/*.*s");
5
+ import { api } from "../_generated/api.js";
6
+ import { schema, modules } from "../../test.js";
9
7
 
10
8
  describe("fee & fund-routing fields (BTS-11)", () => {
11
9
  it("persists destination + fee fields on a payment and reads them back", async () => {
@@ -0,0 +1,88 @@
1
+ // @vitest-environment edge-runtime
2
+ import { convexTest } from "convex-test";
3
+ import { describe, expect, it } from "vitest";
4
+ import { api } from "../_generated/api.js";
5
+ import { schema, modules } from "../../test.js";
6
+
7
+ describe("connect — payment mutations and queries", () => {
8
+ it("upsertPayment inserts new payment", async () => {
9
+ const t = convexTest(schema, modules);
10
+
11
+ await t.mutation(api.connect.mutations.upsertPayment, {
12
+ stripePaymentIntentId: "pi_001",
13
+ userId: "user_pay_001",
14
+ amount: 5000,
15
+ currency: "usd",
16
+ status: "succeeded",
17
+ });
18
+
19
+ const payments = await t.run(async (ctx) => {
20
+ return await ctx.db.query("payments").collect();
21
+ });
22
+
23
+ expect(payments).toHaveLength(1);
24
+ expect(payments[0]).toMatchObject({
25
+ stripePaymentIntentId: "pi_001",
26
+ amount: 5000,
27
+ status: "succeeded",
28
+ });
29
+ });
30
+
31
+ it("upsertPayment updates existing payment by stripePaymentIntentId", async () => {
32
+ const t = convexTest(schema, modules);
33
+
34
+ await t.mutation(api.connect.mutations.upsertPayment, {
35
+ stripePaymentIntentId: "pi_002",
36
+ userId: "user_pay_002",
37
+ amount: 3000,
38
+ currency: "usd",
39
+ status: "processing",
40
+ });
41
+
42
+ await t.mutation(api.connect.mutations.upsertPayment, {
43
+ stripePaymentIntentId: "pi_002",
44
+ userId: "user_pay_002",
45
+ amount: 3000,
46
+ currency: "usd",
47
+ status: "succeeded",
48
+ });
49
+
50
+ const payments = await t.run(async (ctx) => {
51
+ return await ctx.db.query("payments").collect();
52
+ });
53
+
54
+ expect(payments).toHaveLength(1);
55
+ expect(payments[0].status).toBe("succeeded");
56
+ });
57
+
58
+ it("getPaymentByStripeId: returns matching payment", async () => {
59
+ const t = convexTest(schema, modules);
60
+
61
+ await t.mutation(api.connect.mutations.upsertPayment, {
62
+ stripePaymentIntentId: "pi_by_stripe_001",
63
+ userId: "user_pay_003",
64
+ amount: 4200,
65
+ currency: "usd",
66
+ status: "succeeded",
67
+ });
68
+
69
+ const payment = await t.query(api.connect.queries.getPaymentByStripeId, {
70
+ stripePaymentIntentId: "pi_by_stripe_001",
71
+ });
72
+
73
+ expect(payment).not.toBeNull();
74
+ expect(payment!.stripePaymentIntentId).toBe("pi_by_stripe_001");
75
+ expect(payment!.userId).toBe("user_pay_003");
76
+ expect(payment!.amount).toBe(4200);
77
+ });
78
+
79
+ it("getPaymentByStripeId: returns null for unknown id", async () => {
80
+ const t = convexTest(schema, modules);
81
+
82
+ const payment = await t.query(api.connect.queries.getPaymentByStripeId, {
83
+ stripePaymentIntentId: "pi_does_not_exist",
84
+ });
85
+
86
+ expect(payment).toBeNull();
87
+ });
88
+ });