Skip to content

Commit

Permalink
Create billing.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent f2cf6c3 commit 45cda2e
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/payments/billing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// payments/billing.js
const moment = require('moment');

class Invoice {
constructor(userId, amount, dueDate) {
this.userId = userId;
this.amount = amount;
this.dueDate = dueDate;
this.isPaid = false;
}

markAsPaid() {
this.isPaid = true;
console.log(`Invoice for user ${this.userId} has been marked as paid.`);
}

getDetails() {
return {
userId: this.userId,
amount: this.amount,
dueDate: this.dueDate,
isPaid: this.isPaid,
};
}
}

class Billing {
constructor() {
this.invoices = [];
}

generateInvoice(userId, amount) {
const dueDate = moment().add(30, 'days').toDate(); // 30 days from now
const invoice = new Invoice(userId, amount, dueDate);
this.invoices.push(invoice);
console.log(`Invoice generated for user ${userId}: $${amount}, due on ${dueDate}`);
return invoice;
}

getInvoice(userId) {
return this.invoices.filter(invoice => invoice.userId === userId);
}

processPayment(userId, amount) {
const invoice = this.invoices.find(inv => inv.userId === userId && inv.amount === amount && !inv.isPaid);
if (!invoice) {
throw new Error('No unpaid invoice found for this user and amount.');
}
invoice.markAsPaid();
}
}

module.exports = Billing;

0 comments on commit 45cda2e

Please sign in to comment.