create-githat-app 1.8.7 → 1.8.8
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/dist/cli.js +2 -2
- package/package.json +1 -1
- package/templates/agent/app/billing/page.tsx.hbs +158 -0
- package/templates/base/README.md.hbs +84 -0
- package/templates/base/githat/billing/page.tsx.hbs +158 -0
- package/templates/classroom/app/billing/page.tsx.hbs +158 -0
- package/templates/content/app/billing/page.tsx.hbs +158 -0
- package/templates/dashboard/app/billing/page.tsx.hbs +158 -0
- package/templates/marketplace/app/billing/page.tsx.hbs +158 -0
- package/templates/nextjs/app/billing/page.tsx.hbs +158 -0
- package/templates/portfolio/app/billing/page.tsx.hbs +158 -0
- package/templates/saas/app/billing/page.tsx.hbs +158 -0
package/dist/cli.js
CHANGED
|
@@ -21,7 +21,7 @@ var DEPS = {
|
|
|
21
21
|
next: "^16.0.0",
|
|
22
22
|
react: "^19.0.0",
|
|
23
23
|
"react-dom": "^19.0.0",
|
|
24
|
-
"@githat/nextjs": "^0.13.
|
|
24
|
+
"@githat/nextjs": "^0.13.5",
|
|
25
25
|
"@githat/ui": "^1.0.0"
|
|
26
26
|
},
|
|
27
27
|
devDependencies: {
|
|
@@ -36,7 +36,7 @@ var DEPS = {
|
|
|
36
36
|
react: "^19.0.0",
|
|
37
37
|
"react-dom": "^19.0.0",
|
|
38
38
|
"react-router-dom": "^7.0.0",
|
|
39
|
-
"@githat/nextjs": "^0.13.
|
|
39
|
+
"@githat/nextjs": "^0.13.5",
|
|
40
40
|
"@githat/ui": "^1.0.0"
|
|
41
41
|
},
|
|
42
42
|
devDependencies: {
|
package/package.json
CHANGED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState, useCallback } from 'react';
|
|
4
|
+
import { useAuth, useAppBilling } from '@githat/nextjs';
|
|
5
|
+
import type { AppBillingStatus } from '@githat/nextjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* /billing — Subscription management for {{businessName}} end-users.
|
|
9
|
+
*
|
|
10
|
+
* Lets your users manage their subscription on your app. Backed by
|
|
11
|
+
* Stripe Connect — charges go to your connected Stripe account and
|
|
12
|
+
* GitHat takes a small platform fee.
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* 1. Complete Stripe Connect onboarding in the GitHat dashboard
|
|
16
|
+
* (Apps → your app → Billing → Connect Stripe).
|
|
17
|
+
* 2. This page shows users a "Manage subscription" button that
|
|
18
|
+
* opens the Stripe Customer Portal in their name.
|
|
19
|
+
*
|
|
20
|
+
* For new subscriptions, call createCheckout() from useAppBilling()
|
|
21
|
+
* with the relevant Stripe price IDs for your plans.
|
|
22
|
+
*/
|
|
23
|
+
export default function BillingPage() {
|
|
24
|
+
const { user, org } = useAuth();
|
|
25
|
+
const appId = process.env.NEXT_PUBLIC_GITHAT_APP_ID || '';
|
|
26
|
+
const orgId = org?.id || '';
|
|
27
|
+
|
|
28
|
+
const { getStatus, connectOnboarding, openPortal } = useAppBilling(appId, orgId);
|
|
29
|
+
|
|
30
|
+
const [status, setStatus] = useState<AppBillingStatus | null>(null);
|
|
31
|
+
const [loading, setLoading] = useState(true);
|
|
32
|
+
const [error, setError] = useState<string | null>(null);
|
|
33
|
+
const [portalLoading, setPortalLoading] = useState(false);
|
|
34
|
+
|
|
35
|
+
const load = useCallback(async () => {
|
|
36
|
+
if (!appId || !orgId) return;
|
|
37
|
+
setLoading(true);
|
|
38
|
+
setError(null);
|
|
39
|
+
try {
|
|
40
|
+
const data = await getStatus();
|
|
41
|
+
setStatus(data);
|
|
42
|
+
} catch (err: unknown) {
|
|
43
|
+
setError(err instanceof Error ? err.message : 'Failed to load billing info');
|
|
44
|
+
} finally {
|
|
45
|
+
setLoading(false);
|
|
46
|
+
}
|
|
47
|
+
}, [getStatus, appId, orgId]);
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (user) load();
|
|
51
|
+
}, [user, load]);
|
|
52
|
+
|
|
53
|
+
async function handlePortal() {
|
|
54
|
+
if (!user) return;
|
|
55
|
+
setPortalLoading(true);
|
|
56
|
+
setError(null);
|
|
57
|
+
try {
|
|
58
|
+
// In a real app, pass the Stripe customer ID for this user.
|
|
59
|
+
// You would look this up from your database or pass it as a prop.
|
|
60
|
+
const customerId = (user as Record<string, unknown>).stripeCustomerId as string | undefined;
|
|
61
|
+
if (!customerId) {
|
|
62
|
+
setError('No billing account found. Subscribe to a plan first.');
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const { url } = await openPortal({ customerId });
|
|
66
|
+
window.location.href = url;
|
|
67
|
+
} catch (err: unknown) {
|
|
68
|
+
setError(err instanceof Error ? err.message : 'Failed to open billing portal');
|
|
69
|
+
} finally {
|
|
70
|
+
setPortalLoading(false);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!user) {
|
|
75
|
+
return (
|
|
76
|
+
<div style={{ padding: '2rem', textAlign: 'center' }}>
|
|
77
|
+
<p>Sign in to manage your subscription.</p>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<div style={{ maxWidth: '640px', margin: '0 auto', padding: '2rem 1rem' }}>
|
|
84
|
+
<h1 style={{ fontSize: '1.5rem', fontWeight: 700, marginBottom: '0.5rem' }}>
|
|
85
|
+
Billing
|
|
86
|
+
</h1>
|
|
87
|
+
<p style={{ color: '#64748b', marginBottom: '2rem' }}>
|
|
88
|
+
Manage your {{businessName}} subscription.
|
|
89
|
+
</p>
|
|
90
|
+
|
|
91
|
+
{loading && <p style={{ color: '#64748b' }}>Loading…</p>}
|
|
92
|
+
|
|
93
|
+
{error && (
|
|
94
|
+
<div
|
|
95
|
+
style={{
|
|
96
|
+
background: 'rgba(239,68,68,0.1)',
|
|
97
|
+
border: '1px solid #ef4444',
|
|
98
|
+
borderRadius: '0.5rem',
|
|
99
|
+
padding: '0.75rem 1rem',
|
|
100
|
+
color: '#ef4444',
|
|
101
|
+
marginBottom: '1rem',
|
|
102
|
+
}}
|
|
103
|
+
>
|
|
104
|
+
{error}
|
|
105
|
+
</div>
|
|
106
|
+
)}
|
|
107
|
+
|
|
108
|
+
{!loading && status && (
|
|
109
|
+
<div
|
|
110
|
+
style={{
|
|
111
|
+
border: '1px solid #e2e8f0',
|
|
112
|
+
borderRadius: '0.75rem',
|
|
113
|
+
padding: '1.5rem',
|
|
114
|
+
background: '#f8fafc',
|
|
115
|
+
}}
|
|
116
|
+
>
|
|
117
|
+
<div style={{ marginBottom: '1rem' }}>
|
|
118
|
+
<div style={{ fontSize: '0.75rem', textTransform: 'uppercase', letterSpacing: '0.05em', color: '#64748b', marginBottom: '0.25rem' }}>
|
|
119
|
+
Connect status
|
|
120
|
+
</div>
|
|
121
|
+
<div style={{ fontWeight: 600 }}>
|
|
122
|
+
{status.connected ? (
|
|
123
|
+
status.chargesEnabled ? '✅ Active — charges enabled' : '⏳ Onboarding in progress'
|
|
124
|
+
) : '❌ Not connected'}
|
|
125
|
+
</div>
|
|
126
|
+
</div>
|
|
127
|
+
|
|
128
|
+
{status.connected && status.chargesEnabled && (
|
|
129
|
+
<button
|
|
130
|
+
onClick={handlePortal}
|
|
131
|
+
disabled={portalLoading}
|
|
132
|
+
style={{
|
|
133
|
+
background: '#7c3aed',
|
|
134
|
+
color: '#fff',
|
|
135
|
+
border: 'none',
|
|
136
|
+
borderRadius: '0.5rem',
|
|
137
|
+
padding: '0.6rem 1.25rem',
|
|
138
|
+
fontWeight: 600,
|
|
139
|
+
cursor: portalLoading ? 'default' : 'pointer',
|
|
140
|
+
opacity: portalLoading ? 0.7 : 1,
|
|
141
|
+
fontSize: '0.875rem',
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
{portalLoading ? 'Opening portal…' : 'Manage subscription →'}
|
|
145
|
+
</button>
|
|
146
|
+
)}
|
|
147
|
+
|
|
148
|
+
{!status.connected && (
|
|
149
|
+
<p style={{ fontSize: '0.875rem', color: '#64748b', marginTop: '0.5rem' }}>
|
|
150
|
+
The app owner needs to complete Stripe Connect onboarding before subscriptions
|
|
151
|
+
can be managed here.
|
|
152
|
+
</p>
|
|
153
|
+
)}
|
|
154
|
+
</div>
|
|
155
|
+
)}
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
}
|
|
@@ -254,3 +254,87 @@ Key properties:
|
|
|
254
254
|
- **Static-export-friendly** — no server actions. All storage calls are
|
|
255
255
|
client-side fetches against the GitHat API.
|
|
256
256
|
- **Private by default** — set `isPublic: true` for public CDN URLs.
|
|
257
|
+
|
|
258
|
+
## Billing
|
|
259
|
+
|
|
260
|
+
GitHat provides two billing primitives depending on who is paying whom.
|
|
261
|
+
|
|
262
|
+
### Your org's GitHat plan (useBilling)
|
|
263
|
+
|
|
264
|
+
Manage the org's GitHat subscription (Free → Starter $29/mo → Pro $99/mo → Enterprise).
|
|
265
|
+
The `useBilling()` hook wraps `/billing/org/*`:
|
|
266
|
+
|
|
267
|
+
```tsx
|
|
268
|
+
import { useBilling, PricingTable } from '@githat/nextjs';
|
|
269
|
+
|
|
270
|
+
const { getCurrentPlan, openCheckout, openPortal, cancel } = useBilling(org.id);
|
|
271
|
+
|
|
272
|
+
// Render a full pricing table with upgrade CTAs
|
|
273
|
+
<PricingTable orgId={org.id} currentPlan={plan.plan} />
|
|
274
|
+
|
|
275
|
+
// Open Stripe Checkout for a plan upgrade
|
|
276
|
+
const { url } = await openCheckout('pro');
|
|
277
|
+
window.location.href = url;
|
|
278
|
+
|
|
279
|
+
// Open the Stripe Customer Portal (card / cancellation management)
|
|
280
|
+
const { url } = await openPortal();
|
|
281
|
+
window.location.href = url;
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
The GitHat dashboard's `/billing` page shows the current plan, usage bars
|
|
285
|
+
(apps, seats, API calls, emails vs. limits), and inline upgrade cards.
|
|
286
|
+
|
|
287
|
+
### Charging your own users (useAppBilling — Stripe Connect)
|
|
288
|
+
|
|
289
|
+
Customer apps can charge their own end-users through GitHat's Stripe Connect
|
|
290
|
+
layer. GitHat takes a 1% platform fee; the rest goes to your connected account.
|
|
291
|
+
|
|
292
|
+
```tsx
|
|
293
|
+
import { useAppBilling } from '@githat/nextjs';
|
|
294
|
+
|
|
295
|
+
const { getStatus, connectOnboarding, createCheckout, openPortal } =
|
|
296
|
+
useAppBilling(appId, orgId);
|
|
297
|
+
|
|
298
|
+
// Start or refresh Connect onboarding
|
|
299
|
+
const { url } = await connectOnboarding();
|
|
300
|
+
window.location.href = url; // user completes Stripe onboarding
|
|
301
|
+
|
|
302
|
+
// Create a checkout session for an end-user
|
|
303
|
+
const { url } = await createCheckout({
|
|
304
|
+
lineItems: [{ price: 'price_xxx', quantity: 1 }],
|
|
305
|
+
successUrl: 'https://yourapp.com/success',
|
|
306
|
+
cancelUrl: 'https://yourapp.com/cancel',
|
|
307
|
+
});
|
|
308
|
+
window.location.href = url;
|
|
309
|
+
|
|
310
|
+
// Open the billing portal for an end-user
|
|
311
|
+
const { url } = await openPortal({ customerId: 'cus_xxx' });
|
|
312
|
+
window.location.href = url;
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
The scaffolded `/billing` route (available in most templates) renders a
|
|
316
|
+
"Manage subscription" button + billing portal link for your end-users.
|
|
317
|
+
|
|
318
|
+
### Usage metering
|
|
319
|
+
|
|
320
|
+
Track metered events (API calls, AI tokens, storage bytes, etc.) for billing
|
|
321
|
+
and analytics. Events are stored with a 90-day retention window and aggregated
|
|
322
|
+
by day/month:
|
|
323
|
+
|
|
324
|
+
```ts
|
|
325
|
+
// Fire-and-forget from your API route or Server Action
|
|
326
|
+
await fetch('https://api.githat.io/usage/track', {
|
|
327
|
+
method: 'POST',
|
|
328
|
+
headers: {
|
|
329
|
+
'X-GitHat-App-Key': process.env.GITHAT_PUBLISHABLE_KEY,
|
|
330
|
+
'Content-Type': 'application/json',
|
|
331
|
+
},
|
|
332
|
+
body: JSON.stringify({ meter: 'api.call', quantity: 1 }),
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
// Query aggregates from the dashboard or a chart component
|
|
336
|
+
const data = await fetch('/api/usage?meter=api.call&granularity=day&since=2026-05-01');
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
When a Stripe Meter ID is configured for a meter in the GitHat dashboard,
|
|
340
|
+
events are automatically forwarded to Stripe for usage-based pricing.
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState, useCallback } from 'react';
|
|
4
|
+
import { useAuth, useAppBilling } from '@githat/nextjs';
|
|
5
|
+
import type { AppBillingStatus } from '@githat/nextjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* /billing — Subscription management for {{businessName}} end-users.
|
|
9
|
+
*
|
|
10
|
+
* Lets your users manage their subscription on your app. Backed by
|
|
11
|
+
* Stripe Connect — charges go to your connected Stripe account and
|
|
12
|
+
* GitHat takes a small platform fee.
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* 1. Complete Stripe Connect onboarding in the GitHat dashboard
|
|
16
|
+
* (Apps → your app → Billing → Connect Stripe).
|
|
17
|
+
* 2. This page shows users a "Manage subscription" button that
|
|
18
|
+
* opens the Stripe Customer Portal in their name.
|
|
19
|
+
*
|
|
20
|
+
* For new subscriptions, call createCheckout() from useAppBilling()
|
|
21
|
+
* with the relevant Stripe price IDs for your plans.
|
|
22
|
+
*/
|
|
23
|
+
export default function BillingPage() {
|
|
24
|
+
const { user, org } = useAuth();
|
|
25
|
+
const appId = process.env.NEXT_PUBLIC_GITHAT_APP_ID || '';
|
|
26
|
+
const orgId = org?.id || '';
|
|
27
|
+
|
|
28
|
+
const { getStatus, connectOnboarding, openPortal } = useAppBilling(appId, orgId);
|
|
29
|
+
|
|
30
|
+
const [status, setStatus] = useState<AppBillingStatus | null>(null);
|
|
31
|
+
const [loading, setLoading] = useState(true);
|
|
32
|
+
const [error, setError] = useState<string | null>(null);
|
|
33
|
+
const [portalLoading, setPortalLoading] = useState(false);
|
|
34
|
+
|
|
35
|
+
const load = useCallback(async () => {
|
|
36
|
+
if (!appId || !orgId) return;
|
|
37
|
+
setLoading(true);
|
|
38
|
+
setError(null);
|
|
39
|
+
try {
|
|
40
|
+
const data = await getStatus();
|
|
41
|
+
setStatus(data);
|
|
42
|
+
} catch (err: unknown) {
|
|
43
|
+
setError(err instanceof Error ? err.message : 'Failed to load billing info');
|
|
44
|
+
} finally {
|
|
45
|
+
setLoading(false);
|
|
46
|
+
}
|
|
47
|
+
}, [getStatus, appId, orgId]);
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (user) load();
|
|
51
|
+
}, [user, load]);
|
|
52
|
+
|
|
53
|
+
async function handlePortal() {
|
|
54
|
+
if (!user) return;
|
|
55
|
+
setPortalLoading(true);
|
|
56
|
+
setError(null);
|
|
57
|
+
try {
|
|
58
|
+
// In a real app, pass the Stripe customer ID for this user.
|
|
59
|
+
// You would look this up from your database or pass it as a prop.
|
|
60
|
+
const customerId = (user as Record<string, unknown>).stripeCustomerId as string | undefined;
|
|
61
|
+
if (!customerId) {
|
|
62
|
+
setError('No billing account found. Subscribe to a plan first.');
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const { url } = await openPortal({ customerId });
|
|
66
|
+
window.location.href = url;
|
|
67
|
+
} catch (err: unknown) {
|
|
68
|
+
setError(err instanceof Error ? err.message : 'Failed to open billing portal');
|
|
69
|
+
} finally {
|
|
70
|
+
setPortalLoading(false);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!user) {
|
|
75
|
+
return (
|
|
76
|
+
<div style={{ padding: '2rem', textAlign: 'center' }}>
|
|
77
|
+
<p>Sign in to manage your subscription.</p>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<div style={{ maxWidth: '640px', margin: '0 auto', padding: '2rem 1rem' }}>
|
|
84
|
+
<h1 style={{ fontSize: '1.5rem', fontWeight: 700, marginBottom: '0.5rem' }}>
|
|
85
|
+
Billing
|
|
86
|
+
</h1>
|
|
87
|
+
<p style={{ color: '#64748b', marginBottom: '2rem' }}>
|
|
88
|
+
Manage your {{businessName}} subscription.
|
|
89
|
+
</p>
|
|
90
|
+
|
|
91
|
+
{loading && <p style={{ color: '#64748b' }}>Loading…</p>}
|
|
92
|
+
|
|
93
|
+
{error && (
|
|
94
|
+
<div
|
|
95
|
+
style={{
|
|
96
|
+
background: 'rgba(239,68,68,0.1)',
|
|
97
|
+
border: '1px solid #ef4444',
|
|
98
|
+
borderRadius: '0.5rem',
|
|
99
|
+
padding: '0.75rem 1rem',
|
|
100
|
+
color: '#ef4444',
|
|
101
|
+
marginBottom: '1rem',
|
|
102
|
+
}}
|
|
103
|
+
>
|
|
104
|
+
{error}
|
|
105
|
+
</div>
|
|
106
|
+
)}
|
|
107
|
+
|
|
108
|
+
{!loading && status && (
|
|
109
|
+
<div
|
|
110
|
+
style={{
|
|
111
|
+
border: '1px solid #e2e8f0',
|
|
112
|
+
borderRadius: '0.75rem',
|
|
113
|
+
padding: '1.5rem',
|
|
114
|
+
background: '#f8fafc',
|
|
115
|
+
}}
|
|
116
|
+
>
|
|
117
|
+
<div style={{ marginBottom: '1rem' }}>
|
|
118
|
+
<div style={{ fontSize: '0.75rem', textTransform: 'uppercase', letterSpacing: '0.05em', color: '#64748b', marginBottom: '0.25rem' }}>
|
|
119
|
+
Connect status
|
|
120
|
+
</div>
|
|
121
|
+
<div style={{ fontWeight: 600 }}>
|
|
122
|
+
{status.connected ? (
|
|
123
|
+
status.chargesEnabled ? '✅ Active — charges enabled' : '⏳ Onboarding in progress'
|
|
124
|
+
) : '❌ Not connected'}
|
|
125
|
+
</div>
|
|
126
|
+
</div>
|
|
127
|
+
|
|
128
|
+
{status.connected && status.chargesEnabled && (
|
|
129
|
+
<button
|
|
130
|
+
onClick={handlePortal}
|
|
131
|
+
disabled={portalLoading}
|
|
132
|
+
style={{
|
|
133
|
+
background: '#7c3aed',
|
|
134
|
+
color: '#fff',
|
|
135
|
+
border: 'none',
|
|
136
|
+
borderRadius: '0.5rem',
|
|
137
|
+
padding: '0.6rem 1.25rem',
|
|
138
|
+
fontWeight: 600,
|
|
139
|
+
cursor: portalLoading ? 'default' : 'pointer',
|
|
140
|
+
opacity: portalLoading ? 0.7 : 1,
|
|
141
|
+
fontSize: '0.875rem',
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
{portalLoading ? 'Opening portal…' : 'Manage subscription →'}
|
|
145
|
+
</button>
|
|
146
|
+
)}
|
|
147
|
+
|
|
148
|
+
{!status.connected && (
|
|
149
|
+
<p style={{ fontSize: '0.875rem', color: '#64748b', marginTop: '0.5rem' }}>
|
|
150
|
+
The app owner needs to complete Stripe Connect onboarding before subscriptions
|
|
151
|
+
can be managed here.
|
|
152
|
+
</p>
|
|
153
|
+
)}
|
|
154
|
+
</div>
|
|
155
|
+
)}
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { useEffect, useState, useCallback } from 'react';
|
|
4
|
+
import { useAuth, useAppBilling } from '@githat/nextjs';
|
|
5
|
+
import type { AppBillingStatus } from '@githat/nextjs';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* /billing — Subscription management for {{businessName}} end-users.
|
|
9
|
+
*
|
|
10
|
+
* Lets your users manage their subscription on your app. Backed by
|
|
11
|
+
* Stripe Connect — charges go to your connected Stripe account and
|
|
12
|
+
* GitHat takes a small platform fee.
|
|
13
|
+
*
|
|
14
|
+
* Usage:
|
|
15
|
+
* 1. Complete Stripe Connect onboarding in the GitHat dashboard
|
|
16
|
+
* (Apps → your app → Billing → Connect Stripe).
|
|
17
|
+
* 2. This page shows users a "Manage subscription" button that
|
|
18
|
+
* opens the Stripe Customer Portal in their name.
|
|
19
|
+
*
|
|
20
|
+
* For new subscriptions, call createCheckout() from useAppBilling()
|
|
21
|
+
* with the relevant Stripe price IDs for your plans.
|
|
22
|
+
*/
|
|
23
|
+
export default function BillingPage() {
|
|
24
|
+
const { user, org } = useAuth();
|
|
25
|
+
const appId = process.env.NEXT_PUBLIC_GITHAT_APP_ID || '';
|
|
26
|
+
const orgId = org?.id || '';
|
|
27
|
+
|
|
28
|
+
const { getStatus, connectOnboarding, openPortal } = useAppBilling(appId, orgId);
|
|
29
|
+
|
|
30
|
+
const [status, setStatus] = useState<AppBillingStatus | null>(null);
|
|
31
|
+
const [loading, setLoading] = useState(true);
|
|
32
|
+
const [error, setError] = useState<string | null>(null);
|
|
33
|
+
const [portalLoading, setPortalLoading] = useState(false);
|
|
34
|
+
|
|
35
|
+
const load = useCallback(async () => {
|
|
36
|
+
if (!appId || !orgId) return;
|
|
37
|
+
setLoading(true);
|
|
38
|
+
setError(null);
|
|
39
|
+
try {
|
|
40
|
+
const data = await getStatus();
|
|
41
|
+
setStatus(data);
|
|
42
|
+
} catch (err: unknown) {
|
|
43
|
+
setError(err instanceof Error ? err.message : 'Failed to load billing info');
|
|
44
|
+
} finally {
|
|
45
|
+
setLoading(false);
|
|
46
|
+
}
|
|
47
|
+
}, [getStatus, appId, orgId]);
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (user) load();
|
|
51
|
+
}, [user, load]);
|
|
52
|
+
|
|
53
|
+
async function handlePortal() {
|
|
54
|
+
if (!user) return;
|
|
55
|
+
setPortalLoading(true);
|
|
56
|
+
setError(null);
|
|
57
|
+
try {
|
|
58
|
+
// In a real app, pass the Stripe customer ID for this user.
|
|
59
|
+
// You would look this up from your database or pass it as a prop.
|
|
60
|
+
const customerId = (user as Record<string, unknown>).stripeCustomerId as string | undefined;
|
|
61
|
+
if (!customerId) {
|
|
62
|
+
setError('No billing account found. Subscribe to a plan first.');
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const { url } = await openPortal({ customerId });
|
|
66
|
+
window.location.href = url;
|
|
67
|
+
} catch (err: unknown) {
|
|
68
|
+
setError(err instanceof Error ? err.message : 'Failed to open billing portal');
|
|
69
|
+
} finally {
|
|
70
|
+
setPortalLoading(false);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!user) {
|
|
75
|
+
return (
|
|
76
|
+
<div style={{ padding: '2rem', textAlign: 'center' }}>
|
|
77
|
+
<p>Sign in to manage your subscription.</p>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<div style={{ maxWidth: '640px', margin: '0 auto', padding: '2rem 1rem' }}>
|
|
84
|
+
<h1 style={{ fontSize: '1.5rem', fontWeight: 700, marginBottom: '0.5rem' }}>
|
|
85
|
+
Billing
|
|
86
|
+
</h1>
|
|
87
|
+
<p style={{ color: '#64748b', marginBottom: '2rem' }}>
|
|
88
|
+
Manage your {{businessName}} subscription.
|
|
89
|
+
</p>
|
|
90
|
+
|
|
91
|
+
{loading && <p style={{ color: '#64748b' }}>Loading…</p>}
|
|
92
|
+
|
|
93
|
+
{error && (
|
|
94
|
+
<div
|
|
95
|
+
style={{
|
|
96
|
+
background: 'rgba(239,68,68,0.1)',
|
|
97
|
+
border: '1px solid #ef4444',
|
|
98
|
+
borderRadius: '0.5rem',
|
|
99
|
+
padding: '0.75rem 1rem',
|
|
100
|
+
color: '#ef4444',
|
|
101
|
+
marginBottom: '1rem',
|
|
102
|
+
}}
|
|
103
|
+
>
|
|
104
|
+
{error}
|
|
105
|
+
</div>
|
|
106
|
+
)}
|
|
107
|
+
|
|
108
|
+
{!loading && status && (
|
|
109
|
+
<div
|
|
110
|
+
style={{
|
|
111
|
+
border: '1px solid #e2e8f0',
|
|
112
|
+
borderRadius: '0.75rem',
|
|
113
|
+
padding: '1.5rem',
|
|
114
|
+
background: '#f8fafc',
|
|
115
|
+
}}
|
|
116
|
+
>
|
|
117
|
+
<div style={{ marginBottom: '1rem' }}>
|
|
118
|
+
<div style={{ fontSize: '0.75rem', textTransform: 'uppercase', letterSpacing: '0.05em', color: '#64748b', marginBottom: '0.25rem' }}>
|
|
119
|
+
Connect status
|
|
120
|
+
</div>
|
|
121
|
+
<div style={{ fontWeight: 600 }}>
|
|
122
|
+
{status.connected ? (
|
|
123
|
+
status.chargesEnabled ? '✅ Active — charges enabled' : '⏳ Onboarding in progress'
|
|
124
|
+
) : '❌ Not connected'}
|
|
125
|
+
</div>
|
|
126
|
+
</div>
|
|
127
|
+
|
|
128
|
+
{status.connected && status.chargesEnabled && (
|
|
129
|
+
<button
|
|
130
|
+
onClick={handlePortal}
|
|
131
|
+
disabled={portalLoading}
|
|
132
|
+
style={{
|
|
133
|
+
background: '#7c3aed',
|
|
134
|
+
color: '#fff',
|
|
135
|
+
border: 'none',
|
|
136
|
+
borderRadius: '0.5rem',
|
|
137
|
+
padding: '0.6rem 1.25rem',
|
|
138
|
+
fontWeight: 600,
|
|
139
|
+
cursor: portalLoading ? 'default' : 'pointer',
|
|
140
|
+
opacity: portalLoading ? 0.7 : 1,
|
|
141
|
+
fontSize: '0.875rem',
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
{portalLoading ? 'Opening portal…' : 'Manage subscription →'}
|
|
145
|
+
</button>
|
|
146
|
+
)}
|
|
147
|
+
|
|
148
|
+
{!status.connected && (
|
|
149
|
+
<p style={{ fontSize: '0.875rem', color: '#64748b', marginTop: '0.5rem' }}>
|
|
150
|
+
The app owner needs to complete Stripe Connect onboarding before subscriptions
|
|
151
|
+
can be managed here.
|
|
152
|
+
</p>
|
|
153
|
+
)}
|
|
154
|
+
</div>
|
|
155
|
+
)}
|
|
156
|
+
</div>
|
|
157
|
+
);
|
|
158
|
+
}
|