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.

Remove unused Stripe integration files and refactor webhook handling for account deauthorization. Update workspace management in the app settings and streamline API calls for better efficiency.

Kiran Kb7aadd81bedf
diff --git a/apps/web/app/api/stripe/integration/callback/route.ts b/apps/web/app/api/stripe/integration/callback/route.ts
deleted file mode 100644
index 4ca9489d4f..0000000000
--- a/apps/web/app/api/stripe/integration/callback/route.ts
+++ /dev/null
@@ -1,81 +0,0 @@
-import { getSession } from "@/lib/auth";
-import { installIntegration } from "@/lib/integrations/install";
-import { redis } from "@/lib/upstash";
-import z from "@/lib/zod";
-import { prisma } from "@dub/prisma";
-import { APP_DOMAIN, getSearchParams, STRIPE_INTEGRATION_ID } from "@dub/utils";
-import { redirect } from "next/navigation";
-import { NextRequest } from "next/server";
-
-const schema = z.object({
- state: z.string(),
- stripe_user_id: z.string().optional(),
- error: z.string().optional(),
- error_description: z.string().optional(),
-});
-
-export const GET = async (req: NextRequest) => {
- const session = await getSession();
-
- if (!session?.user.id) {
- return new Response("Unauthorized", { status: 401 });
- }
-
- const parsed = schema.safeParse(getSearchParams(req.url));
-
- if (!parsed.success) {
- console.error("[Stripe OAuth callback] Error", parsed.error);
- return new Response("Invalid request", { status: 400 });
- }
-
- const {
- state,
- stripe_user_id: stripeAccountId,
- error,
- error_description,
- } = parsed.data;
-
- // Find workspace that initiated the Stripe app install
- const workspaceId = await redis.get<string>(`stripe:install:state:${state}`);
-
- if (!workspaceId) {
- redirect(APP_DOMAIN);
- }
-
- // Delete the state key from Redis
- await redis.del(`stripe:install:state:${state}`);
-
- if (error) {
- const workspace = await prisma.project.findUnique({
- where: {
- id: workspaceId,
- },
- });
- if (!workspace) {
- redirect(APP_DOMAIN);
- }
- redirect(
- `${APP_DOMAIN}/${workspace.slug}/settings/integrations/stripe?stripeConnectError=${error_description}`,
- );
- } else if (stripeAccountId) {
- // Update the workspace with the Stripe Connect ID
- const workspace = await prisma.project.update({
- where: {
- id: workspaceId,
- },
- data: {
- stripeConnectId: stripeAccountId,
- },
- });
-
- await installIntegration({
- integrationId: STRIPE_INTEGRATION_ID,
- userId: session.user.id,
- workspaceId: workspace.id,
- });
-
- redirect(`${APP_DOMAIN}/${workspace.slug}/settings/integrations/stripe`);
- }
-
- return new Response("Invalid request", { status: 400 });
-};
diff --git a/apps/web/app/api/stripe/integration/webhook/account-application-deauthorized.ts b/apps/web/app/api/stripe/integration/webhook/account-application-deauthorized.ts
index f87fe33f18..607124b29c 100644
--- a/apps/web/app/api/stripe/integration/webhook/account-application-deauthorized.ts
+++ b/apps/web/app/api/stripe/integration/webhook/account-application-deauthorized.ts
@@ -6,7 +6,20 @@ import type Stripe from "stripe";
export async function accountApplicationDeauthorized(event: Stripe.Event) {
const stripeAccountId = event.account;
- const workspace = await prisma.project.update({
+ const workspace = await prisma.project.findUnique({
+ where: {
+ stripeConnectId: stripeAccountId,
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ if (!workspace) {
+ return `Stripe Connect account ${stripeAccountId} deauthorized.`;
+ }
+
+ await prisma.project.update({
where: {
stripeConnectId: stripeAccountId,
},
diff --git a/packages/stripe-app/src/utils/create-link.ts b/packages/stripe-app/src/utils/create-link.ts
deleted file mode 100644
index 235493c754..0000000000
--- a/packages/stripe-app/src/utils/create-link.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-import { DUB_API_HOST } from "./constants";
-import { getValidToken } from "./oauth";
-import { stripe } from "./stripe";
-
-export async function createLink({
- url,
- isPublic,
-}: {
- url: string;
- isPublic: boolean;
-}) {
- const token = isPublic ? null : await getValidToken({ stripe });
-
- const response = await fetch(`${DUB_API_HOST}/links`, {
- method: "POST",
- body: JSON.stringify({
- url,
- ...(token ? { trackConversion: true } : {}),
- }),
- headers: {
- "Content-Type": "application/json",
- ...(!token
- ? { "dub-anonymous-link-creation": "1" }
- : { Authorization: `Bearer ${token.access_token}` }),
- },
- });
-
- const data = await response.json();
-
- if (!response.ok) {
- throw new Error("Failed to create link.", {
- cause: data.error,
- });
- }
-
- return data;
-}
diff --git a/packages/stripe-app/src/utils/dub.ts b/packages/stripe-app/src/utils/dub.ts
index b07c60e988..ac4ccb77ec 100644
--- a/packages/stripe-app/src/utils/dub.ts
+++ b/packages/stripe-app/src/utils/dub.ts
@@ -4,25 +4,20 @@ import { Token } from "./types";
// Update the workspace with stripeAccountId
export async function updateWorkspace({
token,
- workspaceId,
accountId,
}: {
token: Token;
- workspaceId: string;
accountId: string | null;
}) {
- const response = await fetch(
- `${DUB_API_HOST}/stripe/integration?workspaceId=${workspaceId}`,
- {
- method: "PATCH",
- headers: {
- Authorization: `Bearer ${token.access_token}`,
- },
- body: JSON.stringify({
- stripeAccountId: accountId,
- }),
+ const response = await fetch(`${DUB_API_HOST}/stripe/integration`, {
+ method: "PATCH",
+ headers: {
+ Authorization: `Bearer ${token.access_token}`,
},
- );
+ body: JSON.stringify({
+ stripeAccountId: accountId,
+ }),
+ });
if (!response.ok) {
const data = await response.json();
diff --git a/packages/stripe-app/src/views/AppSettings.tsx b/packages/stripe-app/src/views/AppSettings.tsx
index 997c3395a5..6c019b1302 100644
--- a/packages/stripe-app/src/views/AppSettings.tsx
+++ b/packages/stripe-app/src/views/AppSettings.tsx
@@ -22,9 +22,6 @@ import { deleteSecret, setSecret } from "../utils/secrets";
import { stripe } from "../utils/stripe";
import { Workspace } from "../utils/types";
-// TODO:
-// Handle errors and display them to the user
-
const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
const credentialsUsed = useRef(false);
const [oauthState, setOAuthState] = useState("");
@@ -48,7 +45,6 @@ const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
await updateWorkspace({
token,
- workspaceId: workspace.id,
accountId: null,
});
@@ -99,7 +95,6 @@ const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
await updateWorkspace({
token,
- workspaceId: workspace.id,
accountId: userContext.account.id,
});
@@ -189,8 +184,6 @@ const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
brandIcon={appIcon}
/>
)}
-
- {/* <CreateLink workspace={workspace} /> */}
</Box>
);
};
diff --git a/packages/stripe-app/src/views/create-link.tsx b/packages/stripe-app/src/views/create-link.tsx
deleted file mode 100644
index a242cfb7a7..0000000000
--- a/packages/stripe-app/src/views/create-link.tsx
+++ /dev/null
@@ -1,82 +0,0 @@
-import {
- Box,
- Button,
- FormFieldGroup,
- Link,
- TextField,
-} from "@stripe/ui-extension-sdk/ui";
-import { showToast } from "@stripe/ui-extension-sdk/utils";
-import { useState } from "react";
-import { createLink } from "../utils/create-link";
-import { Workspace } from "../utils/types";
-
-export const CreateLink = ({ workspace }: { workspace: Workspace | null }) => {
- const [url, setUrl] = useState("");
- const [shortLink, setShortLink] = useState("");
- const [isSubmitting, setIsSubmitting] = useState(false);
-
- const handleCreateLink = async () => {
- setIsSubmitting(true);
-
- try {
- const link = await createLink({
- url,
- isPublic: !workspace,
- });
-
- showToast("Link created", { type: "success" }); // TODO: This is not working
- setShortLink(link.shortLink);
- setUrl("");
- } catch (error: any) {
- console.error(error);
- showToast(error.message, { type: "caution" });
- } finally {
- setIsSubmitting(false);
- }
- };
-
- return (
- <Box
- css={{
- padding: "large",
- keyline: "neutral",
- borderRadius: "small",
- stack: "y",
- gap: "large",
- }}
- >
- <FormFieldGroup
- legend="Destination URL"
- description="Enter a long URL to shorten"
- >
- <TextField
- label="Destination URL"
- placeholder="https://dub.co/help/article/what-is-dub"
- hiddenElements={["label"]}
- onChange={(e) => setUrl(e.target.value)}
- value={url}
- />
- <Button
- type="primary"
- onPress={handleCreateLink}
- disabled={isSubmitting || !url}
- >
- {isSubmitting ? "Shortening..." : "Shorten link"}
- </Button>
- </FormFieldGroup>
- {shortLink && (
- <Link href={shortLink} target="_blank">
- {shortLink}
- </Link>
- )}
- <Box
- css={{
- borderTopColor: "neutral",
- }}
- >
- Want to claim your links and track conversions for them? Connect your
- Dub workspace.
- </Box>
- </Box>
- );
-};