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.

Allow one Stripe account per mode across workspaces

Kiran K7d6652bca4e9
diff --git a/apps/web/app/(ee)/api/stripe/integration/route.ts b/apps/web/app/(ee)/api/stripe/integration/route.ts
index 0eccc85ad2..9b3f5ff81a 100644
--- a/apps/web/app/(ee)/api/stripe/integration/route.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/route.ts
@@ -105,7 +105,7 @@ export const PATCH = withWorkspace(
if (error.code === "P2002") {
throw new DubApiError({
code: "conflict",
- message: `The stripe connect account "${stripeAccountId}" is already in use.`,
+ message: `The stripe connect account "${stripeAccountId}" is already connected to another workspace in ${stripeMode} mode.`,
});
}
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts
index 004438977d..d6a7423ce9 100644
--- a/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/route.ts
@@ -1,6 +1,5 @@
import { captureWebhookLog } from "@/lib/api-logs/capture-webhook-log";
import { withAxiom } from "@/lib/axiom/server";
-import { prisma } from "@/lib/prisma";
import { isStripeRateLimitError, stripe } from "@/lib/stripe";
import { StripeMode } from "@/lib/types";
import { waitUntil } from "@vercel/functions";
@@ -15,6 +14,7 @@ import { customerSubscriptionDeleted } from "./customer-subscription-deleted";
import { invoicePaid } from "./invoice-paid";
import { promotionCodeUpdated } from "./promotion-code-updated";
import { WebhookHandlerResponse } from "./types";
+import { getWorkspaceForStripeAccount } from "./utils/get-workspace-for-stripe-account";
import { syncCustomer } from "./utils/sync-customer";
export const dynamic = "force-dynamic";
@@ -105,17 +105,9 @@ export const POST = withAxiom(async (req: Request) => {
}
// Find the workspace
- const workspace = await prisma.project.findUnique({
- where: {
- stripeConnectId: event.account,
- },
- select: {
- id: true,
- stripeConnectId: true,
- defaultProgramId: true,
- webhookEnabled: true,
- environment: true,
- },
+ const workspace = await getWorkspaceForStripeAccount({
+ stripeAccountId: event.account,
+ mode,
});
if (!workspace) {
diff --git a/apps/web/app/(ee)/api/stripe/integration/webhook/utils/get-workspace-for-stripe-account.ts b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/get-workspace-for-stripe-account.ts
new file mode 100644
index 0000000000..d17c916551
--- /dev/null
+++ b/apps/web/app/(ee)/api/stripe/integration/webhook/utils/get-workspace-for-stripe-account.ts
@@ -0,0 +1,39 @@
+import { prisma } from "@/lib/prisma";
+import { StripeMode } from "@/lib/types";
+
+export async function getWorkspaceForStripeAccount({
+ stripeAccountId,
+ mode,
+}: {
+ stripeAccountId: string;
+ mode: StripeMode;
+}) {
+ const workspaces = await prisma.project.findMany({
+ where: {
+ stripeConnectId: stripeAccountId,
+ },
+ select: {
+ id: true,
+ stripeConnectId: true,
+ stripeConnectMode: true,
+ defaultProgramId: true,
+ webhookEnabled: true,
+ environment: true,
+ },
+ });
+
+ const workspaceInMode = workspaces.find(
+ ({ stripeConnectMode }) => stripeConnectMode === mode,
+ );
+
+ if (workspaceInMode) {
+ return workspaceInMode;
+ }
+
+ // Fall back to the only connected workspace so existing single-mode installs keep receiving events
+ if (workspaces.length === 1) {
+ return workspaces[0];
+ }
+
+ return null;
+}
diff --git a/apps/web/prisma/schema/workspace.prisma b/apps/web/prisma/schema/workspace.prisma
index 7f09959cc9..9b40cfa73a 100644
--- a/apps/web/prisma/schema/workspace.prisma
+++ b/apps/web/prisma/schema/workspace.prisma
@@ -45,7 +45,7 @@ model Project {
environment WorkspaceEnvironment @default(production)
stagingWorkspaceId String? @unique
- stripeConnectId String? @unique // for Stripe Integration
+ stripeConnectId String? // for Stripe Integration
stripeConnectMode StripeConnectMode? // Stripe mode the account is connected in
shopifyStoreId String? @unique // for Shopify Integration
@@ -116,6 +116,7 @@ model Project {
utmTemplates UtmTemplate[]
yearInReviews YearInReview[]
+ @@unique([stripeConnectId, stripeConnectMode])
@@index(usageLastChecked(sort: Asc))
@@index(trialEndsAt)
}
diff --git a/apps/web/tests/stripe/get-workspace-for-stripe-account.test.ts b/apps/web/tests/stripe/get-workspace-for-stripe-account.test.ts
new file mode 100644
index 0000000000..b20b9df953
--- /dev/null
+++ b/apps/web/tests/stripe/get-workspace-for-stripe-account.test.ts
@@ -0,0 +1,81 @@
+import { beforeEach, describe, expect, test, vi } from "vitest";
+import { getWorkspaceForStripeAccount } from "../../app/(ee)/api/stripe/integration/webhook/utils/get-workspace-for-stripe-account";
+
+const mocks = vi.hoisted(() => ({
+ findMany: vi.fn(),
+}));
+
+vi.mock("@/lib/prisma", () => ({
+ prisma: {
+ project: {
+ findMany: mocks.findMany,
+ },
+ },
+}));
+
+const stripeAccountId = "acct_test";
+
+const liveWorkspace = {
+ id: "ws_live",
+ stripeConnectId: stripeAccountId,
+ stripeConnectMode: "live",
+ defaultProgramId: null,
+ webhookEnabled: false,
+ environment: "production",
+};
+
+const testWorkspace = {
+ ...liveWorkspace,
+ id: "ws_test",
+ stripeConnectMode: "test",
+ environment: "staging",
+};
+
+describe("getWorkspaceForStripeAccount", () => {
+ beforeEach(() => {
+ mocks.findMany.mockReset();
+ });
+
+ test("returns the workspace connected in the event's mode", async () => {
+ mocks.findMany.mockResolvedValue([liveWorkspace, testWorkspace]);
+
+ await expect(
+ getWorkspaceForStripeAccount({ stripeAccountId, mode: "live" }),
+ ).resolves.toBe(liveWorkspace);
+ await expect(
+ getWorkspaceForStripeAccount({ stripeAccountId, mode: "test" }),
+ ).resolves.toBe(testWorkspace);
+
+ expect(mocks.findMany).toHaveBeenCalledWith(
+ expect.objectContaining({
+ where: {
+ stripeConnectId: stripeAccountId,
+ },
+ }),
+ );
+ });
+
+ test("falls back to the only workspace connected to the account", async () => {
+ mocks.findMany.mockResolvedValue([liveWorkspace]);
+
+ await expect(
+ getWorkspaceForStripeAccount({ stripeAccountId, mode: "test" }),
+ ).resolves.toBe(liveWorkspace);
+ });
+
+ test("returns null when several workspaces share the account and none match the mode", async () => {
+ mocks.findMany.mockResolvedValue([liveWorkspace, testWorkspace]);
+
+ await expect(
+ getWorkspaceForStripeAccount({ stripeAccountId, mode: "sandbox" }),
+ ).resolves.toBeNull();
+ });
+
+ test("returns null when no workspace is connected to the account", async () => {
+ mocks.findMany.mockResolvedValue([]);
+
+ await expect(
+ getWorkspaceForStripeAccount({ stripeAccountId, mode: "live" }),
+ ).resolves.toBeNull();
+ });
+});