From bde4bd8ad31c52b78d18fb142832d1b9a5d8f83b Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Thu, 25 Feb 2021 15:53:56 +0100 Subject: [PATCH] foo --- ...{activeSubscription.js => subscription.js} | 9 +++- packages/dashboard/src/pages/payments.js | 46 +++++++++++++++++-- 2 files changed, 48 insertions(+), 7 deletions(-) rename packages/dashboard/src/pages/api/stripe/{activeSubscription.js => subscription.js} (72%) 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