Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions apps/web/app/(ee)/api/customers/[id]/stripe-invoices/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { getCustomerOrThrow } from "@/lib/api/customers/get-customer-or-throw";
import { getCustomerStripeInvoices } from "@/lib/api/customers/get-customer-stripe-invoices";
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { NextResponse } from "next/server";
export const GET = withWorkspace(async ({ workspace, params }) => {
const { id: customerId } = params;

if (!workspace.stripeConnectId) {
throw new DubApiError({
code: "bad_request",
message:
"Your workspace isn't connected to Stripe yet. Please install the Stripe integration under /settings/integrations/stripe to proceed.",
});
}

const customer = await getCustomerOrThrow({
workspaceId: workspace.id,
id: customerId,
});

if (!customer.stripeCustomerId) {
throw new DubApiError({
code: "bad_request",
message:
"Customer doesn't have a Stripe customer ID. Please add a Stripe customer ID to the customer before proceeding.",
});
}

const stripeCustomerInvoices = await getCustomerStripeInvoices({
stripeCustomerId: customer.stripeCustomerId,
stripeConnectId: workspace.stripeConnectId,
});

return NextResponse.json(stripeCustomerInvoices);
});
65 changes: 65 additions & 0 deletions apps/web/app/(ee)/api/customers/search-stripe/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { DubApiError } from "@/lib/api/errors";
import { withWorkspace } from "@/lib/auth";
import { stripeAppClient } from "@/lib/stripe";
import { StripeCustomerSchema } from "@/lib/zod/schemas/customers";
import { prisma } from "@dub/prisma";
import { NextResponse } from "next/server";
import * as z from "zod/v4";

const stripe = stripeAppClient({
...(process.env.VERCEL_ENV && { mode: "live" }),
});

export const GET = withWorkspace(async ({ workspace, searchParams }) => {
const { search } = z
.object({
search: z.string(),
})
.parse(searchParams);

if (!workspace.stripeConnectId) {
throw new DubApiError({
code: "bad_request",
message:
"Your workspace isn't connected to Stripe yet. Please install the Stripe integration under /settings/integrations/stripe to proceed.",
});
}

const { data } = await stripe.customers.search(
{
query: `email~"${search}"`,
expand: ["data.subscriptions"],
},
{
stripeAccount: workspace.stripeConnectId,
},
);

const existingCustomers = await prisma.customer.findMany({
where: {
stripeCustomerId: {
in: data.map((customer) => customer.id),
},
projectId: workspace.id,
},
select: {
id: true,
stripeCustomerId: true,
},
});

const stripeCustomers = StripeCustomerSchema.array().parse(
data.map((customer) => ({
id: customer.id,
email: customer.email,
name: customer.name,
country: customer.address?.country ?? null,
subscriptions: customer.subscriptions?.data.length ?? 0,
dubCustomerId:
existingCustomers.find((c) => c.stripeCustomerId === customer.id)?.id ??
null,
})),
);

return NextResponse.json(stripeCustomers);
});
2 changes: 1 addition & 1 deletion apps/web/app/(ee)/api/discount-codes/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const POST = withWorkspace(
throw new DubApiError({
code: "bad_request",
message:
"Your workspace isn't connected to Stripe yet. Please install the Dub Stripe app in settings to create discount codes.",
"Your workspace isn't connected to Stripe yet. Please install the Stripe integration under /settings/integrations/stripe to proceed.",
});
}

Expand Down
Loading