From 2b78e996b1898f7d6414d6b17d0699ce6f980600 Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Tue, 3 Dec 2024 22:43:01 +0700 Subject: [PATCH] Create paymentPlan.js --- src/payments/paymentPlan.js | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/payments/paymentPlan.js diff --git a/src/payments/paymentPlan.js b/src/payments/paymentPlan.js new file mode 100644 index 000000000..9e7e50961 --- /dev/null +++ b/src/payments/paymentPlan.js @@ -0,0 +1,62 @@ +// paymentPlan.js + +class PaymentPlan { + constructor() { + this.plans = []; + } + + // Create a new payment plan + createPlan(userId, totalAmount, durationMonths) { + const plan = { + userId, + totalAmount, + durationMonths, + monthlyPayment: totalAmount / durationMonths, + status: 'active', + createdAt: new Date(), + }; + this.plans.push(plan); + console.log(`Payment plan created for user ${userId}:`, plan); + return plan; + } + + // Get all payment plans for a user + getUser Plans(userId) { + return this.plans.filter(plan => plan.userId === userId); + } + + // Update a payment plan status + updatePlanStatus(userId, planId, newStatus) { + const plan = this.plans.find(p => p.userId === userId && p.id === planId); + if (plan) { + plan.status = newStatus; + console.log(`Payment plan status updated for user ${userId}:`, plan); + return plan; + } else { + throw new Error('Payment plan not found.'); + } + } + + // Cancel a payment plan + cancelPlan(userId, planId) { + const planIndex = this.plans.findIndex(p => p.userId === userId && p.id === planId); + if (planIndex !== -1) { + const canceledPlan = this.plans.splice(planIndex, 1); + console.log(`Payment plan canceled for user ${userId}:`, canceledPlan); + return canceledPlan; + } else { + throw new Error('Payment plan not found.'); + } + } +} + +// Example usage +const paymentPlanManager = new PaymentPlan(); +const newPlan = paymentPlanManager.createPlan('user123', 1200, 12); // $1200 over 12 months +const userPlans = paymentPlanManager.getUser Plans('user123'); +console.log('User Plans:', userPlans); + +paymentPlanManager.updatePlanStatus('user123', newPlan.id, 'completed'); +paymentPlanManager.cancelPlan('user123', newPlan.id); + +export default PaymentPlan;