Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

configure Stripe install setup

Kiran Ke9ad701e5fc9
diff --git a/apps/web/.env.example b/apps/web/.env.example
index e6d38c5fbc..9057687c70 100644
--- a/apps/web/.env.example
+++ b/apps/web/.env.example
@@ -53,6 +53,11 @@ NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=
STRIPE_SECRET_KEY=
STRIPE_WEBHOOK_SECRET=
+# Stripe App
+STRIPE_APP_CLIENT_ID=
+STRIPE_APP_REDIRECT_URI=
+STRIPE_APP_WEBHOOK_SECRET=
+
# Used for Google Login
# Learn how to set this up here: https://next-auth.js.org/providers/google
GOOGLE_CLIENT_ID=
diff --git a/apps/web/app/api/campaigns/callback/stripe/route.ts b/apps/web/app/api/campaigns/callback/stripe/route.ts
index 129a38e029..ac088f4d18 100644
--- a/apps/web/app/api/campaigns/callback/stripe/route.ts
+++ b/apps/web/app/api/campaigns/callback/stripe/route.ts
@@ -1,33 +1,47 @@
import { prismaEdge } from "@/lib/prisma/edge";
+import { redis } from "@/lib/upstash";
import z from "@/lib/zod";
import { getSearchParams } from "@dub/utils";
import { redirect } from "next/navigation";
import { NextRequest } from "next/server";
+export const runtime = "edge";
+
const schema = z.object({
state: z.string(),
stripe_user_id: z.string(),
});
-export const runtime = "edge";
-
export const GET = async (req: NextRequest) => {
- const { state: workspaceId, stripe_user_id: stripeConnectId } = schema.parse(
- getSearchParams(req.url),
- );
+ const parsed = schema.safeParse(getSearchParams(req.url));
+
+ if (!parsed.success) {
+ console.error(parsed.error);
+ return new Response("Invalid request", { status: 400 });
+ }
+
+ const { state, stripe_user_id: stripeAccountId } = parsed.data;
+
+ // Find workspace that initiated the Stripe app install
+ const workspaceId = await redis.get(`stripe:install:state:${state}`);
+
+ if (!workspaceId) {
+ console.error("Invalid state", state);
+ return new Response("Invalid request", { status: 400 });
+ }
- // TODO:
- // Create a unique state before redirecting to Stripe (and store it in the Redis)
- // And validate it here
+ // Delete the state key from Redis
+ await redis.del(`stripe:install:state:${state}`);
+ // Update the workspace with the Stripe Connect ID
const workspace = await prismaEdge.project.update({
where: {
- id: workspaceId.replace("ws_", ""),
+ id: workspaceId as string,
},
data: {
- stripeConnectId,
+ stripeConnectId: stripeAccountId,
},
});
- redirect(`/${workspace.slug}`);
+ redirect(`/${workspace.slug}/settings`);
};
diff --git a/apps/web/app/api/campaigns/connect-stripe/route.ts b/apps/web/app/api/campaigns/connect-stripe/route.ts
new file mode 100644
index 0000000000..6cbf527c31
--- /dev/null
+++ b/apps/web/app/api/campaigns/connect-stripe/route.ts
@@ -0,0 +1,44 @@
+import { DubApiError } from "@/lib/api/errors";
+import { withWorkspace } from "@/lib/auth";
+import { redis } from "@/lib/upstash";
+import z from "@/lib/zod";
+import { nanoid } from "@dub/utils";
+import { NextResponse } from "next/server";
+
+const envSchema = z.object({
+ STRIPE_APP_INSTALL_URL: z.string(),
+ STRIPE_APP_CLIENT_ID: z.string(),
+ STRIPE_APP_REDIRECT_URI: z.string(),
+ STRIPE_APP_WEBHOOK_SECRET: z.string(),
+});
+
+export const POST = withWorkspace(async ({ workspace }) => {
+ const state = nanoid(16);
+ await redis.set(`stripe:install:state:${state}`, workspace.id, {
+ ex: 5 * 60,
+ });
+
+ const env = envSchema.safeParse(process.env);
+
+ if (!env.success) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: "Stripe environment variables are not configured properly.",
+ });
+ }
+
+ const {
+ STRIPE_APP_INSTALL_URL,
+ STRIPE_APP_CLIENT_ID,
+ STRIPE_APP_REDIRECT_URI,
+ } = env.data;
+
+ const url = new URL(STRIPE_APP_INSTALL_URL);
+ url.searchParams.set("client_id", STRIPE_APP_CLIENT_ID);
+ url.searchParams.set("redirect_uri", STRIPE_APP_REDIRECT_URI);
+ url.searchParams.set("state", state);
+
+ console.log("Redirecting to", url.toString());
+
+ return NextResponse.json({ url: url.toString() });
+});
diff --git a/apps/web/app/api/campaigns/webhooks/stripe/customer-created.ts b/apps/web/app/api/campaigns/webhooks/stripe/customer-created.ts
index 48459c208e..0c964b00e4 100644
--- a/apps/web/app/api/campaigns/webhooks/stripe/customer-created.ts
+++ b/apps/web/app/api/campaigns/webhooks/stripe/customer-created.ts
@@ -45,7 +45,7 @@ export async function customerCreated(event: Stripe.Event) {
});
await Promise.all([
- // Record customer in TB
+ // Record customer
recordCustomer({
workspace_id: customer.projectId,
customer_id: customer.id,
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/page-client.tsx
index 5a2a40a221..fb176a2938 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/settings/page-client.tsx
@@ -13,8 +13,31 @@ export default function WorkspaceSettingsClient() {
const router = useRouter();
const { id, name, slug, isOwner } = useWorkspace();
+ const redirectToStripe = async () => {
+ const response = await fetch(
+ `/api/campaigns/connect-stripe?workspaceId=${id}`,
+ {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ },
+ );
+
+ const { url } = await response.json();
+
+ window.location.href = url;
+ };
+
return (
<>
+ {/* TODO: Move the button to proper place */}
+ <button
+ onClick={redirectToStripe}
+ className="group flex h-10 w-44 items-center justify-center space-x-2 rounded-md border border-black bg-black px-4 text-sm text-white"
+ >
+ Connect to Stripe
+ </button>
<Form
title="Workspace Name"
description={`This is the name of your workspace on ${process.env.NEXT_PUBLIC_APP_NAME}.`}
diff --git a/packages/stripe-app/stripe-app.json b/packages/stripe-app/stripe-app.json
index a409cd369b..ebd740687a 100644
--- a/packages/stripe-app/stripe-app.json
+++ b/packages/stripe-app/stripe-app.json
@@ -1,6 +1,6 @@
{
"id": "com.example.dub",
- "version": "0.0.15",
+ "version": "0.0.16",
"name": "Dub",
"icon": "",
"permissions": [
@@ -61,4 +61,4 @@
],
"stripe_api_access_type": "oauth",
"distribution_type": "public"
-}
\ No newline at end of file
+}