generated from pinecone-io/pinecone-vercel-starter
-
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.
finish first version with membership and Stripegit add .
- Loading branch information
Showing
20 changed files
with
239 additions
and
84 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { auth, currentUser } from "@clerk/nextjs"; | ||
import { NextResponse } from "next/server"; | ||
|
||
import prismadb from "../../lib/prismadb"; | ||
import { stripe } from "../../lib/stripe"; | ||
import { absoluteUrl } from "@/lib/utils"; | ||
|
||
const settingsUrl = absoluteUrl("/settings"); | ||
|
||
export async function GET() { | ||
try { | ||
const { userId } = auth(); | ||
const user = await currentUser(); | ||
|
||
if (!userId || !user) { | ||
return new NextResponse("Unauthorized", { status: 401 }); | ||
} | ||
|
||
const userSubscription = await prismadb.userSubscription.findUnique({ | ||
where: { | ||
userId | ||
} | ||
}) | ||
|
||
if (userSubscription && userSubscription.stripeCustomerId) { | ||
const stripeSession = await stripe.billingPortal.sessions.create({ | ||
customer: userSubscription.stripeCustomerId, | ||
return_url: settingsUrl, | ||
}) | ||
|
||
return new NextResponse(JSON.stringify({ url: stripeSession.url })) | ||
} | ||
|
||
const stripeSession = await stripe.checkout.sessions.create({ | ||
success_url: settingsUrl, | ||
cancel_url: settingsUrl, | ||
payment_method_types: ["card"], | ||
mode: "subscription", | ||
billing_address_collection: "auto", | ||
customer_email: user.emailAddresses[0].emailAddress, | ||
line_items: [ | ||
{ | ||
price_data: { | ||
currency: "HKD", | ||
product_data: { | ||
name: "FastLegal Pro", | ||
description: "Supercharge your legal research" | ||
}, | ||
unit_amount: 150000, | ||
recurring: { | ||
interval: "month" | ||
} | ||
}, | ||
quantity: 1, | ||
}, | ||
], | ||
metadata: { | ||
userId, | ||
}, | ||
}) | ||
|
||
return new NextResponse(JSON.stringify({ url: stripeSession.url })) | ||
} catch (error) { | ||
console.log("[STRIPE]", error); | ||
return new NextResponse("Internal 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,83 @@ | ||
import Stripe from "stripe" | ||
import { headers } from "next/headers" | ||
import { NextResponse } from "next/server" | ||
|
||
import prismadb from "@/lib/prismadb" | ||
import { stripe } from "@/lib/stripe" | ||
|
||
import { incrementSearchCredit } from '@/lib/searchCredits'; | ||
|
||
export async function POST(req: Request) { | ||
const body = await req.text() | ||
const signature = headers().get("Stripe-Signature") as string | ||
|
||
let event: Stripe.Event | ||
|
||
try { | ||
event = stripe.webhooks.constructEvent( | ||
body, | ||
signature, | ||
process.env.STRIPE_WEBHOOK_SECRET! | ||
) | ||
} catch (error: any) { | ||
return new NextResponse(`Webhook Error: ${error.message}`, { status: 400 }) | ||
} | ||
|
||
const session = event.data.object as Stripe.Checkout.Session | ||
|
||
if (event.type === "checkout.session.completed") { | ||
const userId = session?.metadata?.userId; | ||
if (!userId) { | ||
return new NextResponse("User id is required", { status: 400 }); | ||
} | ||
const subscription = await stripe.subscriptions.retrieve( | ||
session.subscription as string | ||
) | ||
|
||
if (!session?.metadata?.userId) { | ||
return new NextResponse("User id is required", { status: 400 }); | ||
} | ||
|
||
await prismadb.userSubscription.create({ | ||
data: { | ||
userId: session?.metadata?.userId, | ||
stripeSubscriptionId: subscription.id, | ||
stripeCustomerId: subscription.customer as string, | ||
stripePriceId: subscription.items.data[0].price.id, | ||
stripeCurrentPeriodEnd: new Date( | ||
subscription.current_period_end * 1000 | ||
), | ||
}, | ||
}) | ||
|
||
await incrementSearchCredit(userId, 50) | ||
|
||
} | ||
|
||
if (event.type === "invoice.payment_succeeded") { | ||
const userId = session?.metadata?.userId; | ||
if (!userId) { | ||
return new NextResponse("User id is required", { status: 400 }); | ||
} | ||
const subscription = await stripe.subscriptions.retrieve( | ||
session.subscription as string | ||
) | ||
|
||
await prismadb.userSubscription.update({ | ||
where: { | ||
stripeSubscriptionId: subscription.id, | ||
}, | ||
data: { | ||
stripePriceId: subscription.items.data[0].price.id, | ||
stripeCurrentPeriodEnd: new Date( | ||
subscription.current_period_end * 1000 | ||
), | ||
}, | ||
}) | ||
|
||
await incrementSearchCredit(userId, 50) | ||
|
||
} | ||
|
||
return new NextResponse(null, { status: 200 }) | ||
}; |
Oops, something went wrong.