create-githat-app 1.8.7 → 1.8.9
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 +148 -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
|
@@ -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
|
+
}
|
|
@@ -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
|
+
}
|