-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a69a251
commit 5f007c3
Showing
11 changed files
with
18,684 additions
and
9,317 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,32 +1,24 @@ | ||
import { findOrCreateCustomerId } from "@/lib/findOrCreateCustomerId"; | ||
import { stripe } from "@/lib/stripe"; | ||
import { auth } from "@clerk/nextjs"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function POST(req: NextRequest) { | ||
const origin = req.headers.get("origin") || "http://localhost:3000"; | ||
try { | ||
const { userId, orgId } = auth(); | ||
if (!userId) | ||
return NextResponse.json( | ||
{ message: "You must be logged in to manage billing" }, | ||
{ status: 500 } | ||
); | ||
const customerId = await findOrCreateCustomerId({ | ||
clerkUserId: userId, | ||
clerkOrgId: orgId, | ||
}); | ||
|
||
const { url } = await stripe.billingPortal.sessions.create({ | ||
customer: customerId, | ||
return_url: `${origin}/dashboard`, | ||
}); | ||
|
||
return NextResponse.json({ url }, { status: 200 }); | ||
} catch (e) { | ||
console.error(e, "Stripe Billing Portal redirect error"); | ||
|
||
// Here, consider redirecting the user to an error page | ||
return NextResponse.error(); | ||
} | ||
export async function POST() { | ||
1; // const origin = req.headers.get("origin") || "http://localhost:3000"; | ||
// try { | ||
// const { userId, orgId } = auth(); | ||
// if (!userId) | ||
// return NextResponse.json( | ||
// { message: "You must be logged in to manage billing" }, | ||
// { status: 500 } | ||
// ); | ||
// const customerId = await findOrCreateCustomerId({ | ||
// clerkUserId: userId, | ||
// clerkOrgId: orgId, | ||
// }); | ||
// const { url } = await stripe.billingPortal.sessions.create({ | ||
// customer: customerId, | ||
// return_url: `${origin}/dashboard`, | ||
// }); | ||
// return NextResponse.json({ url }, { status: 200 }); | ||
// } catch (e) { | ||
// console.error(e, "Stripe Billing Portal redirect error"); | ||
// // Here, consider redirecting the user to an error page | ||
// return NextResponse.error(); | ||
// } | ||
} |
This file contains 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 |
---|---|---|
@@ -1,52 +1,48 @@ | ||
import { findOrCreateCustomerId } from "@/lib/findOrCreateCustomerId"; | ||
import { stripe } from "@/lib/stripe"; | ||
import { auth } from "@clerk/nextjs"; | ||
import { NextResponse } from "next/server"; | ||
import Stripe from "stripe"; | ||
// import { findOrCreateCustomerId } from "@/lib/findOrCreateCustomerId"; | ||
// import { stripe } from "@/lib/stripe"; | ||
// import { NextResponse } from "next/server"; | ||
// import Stripe from "stripe"; | ||
|
||
interface CheckoutSubscriptionBody { | ||
priceId: string; | ||
} | ||
|
||
export async function POST(req: Request) { | ||
const body = (await req.json()) as CheckoutSubscriptionBody; | ||
const origin = req.headers.get("origin") || "http://localhost:3000"; | ||
|
||
try { | ||
const { userId, orgId, user } = auth(); | ||
if (!userId) | ||
return NextResponse.json( | ||
{ message: "You must be logged in to subscribe" }, | ||
{ status: 500 } | ||
); | ||
const customerId = await findOrCreateCustomerId({ | ||
clerkUserId: userId, | ||
clerkOrgId: orgId, | ||
}); | ||
|
||
const session = await stripe.checkout.sessions.create({ | ||
customer_email: user?.emailAddresses[0].emailAddress, | ||
billing_address_collection: "auto", | ||
customer: customerId, | ||
line_items: [ | ||
{ | ||
price: body.priceId, | ||
quantity: 1, | ||
}, | ||
], | ||
mode: "subscription", | ||
success_url: `${origin}/dashboard`, | ||
cancel_url: `${origin}`, | ||
}); | ||
if (session.url === null) | ||
return NextResponse.json({ message: "No session url" }, { status: 500 }); | ||
// interface CheckoutSubscriptionBody { | ||
// priceId: string; | ||
// } | ||
|
||
return NextResponse.json({ url: session.url }, { status: 200 }); | ||
} catch (error) { | ||
if (error instanceof Stripe.errors.StripeError) { | ||
const { message } = error; | ||
return NextResponse.json({ message }, { status: error.statusCode }); | ||
} | ||
return NextResponse.json({ error }, { status: 500 }); | ||
} | ||
export async function POST() { | ||
// const body = (await req.json()) as CheckoutSubscriptionBody; | ||
// const origin = req.headers.get("origin") || "http://localhost:3000"; | ||
// try { | ||
// const { userId, orgId, user } = auth(); | ||
// if (!userId) | ||
// return NextResponse.json( | ||
// { message: "You must be logged in to subscribe" }, | ||
// { status: 500 } | ||
// ); | ||
// const customerId = await findOrCreateCustomerId({ | ||
// clerkUserId: userId, | ||
// clerkOrgId: orgId, | ||
// }); | ||
// const session = await stripe.checkout.sessions.create({ | ||
// customer_email: user?.emailAddresses[0].emailAddress, | ||
// billing_address_collection: "auto", | ||
// customer: customerId, | ||
// line_items: [ | ||
// { | ||
// price: body.priceId, | ||
// quantity: 1, | ||
// }, | ||
// ], | ||
// mode: "subscription", | ||
// success_url: `${origin}/dashboard`, | ||
// cancel_url: `${origin}`, | ||
// }); | ||
// if (session.url === null) | ||
// return NextResponse.json({ message: "No session url" }, { status: 500 }); | ||
// return NextResponse.json({ url: session.url }, { status: 200 }); | ||
// } catch (error) { | ||
// if (error instanceof Stripe.errors.StripeError) { | ||
// const { message } = error; | ||
// return NextResponse.json({ message }, { status: error.statusCode }); | ||
// } | ||
// return NextResponse.json({ error }, { status: 500 }); | ||
// } | ||
} |
This file contains 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,13 @@ | ||
export async function GET() { | ||
// const { sessionClaims } = auth(); | ||
// const stripeCustomerId = getStripeCustomerId(sessionClaims); | ||
// const subscription = await stripe.subscriptions.list({ | ||
// customer: stripeCustomerId, | ||
// status: "active", | ||
// }); | ||
// const subscribedProductId = subscription.data[0]?.items?.data[0].plan.product; | ||
// if (!subscribedProductId || subscribedProductId === null) | ||
// return NextResponse.json({ message: "No product found" }, { status: 500 }); | ||
// const product = await stripe.products.retrieve(subscribedProductId as string); | ||
// return NextResponse.json(product); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,67 +1,66 @@ | ||
import { clerkClient } from "@clerk/nextjs"; | ||
import { stripe } from "./stripe"; | ||
// import { stripe } from "./stripe"; | ||
|
||
export const findOrCreateCustomerId = async ({ | ||
clerkUserId, | ||
clerkOrgId, | ||
}: { | ||
clerkUserId: string; | ||
clerkOrgId?: string; | ||
}) => { | ||
let stripeCustomerId; | ||
// export const findOrCreateCustomerId = async ({ | ||
// clerkUserId, | ||
// clerkOrgId, | ||
// }: { | ||
// clerkUserId: string; | ||
// clerkOrgId?: string; | ||
// }) => { | ||
// let stripeCustomerId; | ||
|
||
if (clerkOrgId && clerkUserId) { | ||
const user = await clerkClient.users.getUser(clerkUserId); | ||
let organization = await clerkClient.organizations.getOrganization({ | ||
organizationId: clerkOrgId, | ||
}); | ||
// if user already has a stripeCustomerId, return it | ||
if (organization.publicMetadata?.stripeCustomerId) { | ||
return organization.publicMetadata.stripeCustomerId as string; | ||
} | ||
// otherwise, create a new customer and save the stripeCustomerId | ||
const createdCustomer = await stripe.customers.create({ | ||
name: `${organization.name}`, | ||
email: user.emailAddresses.find( | ||
(x) => x.id === user.primaryEmailAddressId | ||
)!.emailAddress, | ||
metadata: { | ||
orgId: user.id, | ||
}, | ||
}); | ||
// update the organization's publicMetadata with the stripeCustomerId | ||
organization = await clerkClient.organizations.updateOrganization( | ||
organization.id, | ||
{ | ||
publicMetadata: { | ||
stripeCustomerId: createdCustomer.id, | ||
}, | ||
} | ||
); | ||
stripeCustomerId = createdCustomer.id; | ||
} | ||
// if (clerkOrgId && clerkUserId) { | ||
// const user = await clerkClient.users.getUser(clerkUserId); | ||
// let organization = await clerkClient.organizations.getOrganization({ | ||
// organizationId: clerkOrgId, | ||
// }); | ||
// // if user already has a stripeCustomerId, return it | ||
// if (organization.publicMetadata?.stripeCustomerId) { | ||
// return organization.publicMetadata.stripeCustomerId as string; | ||
// } | ||
// // otherwise, create a new customer and save the stripeCustomerId | ||
// const createdCustomer = await stripe.customers.create({ | ||
// name: `${organization.name}`, | ||
// email: user.emailAddresses.find( | ||
// (x) => x.id === user.primaryEmailAddressId | ||
// )!.emailAddress, | ||
// metadata: { | ||
// orgId: user.id, | ||
// }, | ||
// }); | ||
// // update the organization's publicMetadata with the stripeCustomerId | ||
// organization = await clerkClient.organizations.updateOrganization( | ||
// organization.id, | ||
// { | ||
// publicMetadata: { | ||
// stripeCustomerId: createdCustomer.id, | ||
// }, | ||
// } | ||
// ); | ||
// stripeCustomerId = createdCustomer.id; | ||
// } | ||
|
||
let user = await clerkClient.users.getUser(clerkUserId); | ||
// if user already has a stripeCustomerId, return it | ||
if (user.publicMetadata.stripeCustomerId) { | ||
return user.publicMetadata.stripeCustomerId as string; | ||
} | ||
// otherwise, create a new customer and save the stripeCustomerId | ||
const createdCustomer = await stripe.customers.create({ | ||
name: `${user.firstName} ${user.lastName}`, | ||
email: user.emailAddresses.find((x) => x.id === user.primaryEmailAddressId)! | ||
.emailAddress, | ||
metadata: { | ||
userId: user.id, | ||
}, | ||
}); | ||
// update the user's publicMetadata with the stripeCustomerId | ||
user = await clerkClient.users.updateUser(user.id, { | ||
publicMetadata: { | ||
stripeCustomerId: createdCustomer.id, | ||
}, | ||
}); | ||
stripeCustomerId = createdCustomer.id; | ||
// let user = await clerkClient.users.getUser(clerkUserId); | ||
// // if user already has a stripeCustomerId, return it | ||
// if (user.publicMetadata.stripeCustomerId) { | ||
// return user.publicMetadata.stripeCustomerId as string; | ||
// } | ||
// // otherwise, create a new customer and save the stripeCustomerId | ||
// const createdCustomer = await stripe.customers.create({ | ||
// name: `${user.firstName} ${user.lastName}`, | ||
// email: user.emailAddresses.find((x) => x.id === user.primaryEmailAddressId)! | ||
// .emailAddress, | ||
// metadata: { | ||
// userId: user.id, | ||
// }, | ||
// }); | ||
// // update the user's publicMetadata with the stripeCustomerId | ||
// user = await clerkClient.users.updateUser(user.id, { | ||
// publicMetadata: { | ||
// stripeCustomerId: createdCustomer.id, | ||
// }, | ||
// }); | ||
// stripeCustomerId = createdCustomer.id; | ||
|
||
return stripeCustomerId; | ||
}; | ||
// return stripeCustomerId; | ||
// }; |
Oops, something went wrong.
5f007c3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
hubone – ./
hubone-git-main-ihsen.vercel.app
*.huboneapp.com
huboneapp.com
hubone-ihsen.vercel.app
hubone.vercel.app