1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
"use client"
import { useEffect, useRef, useState } from "react"
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { useSearchParams } from "next/navigation"
import { useUserProfile } from "@/lib/queries/use-user-profile"
import { queryKeys } from "@/lib/queries/query-keys"
import { TIER_LIMITS } from "@asa-news/shared"
import { classNames } from "@/lib/utilities"
import { notify } from "@/lib/notify"
function useCreateCheckoutSession() {
return useMutation({
mutationFn: async ({
billingInterval,
targetTier,
}: {
billingInterval: "monthly" | "yearly"
targetTier: "pro" | "developer"
}) => {
const response = await fetch("/api/billing/create-checkout-session", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ billingInterval, targetTier }),
})
if (!response.ok) {
const data = await response.json()
throw new Error(data.error || "failed to create checkout session")
}
const data = await response.json()
return data as { url?: string; upgraded?: boolean }
},
})
}
function useCreatePortalSession() {
return useMutation({
mutationFn: async () => {
const response = await fetch("/api/billing/create-portal-session", {
method: "POST",
})
if (!response.ok) {
const data = await response.json()
throw new Error(data.error || "failed to create portal session")
}
const data = await response.json()
return data as { url: string }
},
})
}
const PRO_FEATURES = [
`${TIER_LIMITS.pro.maximumFeeds} feeds`,
`${TIER_LIMITS.pro.historyRetentionDays}-day history retention`,
`${TIER_LIMITS.pro.refreshIntervalSeconds / 60}-minute refresh interval`,
"authenticated feeds",
"opml export",
"manual feed refresh",
]
const DEVELOPER_FEATURES = [
`${TIER_LIMITS.developer.maximumFeeds} feeds`,
"everything in pro",
"read-only rest api",
"webhook push notifications",
]
function UpgradeCard({
targetTier,
features,
monthlyPrice,
yearlyPrice,
}: {
targetTier: "pro" | "developer"
features: string[]
monthlyPrice: string
yearlyPrice: string
}) {
const [billingInterval, setBillingInterval] = useState<
"monthly" | "yearly"
>("yearly")
const queryClient = useQueryClient()
const createCheckoutSession = useCreateCheckoutSession()
function handleUpgrade() {
createCheckoutSession.mutate(
{ billingInterval, targetTier },
{
onSuccess: (data) => {
if (data.upgraded) {
queryClient.invalidateQueries({
queryKey: queryKeys.userProfile.all,
})
notify(`upgraded to ${targetTier}!`)
} else if (data.url) {
window.location.href = data.url
}
},
onError: (error: Error) => {
notify("failed to start checkout: " + error.message)
},
}
)
}
return (
<div className="mb-6">
<h3 className="mb-2 text-text-primary">upgrade to {targetTier}</h3>
<ul className="mb-4 space-y-1">
{features.map((feature) => (
<li key={feature} className="text-text-secondary">
+ {feature}
</li>
))}
</ul>
<div className="mb-4 flex items-center gap-2">
<button
type="button"
onClick={() => setBillingInterval("monthly")}
className={classNames(
"border px-3 py-1 transition-colors",
billingInterval === "monthly"
? "border-text-primary text-text-primary"
: "border-border text-text-dim hover:text-text-secondary"
)}
>
{monthlyPrice} / month
</button>
<button
type="button"
onClick={() => setBillingInterval("yearly")}
className={classNames(
"border px-3 py-1 transition-colors",
billingInterval === "yearly"
? "border-text-primary text-text-primary"
: "border-border text-text-dim hover:text-text-secondary"
)}
>
{yearlyPrice} / year
</button>
</div>
<button
onClick={handleUpgrade}
disabled={createCheckoutSession.isPending}
className="border border-text-primary px-4 py-2 text-text-primary transition-colors hover:bg-text-primary hover:text-background-primary disabled:opacity-50"
>
{createCheckoutSession.isPending
? "redirecting ..."
: `upgrade to ${targetTier}`}
</button>
</div>
)
}
export function BillingSettings() {
const { data: userProfile, isLoading } = useUserProfile()
const queryClient = useQueryClient()
const searchParameters = useSearchParams()
const hasShownSuccessToast = useRef(false)
const createPortalSession = useCreatePortalSession()
useEffect(() => {
if (
searchParameters.get("billing") === "success" &&
!hasShownSuccessToast.current
) {
hasShownSuccessToast.current = true
queryClient.invalidateQueries({ queryKey: queryKeys.userProfile.all })
const tierName = userProfile?.tier === "developer" ? "Developer" : userProfile?.tier === "pro" ? "Pro" : null
notify(tierName ? `welcome to ${tierName}` : "subscription activated")
const url = new URL(window.location.href)
url.searchParams.delete("billing")
window.history.replaceState({}, "", url.pathname)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [searchParameters, queryClient])
if (isLoading) {
return <p className="px-4 py-6 text-text-dim">loading billing ...</p>
}
if (!userProfile) {
return <p className="px-4 py-6 text-text-dim">failed to load billing</p>
}
function handleManageSubscription() {
createPortalSession.mutate(undefined, {
onSuccess: (data) => {
window.location.href = data.url
},
onError: (error: Error) => {
notify("failed to open billing portal: " + error.message)
},
})
}
const isPaidTier =
userProfile.tier === "pro" || userProfile.tier === "developer"
const isCancelling =
userProfile.stripeSubscriptionStatus === "active" &&
isPaidTier &&
userProfile.stripeCurrentPeriodEnd !== null
return (
<div className="px-4 py-3">
<div className="mb-6">
<h3 className="mb-2 text-text-primary">current plan</h3>
<span className="border border-border px-2 py-1 text-text-secondary">
{userProfile.tier}
</span>
{userProfile.stripeSubscriptionStatus && (
<span className="ml-2 text-text-dim">
({userProfile.stripeSubscriptionStatus})
</span>
)}
</div>
{userProfile.tier === "free" && (
<>
<UpgradeCard
targetTier="pro"
features={PRO_FEATURES}
monthlyPrice="$3"
yearlyPrice="$30"
/>
<UpgradeCard
targetTier="developer"
features={DEVELOPER_FEATURES}
monthlyPrice="$6"
yearlyPrice="$60"
/>
</>
)}
{userProfile.tier === "pro" && (
<>
<UpgradeCard
targetTier="developer"
features={DEVELOPER_FEATURES}
monthlyPrice="$6"
yearlyPrice="$60"
/>
<div className="mb-6">
{isCancelling && userProfile.stripeCurrentPeriodEnd && (
<p className="mb-3 text-text-secondary">
your pro plan is active until{" "}
{new Date(
userProfile.stripeCurrentPeriodEnd
).toLocaleDateString()}
</p>
)}
{userProfile.stripeSubscriptionStatus === "past_due" && (
<p className="mb-3 text-status-error">
payment failed — please update your payment method
</p>
)}
<button
onClick={handleManageSubscription}
disabled={createPortalSession.isPending}
className="border border-border px-4 py-2 text-text-secondary transition-colors hover:border-text-dim hover:text-text-primary disabled:opacity-50"
>
{createPortalSession.isPending
? "redirecting ..."
: "manage subscription"}
</button>
</div>
</>
)}
{userProfile.tier === "developer" && (
<div className="mb-6">
{isCancelling && userProfile.stripeCurrentPeriodEnd && (
<p className="mb-3 text-text-secondary">
your developer plan is active until{" "}
{new Date(
userProfile.stripeCurrentPeriodEnd
).toLocaleDateString()}
</p>
)}
{userProfile.stripeSubscriptionStatus === "past_due" && (
<p className="mb-3 text-status-error">
payment failed — please update your payment method
</p>
)}
<button
onClick={handleManageSubscription}
disabled={createPortalSession.isPending}
className="border border-border px-4 py-2 text-text-secondary transition-colors hover:border-text-dim hover:text-text-primary disabled:opacity-50"
>
{createPortalSession.isPending
? "redirecting ..."
: "manage subscription"}
</button>
</div>
)}
</div>
)
}
|