The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Added redirect to Stripe checkout session
Steven Tey81386897cd47
diff --git a/components/app/plan-usage.tsx b/components/app/plan-usage.tsxindex 515b0697f6..971d7eafc5 100644--- a/components/app/plan-usage.tsx+++ b/components/app/plan-usage.tsx@@ -1,10 +1,17 @@import useSWR from "swr";+import { useState } from "react";import { fetcher, nFormatter } from "@/lib/utils";import { useRouter } from "next/router";import { ProjectProps } from "@/lib/types";import Tooltip from "@/components/shared/tooltip";-import { Divider, Infinity, QuestionCircle } from "@/components/shared/icons";+import {+ Divider,+ Infinity,+ QuestionCircle,+ LoadingDots,+} from "@/components/shared/icons";import { motion } from "framer-motion";+import { getStripe } from "@/lib/stripe";export default function PlanUsage({ project }: { project: ProjectProps }) {const router = useRouter();@@ -20,6 +27,8 @@ export default function PlanUsage({ project }: { project: ProjectProps }) {fetcher);+ const [upgrading, setUpgrading] = useState(false);+return (<div className="bg-white rounded-lg border border-gray-200"><div className="flex flex-col space-y-3 p-10">@@ -93,8 +102,32 @@ export default function PlanUsage({ project }: { project: ProjectProps }) {</Tooltip>, upgrade to the Pro plan.</p>- <button className="bg-blue-500 text-white border-blue-500 hover:text-blue-500 hover:bg-white h-9 w-24 text-sm border-solid border rounded-md focus:outline-none transition-all ease-in-out duration-150">- Upgrade+ <button+ onClick={() => {+ setUpgrading(true);+ fetch(`/api/projects/${slug}/upgrade`, {+ method: "POST",+ })+ .then(async (res) => {+ const data = await res.json();+ console.log(data);+ const { id: sessionId } = data;+ const stripe = await getStripe();+ stripe.redirectToCheckout({ sessionId });+ })+ .catch((err) => {+ alert(err);+ setUpgrading(false);+ });+ }}+ disabled={upgrading}+ className={`${+ upgrading+ ? "cursor-not-allowed bg-gray-100 border-gray-200"+ : "bg-blue-500 text-white border-blue-500 hover:text-blue-500 hover:bg-white"+ } h-9 w-24 text-sm border rounded-md focus:outline-none transition-all ease-in-out duration-150`}+ >+ {upgrading ? <LoadingDots /> : "Upgrade"}</button></div></div>diff --git a/lib/stripe.ts b/lib/stripe.tsindex 8d8b9d00e9..139504b378 100644--- a/lib/stripe.ts+++ b/lib/stripe.ts@@ -3,13 +3,27 @@ import Stripe from "stripe";export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY_LIVE ?? process.env.STRIPE_SECRET_KEY ?? "",{- // https://github.com/stripe/stripe-node#usage-with-typescriptapiVersion: "2022-08-01",- // Register this as an official Stripe plugin.- // https://stripe.com/docs/building-plugins#setappinfoappInfo: {name: "Dub.sh",version: "0.1.0",},});++// Stripe Client SDK+import { loadStripe, Stripe as StripeProps } from "@stripe/stripe-js";++let stripePromise: Promise<StripeProps | null>;++export const getStripe = () => {+ if (!stripePromise) {+ stripePromise = loadStripe(+ process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY_LIVE ??+ process.env.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY ??+ ""+ );+ }++ return stripePromise;+};diff --git a/package-lock.json b/package-lock.jsonindex bace8e838d..4d0ddbaff0 100644--- a/package-lock.json+++ b/package-lock.json@@ -9,6 +9,7 @@"@next-auth/prisma-adapter": "^1.0.4","@prisma/client": "^4.3.1","@radix-ui/react-tooltip": "^1.0.0",+ "@stripe/stripe-js": "^1.36.0","@tailwindcss/forms": "^0.5.3","@upstash/ratelimit": "^0.1.5","@upstash/redis": "^1.12.0",@@ -720,6 +721,11 @@"@babel/runtime": "^7.13.10"}},+ "node_modules/@stripe/stripe-js": {+ "version": "1.36.0",+ "resolved": "https://registry.npmjs.org/@stripe/stripe-js/-/stripe-js-1.36.0.tgz",+ "integrity": "sha512-m45BD9JxOfIBT0Tz4MupiKzM8M58NX/We8wKlf+54TCZpW1RVAyFpJ58CbtyU/LxAM+opT6cewHRVfs7bTUtBA=="+ },"node_modules/@swc/helpers": {"version": "0.4.3","resolved": "https://registry.npmjs.org/@swc/helpers/-/helpers-0.4.3.tgz",@@ -5624,6 +5630,11 @@"@babel/runtime": "^7.13.10"}},+ "@stripe/stripe-js": {+ "version": "1.36.0",+ "resolved": "https://registry.npmjs.org/@stripe/stripe-js/-/stripe-js-1.36.0.tgz",+ "integrity": "sha512-m45BD9JxOfIBT0Tz4MupiKzM8M58NX/We8wKlf+54TCZpW1RVAyFpJ58CbtyU/LxAM+opT6cewHRVfs7bTUtBA=="+ },"@swc/helpers": {"version": "0.4.3","resolved": "https://registry.npmjs.org/@swc/helpers/-/helpers-0.4.3.tgz",diff --git a/package.json b/package.jsonindex 36f1f5b17a..4c56ccb1ab 100644--- a/package.json+++ b/package.json@@ -10,6 +10,7 @@"@next-auth/prisma-adapter": "^1.0.4","@prisma/client": "^4.3.1","@radix-ui/react-tooltip": "^1.0.0",+ "@stripe/stripe-js": "^1.36.0","@tailwindcss/forms": "^0.5.3","@upstash/ratelimit": "^0.1.5","@upstash/redis": "^1.12.0",diff --git a/pages/api/projects/[slug]/index.ts b/pages/api/projects/[slug]/index.tsindex e2813fd90f..c1afa36c2d 100644--- a/pages/api/projects/[slug]/index.ts+++ b/pages/api/projects/[slug]/index.ts@@ -1,7 +1,6 @@import { NextApiRequest, NextApiResponse } from "next";import { getSession } from "@/lib/auth";import prisma from "@/lib/prisma";-import { redis, getLinksForProject } from "@/lib/upstash";export default async function handler(req: NextApiRequest,diff --git a/pages/api/projects/[slug]/upgrade.ts b/pages/api/projects/[slug]/upgrade.tsnew file mode 100644index 0000000000..aa1a6ccc6c--- /dev/null+++ b/pages/api/projects/[slug]/upgrade.ts@@ -0,0 +1,34 @@+import { NextApiRequest, NextApiResponse } from "next";+import { withProjectAuth } from "@/lib/auth";+import { stripe } from "@/lib/stripe";++export default withProjectAuth(+ async (req: NextApiRequest, res: NextApiResponse) => {+ // GET /api/projects/[slug]/upgrade – upgrade a project+ if (req.method === "POST") {+ const { slug } = req.query as { slug: string };+ const session = await stripe.checkout.sessions.create({+ payment_method_types: ["card"],+ billing_address_collection: "required",+ success_url: `${+ process.env.VERCEL === "1"+ ? "https://app.dub.sh"+ : "http://app.localhost:3000"+ }/${slug}/settings`,+ cancel_url: `${+ process.env.VERCEL === "1"+ ? "https://app.dub.sh"+ : "http://app.localhost:3000"+ }/${slug}/settings`,+ line_items: [{ price: "price_1Lis30AlJJEpqkPVAuSVxbT1", quantity: 1 }],+ mode: "subscription",+ });+ return res.status(200).json(session);+ } else {+ res.setHeader("Allow", ["GET"]);+ return res+ .status(405)+ .json({ error: `Method ${req.method} Not Allowed` });+ }+ }+);diff --git a/pages/app/[slug]/settings.tsx b/pages/app/[slug]/settings.tsxindex ae74ffea22..0dc7771a83 100644--- a/pages/app/[slug]/settings.tsx+++ b/pages/app/[slug]/settings.tsx@@ -35,8 +35,8 @@ export default function ProjectLinks() {</div><MaxWidthWrapper><div className="py-10 grid gap-5">- <CustomDomain domain={project?.domain || ""} /><PlanUsage project={project} />+ <CustomDomain domain={project?.domain || ""} /></div></MaxWidthWrapper></AppLayout>diff --git a/yarn.lock b/yarn.lockindex ae2b1d62ed..cd237f68b7 100644--- a/yarn.lock+++ b/yarn.lock@@ -488,6 +488,11 @@dependencies:"@babel/runtime" "^7.13.10"+"@stripe/stripe-js@^1.36.0":+ "integrity" "sha512-m45BD9JxOfIBT0Tz4MupiKzM8M58NX/We8wKlf+54TCZpW1RVAyFpJ58CbtyU/LxAM+opT6cewHRVfs7bTUtBA=="+ "resolved" "https://registry.npmjs.org/@stripe/stripe-js/-/stripe-js-1.36.0.tgz"+ "version" "1.36.0"+"@swc/[email protected]":"integrity" "sha512-6JrF+fdUK2zbGpJIlN7G3v966PQjyx/dPt1T9km2wj+EUBqgrxCk3uX4Kct16MIm9gGxfKRcfax2hVf5jvlTzA==""resolved" "https://registry.npmjs.org/@swc/helpers/-/helpers-0.4.3.tgz"