@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.
- package/CHANGELOG.md +12 -0
- package/README.md +14 -2
- package/docs/affiliate-clawbacks.md +5 -5
- package/docs/settlement.md +1 -1
- package/package.json +2 -2
- package/src/component/{affiliate-direct-payout.test.ts → affiliatePayouts/affiliate-direct-payout.test.ts} +6 -7
- package/src/component/{affiliate-component-pagination.test.ts → affiliateRecovery/affiliate-component-pagination.test.ts} +7 -10
- package/src/component/{affiliate-recovery-lifecycle.test.ts → affiliateRecovery/affiliate-recovery-lifecycle.test.ts} +6 -7
- package/src/component/billing/checkout.test.ts +89 -0
- package/src/component/billing/invoices.test.ts +294 -0
- package/src/component/{store-subscriptions.test.ts → billing/store-subscriptions.test.ts} +2 -3
- package/src/component/{subscription-scoping.test.ts → billing/subscription-scoping.test.ts} +2 -4
- package/src/component/billing/subscriptions.test.ts +477 -0
- package/src/component/{connect.test.ts → connect/connect.test.ts} +2 -4
- package/src/component/{fee-routing.test.ts → connect/fee-routing.test.ts} +2 -4
- package/src/component/connect/payments.test.ts +88 -0
- package/src/component/connect/payouts.test.ts +173 -0
- package/src/component/{reversal-races.test.ts → connect/reversal-races.test.ts} +4 -6
- package/src/component/{seller-case-ownership.test.ts → connect/seller-case-ownership.test.ts} +2 -3
- package/src/component/{transfers.test.ts → connect/transfers.test.ts} +3 -5
- package/src/component/core/accounts.test.ts +266 -0
- package/src/component/{config-queries.test.ts → core/config-queries.test.ts} +2 -4
- package/src/component/{payout-reset-safety.test.ts → core/payout-reset-safety.test.ts} +1 -2
- package/src/component/core/reset.test.ts +124 -0
- package/src/component/{cost-recovery.test.ts → costRecovery/cost-recovery.test.ts} +1 -2
- package/src/component/{currency-migration.test.ts → earnings/currency-migration.test.ts} +2 -3
- package/src/component/{currency-totals.test.ts → earnings/currency-totals.test.ts} +2 -3
- package/src/component/{earnings-totals.test.ts → earnings/earnings-totals.test.ts} +3 -5
- package/src/component/{summary-arithmetic.test.ts → earnings/summary-arithmetic.test.ts} +2 -3
- package/src/component/{payout-policy-adoption.test.ts → payoutPolicy/payout-policy-adoption.test.ts} +2 -3
- package/src/component/{payout-policy.test.ts → payoutPolicy/payout-policy.test.ts} +1 -2
- package/src/component/{payout-adoption.test.ts → payoutRelease/payout-adoption.test.ts} +2 -6
- package/src/component/{payout-fees.test.ts → payoutRelease/payout-fees.test.ts} +2 -3
- package/src/component/{payout-recovery-integration.test.ts → payoutRelease/payout-recovery-integration.test.ts} +3 -4
- package/src/component/{payout-release-snapshot.test.ts → payoutRelease/payout-release-snapshot.test.ts} +1 -2
- package/src/component/{payout-release.test.ts → payoutRelease/payout-release.test.ts} +2 -3
- package/src/component/{payout-revisions.test.ts → payoutSnapshots/payout-revisions.test.ts} +1 -2
- package/src/component/{payout-snapshots.test.ts → payoutSnapshots/payout-snapshots.test.ts} +4 -5
- package/src/component/{payout-sync.test.ts → payoutSync/payout-sync.test.ts} +1 -2
- package/src/component/{payout-component-pagination.test.ts → payoutWorker/payout-component-pagination.test.ts} +8 -9
- package/src/component/{payout-worker.test.ts → payoutWorker/payout-worker.test.ts} +1 -2
- package/src/component/products/products.test.ts +239 -0
- package/src/component/{read-pagination.test.ts → products/read-pagination.test.ts} +2 -3
- package/src/component/{refund-history.test.ts → refundHistory/refund-history.test.ts} +2 -3
- package/src/component/{affiliate-clawbacks.test.ts → settlement/affiliate-clawbacks.test.ts} +3 -4
- package/src/component/{settlement-claims.test.ts → settlement/settlement-claims.test.ts} +2 -3
- package/src/component/webhooks/events.test.ts +121 -0
- package/src/component/{triggers.test.ts → webhooks/triggers.test.ts} +2 -4
- package/src/component/public.test.ts +0 -1858
package/CHANGELOG.md
CHANGED
|
@@ -26,6 +26,18 @@
|
|
|
26
26
|
- Remove internal tracker references from public documentation and add a
|
|
27
27
|
regression check that prevents their reintroduction.
|
|
28
28
|
|
|
29
|
+
## [0.30.3](https://github.com/FantasticDevHQ/better-stripe/compare/v0.30.2...v0.30.3) (2026-10-16)
|
|
30
|
+
|
|
31
|
+
### Refactoring
|
|
32
|
+
|
|
33
|
+
- organize Convex domains and ratchet coverage floors ([#231](https://github.com/FantasticDevHQ/better-stripe/issues/231)) ([c881b44](https://github.com/FantasticDevHQ/better-stripe/commit/c881b44b611f6d979df71a4b540466c0cf28eddf))
|
|
34
|
+
|
|
35
|
+
## [0.30.2](https://github.com/FantasticDevHQ/better-stripe/compare/v0.30.1...v0.30.2) (2026-10-16)
|
|
36
|
+
|
|
37
|
+
### Bug Fixes
|
|
38
|
+
|
|
39
|
+
- **example:** complete sandbox fixtures and deferred recovery ([#229](https://github.com/FantasticDevHQ/better-stripe/issues/229)) ([84b0dc6](https://github.com/FantasticDevHQ/better-stripe/commit/84b0dc6f93512ec6021ec5a15b702abd320a7762))
|
|
40
|
+
|
|
29
41
|
## [0.30.1](https://github.com/FantasticDevHQ/better-stripe/compare/v0.30.0...v0.30.1) (2026-10-16)
|
|
30
42
|
|
|
31
43
|
### Bug Fixes
|
package/README.md
CHANGED
|
@@ -70,7 +70,7 @@ claim execution are completed.
|
|
|
70
70
|
|
|
71
71
|
## Status
|
|
72
72
|
|
|
73
|
-
**Version**: `0.30.
|
|
73
|
+
**Version**: `0.30.3` <!-- x-release-please-version --> (pre-1.0). API may change between minor versions until 1.0.
|
|
74
74
|
|
|
75
75
|
Used in production by its authors. Webhook pipeline is covered by unit tests and a live E2E harness (`npm run e2e:webhooks`) that fires real Stripe-signed events and asserts the ledger.
|
|
76
76
|
|
|
@@ -101,7 +101,10 @@ Node.js 22.12+ and a configured Convex development deployment; see
|
|
|
101
101
|
[the example setup guide](example/README.md).
|
|
102
102
|
|
|
103
103
|
The BetterStripe example separates visitor, customer, affiliate, seller, and
|
|
104
|
-
platform-admin workflows.
|
|
104
|
+
platform-admin workflows. Fresh fictional Maya and Sasha stores explicitly
|
|
105
|
+
configure 20% Forever referral commissions to fund the affiliate payout example;
|
|
106
|
+
ordinary stores still default to zero, and seed reruns preserve later edits.
|
|
107
|
+
Its resumable Stripe sandbox fixtures populate sales,
|
|
105
108
|
referrals, refunds, disputes, subscriptions, and payouts. Seller plans include
|
|
106
109
|
monthly billing, actual Stripe fee reconciliation, and refund/dispute cost
|
|
107
110
|
recovery. The example also demonstrates optional balance-funded subscriptions
|
|
@@ -136,6 +139,15 @@ formatting, typechecks, codegen drift check, tests, and repository quality gates
|
|
|
136
139
|
used by CI. Policy lives in `repo-gates.config.json`; measured baselines live in
|
|
137
140
|
`gates/`. Fix failures without weakening the gates or raising existing budgets.
|
|
138
141
|
|
|
142
|
+
Successful full coverage checks automatically raise the floors in
|
|
143
|
+
`gates/coverage-budgets.json`. Run `pnpm run check:coverage` or
|
|
144
|
+
`pnpm run check:all`, then commit any budget changes. Each metric retains the
|
|
145
|
+
existing 0.5 percentage point margin below its measured baseline, and floors
|
|
146
|
+
never decrease. Failed checks leave the budgets untouched. CI checks the same
|
|
147
|
+
ratchet and fails if an improvement has not been committed. `pnpm test:coverage`
|
|
148
|
+
only generates the report; filtered or skipped runs cannot update floors through
|
|
149
|
+
the automatic ratchet.
|
|
150
|
+
|
|
139
151
|
Builds and typechecks use TypeScript 7 (`@typescript/native` aliases
|
|
140
152
|
`typescript`). ESLint and Convex code generation still need the TypeScript 6
|
|
141
153
|
API, so `typescript` aliases Microsoft's `@typescript/typescript6` compatibility
|
|
@@ -219,7 +219,7 @@ active distinct store/affiliate Connect recipients, run the internal fixture
|
|
|
219
219
|
from `example/`:
|
|
220
220
|
|
|
221
221
|
```sh
|
|
222
|
-
pnpm exec convex run
|
|
222
|
+
pnpm exec convex run affiliateRecovery/sandbox/actions:seed '{"runKey":"recovery-partial-1","storeAccountId":"acct_store","affiliateAccountId":"acct_affiliate","scenario":"refund_partial"}'
|
|
223
223
|
```
|
|
224
224
|
|
|
225
225
|
The driver creates a real test payment, settles the original allocation, creates
|
|
@@ -237,7 +237,7 @@ post-settlement dispute workflow for those checks. Do not manufacture terminal
|
|
|
237
237
|
webhooks as proof that provider funds were restored.
|
|
238
238
|
|
|
239
239
|
`paid_out` uses Stripe's `pm_card_bypassPending` test method and first returns
|
|
240
|
-
`operator_step_required`. Run internal `
|
|
240
|
+
`operator_step_required`. Run internal `affiliateRecovery/sandbox/actions:externalPayout`
|
|
241
241
|
with the same `runKey`, `affiliateAccountId` and actual affiliate
|
|
242
242
|
`stripeTransferId`. It verifies fixture provenance, checks actual available
|
|
243
243
|
balance and creates an external test payout for that transfer's amount. Replays
|
|
@@ -248,18 +248,18 @@ must be reported as blocked. Recovery may still succeed using other account
|
|
|
248
248
|
funds; a paid payout does not itself prove insufficient-balance debt.
|
|
249
249
|
|
|
250
250
|
If prior sandbox refunds/disputes depleted platform funding, internal
|
|
251
|
-
`
|
|
251
|
+
`affiliateRecovery/sandbox/actions:fundPlatform` creates a bounded test payment using
|
|
252
252
|
`runKey` and `amount` of at most 100,000 USD minor units. It returns the actual
|
|
253
253
|
payment and before/after available balances. Balance updates can be asynchronous.
|
|
254
254
|
Retain the funding receipt and do not label it commission recovery.
|
|
255
255
|
|
|
256
256
|
For isolated recurring sources, run internal
|
|
257
|
-
`
|
|
257
|
+
`affiliateRecovery/billingSandbox/actions:prepare` with a stable `runKey` and fixed
|
|
258
258
|
`frozenTime` Unix timestamp. Retain those exact arguments on replay. It creates
|
|
259
259
|
an isolated test-clock customer and monthly subscription and returns the actual
|
|
260
260
|
paid `invoiceId`, `paymentIntentId`, `subscriptionId`, `clockId` and `periodEnd`.
|
|
261
261
|
Pass the invoice/payment/subscription references to `seed` with a distinct run
|
|
262
|
-
key per invoice. `
|
|
262
|
+
key per invoice. `affiliateRecovery/billingSandbox/actions:advance` accepts that fixture's
|
|
263
263
|
`runKey`, `clockId` and a new `frozenTime`; after the clock finishes advancing,
|
|
264
264
|
rerun `prepare` to read the next paid invoice. Existing persona subscriptions
|
|
265
265
|
are not changed by these helpers.
|
package/docs/settlement.md
CHANGED
|
@@ -113,7 +113,7 @@ settlement can finish; deduplicate actual payment execution with `settleSale`.
|
|
|
113
113
|
If historical pagination exceeds the configured bound, selection refuses to guess.
|
|
114
114
|
|
|
115
115
|
The Vite example migrates its old subscription claims with
|
|
116
|
-
`pnpm exec convex run commissionMigration:run '{}'` from `example/`. Repeat until
|
|
116
|
+
`pnpm exec convex run marketplace/commissionMigration/mutations:run '{}'` from `example/`. Repeat until
|
|
117
117
|
it returns `0`. Run this after deploying the new component and before replaying
|
|
118
118
|
historical subscription events. The migration preserves terms and first invoices;
|
|
119
119
|
its batch deletes are transactional with component writes.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fantastic.dev/better-stripe",
|
|
3
|
-
"version": "0.30.
|
|
3
|
+
"version": "0.30.3",
|
|
4
4
|
"description": "A Convex component for Stripe V2 Accounts API integration.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"stripe",
|
|
@@ -98,7 +98,7 @@
|
|
|
98
98
|
"check:circular": "repo-gates check-circular",
|
|
99
99
|
"check:secrets": "repo-gates check-secrets",
|
|
100
100
|
"check:ci-parity": "repo-gates check-ci-parity",
|
|
101
|
-
"check:coverage": "
|
|
101
|
+
"check:coverage": "node scripts/check-coverage.mjs",
|
|
102
102
|
"format:check": "pnpm run format",
|
|
103
103
|
"check:agents": "repo-gates check-agents"
|
|
104
104
|
},
|
|
@@ -2,13 +2,12 @@
|
|
|
2
2
|
import { convexTest } from "convex-test";
|
|
3
3
|
import { expect, it, vi } from "vitest";
|
|
4
4
|
import type Stripe from "stripe";
|
|
5
|
-
import { api } from "
|
|
6
|
-
import schema from "
|
|
7
|
-
import { changeBalance } from "
|
|
8
|
-
import { createPayout } from "
|
|
9
|
-
import { AffiliateRecoveryClient } from "
|
|
10
|
-
import type { Component, RunCtx } from "
|
|
11
|
-
const modules = import.meta.glob("./**/*.*s");
|
|
5
|
+
import { api } from "../_generated/api.js";
|
|
6
|
+
import { schema, modules } from "../../test.js";
|
|
7
|
+
import { changeBalance } from "../affiliateRecovery/accounting.js";
|
|
8
|
+
import { createPayout } from "../../client/connect/payouts.js";
|
|
9
|
+
import { AffiliateRecoveryClient } from "../../client/affiliateRecovery.js";
|
|
10
|
+
import type { Component, RunCtx } from "../../client/helpers.js";
|
|
12
11
|
const symbol = Symbol.for("toReferencePath");
|
|
13
12
|
function reference(path = "betterStripe"): Component {
|
|
14
13
|
return new Proxy(
|
|
@@ -1,20 +1,17 @@
|
|
|
1
1
|
// @vitest-environment edge-runtime
|
|
2
2
|
import { convexTest } from "convex-test";
|
|
3
|
-
import { componentDb } from "
|
|
3
|
+
import { componentDb } from "../../testing/component-db.js";
|
|
4
4
|
import { expect, it } from "vitest";
|
|
5
5
|
import type {
|
|
6
6
|
RegisteredQuery,
|
|
7
7
|
RegisteredMutation,
|
|
8
8
|
DefaultFunctionArgs,
|
|
9
9
|
} from "convex/server";
|
|
10
|
-
import type { MutationCtx } from "
|
|
11
|
-
import schema from "
|
|
12
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
} from "./affiliateRecovery/queries.js";
|
|
16
|
-
import { listAffiliateObligations } from "./settlement/queries.js";
|
|
17
|
-
import { migrateAffiliateObligations } from "./settlement/mutations.js";
|
|
10
|
+
import type { MutationCtx } from "../_generated/server.js";
|
|
11
|
+
import { schema, modules } from "../../test.js";
|
|
12
|
+
import { recoverySnapshot, pendingOperations } from "./queries.js";
|
|
13
|
+
import { listAffiliateObligations } from "../settlement/queries.js";
|
|
14
|
+
import { migrateAffiliateObligations } from "../settlement/mutations.js";
|
|
18
15
|
|
|
19
16
|
function handler<Args extends DefaultFunctionArgs, Result>(
|
|
20
17
|
fn:
|
|
@@ -27,7 +24,7 @@ function handler<Args extends DefaultFunctionArgs, Result>(
|
|
|
27
24
|
}
|
|
28
25
|
|
|
29
26
|
it("reads recovery pages and migrates inside the deployed component pagination boundary", async () => {
|
|
30
|
-
const t = convexTest(schema,
|
|
27
|
+
const t = convexTest(schema, modules);
|
|
31
28
|
await t.run(async (ctx) => {
|
|
32
29
|
const componentCtx = { ...ctx, db: componentDb(ctx.db) };
|
|
33
30
|
const paginationOpts = { numItems: 10, cursor: null };
|
|
@@ -2,18 +2,17 @@
|
|
|
2
2
|
import type Stripe from "stripe";
|
|
3
3
|
import { convexTest } from "convex-test";
|
|
4
4
|
import { describe, expect, it } from "vitest";
|
|
5
|
-
import { api } from "
|
|
6
|
-
import schema from "
|
|
7
|
-
import type { Component, RunCtx } from "
|
|
5
|
+
import { api } from "../_generated/api.js";
|
|
6
|
+
import { schema, modules } from "../../test.js";
|
|
7
|
+
import type { Component, RunCtx } from "../../client/helpers.js";
|
|
8
8
|
import {
|
|
9
9
|
reverseTransfers,
|
|
10
10
|
reclaimReversalClaim,
|
|
11
11
|
reverseTransfersForRefund,
|
|
12
12
|
reverseTransfersForDispute,
|
|
13
|
-
} from "
|
|
14
|
-
import { executeResolvedTransfers } from "
|
|
15
|
-
import { reconcileObligation } from "
|
|
16
|
-
const modules = import.meta.glob("./**/*.*s");
|
|
13
|
+
} from "../../client/connect/transfers.js";
|
|
14
|
+
import { executeResolvedTransfers } from "../../client/settlement/execution.js";
|
|
15
|
+
import { reconcileObligation } from "../../client/affiliateRecovery/engine.js";
|
|
17
16
|
const REF = Symbol.for("toReferencePath");
|
|
18
17
|
function component(path = "betterStripe"): Component {
|
|
19
18
|
return new Proxy(
|
|
@@ -0,0 +1,89 @@
|
|
|
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 — checkout session mutations and queries", () => {
|
|
8
|
+
it("upsertCheckoutSession + getCheckoutSessionByStripeId", async () => {
|
|
9
|
+
const t = convexTest(schema, modules);
|
|
10
|
+
|
|
11
|
+
await t.mutation(api.billing.mutations.upsertCheckoutSession, {
|
|
12
|
+
stripeSessionId: "cs_001",
|
|
13
|
+
userId: "user_020",
|
|
14
|
+
mode: "subscription",
|
|
15
|
+
status: "open",
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const session = await t.query(
|
|
19
|
+
api.billing.queries.getCheckoutSessionByStripeId,
|
|
20
|
+
{
|
|
21
|
+
stripeSessionId: "cs_001",
|
|
22
|
+
},
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
expect(session).not.toBeNull();
|
|
26
|
+
expect(session!.mode).toBe("subscription");
|
|
27
|
+
expect(session!.status).toBe("open");
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe("billing — listCheckoutSessionsByUser", () => {
|
|
32
|
+
it("lists sessions for the given user", async () => {
|
|
33
|
+
const t = convexTest(schema, modules);
|
|
34
|
+
|
|
35
|
+
await t.mutation(api.billing.mutations.upsertCheckoutSession, {
|
|
36
|
+
stripeSessionId: "cs_u1_a",
|
|
37
|
+
userId: "user_cs_1",
|
|
38
|
+
mode: "subscription",
|
|
39
|
+
status: "complete",
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
await t.mutation(api.billing.mutations.upsertCheckoutSession, {
|
|
43
|
+
stripeSessionId: "cs_u1_b",
|
|
44
|
+
userId: "user_cs_1",
|
|
45
|
+
mode: "payment",
|
|
46
|
+
status: "open",
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
await t.mutation(api.billing.mutations.upsertCheckoutSession, {
|
|
50
|
+
stripeSessionId: "cs_u2",
|
|
51
|
+
userId: "user_cs_2",
|
|
52
|
+
mode: "subscription",
|
|
53
|
+
status: "open",
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const sessions = await t.query(
|
|
57
|
+
api.billing.queries.listCheckoutSessionsByUser,
|
|
58
|
+
{ userId: "user_cs_1" },
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
expect(sessions).toHaveLength(2);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("filters by status", async () => {
|
|
65
|
+
const t = convexTest(schema, modules);
|
|
66
|
+
|
|
67
|
+
await t.mutation(api.billing.mutations.upsertCheckoutSession, {
|
|
68
|
+
stripeSessionId: "cs_filter_open",
|
|
69
|
+
userId: "user_filter",
|
|
70
|
+
mode: "subscription",
|
|
71
|
+
status: "open",
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
await t.mutation(api.billing.mutations.upsertCheckoutSession, {
|
|
75
|
+
stripeSessionId: "cs_filter_complete",
|
|
76
|
+
userId: "user_filter",
|
|
77
|
+
mode: "subscription",
|
|
78
|
+
status: "complete",
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const open = await t.query(api.billing.queries.listCheckoutSessionsByUser, {
|
|
82
|
+
userId: "user_filter",
|
|
83
|
+
status: "open",
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
expect(open).toHaveLength(1);
|
|
87
|
+
expect(open[0].status).toBe("open");
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -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 "
|
|
4
|
-
import schema from "
|
|
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 "
|
|
6
|
-
import schema from "
|
|
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 () => {
|