Skip to content

Commit e1989b4

Browse files
committed
Added the Stripe(Payment Gateway) now the Users need to pay to view any course and if He is owner/CourseCreator then he has free Access!
1 parent 782297c commit e1989b4

File tree

15 files changed

+437
-126
lines changed

15 files changed

+437
-126
lines changed

.env.example

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ GROQ_API_KEY = ""
55
AWS_ACCESS_KEY_ID=""
66
AWS_SECRET_ACCESS_KEY=""
77
BUCKET_NAME= ""
8-
ENDPOINT ="https://...com"
8+
ENDPOINT ="https://...com"
9+
STRIPE_SECRET_KEY = ""
10+
NEXT_PUBLIC_STRIPE_KEY=""
11+
NEXT_PUBLIC_BASE_URL = "http://localhost:3000"

package-lock.json

+37-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@radix-ui/react-slot": "^1.1.1",
2828
"@radix-ui/react-tabs": "^1.1.2",
2929
"@radix-ui/react-tooltip": "^1.1.5",
30+
"@stripe/stripe-js": "^5.4.0",
3031
"@tabler/icons-react": "^3.26.0",
3132
"@types/bcryptjs": "^2.4.6",
3233
"axios": "^1.7.9",
@@ -44,6 +45,7 @@
4445
"react-dom": "^18.0.0",
4546
"react-dropzone": "^14.3.5",
4647
"react-hot-toast": "^2.4.1",
48+
"stripe": "^17.5.0",
4749
"tailwind-merge": "^2.5.5",
4850
"tailwindcss-animate": "^1.0.7",
4951
"zod": "^3.24.1"
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import prisma from '@/lib/prisma';
2+
import { NextRequest, NextResponse } from 'next/server';
3+
4+
export async function GET(req: NextRequest) {
5+
const userId = req.headers.get('X-User-Id');
6+
const courseId = req.headers.get('X-Course-Id');
7+
8+
if (!userId || !courseId) {
9+
return NextResponse.json({ error: 'Missing required headers' }, { status: 400 });
10+
}
11+
12+
try {
13+
const course = await prisma.course.findUnique({
14+
where: { course_id: parseInt(courseId) },
15+
include: { creator: true }
16+
});
17+
18+
if (!course) {
19+
return NextResponse.json({ error: 'Course not found' }, { status: 404 });
20+
}
21+
22+
if (course.creator_id === userId) {
23+
return NextResponse.json({ isCreator: true, hasPurchased: false });
24+
}
25+
26+
const purchase = await prisma.coursePurchase.findFirst({
27+
where: {
28+
user_id: userId,
29+
course_id: parseInt(courseId),
30+
payment_status: true
31+
}
32+
});
33+
34+
return NextResponse.json({
35+
isCreator: false,
36+
hasPurchased: Boolean(purchase)
37+
});
38+
} catch (error) {
39+
return NextResponse.json({ error: 'Server error' }, { status: 500 });
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { NextRequest, NextResponse } from 'next/server';
2+
import Stripe from 'stripe';
3+
4+
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
5+
6+
export async function POST(req: NextRequest) {
7+
try {
8+
const { courseId, userId, price, title } = await req.json();
9+
10+
const session = await stripe.checkout.sessions.create({
11+
payment_method_types: ['card'],
12+
line_items: [
13+
{
14+
price_data: {
15+
currency: 'usd',
16+
product_data: {
17+
name: title,
18+
},
19+
unit_amount: Math.round(price * 100),
20+
},
21+
quantity: 1,
22+
},
23+
],
24+
mode: 'payment',
25+
success_url: `${process.env.NEXT_PUBLIC_BASE_URL}/api/payment/success?session_id={CHECKOUT_SESSION_ID}&courseId=${courseId}&userId=${userId}`,
26+
cancel_url: `${process.env.NEXT_PUBLIC_BASE_URL}/dashboard`,
27+
});
28+
29+
return NextResponse.json({ sessionId: session.id });
30+
} catch (error) {
31+
return NextResponse.json({ error: 'Error creating checkout session' }, { status: 500 });
32+
}
33+
}

0 commit comments

Comments
 (0)