DEV Community

Susheel kumar
Susheel kumar

Posted on

Saksh Cart Library: Your All-in-One Solution for E-Commerce Management

Saksh Cart Library Features

Shopping Cart Management

  • Add items to the cart.
  • View items in the cart.
  • Remove items from the cart.
  • Update item quantities.

Order Management

  • Create and manage orders.
  • Track order status.
  • View order history.

Checkout Process

  • Process checkout with various payment methods (e.g., credit card, check).
  • Apply discounts and coupons during checkout.
  • Generate payment confirmations.

Coupon Management

  • Create and manage discount coupons.
  • Apply coupons to orders.
  • Track coupon usage and validity.

Reporting

  • Generate sales reports.
  • Track the number of orders and refunds.
  • Analyze sales by user.
  • Identify high return products.

Refund Management

  • Process refunds for orders.
  • Track refund status and history.

Instant Payment Notification (IPN)

  • Handle IPN notifications for payment updates.
  • Update order status based on IPN notifications.

Wishlist Management

  • Add items to the wishlist.
  • View and manage wishlist items.
  • Remove items from the wishlist.

These features make the saksh-cart library a comprehensive solution for managing an e-commerce platform, covering everything from cart operations to reporting and refund management. If you have any specific questions about these features or need further details, feel free to ask!



const {
    sakshShoppingCart,
    sakshOrderManagement,
    sakshCheckout,
    sakshCouponManagement,
    sakshReporting,
    sakshRefundManagement,
    sakshIPN,
    sakshWishlistManagement,
} = require('saksh-cart');

const mongoose = require('mongoose');

async function main() {
    try {
        // Connect to MongoDB
        await mongoose.connect('mongodb://localhost:27017/shopping_cart', {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            serverSelectionTimeoutMS: 5000, // Add a timeout for server selection
        });

        const cart = new sakshShoppingCart();
        const orderManagement = new sakshOrderManagement();
        const checkout = new sakshCheckout();
        const couponManagement = new sakshCouponManagement();
        const reporting = new sakshReporting();
        const refundManagement = new sakshRefundManagement();
        const ipn = new sakshIPN();
        const wishlist = new sakshWishlistManagement();
        const userId = 'user123';
        const item = { id: 'item1', name: 'Laptop', price: 1000, quantity: 1 };

        // Add item to cart
        await cart.sakshAddToCart(userId, item);

        // View cart
        const userCart = await cart.sakshViewCart(userId);
        console.log('Cart:', userCart);

        // Add item to wishlist
        await wishlist.sakshAddToWishlist(userId, { id: 'item2', name: 'Smartphone', price: 500 });

        // View wishlist
        const userWishlist = await wishlist.sakshViewWishlist(userId);
        console.log('Wishlist:', userWishlist);

        // Remove item from wishlist
        await wishlist.sakshRemoveFromWishlist(userId, 'item2');

        // View updated wishlist
        const updatedWishlist = await wishlist.sakshViewWishlist(userId);
        console.log('Updated Wishlist:', updatedWishlist);

        // Create a coupon
        const coupon = await couponManagement.sakshCreateCoupon('DISCOUNT10', 10, new Date('2024-12-31'));
        console.log('Coupon:', coupon);

        // Checkout with check payment
        try {
            const checkNumber = 'CHK123456';
            const { order, paymentConfirmation } = await checkout.sakshProcessCheckoutWithCheck(userId, 'DISCOUNT10', checkNumber);
            console.log('Order:', order);
            console.log('Payment Confirmation:', paymentConfirmation);
        } catch (error) {
            console.error('Checkout error:', error.message);
        }

        // Simulate IPN notification
        const notification = {
            orderId: 'order123',
            status: 'COMPLETED',
            transactionId: 'txn123456',
        };

        try {
            const updatedOrder = await ipn.sakshProcessNotification(notification);
            console.log('Updated Order:', updatedOrder);
        } catch (error) {
            console.error('IPN processing error:', error.message);
        }

        // Generate reports
        const totalSales = await reporting.sakshGetTotalSales();
        console.log('Total Sales:', totalSales);

        const numberOfOrders = await reporting.sakshGetNumberOfOrders();
        console.log('Number of Orders:', numberOfOrders);

        const salesByUser = await reporting.sakshGetSalesByUser(userId);
        console.log(`Sales by ${userId}:`, salesByUser);

        const shippedOrders = await reporting.sakshGetOrdersByStatus('Shipped');
        console.log('Shipped Orders:', shippedOrders);

        const totalRefunds = await reporting.sakshGetTotalRefunds();
        console.log('Total Refunds:', totalRefunds);

        const numberOfRefunds = await reporting.sakshGetNumberOfRefunds();
        console.log('Number of Refunds:', numberOfRefunds);

        const refundsByUser = await reporting.sakshGetRefundsByUser(userId);
        console.log(`Refunds by ${userId}:`, refundsByUser);

        const highReturnProducts = await reporting.sakshGetHighReturnProducts();
        console.log('High Return Products:', highReturnProducts);
    } catch (error) {
        console.error('Main function error:', error.message);
    } finally {
        // Close the MongoDB connection
        mongoose.connection.close();
    }
}

