DEV Community

Susheel kumar
Susheel kumar

Posted on

Recurring payments functionality in the Saksh-Wallet system.

Sure! Here’s a cleaned-up version of the code for the recurring payments functionality in the SakshWallet system. I've removed unnecessary comments and formatted it for better readability.

Recurring Payments Functionality

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

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

const wallet = new SakshWallet();

// Listen for events
wallet.recurringPayments.on('recurringPaymentCreated', (payment) => {
    console.log('Recurring payment created:', payment);
});

wallet.recurringPayments.on('recurringPaymentUpdated', (payment) => {
    console.log('Recurring payment updated:', payment);
});

wallet.recurringPayments.on('recurringPaymentDeleted', (payment) => {
    console.log('Recurring payment deleted:', payment);
});

async function main() {
    try {
        // Create a recurring payment
        const payment = await wallet.sakshCreateRecurringPayment(
            'user1', // userId
            'user2', // toUserId
            100,     // amount
            'USD',   // currency
            'Monthly subscription', // description
            'REF123', // referenceNumber
            'monthly' // interval
        );
        console.log('Created Recurring Payment:', payment);

        // Update the recurring payment
        const updatedPayment = await wallet.sakshUpdateRecurringPayment(payment._id, {
            amount: 150
        });
        console.log('Updated Recurring Payment:', updatedPayment);

        // Delete the recurring payment
        await wallet.sakshDeleteRecurringPayment(payment._id);
        console.log('Deleted Recurring Payment:', payment._id);

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

main();
Enter fullscreen mode Exit fullscreen mode

Processing Recurring Payments

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

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

const wallet = new SakshWallet();

// Schedule the cron job to run every minute
cron.schedule('* * * * *', async () => {
    await wallet.sakshProcessDuePayments();
});

// Run the cron script using Node.js
// Command: node processRecurringPayments.js
Enter fullscreen mode Exit fullscreen mode

Methods in SakshRecurringPayments Class

  • sakshCreateRecurringPayment: Creates a new recurring payment with the specified details.
  • sakshUpdateRecurringPayment: Updates an existing recurring payment with new details.
  • sakshDeleteRecurringPayment: Deletes a recurring payment by its ID.
  • sakshCalculateNextPaymentDate: Calculates the next payment date based on the specified interval.
  • sakshProcessDuePayments: Processes all due payments and updates their next payment dates.

This cleaned-up version should be easier to read and understand. If you have any further questions or need additional modifications, feel free to ask!

Top comments (0)