Skip to content

Commit e2d2d39

Browse files
authored
Merge pull request #3482 from dubinc/import-stripe-customers
2 parents 87fef53 + 3a9554c commit e2d2d39

File tree

20 files changed

+1673
-964
lines changed

20 files changed

+1673
-964
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { getCustomerOrThrow } from "@/lib/api/customers/get-customer-or-throw";
2+
import { getCustomerStripeInvoices } from "@/lib/api/customers/get-customer-stripe-invoices";
3+
import { DubApiError } from "@/lib/api/errors";
4+
import { withWorkspace } from "@/lib/auth";
5+
import { NextResponse } from "next/server";
6+
export const GET = withWorkspace(async ({ workspace, params }) => {
7+
const { id: customerId } = params;
8+
9+
if (!workspace.stripeConnectId) {
10+
throw new DubApiError({
11+
code: "bad_request",
12+
message:
13+
"Your workspace isn't connected to Stripe yet. Please install the Stripe integration under /settings/integrations/stripe to proceed.",
14+
});
15+
}
16+
17+
const customer = await getCustomerOrThrow({
18+
workspaceId: workspace.id,
19+
id: customerId,
20+
});
21+
22+
if (!customer.stripeCustomerId) {
23+
throw new DubApiError({
24+
code: "bad_request",
25+
message:
26+
"Customer doesn't have a Stripe customer ID. Please add a Stripe customer ID to the customer before proceeding.",
27+
});
28+
}
29+
30+
const stripeCustomerInvoices = await getCustomerStripeInvoices({
31+
stripeCustomerId: customer.stripeCustomerId,
32+
stripeConnectId: workspace.stripeConnectId,
33+
});
34+
35+
return NextResponse.json(stripeCustomerInvoices);
36+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { DubApiError } from "@/lib/api/errors";
2+
import { withWorkspace } from "@/lib/auth";
3+
import { stripeAppClient } from "@/lib/stripe";
4+
import { StripeCustomerSchema } from "@/lib/zod/schemas/customers";
5+
import { prisma } from "@dub/prisma";
6+
import { NextResponse } from "next/server";
7+
import * as z from "zod/v4";
8+
9+
const stripe = stripeAppClient({
10+
...(process.env.VERCEL_ENV && { mode: "live" }),
11+
});
12+
13+
export const GET = withWorkspace(async ({ workspace, searchParams }) => {
14+
const { search } = z
15+
.object({
16+
search: z.string(),
17+
})
18+
.parse(searchParams);
19+
20+
if (!workspace.stripeConnectId) {
21+
throw new DubApiError({
22+
code: "bad_request",
23+
message:
24+
"Your workspace isn't connected to Stripe yet. Please install the Stripe integration under /settings/integrations/stripe to proceed.",
25+
});
26+
}
27+
28+
const { data } = await stripe.customers.search(
29+
{
30+
query: `email~"${search}"`,
31+
expand: ["data.subscriptions"],
32+
},
33+
{
34+
stripeAccount: workspace.stripeConnectId,
35+
},
36+
);
37+
38+
const existingCustomers = await prisma.customer.findMany({
39+
where: {
40+
stripeCustomerId: {
41+
in: data.map((customer) => customer.id),
42+
},
43+
projectId: workspace.id,
44+
},
45+
select: {
46+
id: true,
47+
stripeCustomerId: true,
48+
},
49+
});
50+
51+
const stripeCustomers = StripeCustomerSchema.array().parse(
52+
data.map((customer) => ({
53+
id: customer.id,
54+
email: customer.email,
55+
name: customer.name,
56+
country: customer.address?.country ?? null,
57+
subscriptions: customer.subscriptions?.data.length ?? 0,
58+
dubCustomerId:
59+
existingCustomers.find((c) => c.stripeCustomerId === customer.id)?.id ??
60+
null,
61+
})),
62+
);
63+
64+
return NextResponse.json(stripeCustomers);
65+
});

apps/web/app/(ee)/api/discount-codes/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const POST = withWorkspace(
6060
throw new DubApiError({
6161
code: "bad_request",
6262
message:
63-
"Your workspace isn't connected to Stripe yet. Please install the Dub Stripe app in settings to create discount codes.",
63+
"Your workspace isn't connected to Stripe yet. Please install the Stripe integration under /settings/integrations/stripe to proceed.",
6464
});
6565
}
6666

0 commit comments

Comments
 (0)