main();

Enter fullscreen mode Exit fullscreen mode

with Express js


const express = require('express');
const {
    sakshShoppingCart,
    sakshOrderManagement,
    sakshCheckout,
    sakshCouponManagement,
    sakshReporting,
    sakshRefundManagement,
    sakshIPN,
    sakshWishlistManagement,
} = require('saksh-cart');

const router = express.Router();

const cart = new sakshShoppingCart();
const orderManagement = new sakshOrderManagement();
const checkout = new sakshCheckout();
const couponManagement = new sakshCouponManagement();
const reporting = new sakshReporting();
const refundManagement = new sakshRefundManagement();
const ipn = new sakshIPN();
const wishlist = new sakshWishlistManagement();

const userId = 'user123'; // Example user ID

// Add item to cart
router.post('/cart', async (req, res) => {
    const item = req.body;
    try {
        await cart.sakshAddToCart(userId, item);
        res.status(200).send('Item added to cart');
    } catch (error) {
        res.status(500).send(error.message);
    }
});

// View cart
router.get('/cart', async (req, res) => {
    try {
        const userCart = await cart.sakshViewCart(userId);
        res.status(200).json(userCart);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

// Add item to wishlist
router.post('/wishlist', async (req, res) => {
    const item = req.body;
    try {
        await wishlist.sakshAddToWishlist(userId, item);
        res.status(200).send('Item added to wishlist');
    } catch (error) {
        res.status(500).send(error.message);
    }
});

// View wishlist
router.get('/wishlist', async (req, res) => {
    try {
        const userWishlist = await wishlist.sakshViewWishlist(userId);
        res.status(200).json(userWishlist);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

// Remove item from wishlist
router.delete('/wishlist/:itemId', async (req, res) => {
    const itemId = req.params.itemId;
    try {
        await wishlist.sakshRemoveFromWishlist(userId, itemId);
        res.status(200).send('Item removed from wishlist');
    } catch (error) {
        res.status(500).send(error.message);
    }
});

// Create a coupon
router.post('/coupon', async (req, res) => {
    const { code, discount, expiryDate } = req.body;
    try {
        const coupon = await couponManagement.sakshCreateCoupon(code, discount, new Date(expiryDate));
        res.status(200).json(coupon);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

// Checkout with check payment
router.post('/checkout', async (req, res) => {
    const { couponCode, checkNumber } = req.body;
    try {
        const { order, paymentConfirmation } = await checkout.sakshProcessCheckoutWithCheck(userId, couponCode, checkNumber);
        res.status(200).json({ order, paymentConfirmation });
    } catch (error) {
        res.status(500).send(error.message);
    }
});

// Process IPN notification
router.post('/ipn', async (req, res) => {
    const notification = req.body;
    try {
        const updatedOrder = await ipn.sakshProcessNotification(notification);
        res.status(200).json(updatedOrder);
    } catch (error) {
        res.status(500).send(error.message);
    }
});

// Generate reports
router.get('/reports', async (req, res) => {
    try {
        const totalSales = await reporting.sakshGetTotalSales();
        const numberOfOrders = await reporting.sakshGetNumberOfOrders();
        const salesByUser = await reporting.sakshGetSalesByUser(userId);
        const shippedOrders = await reporting.sakshGetOrdersByStatus('Shipped');
        const totalRefunds = await reporting.sakshGetTotalRefunds();
        const numberOfRefunds = await reporting.sakshGetNumberOfRefunds();
        const refundsByUser = await reporting.sakshGetRefundsByUser(userId);
        const highReturnProducts = await reporting.sakshGetHighReturnProducts();

        res.status(200).json({
            totalSales,
            numberOfOrders,
            salesByUser,
            shippedOrders,
            totalRefunds,
            numberOfRefunds,
            refundsByUser,
            highReturnProducts,
        });
    } catch (error) {
        res.status(500).send(error.message);
    }
});

module.exports = sakshRouter;

Enter fullscreen mode Exit fullscreen mode

You can include all these in your server.js



const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const sakshRoutes = require('saksh-cart');

const app = express();
app.use(bodyParser.json());
app.use('/api', sakshRoutes );  //this is the line which you need to copy paste

// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/shopping_cart', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});


Enter fullscreen mode Exit fullscreen mode

Top comments (1)