@startsimpli/billing 0.4.15 → 0.4.17
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/README.md +2 -2
- package/package.json +1 -1
- package/src/__tests__/theme-contract.test.ts +40 -0
- package/src/components/PlanGate.test.tsx +24 -0
- package/src/components/PlanGate.tsx +7 -1
- package/src/components/SubscriptionManager.test.tsx +24 -0
- package/src/components/SubscriptionManager.tsx +3 -3
- package/src/hooks/useSubscription.test.tsx +18 -0
- package/src/hooks/useSubscription.ts +6 -3
- package/src/utils/api.ts +8 -3
package/README.md
CHANGED
|
@@ -207,14 +207,14 @@ BILLING_USAGE_ADAPTERS = {
|
|
|
207
207
|
| `PricingPage` / `PricingSection` / `PricingDetailPage` | Pre-billing display surfaces. | For marketing / landing pages. |
|
|
208
208
|
| `UpgradeModal` | Modal pricing overlay. | For mid-flow upgrade prompts. |
|
|
209
209
|
| `ManageSubscription` | Bare portal redirect button. | Use `SubscriptionManager` instead unless you really just want the button. |
|
|
210
|
-
| `PlanGate` | Feature-gate wrapper. | Show / hide children based on the user's offer features. |
|
|
210
|
+
| `PlanGate` | Feature-gate wrapper. | Show / hide children based on the user's offer features. Pass `productId`, or a paid plan on another product opens the gate. |
|
|
211
211
|
|
|
212
212
|
### Hooks
|
|
213
213
|
|
|
214
214
|
| Hook | Returns | Notes |
|
|
215
215
|
|---|---|---|
|
|
216
216
|
| `useProduct(slug)` | `{ product, loading, error, refetch }` | Fetch product + offers |
|
|
217
|
-
| `useSubscription()` | `{ subscription, loading, error, refetch }` | Current sub incl. `viewerIsOwner` + `ownerEmail
|
|
217
|
+
| `useSubscription(productId)` | `{ subscription, loading, error, refetch }` | Current sub for that product, incl. `viewerIsOwner` + `ownerEmail`. Always pass the product: without it the newest sub for ANY product comes back. |
|
|
218
218
|
| `useUsage(slug)` | `{ usage, loading, error, refetch }` | `{feature_key: {used, limit}}` map; auto-refetches on `visibilitychange` + `billing:usage-changed` event |
|
|
219
219
|
| `useCheckout()` | `{ checkout, subscribeFree, loading, error }` | Stripe session creator + free-subscribe in one |
|
|
220
220
|
| `usePortal()` | `{ openPortal, loading, error }` | Stripe customer portal session |
|
package/package.json
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Theme-contract guard for @startsimpli/billing (startsim-1f3n.3).
|
|
3
|
+
*
|
|
4
|
+
* Shared components may only use the semantic tokens from
|
|
5
|
+
* packages/ui/theme/contract.css. The numeric Tailwind ramp
|
|
6
|
+
* (`bg-primary-600`, `focus:ring-primary-500`, …) is not part of it — an app
|
|
7
|
+
* that declares `--color-primary` without a ramp gets NO rule for those
|
|
8
|
+
* classes, which renders a plan card border as invisible white-on-white.
|
|
9
|
+
* Use an opacity modifier on the token instead (`hover:bg-primary/90`).
|
|
10
|
+
*/
|
|
11
|
+
import * as fs from 'fs'
|
|
12
|
+
import * as path from 'path'
|
|
13
|
+
import { describe, it, expect } from 'vitest'
|
|
14
|
+
|
|
15
|
+
const SRC = path.resolve(__dirname, '..')
|
|
16
|
+
const RAMP = /\b[a-z-]*-(primary|accent|secondary)-\d{2,3}\b/
|
|
17
|
+
|
|
18
|
+
function sources(dir: string, acc: string[] = []): string[] {
|
|
19
|
+
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
20
|
+
const full = path.join(dir, e.name)
|
|
21
|
+
if (e.isDirectory()) {
|
|
22
|
+
if (e.name !== '__tests__' && e.name !== 'node_modules') sources(full, acc)
|
|
23
|
+
} else if (/\.tsx?$/.test(e.name)) acc.push(full)
|
|
24
|
+
}
|
|
25
|
+
return acc
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('@startsimpli/billing theme contract', () => {
|
|
29
|
+
it('no component styles against a numeric color ramp', () => {
|
|
30
|
+
const offenders: string[] = []
|
|
31
|
+
for (const file of sources(SRC)) {
|
|
32
|
+
fs.readFileSync(file, 'utf8').split('\n').forEach((line, i) => {
|
|
33
|
+
const code = line.replace(/^\s*(\*|\/\/).*$/, '')
|
|
34
|
+
const hit = code.match(RAMP)
|
|
35
|
+
if (hit) offenders.push(`${path.relative(SRC, file)}:${i + 1} ${hit[0]}`)
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
expect(offenders).toEqual([])
|
|
39
|
+
})
|
|
40
|
+
})
|
|
@@ -130,4 +130,28 @@ describe("PlanGate", () => {
|
|
|
130
130
|
expect(screen.getAllByText(/paid plan|upgrade/i).length).toBeGreaterThan(0)
|
|
131
131
|
);
|
|
132
132
|
});
|
|
133
|
+
|
|
134
|
+
it("gates on the given product's subscription only (startsim-oyr3a.2)", async () => {
|
|
135
|
+
const fetcher = vi.fn(async () => ({
|
|
136
|
+
ok: false,
|
|
137
|
+
status: 404,
|
|
138
|
+
async json() {
|
|
139
|
+
return {};
|
|
140
|
+
},
|
|
141
|
+
})) as unknown as typeof fetch;
|
|
142
|
+
|
|
143
|
+
render(
|
|
144
|
+
<BillingProvider apiBaseUrl="/api" fetcher={fetcher}>
|
|
145
|
+
<PlanGate productId="crochet-patterns" fallback={<span>upgrade</span>}>
|
|
146
|
+
<span>pro feature</span>
|
|
147
|
+
</PlanGate>
|
|
148
|
+
</BillingProvider>
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
await waitFor(() => expect(screen.getByText("upgrade")).toBeDefined());
|
|
152
|
+
expect(fetcher).toHaveBeenCalledWith(
|
|
153
|
+
"/api/billing/subscription/current/?product=crochet-patterns",
|
|
154
|
+
expect.anything()
|
|
155
|
+
);
|
|
156
|
+
});
|
|
133
157
|
});
|
|
@@ -6,6 +6,11 @@ import { isPaidSubscription } from "../utils/plan";
|
|
|
6
6
|
|
|
7
7
|
export interface PlanGateProps {
|
|
8
8
|
children: React.ReactNode;
|
|
9
|
+
/**
|
|
10
|
+
* The app's product slug. Without it, a paid plan on ANY product opens the
|
|
11
|
+
* gate (startsim-oyr3a.2).
|
|
12
|
+
*/
|
|
13
|
+
productId?: string;
|
|
9
14
|
/**
|
|
10
15
|
* Element rendered in place of children when user is not on a paid plan.
|
|
11
16
|
* Defaults to a minimal upgrade nudge pointing at /pricing.
|
|
@@ -40,8 +45,9 @@ export function PlanGate({
|
|
|
40
45
|
children,
|
|
41
46
|
fallback,
|
|
42
47
|
upgradeHref = "/pricing",
|
|
48
|
+
productId,
|
|
43
49
|
}: PlanGateProps) {
|
|
44
|
-
const { subscription, loading } = useSubscription();
|
|
50
|
+
const { subscription, loading } = useSubscription(productId);
|
|
45
51
|
|
|
46
52
|
if (loading) return <>{children}</>;
|
|
47
53
|
|
|
@@ -1279,4 +1279,28 @@ describe("SubscriptionManager", () => {
|
|
|
1279
1279
|
expect(screen.getByText("No active subscription")).toBeDefined()
|
|
1280
1280
|
);
|
|
1281
1281
|
});
|
|
1282
|
+
|
|
1283
|
+
it("asks only for its own product's subscription (startsim-oyr3a.2)", async () => {
|
|
1284
|
+
const fetcher = mockFetchRouter({
|
|
1285
|
+
"/billing/subscription/current/": null,
|
|
1286
|
+
"/billing/products/": mockProduct,
|
|
1287
|
+
});
|
|
1288
|
+
const Wrapper = createWrapper(fetcher);
|
|
1289
|
+
|
|
1290
|
+
render(
|
|
1291
|
+
<Wrapper>
|
|
1292
|
+
<SubscriptionManager
|
|
1293
|
+
productId="raise-simpli"
|
|
1294
|
+
returnUrl="https://app.test.com/settings"
|
|
1295
|
+
/>
|
|
1296
|
+
</Wrapper>
|
|
1297
|
+
);
|
|
1298
|
+
|
|
1299
|
+
await waitFor(() =>
|
|
1300
|
+
expect(fetcher).toHaveBeenCalledWith(
|
|
1301
|
+
"https://api.test.com/api/v1/billing/subscription/current/?product=raise-simpli",
|
|
1302
|
+
expect.anything()
|
|
1303
|
+
)
|
|
1304
|
+
);
|
|
1305
|
+
});
|
|
1282
1306
|
});
|
|
@@ -172,7 +172,7 @@ export function SubscriptionManager({
|
|
|
172
172
|
onPlanChange,
|
|
173
173
|
statusMessage,
|
|
174
174
|
}: SubscriptionManagerProps) {
|
|
175
|
-
const { subscription, loading: subLoading, error: subError, refetch: refetchSubscription } = useSubscription();
|
|
175
|
+
const { subscription, loading: subLoading, error: subError, refetch: refetchSubscription } = useSubscription(productId);
|
|
176
176
|
const { product, loading: productLoading, error: productError, refetch: refetchProduct } = useProduct(productId);
|
|
177
177
|
const { openPortal, loading: portalLoading } = usePortal();
|
|
178
178
|
const { subscribeFree } = useCheckout();
|
|
@@ -433,7 +433,7 @@ export function SubscriptionManager({
|
|
|
433
433
|
|
|
434
434
|
// Border: current = solid ring; featured upgrade = blue; else gray.
|
|
435
435
|
const cardBorder =
|
|
436
|
-
isCurrent ? "border-2 border-primary
|
|
436
|
+
isCurrent ? "border-2 border-primary ring-2 ring-primary/20 bg-white shadow-md"
|
|
437
437
|
: offer.isFeatured ? "border border-blue-600 shadow-md bg-white"
|
|
438
438
|
: "border border-gray-200 bg-white shadow-sm";
|
|
439
439
|
|
|
@@ -457,7 +457,7 @@ export function SubscriptionManager({
|
|
|
457
457
|
{isCurrent && (
|
|
458
458
|
<span
|
|
459
459
|
data-testid={`current-plan-badge-${offer.slug}`}
|
|
460
|
-
className="absolute -top-2.5 right-4 inline-block bg-primary
|
|
460
|
+
className="absolute -top-2.5 right-4 inline-block bg-primary text-primary-foreground text-xs font-bold uppercase tracking-wide px-2.5 py-0.5 rounded-full"
|
|
461
461
|
>
|
|
462
462
|
Current Plan
|
|
463
463
|
</span>
|
|
@@ -88,6 +88,24 @@ describe("useSubscription", () => {
|
|
|
88
88
|
);
|
|
89
89
|
});
|
|
90
90
|
|
|
91
|
+
it("asks only for the given product's subscription (startsim-oyr3a.2)", async () => {
|
|
92
|
+
const mockFetch = vi.fn().mockResolvedValue({
|
|
93
|
+
ok: true,
|
|
94
|
+
status: 200,
|
|
95
|
+
json: () => Promise.resolve(mockSubscription),
|
|
96
|
+
});
|
|
97
|
+
const wrapper = createWrapper(mockFetch as unknown as typeof fetch);
|
|
98
|
+
|
|
99
|
+
const { result } = renderHook(() => useSubscription("crochet-patterns"), { wrapper });
|
|
100
|
+
|
|
101
|
+
await waitFor(() => expect(result.current.loading).toBe(false));
|
|
102
|
+
|
|
103
|
+
expect(mockFetch).toHaveBeenCalledWith(
|
|
104
|
+
"https://api.test.com/api/v1/billing/subscription/current/?product=crochet-patterns",
|
|
105
|
+
expect.anything()
|
|
106
|
+
);
|
|
107
|
+
});
|
|
108
|
+
|
|
91
109
|
it("returns null when no subscription exists (404)", async () => {
|
|
92
110
|
const mockFetch = vi.fn().mockResolvedValue({
|
|
93
111
|
ok: false,
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* useSubscription — Fetch current user's active subscription.
|
|
3
|
+
*
|
|
4
|
+
* Pass the app's product slug so another app's plan is never returned
|
|
5
|
+
* (startsim-oyr3a.2).
|
|
3
6
|
*/
|
|
4
7
|
|
|
5
8
|
import { useCallback, useEffect, useState } from "react";
|
|
@@ -13,7 +16,7 @@ interface UseSubscriptionResult {
|
|
|
13
16
|
refetch: () => void;
|
|
14
17
|
}
|
|
15
18
|
|
|
16
|
-
export function useSubscription(): UseSubscriptionResult {
|
|
19
|
+
export function useSubscription(productId?: string): UseSubscriptionResult {
|
|
17
20
|
const { client } = useBillingContext();
|
|
18
21
|
const [subscription, setSubscription] = useState<SubscriptionInfo | null>(null);
|
|
19
22
|
const [loading, setLoading] = useState(true);
|
|
@@ -23,14 +26,14 @@ export function useSubscription(): UseSubscriptionResult {
|
|
|
23
26
|
setLoading(true);
|
|
24
27
|
setError(null);
|
|
25
28
|
try {
|
|
26
|
-
const data = await client.getSubscription();
|
|
29
|
+
const data = await client.getSubscription(productId);
|
|
27
30
|
setSubscription(data);
|
|
28
31
|
} catch (e) {
|
|
29
32
|
setError(e instanceof Error ? e : new Error(String(e)));
|
|
30
33
|
} finally {
|
|
31
34
|
setLoading(false);
|
|
32
35
|
}
|
|
33
|
-
}, [client]);
|
|
36
|
+
}, [client, productId]);
|
|
34
37
|
|
|
35
38
|
useEffect(() => {
|
|
36
39
|
fetchSubscription();
|
package/src/utils/api.ts
CHANGED
|
@@ -134,9 +134,14 @@ export class BillingApiClient {
|
|
|
134
134
|
return (await resp.json()) as UsageMap;
|
|
135
135
|
}
|
|
136
136
|
|
|
137
|
-
/**
|
|
138
|
-
|
|
139
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Get current user's subscription. Pass the app's product slug: a team can
|
|
139
|
+
* hold subscriptions to several products, and without it the backend returns
|
|
140
|
+
* the newest one for ANY product (startsim-oyr3a.2).
|
|
141
|
+
*/
|
|
142
|
+
async getSubscription(productSlug?: string): Promise<SubscriptionInfo | null> {
|
|
143
|
+
const query = productSlug ? `?product=${encodeURIComponent(productSlug)}` : "";
|
|
144
|
+
const url = `${this.config.apiBaseUrl}/billing/subscription/current/${query}`;
|
|
140
145
|
const resp = await this.fetcher(url, {
|
|
141
146
|
headers: this.headers,
|
|
142
147
|
});
|