-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Import customers from Stripe #3482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,673
−964
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d0874d9
Import customers from Stripe
steven-tey 050723f
fetch customer stripe invoices
steven-tey 4c8c719
finalize backend changes (createManualCommissionAction)
steven-tey 34deef8
smart initialData prefilling in AddCustomerModal
steven-tey 9029f47
final changes, address coderabbit feedback
steven-tey 1ef0b70
finalize changes
steven-tey 6337810
Merge branch 'main' into import-stripe-customers
steven-tey 9dee0b5
fix tokenCacheItemSchema
steven-tey e4211b6
fix group move test to use "between" instead of "gte"
steven-tey cdc8b8e
fix group move workflow test
steven-tey b010c22
Update award-bounty-workflow.test.ts
steven-tey 62d2312
Merge branch 'main' into import-stripe-customers
steven-tey 3a9554c
Update award-bounty-workflow.test.ts
steven-tey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
apps/web/app/(ee)/api/customers/[id]/stripe-invoices/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.