sakshstore / saksh-easy-wallet

0 stars 0 forks source link

Here's a practical example demonstrating how to use the SakshWallet class in a Node.js application: #1

Open sakshstore opened 1 month ago

sakshstore commented 1 month ago

Example Usage

Here's a practical example demonstrating how to use the SakshWallet class in a Node.js application:

const mongoose = require('mongoose');
const SakshWallet = require('saksh-easy-wallet');

// Example usage
(async () => {
    try {
        const userEmail = 'user@example.com';

        // Connect to the database
        await mongoose.connect('mongodb://localhost:27017/saksh2323Cart', {
            useNewUrlParser: true,
            useUnifiedTopology: true,
        });
        console.log('Database connected');

        const wallet = new SakshWallet();

        // Set admin email (optional)
        wallet.setAdmin('admin@example.com');

        // Credit an amount to the user's wallet
        console.log(await wallet.sakshCredit(userEmail, 500, 'USD', 'ref123', 'Salary payment')); // Credited 500 USD. New balance is 500

        // Debit an amount from the user's wallet
        console.log(await wallet.sakshDebit(userEmail, 200, 'USD', 'ref124', 'Grocery shopping', 5)); // Debited 200 USD. New balance is 295

        // Get balance for a user
        const balance = await wallet.sakshGetBalance(userEmail, 'USD');
        console.log('User Balance:', balance); // User Balance: { email: 'user@example.com', currency: 'USD', balance: 295 }

        // Get balance summary for a user
        const balanceSummary = await wallet.sakshGetBalanceSummary(userEmail);
        console.log('Balance Summary:', balanceSummary); // Balance Summary: { email: 'user@example.com', balance: { 'USD': 295 } }

        // Close the MongoDB connection
        await mongoose.connection.close();
    } catch (error) {
        console.error('Error:', error);
    }
})();

Explanation

Make sure to replace the database connection string and user email with your actual values as needed. This example provides a clear demonstration of how to utilize the Saksh Easy Wallet in your application.

sakshstore commented 1 month ago
const mongoose = require('mongoose');
const SakshWallet = require('saksh-easy-wallet');

// Example usage with events
(async () => {
    try {
        const userEmail = 'user@example.com';

        // Connect to the database
        await mongoose.connect('mongodb://localhost:27017/saksh2323Cart', {
            useNewUrlParser: true,
            useUnifiedTopology: true,
        });
        console.log('Database connected');

        const wallet = new SakshWallet();

        // Set admin email (optional)
        wallet.setAdmin('admin@example.com');

        // Event listeners
        wallet.on('credit', (data) => {
            console.log(`Credit Event: ${data.amount} ${data.currency} credited to ${data.email}. New balance: ${data.newBalance}`);
        });

        wallet.on('debit', (data) => {
            console.log(`Debit Event: ${data.amount} ${data.currency} debited from ${data.email}. New balance: ${data.newBalance}`);
        });

        // Credit an amount to the user's wallet
        console.log(await wallet.sakshCredit(userEmail, 500, 'USD', 'ref123', 'Salary payment')); // Credited 500 USD. New balance is 500

        // Debit an amount from the user's wallet
        console.log(await wallet.sakshDebit(userEmail, 200, 'USD', 'ref124', 'Grocery shopping', 5)); // Debited 200 USD. New balance is 295

        // Get balance for a user
        const balance = await wallet.sakshGetBalance(userEmail, 'USD');
        console.log('User Balance:', balance); // User Balance: { email: 'user@example.com', currency: 'USD', balance: 295 }

        // Get balance summary for a user
        const balanceSummary = await wallet.sakshGetBalanceSummary(userEmail);
        console.log('Balance Summary:', balanceSummary); // Balance Summary: { email: 'user@example.com', balance: { 'USD': 295 } }

        // Close the MongoDB connection
        await mongoose.connection.close();
    } catch (error) {
        console.error('Error:', error);
    }
})();

Explanation: