@fantastic.dev/better-stripe 0.30.2 → 0.31.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.
Files changed (61) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +10 -1
  3. package/dist/client/billing/invoicePages.d.ts +28 -0
  4. package/dist/client/billing/invoicePages.d.ts.map +1 -0
  5. package/dist/client/billing/invoicePages.js +86 -0
  6. package/dist/client/billing/invoicePages.js.map +1 -0
  7. package/dist/client/index.d.ts +4 -0
  8. package/dist/client/index.d.ts.map +1 -1
  9. package/dist/client/index.js +5 -0
  10. package/dist/client/index.js.map +1 -1
  11. package/docs/affiliate-clawbacks.md +5 -5
  12. package/docs/read-contracts.md +32 -0
  13. package/docs/settlement.md +1 -1
  14. package/package.json +4 -3
  15. package/src/client/billing/invoicePages.test.ts +174 -0
  16. package/src/client/billing/invoicePages.ts +110 -0
  17. package/src/client/index.ts +13 -0
  18. package/src/component/{affiliate-direct-payout.test.ts → affiliatePayouts/affiliate-direct-payout.test.ts} +6 -7
  19. package/src/component/{affiliate-component-pagination.test.ts → affiliateRecovery/affiliate-component-pagination.test.ts} +7 -10
  20. package/src/component/{affiliate-recovery-lifecycle.test.ts → affiliateRecovery/affiliate-recovery-lifecycle.test.ts} +6 -7
  21. package/src/component/billing/checkout.test.ts +89 -0
  22. package/src/component/billing/invoices.test.ts +294 -0
  23. package/src/component/{store-subscriptions.test.ts → billing/store-subscriptions.test.ts} +2 -3
  24. package/src/component/{subscription-scoping.test.ts → billing/subscription-scoping.test.ts} +2 -4
  25. package/src/component/billing/subscriptions.test.ts +477 -0
  26. package/src/component/{connect.test.ts → connect/connect.test.ts} +2 -4
  27. package/src/component/{fee-routing.test.ts → connect/fee-routing.test.ts} +2 -4
  28. package/src/component/connect/payments.test.ts +88 -0
  29. package/src/component/connect/payouts.test.ts +173 -0
  30. package/src/component/{reversal-races.test.ts → connect/reversal-races.test.ts} +4 -6
  31. package/src/component/{seller-case-ownership.test.ts → connect/seller-case-ownership.test.ts} +2 -3
  32. package/src/component/{transfers.test.ts → connect/transfers.test.ts} +3 -5
  33. package/src/component/core/accounts.test.ts +266 -0
  34. package/src/component/{config-queries.test.ts → core/config-queries.test.ts} +2 -4
  35. package/src/component/{payout-reset-safety.test.ts → core/payout-reset-safety.test.ts} +1 -2
  36. package/src/component/core/reset.test.ts +124 -0
  37. package/src/component/{cost-recovery.test.ts → costRecovery/cost-recovery.test.ts} +1 -2
  38. package/src/component/{currency-migration.test.ts → earnings/currency-migration.test.ts} +2 -3
  39. package/src/component/{currency-totals.test.ts → earnings/currency-totals.test.ts} +2 -3
  40. package/src/component/{earnings-totals.test.ts → earnings/earnings-totals.test.ts} +3 -5
  41. package/src/component/{summary-arithmetic.test.ts → earnings/summary-arithmetic.test.ts} +2 -3
  42. package/src/component/{payout-policy-adoption.test.ts → payoutPolicy/payout-policy-adoption.test.ts} +2 -3
  43. package/src/component/{payout-policy.test.ts → payoutPolicy/payout-policy.test.ts} +1 -2
  44. package/src/component/{payout-adoption.test.ts → payoutRelease/payout-adoption.test.ts} +2 -6
  45. package/src/component/{payout-fees.test.ts → payoutRelease/payout-fees.test.ts} +2 -3
  46. package/src/component/{payout-recovery-integration.test.ts → payoutRelease/payout-recovery-integration.test.ts} +3 -4
  47. package/src/component/{payout-release-snapshot.test.ts → payoutRelease/payout-release-snapshot.test.ts} +1 -2
  48. package/src/component/{payout-release.test.ts → payoutRelease/payout-release.test.ts} +2 -3
  49. package/src/component/{payout-revisions.test.ts → payoutSnapshots/payout-revisions.test.ts} +1 -2
  50. package/src/component/{payout-snapshots.test.ts → payoutSnapshots/payout-snapshots.test.ts} +4 -5
  51. package/src/component/{payout-sync.test.ts → payoutSync/payout-sync.test.ts} +1 -2
  52. package/src/component/{payout-component-pagination.test.ts → payoutWorker/payout-component-pagination.test.ts} +8 -9
  53. package/src/component/{payout-worker.test.ts → payoutWorker/payout-worker.test.ts} +1 -2
  54. package/src/component/products/products.test.ts +239 -0
  55. package/src/component/{read-pagination.test.ts → products/read-pagination.test.ts} +2 -3
  56. package/src/component/{refund-history.test.ts → refundHistory/refund-history.test.ts} +2 -3
  57. package/src/component/{affiliate-clawbacks.test.ts → settlement/affiliate-clawbacks.test.ts} +3 -4
  58. package/src/component/{settlement-claims.test.ts → settlement/settlement-claims.test.ts} +2 -3
  59. package/src/component/webhooks/events.test.ts +121 -0
  60. package/src/component/{triggers.test.ts → webhooks/triggers.test.ts} +2 -4
  61. package/src/component/public.test.ts +0 -1858
@@ -0,0 +1,294 @@
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 — invoice mutations and queries", () => {
8
+ it("rejects an invalid invoice status with a validation error", async () => {
9
+ const t = convexTest(schema, modules);
10
+
11
+ await expect(
12
+ t.mutation(api.billing.mutations.upsertInvoice, {
13
+ stripeInvoiceId: "inv_bad_status",
14
+ userId: "user_030",
15
+ // @ts-expect-error — invalid status must be rejected by the validator
16
+ status: "not_a_real_status",
17
+ currency: "usd",
18
+ amountDue: 1000,
19
+ amountPaid: 0,
20
+ }),
21
+ ).rejects.toThrow(/Validator error/);
22
+ });
23
+
24
+ it("upsertInvoice + listInvoices", async () => {
25
+ const t = convexTest(schema, modules);
26
+
27
+ await t.mutation(api.billing.mutations.upsertInvoice, {
28
+ stripeInvoiceId: "inv_001",
29
+ userId: "user_030",
30
+ status: "paid",
31
+ currency: "usd",
32
+ amountDue: 2000,
33
+ amountPaid: 2000,
34
+ });
35
+
36
+ await t.mutation(api.billing.mutations.upsertInvoice, {
37
+ stripeInvoiceId: "inv_002",
38
+ userId: "user_030",
39
+ status: "open",
40
+ currency: "usd",
41
+ amountDue: 5000,
42
+ amountPaid: 0,
43
+ });
44
+
45
+ const invoices = await t.query(api.billing.queries.listInvoices, {
46
+ userId: "user_030",
47
+ });
48
+
49
+ expect(invoices).toHaveLength(2);
50
+ });
51
+
52
+ it("getInvoiceByStripeId: returns matching invoice", async () => {
53
+ const t = convexTest(schema, modules);
54
+
55
+ await t.mutation(api.billing.mutations.upsertInvoice, {
56
+ stripeInvoiceId: "inv_by_stripe_001",
57
+ userId: "user_031",
58
+ status: "paid",
59
+ currency: "usd",
60
+ amountDue: 1500,
61
+ amountPaid: 1500,
62
+ });
63
+
64
+ const invoice = await t.query(api.billing.queries.getInvoiceByStripeId, {
65
+ stripeInvoiceId: "inv_by_stripe_001",
66
+ });
67
+
68
+ expect(invoice).not.toBeNull();
69
+ expect(invoice!.stripeInvoiceId).toBe("inv_by_stripe_001");
70
+ expect(invoice!.userId).toBe("user_031");
71
+ expect(invoice!.amountDue).toBe(1500);
72
+ });
73
+
74
+ // BTS-33: a failed subscription invoice carries the smart-retry schedule
75
+ // (`next_payment_attempt`) and attempt count so a dunning hook can tell the
76
+ // buyer when Stripe will retry. Both persist as optional invoice fields.
77
+ it("persists smart-retry metadata (nextPaymentAttempt + attemptCount)", async () => {
78
+ const t = convexTest(schema, modules);
79
+
80
+ await t.mutation(api.billing.mutations.upsertInvoice, {
81
+ stripeInvoiceId: "inv_retry_001",
82
+ userId: "user_dunning",
83
+ status: "open",
84
+ currency: "usd",
85
+ amountDue: 5000,
86
+ amountPaid: 0,
87
+ nextPaymentAttempt: "2030-02-01T00:00:00.000Z",
88
+ attemptCount: 1,
89
+ });
90
+
91
+ const invoice = await t.query(api.billing.queries.getInvoiceByStripeId, {
92
+ stripeInvoiceId: "inv_retry_001",
93
+ });
94
+ expect(invoice!.status).toBe("open");
95
+ expect(invoice!.nextPaymentAttempt).toBe("2030-02-01T00:00:00.000Z");
96
+ expect(invoice!.attemptCount).toBe(1);
97
+ });
98
+
99
+ it("getInvoiceByStripeId: returns null for unknown id", async () => {
100
+ const t = convexTest(schema, modules);
101
+
102
+ const invoice = await t.query(api.billing.queries.getInvoiceByStripeId, {
103
+ stripeInvoiceId: "inv_does_not_exist",
104
+ });
105
+
106
+ expect(invoice).toBeNull();
107
+ });
108
+ });
109
+
110
+ describe("billing — listInvoices with filters", () => {
111
+ it("filters by subscriptionId", async () => {
112
+ const t = convexTest(schema, modules);
113
+
114
+ await t.mutation(api.billing.mutations.upsertInvoice, {
115
+ stripeInvoiceId: "inv_sub_a",
116
+ userId: "user_inv",
117
+ subscriptionId: "sub_inv_a",
118
+ status: "paid",
119
+ currency: "usd",
120
+ amountDue: 1000,
121
+ amountPaid: 1000,
122
+ });
123
+
124
+ await t.mutation(api.billing.mutations.upsertInvoice, {
125
+ stripeInvoiceId: "inv_sub_b",
126
+ userId: "user_inv",
127
+ subscriptionId: "sub_inv_b",
128
+ status: "paid",
129
+ currency: "usd",
130
+ amountDue: 2000,
131
+ amountPaid: 2000,
132
+ });
133
+
134
+ const invoices = await t.query(api.billing.queries.listInvoices, {
135
+ subscriptionId: "sub_inv_a",
136
+ });
137
+
138
+ expect(invoices).toHaveLength(1);
139
+ expect(invoices[0].stripeInvoiceId).toBe("inv_sub_a");
140
+ });
141
+
142
+ it("filters by status", async () => {
143
+ const t = convexTest(schema, modules);
144
+
145
+ await t.mutation(api.billing.mutations.upsertInvoice, {
146
+ stripeInvoiceId: "inv_st_paid",
147
+ userId: "user_st",
148
+ status: "paid",
149
+ currency: "usd",
150
+ amountDue: 1000,
151
+ amountPaid: 1000,
152
+ });
153
+
154
+ await t.mutation(api.billing.mutations.upsertInvoice, {
155
+ stripeInvoiceId: "inv_st_open",
156
+ userId: "user_st",
157
+ status: "open",
158
+ currency: "usd",
159
+ amountDue: 2000,
160
+ amountPaid: 0,
161
+ });
162
+
163
+ const paidOnly = await t.query(api.billing.queries.listInvoices, {
164
+ userId: "user_st",
165
+ status: "paid",
166
+ });
167
+
168
+ expect(paidOnly).toHaveLength(1);
169
+ expect(paidOnly[0].status).toBe("paid");
170
+ });
171
+
172
+ it("filters by accountId", async () => {
173
+ const t = convexTest(schema, modules);
174
+
175
+ await t.mutation(api.billing.mutations.upsertInvoice, {
176
+ stripeInvoiceId: "inv_acct_a1",
177
+ userId: "user_acct_a",
178
+ accountId: "acct_inv_a",
179
+ status: "paid",
180
+ currency: "usd",
181
+ amountDue: 1000,
182
+ amountPaid: 1000,
183
+ });
184
+
185
+ await t.mutation(api.billing.mutations.upsertInvoice, {
186
+ stripeInvoiceId: "inv_acct_a2",
187
+ userId: "user_acct_a",
188
+ accountId: "acct_inv_a",
189
+ status: "open",
190
+ currency: "usd",
191
+ amountDue: 2000,
192
+ amountPaid: 0,
193
+ });
194
+
195
+ await t.mutation(api.billing.mutations.upsertInvoice, {
196
+ stripeInvoiceId: "inv_acct_b1",
197
+ userId: "user_acct_b",
198
+ accountId: "acct_inv_b",
199
+ status: "paid",
200
+ currency: "usd",
201
+ amountDue: 3000,
202
+ amountPaid: 3000,
203
+ });
204
+
205
+ const invoices = await t.query(api.billing.queries.listInvoices, {
206
+ accountId: "acct_inv_a",
207
+ });
208
+
209
+ expect(invoices).toHaveLength(2);
210
+ for (const invoice of invoices) {
211
+ expect(invoice.accountId).toBe("acct_inv_a");
212
+ }
213
+
214
+ const statusFiltered = await t.query(api.billing.queries.listInvoices, {
215
+ accountId: "acct_inv_a",
216
+ status: "paid",
217
+ });
218
+
219
+ expect(statusFiltered).toHaveLength(1);
220
+ expect(statusFiltered[0].stripeInvoiceId).toBe("inv_acct_a1");
221
+ });
222
+ });
223
+
224
+ describe("billing — subscription backfills orphaned invoices", () => {
225
+ it("upsertSubscription patches orphaned invoices with userId", async () => {
226
+ const t = convexTest(schema, modules);
227
+
228
+ // Insert an invoice linked to a subscription but without userId
229
+ await t.run(async (ctx) => {
230
+ await ctx.db.insert("invoices", {
231
+ stripeInvoiceId: "inv_orphan",
232
+ userId: "",
233
+ subscriptionId: "sub_backfill",
234
+ status: "paid",
235
+ currency: "usd",
236
+ amountDue: 3000,
237
+ amountPaid: 3000,
238
+ });
239
+ });
240
+
241
+ // Now upsert the subscription — should backfill the invoice
242
+ await t.mutation(api.billing.mutations.upsertSubscription, {
243
+ stripeSubscriptionId: "sub_backfill",
244
+ userId: "user_backfill",
245
+ status: "active",
246
+ cancelAtPeriodEnd: false,
247
+ isTrialing: false,
248
+ });
249
+
250
+ const invoices = await t.query(api.billing.queries.listInvoices, {
251
+ userId: "user_backfill",
252
+ });
253
+
254
+ expect(invoices).toHaveLength(1);
255
+ expect(invoices[0].stripeInvoiceId).toBe("inv_orphan");
256
+ });
257
+ });
258
+
259
+ describe("regression — listInvoices returns complete results beyond limit (userId branch)", () => {
260
+ it("returns all paid invoices even when other statuses fill the default limit", async () => {
261
+ const t = convexTest(schema, modules);
262
+ const userId = "user_regression_inv";
263
+
264
+ // Insert 55 open then 5 paid invoices for the same userId.
265
+ for (let i = 0; i < 55; i++) {
266
+ await t.mutation(api.billing.mutations.upsertInvoice, {
267
+ stripeInvoiceId: `inv_open_${i}`,
268
+ userId,
269
+ status: "open",
270
+ currency: "usd",
271
+ amountDue: 1000,
272
+ amountPaid: 0,
273
+ });
274
+ }
275
+ for (let i = 0; i < 5; i++) {
276
+ await t.mutation(api.billing.mutations.upsertInvoice, {
277
+ stripeInvoiceId: `inv_paid_${i}`,
278
+ userId,
279
+ status: "paid",
280
+ currency: "usd",
281
+ amountDue: 1000,
282
+ amountPaid: 1000,
283
+ });
284
+ }
285
+
286
+ const result = await t.query(api.billing.queries.listInvoices, {
287
+ userId,
288
+ status: "paid",
289
+ });
290
+
291
+ expect(result).toHaveLength(5);
292
+ expect(result.every((i) => i.status === "paid")).toBe(true);
293
+ });
294
+ });
@@ -1,8 +1,7 @@
1
1
  import { convexTest } from "convex-test";
2
2
  import { expect, it } from "vitest";
3
- import { api } from "./_generated/api";
4
- import schema from "./schema";
5
- const modules = import.meta.glob("./**/*.*s");
3
+ import { api } from "../_generated/api";
4
+ import { schema, modules } from "../../test.js";
6
5
  it("scopes seller subscriptions by catalog price for both destination and split sales", async () => {
7
6
  const t = convexTest(schema, modules);
8
7
  await t.run(async (ctx) => {
@@ -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("per-store subscription scoping (BTS-19)", () => {
11
9
  it("lists a buyer's subscriptions scoped to one store (destinationAccountId)", async () => {