From 1907e7e59c5ae95dcd17b91e9fd0990ceca65430 Mon Sep 17 00:00:00 2001 From: Mauricio Siu <47042324+Siumauricio@users.noreply.github.com> Date: Sun, 20 Oct 2024 23:08:26 -0600 Subject: [PATCH] feat: add webhook --- .../settings/billing/show-billing.tsx | 199 +- .../dashboard/settings/servers/add-server.tsx | 17 + .../settings/servers/show-servers.tsx | 37 +- .../dokploy/drizzle/0043_legal_power_pack.sql | 9 + apps/dokploy/drizzle/meta/0043_snapshot.json | 3944 +++++++++++++++++ apps/dokploy/drizzle/meta/_journal.json | 7 + apps/dokploy/pages/api/stripe/webhook.ts | 425 +- apps/dokploy/server/api/routers/settings.ts | 2 +- apps/dokploy/server/api/routers/stripe.ts | 462 +- apps/dokploy/server/utils/stripe.ts | 193 +- packages/server/src/db/schema/admin.ts | 3 +- packages/server/src/db/schema/server.ts | 6 +- pnpm-lock.yaml | 6 +- 13 files changed, 4483 insertions(+), 827 deletions(-) create mode 100644 apps/dokploy/drizzle/0043_legal_power_pack.sql create mode 100644 apps/dokploy/drizzle/meta/0043_snapshot.json diff --git a/apps/dokploy/components/dashboard/settings/billing/show-billing.tsx b/apps/dokploy/components/dashboard/settings/billing/show-billing.tsx index 9fdd75e91..b83597324 100644 --- a/apps/dokploy/components/dashboard/settings/billing/show-billing.tsx +++ b/apps/dokploy/components/dashboard/settings/billing/show-billing.tsx @@ -1,16 +1,13 @@ -import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { NumberInput } from "@/components/ui/input"; +import { Progress } from "@/components/ui/progress"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { cn } from "@/lib/utils"; import { api } from "@/utils/api"; import { loadStripe } from "@stripe/stripe-js"; import clsx from "clsx"; -import { CheckIcon, MinusIcon, PlusIcon } from "lucide-react"; -import { useRouter } from "next/router"; +import { AlertTriangle, CheckIcon, MinusIcon, PlusIcon } from "lucide-react"; import React, { useState } from "react"; -import { toast } from "sonner"; -import { ReviewPayment } from "./review-payment"; const stripePromise = loadStripe( "pk_test_51QAm7bF3cxQuHeOz0xg04o9teeyTbbNHQPJ5Tr98MlTEan9MzewT3gwh0jSWBNvrRWZ5vASoBgxUSF4gPWsJwATk00Ir2JZ0S1", @@ -18,29 +15,17 @@ const stripePromise = loadStripe( export const calculatePrice = (count: number, isAnnual = false) => { if (isAnnual) { - if (count === 1) return 40.8; - if (count <= 3) return 81.5; - return 81.5 + (count - 3) * 35.7; + if (count <= 1) return 45.9; + return 35.7 * count; } - if (count === 1) return 4.0; - if (count <= 3) return 7.99; - return 7.99 + (count - 3) * 3.5; + if (count <= 1) return 4.5; + return count * 3.5; }; - -export const calculateYearlyCost = (serverQuantity: number) => { - const count = serverQuantity; - if (count === 1) return 4.0 * 12; - if (count <= 3) return 7.99 * 12; - return (7.99 + (count - 3) * 3.5) * 12; -}; - +// 178.156.147.118 export const ShowBilling = () => { - const router = useRouter(); - const { data: billingSubscription } = - api.stripe.getBillingSubscription.useQuery(undefined); const { data: servers } = api.server.all.useQuery(undefined); const { data: admin } = api.admin.one.useQuery(); - const { data, refetch } = api.stripe.getProducts.useQuery(); + const { data } = api.stripe.getProducts.useQuery(); const { mutateAsync: createCheckoutSession } = api.stripe.createCheckoutSession.useMutation(); @@ -48,68 +33,11 @@ export const ShowBilling = () => { api.stripe.createCustomerPortalSession.useMutation(); const [serverQuantity, setServerQuantity] = useState(3); - - const { mutateAsync: upgradeSubscriptionMonthly } = - api.stripe.upgradeSubscriptionMonthly.useMutation(); - - const { mutateAsync: upgradeSubscriptionAnnual } = - api.stripe.upgradeSubscriptionAnnual.useMutation(); const [isAnnual, setIsAnnual] = useState(false); - // useEffect(() => { - // if (billingSubscription) { - // setIsAnnual( - // (prevIsAnnual) => - // billingSubscription.billingInterval === "year" && - // prevIsAnnual !== true, - // ); - // } - // }, [billingSubscription]); - const handleCheckout = async (productId: string) => { const stripe = await stripePromise; - - if (data && admin?.stripeSubscriptionId && data.subscriptions.length > 0) { - if (isAnnual) { - upgradeSubscriptionAnnual({ - subscriptionId: admin?.stripeSubscriptionId, - serverQuantity, - }) - .then(async (subscription) => { - if (subscription.type === "new") { - await stripe?.redirectToCheckout({ - sessionId: subscription.sessionId, - }); - return; - } - toast.success("Subscription upgraded successfully"); - await refetch(); - }) - .catch((error) => { - toast.error("Error to upgrade the subscription"); - console.error(error); - }); - } else { - upgradeSubscriptionMonthly({ - subscriptionId: admin?.stripeSubscriptionId, - serverQuantity, - }) - .then(async (subscription) => { - if (subscription.type === "new") { - await stripe?.redirectToCheckout({ - sessionId: subscription.sessionId, - }); - return; - } - toast.success("Subscription upgraded successfully"); - await refetch(); - }) - .catch((error) => { - toast.error("Error to upgrade the subscription"); - console.error(error); - }); - } - } else { + if (data && data.subscriptions.length === 0) { createCheckoutSession({ productId, serverQuantity: serverQuantity, @@ -126,9 +54,12 @@ export const ShowBilling = () => { return isAnnual ? interval === "year" : interval === "month"; }); + const maxServers = admin?.serversQuantity ?? 1; + const percentage = ((servers?.length ?? 0) / maxServers) * 100; + const safePercentage = Math.min(percentage, 100); + return (
+ You have {servers?.length} server on your plan of{" "} + {admin?.serversQuantity} servers +
+