UNPKG

saksh-wallet

Version:

A Node.js library for managing user wallets, including functionalities for crediting, debiting, and converting currencies, as well as retrieving balances and transaction reports.

143 lines (120 loc) 5.3 kB
const EventEmitter = require('events'); const RecurringPayment = require('./models/RecurringPayment'); class SakshRecurringPayments extends EventEmitter { constructor(recurringPaymentModel = RecurringPayment) { super(); this.RecurringPayment = recurringPaymentModel; } /** * Create a new recurring payment. * @param {String} userId - ID of the user making the payment. * @param {String} toUserId - ID of the recipient user. * @param {Number} amount - Amount of the payment. * @param {String} currency - Currency of the payment. * @param {String} description - Description of the payment. * @param {String} referenceNumber - Reference number for the payment. * @param {String} interval - Interval of the payment (daily, weekly, monthly). * @param {Function} [feeCallback=null] - Optional callback for calculating fees. * @returns {Object} - The created recurring payment. */ async sakshCreateRecurringPayment(userId, toUserId, amount, currency, description, referenceNumber, interval, feeCallback = null) { // Calculate the next payment date based on the interval const nextPaymentDate = this.sakshCalculateNextPaymentDate(interval); // Create a new recurring payment document const recurringPayment = new this.RecurringPayment({ userId, toUserId, amount, currency, description, referenceNumber, interval, nextPaymentDate, feeCallback }); // Save the recurring payment to the database await recurringPayment.save(); // Emit an event indicating that a recurring payment has been created this.emit('recurringPaymentCreated', recurringPayment); return recurringPayment; } /** * Update an existing recurring payment. * @param {String} paymentId - ID of the recurring payment to update. * @param {Object} updates - Updates to apply to the recurring payment. * @returns {Object} - The updated recurring payment. */ async sakshUpdateRecurringPayment(paymentId, updates) { // Find the recurring payment by ID const recurringPayment = await this.RecurringPayment.findById(paymentId); if (!recurringPayment) { throw new Error('Recurring payment not found'); } // Apply the updates to the recurring payment Object.assign(recurringPayment, updates); // Save the updated recurring payment to the database await recurringPayment.save(); // Emit an event indicating that a recurring payment has been updated this.emit('recurringPaymentUpdated', recurringPayment); return recurringPayment; } /** * Delete a recurring payment. * @param {String} paymentId - ID of the recurring payment to delete. */ async sakshDeleteRecurringPayment(paymentId) { // Find and delete the recurring payment by ID const recurringPayment = await this.RecurringPayment.findByIdAndDelete(paymentId); if (recurringPayment) { // Emit an event indicating that a recurring payment has been deleted this.emit('recurringPaymentDeleted', recurringPayment); } } /** * Calculate the next payment date based on the interval. * @param {String} interval - Interval of the payment (daily, weekly, monthly). * @returns {Date} - The next payment date. */ sakshCalculateNextPaymentDate(interval) { const now = new Date(); switch (interval) { case 'daily': return new Date(now.setDate(now.getDate() + 1)); case 'weekly': return new Date(now.setDate(now.getDate() + 7)); case 'monthly': return new Date(now.setMonth(now.getMonth() + 1)); default: throw new Error('Invalid interval'); } } /** * Process due payments. */ async sakshProcessDuePayments() { const now = new Date(); // Find all recurring payments that are due const duePayments = await this.RecurringPayment.find({ nextPaymentDate: { $lte: now } }); for (const payment of duePayments) { try { // Transfer funds for the due payment await this.transactionManagement.sakshTransferFunds( payment.userId, payment.toUserId, payment.amount, payment.currency, payment.description, payment.referenceNumber, 'recurring' ); // Update the next payment date payment.nextPaymentDate = this.sakshCalculateNextPaymentDate(payment.interval); // Save the updated payment to the database await payment.save(); } catch (error) { console.error(`Failed to process recurring payment: ${error.message}`); } } } } module.exports = SakshRecurringPayments;