E-commerce Project - Part 3
Finish with payments, reviews and admin panel.
Stripe Integration
typescript
// src/modules/payments/payment.service.tsimport Stripe from 'stripe'; const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); export class PaymentService { async createCheckout(orderId: string, userId: string) { const order = await prisma.order.findFirst({ where: { id: orderId, userId, status: 'PENDING' }, include: { items: true }, }); if (!order) throw new AppError('Order not found', 404); const session = await stripe.checkout.sessions.create({ payment_method_types: ['card'], mode: 'payment', client_reference_id: order.id, line_items: order.items.map(item => ({ price_data: { currency: 'usd', product_data: { name: item.name }, unit_amount: Math.round(Number(item.price) * 100), }, quantity: item.quantity, })), success_url: `${process.env.FRONTEND_URL}/orders/${order.id}?success=true`, cancel_url: `${process.env.FRONTEND_URL}/orders/${order.id}?canceled=true`, }); return { url: session.url }; } async handleWebhook(payload: Buffer, signature: string) { const event = stripe.webhooks.constructEvent( payload, signature, process.env.STRIPE_WEBHOOK_SECRET! ); if (event.type === 'checkout.session.completed') { const session = event.data.object; await prisma.order.update({ where: { id: session.client_reference_id }, data: { status: 'PAID' }, }); } }}Review Service
typescript
export class ReviewService { async create(data: CreateReviewDTO) { const hasPurchased = await prisma.orderItem.findFirst({ where: { productId: data.productId, order: { userId: data.userId, status: { in: ['PAID', 'DELIVERED'] } }, }, }); if (!hasPurchased) { throw new AppError('You must purchase to review', 403); } return prisma.review.create({ data }); }}Admin Dashboard
typescript
export class AdminService { async getDashboard() { const [users, products, orders, revenue] = await Promise.all([ prisma.user.count(), prisma.product.count({ where: { active: true } }), prisma.order.count(), prisma.order.aggregate({ where: { status: { in: ['PAID', 'DELIVERED'] } }, _sum: { total: true }, }), ]); return { stats: { users, products, orders, revenue: revenue._sum.total || 0 }, }; }}Project Complete!
Congratulations! You built a complete e-commerce API with:
- ✅ JWT Authentication
- ✅ Product CRUD with filters
- ✅ Shopping cart
- ✅ Order system
- ✅ Stripe payments
- ✅ Product reviews
- ✅ Admin panel
- ✅ Redis cache
- ✅ Automated tests
- ✅ Swagger docs
- ✅ Docker deploy
Next Steps
- Add Elasticsearch for full-text search
- Implement push notifications
- Create wishlist system
- Add loyalty program
🎉 Take the final quiz to get your certificate!