-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrated Stripe successfully in test mode
- Loading branch information
1 parent
c718971
commit bea4749
Showing
21 changed files
with
310 additions
and
43 deletions.
There are no files selected for viewing
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,7 @@ | ||
import * as z from "zod"; | ||
|
||
export const formSchema = z.object({ | ||
prompt: z.string().min(1, { | ||
message: "Prompt is required." | ||
}), | ||
}); |
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,31 @@ | ||
import { Settings } from "lucide-react"; | ||
|
||
import { Heading } from "@/components/heading"; | ||
import { SubscriptionButton } from "@/components/subscription-button"; | ||
import { checkSubscription } from "@/lib/subscription"; | ||
|
||
const SettingsPage = async () => { | ||
const isPro = await checkSubscription(); | ||
|
||
return ( | ||
<div> | ||
<Heading | ||
title="Settings" | ||
description="Manage account settings." | ||
icon={Settings} | ||
iconColor="text-gray-700" | ||
bgColor="bg-gray-700/1 | ||
0" | ||
/> | ||
<div className="px-4 lg:px-8 space-y-4"> | ||
<div className="text-muted-foreground text-sm"> | ||
{isPro ? "You are currently on a Pro plan." : "You are currently on a free plan."} | ||
</div> | ||
<SubscriptionButton isPro={isPro} /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default SettingsPage; | ||
|
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: "INR", | ||
product_data: { | ||
name: "Genius Pro", | ||
description: "Unlimited AI Generations" | ||
}, | ||
unit_amount: 200000, | ||
recurring: { | ||
interval: "month" | ||
} | ||
}, | ||
quantity: 1, | ||
}, | ||
], | ||
metadata: { | ||
userId, | ||
}, | ||
}) | ||
|
||
return new NextResponse(JSON.stringify({ url: stripeSession.url })) | ||
} catch (error) { | ||
console.log("[STRIPE_ERROR]", 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
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 Stripe from "stripe" | ||
import { headers } from "next/headers" | ||
import { NextResponse } from "next/server" | ||
|
||
import prismadb from "@/lib/prismadb" | ||
import { stripe } from "@/lib/stripe" | ||
|
||
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 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 | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
if (event.type === "invoice.payment_succeeded") { | ||
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 | ||
), | ||
}, | ||
}) | ||
} | ||
|
||
return new NextResponse(null, { status: 200 }) | ||
}; |
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
Oops, something went wrong.