diff --git a/packages/dashboard/src/pages/api/stripe/activeSubscription.js b/packages/dashboard/src/pages/api/stripe/subscription.js similarity index 72% rename from packages/dashboard/src/pages/api/stripe/activeSubscription.js rename to packages/dashboard/src/pages/api/stripe/subscription.js index 459c7136..444bc0e9 100644 --- a/packages/dashboard/src/pages/api/stripe/activeSubscription.js +++ b/packages/dashboard/src/pages/api/stripe/subscription.js @@ -9,9 +9,14 @@ export default async (req, res) => { const authorization = req.headers.authorization; // authorization header from request const { stripeCustomerId } = await got("http://accounts:3000/user", { headers: { authorization } }).json(); const stripeCustomer = await stripe.customers.retrieve(stripeCustomerId, { expand: ["subscriptions"] }); - // const { subscriptions } = stripeCustomer; + const { subscriptions } = stripeCustomer; - res.json(stripeCustomer); + // todo: find a better way to get current subscription + if (subscriptions.total_count) { + return res.json(subscriptions.data[0]); + } + + res.status(StatusCodes.NO_CONTENT).end(); } catch ({ message }) { res.status(StatusCodes.BAD_REQUEST).json({ error: { message } }); } diff --git a/packages/dashboard/src/pages/payments.js b/packages/dashboard/src/pages/payments.js index c42c3fb9..a0fd4687 100644 --- a/packages/dashboard/src/pages/payments.js +++ b/packages/dashboard/src/pages/payments.js @@ -27,8 +27,6 @@ const plans = [ description: "20 TB premium bandwidth with full speed", }, ]; -const stripeCustomerId = "cus_J09ECKPgFEPXoq"; -const activePlanId = "initial_free"; const fetcher = (url) => fetch(url).then((r) => r.json()); @@ -42,7 +40,9 @@ const ActiveBadge = () => { export default function Payments() { const { data: user } = useSWR("/user", fetcher); + const { data: subscription } = useSWR("/api/stripe/subscription", fetcher); const [selectedPlanId, setSelectedPlanId] = useState("initial_free"); + const [activePlanId, setActivePlanId] = useState(null); const selectedPlan = plans.find(({ id }) => selectedPlanId === id); const handleSubscribe = async () => { try { @@ -55,6 +55,10 @@ export default function Payments() { } }; + useEffect(() => { + setActivePlanId(subscription?.plan?.id ?? null); + }, [subscription, setActivePlanId]); + return (
@@ -69,13 +73,45 @@ export default function Payments() {
-
Current payment
-
+
Subscription status
+
{subscription?.status ?? "—"}
+ + {/*
+
+
+
+ + + +
+
+
Subscription status
+
+
Active
+
+
+
+
+
+
All paid up!
+
+
*/}
-
Plan usage this month
+
asdas