@startsimpli/billing 0.4.16 → 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/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 +1 -1
- 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
|
@@ -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();
|
|
@@ -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
|
});
|