Skip to content

Commit

Permalink
Create paymentPlan.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent 98ff184 commit 2b78e99
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/payments/paymentPlan.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 2b78e99

Please sign in to comment.