Subscription & Billing
This document provides a detailed overview of the subscription and billing system implemented in the backend services. It describes the main entities, services, and workflows related to subscription management, plan management, and invoicing.
Overview
The subscription and billing module handles:
- User subscription management (creation, update, activation, revocation, deletion)
- Plan management (create, update, delete, list)
- Invoice management (automatic invoice generation, retrieval, PDF generation)
- Pricing calculation (subscription + optional VPS costs)
It ensures that users can subscribe to plans, switch plans, and receive accurate billing information for their subscriptions and hosted services.
Entities
The system revolves around the following core entities:
Subscription
- Represents a user's subscription to a specific plan.
- Fields:
subscriptionId: unique identifieruser: the subscribed userplan: the plan subscribed toprice: price of the subscriptionstartDate/endDate: subscription periodnextBillingDate: next billing dateactive: status of the subscription
- Relationships:
- One-to-One with
User - One-to-One with
Invoice - One-to-One with
Plan
- One-to-One with
Plan
- Represents a subscription plan with pricing and features.
- Fields:
planId: unique identifiername: plan nameprice: plan pricebillingCycle: monthly or yearlyfeatures: optional features listrecommended: flag for recommended plansactive: status
- Relationships:
- One-to-Many with
Subscription
- One-to-Many with
Invoice
- Represents billing information generated for a subscription.
- Fields:
invoiceId: unique identifierinvoiceNumber: formatted asINV-{YEAR}-{COUNT}subscription: associated subscriptionsubscriptionPrice: subscription costvpsPrice: total VPS cost from user's projects/servicesamount: total amount (subscription + VPS)issueDate: date of invoicepaid: invoice payment status
- Relationships:
- One-to-One with
Subscription
- One-to-One with
Services
SubscriptionService
Responsible for subscription lifecycle management:
Key Methods:
-
createSubscription(SubscriptionRequest request)- Creates a subscription for a user.
- Checks if the user already has an active subscription.
- Assigns plan, start date, price, and calculates end date based on the billing cycle.
- Automatically generates the first invoice via
InvoiceService.
-
getSubscriptionByUserId(String userId)- Retrieves subscription details for a specific user.
-
updateSubscription(Long subscriptionId, SubscriptionRequest request)- Updates the plan, price, and end date of an existing subscription.
-
deleteSubscription(Long subscriptionId)- Deletes a subscription.
- First removes any associated invoices to maintain referential integrity.
-
changeUserSubscriptionPlan(String userId, Long newPlanId)- Switches the user to a new plan.
- Updates price and end date accordingly.
-
revokeSubscription(String userId)- Revokes (terminates) an active subscription.
- Deletes subscription entity and removes association from user.
-
activateSubscription(String userId)- Reactivates an inactive subscription and recalculates the end date.
-
Helper:
calculateEndDate(Plan plan)- Calculates subscription end date based on billing cycle (MONTHLY/YEARLY).
PlanService
Handles subscription plan CRUD operations:
Key Methods:
createPlan(PlanRequest request)– Create a new subscription plan.updatePlan(Long planId, PlanRequest request)– Update plan details.deletePlan(Long planId)– Delete a plan.getPlanById(Long planId)– Retrieve plan details.getAllPlans()– List all available plans.
InvoiceService
Manages billing and invoice generation:
Key Methods:
-
createInvoice(InvoiceRequest request)- Calculates subscription price and any linked VPS costs.
- Generates the total amount and stores the invoice.
- Invoice numbers are sequential and formatted as
INV-{YEAR}-{COUNT}.
-
getInvoiceById(Long invoiceId)– Retrieve a specific invoice. -
getAllInvoices()– Retrieve all invoices in the system. -
getInvoicesBySubscriptionId(Long subscriptionId)– Retrieve invoices for a specific subscription. -
deleteInvoice(Long invoiceId)– Delete an invoice. -
generateInvoicePdf(Long invoiceId)- Generates a PDF representation of the invoice using Apache PDFBox.
- Includes:
- Invoice number
- Issue date
- Paid status
- Subscription price
- Total VPS price
- Total amount
- Subscription ID
-
getInvoicesForAuthenticatedUser()- Returns all invoices belonging to the authenticated user.
Workflow Examples
Subscription Creation
- User selects a plan.
SubscriptionService.createSubscriptionvalidates the user and plan.- Creates subscription entity with calculated end date and next billing date.
- Generates the first invoice via
InvoiceService.
Plan Upgrade
- User requests a plan change.
changeUserSubscriptionPlanvalidates subscription status.- Updates subscription with the new plan and recalculates price and end date.
Invoice Generation
- Upon subscription creation or billing cycle renewal,
InvoiceService.createInvoicecalculates total cost. - PDF generation is available via
generateInvoicePdf. - The invoice includes subscription and any associated VPS costs.
Notes
- Subscriptions are currently One-to-One with
User. Future improvements may support multiple subscriptions per user. - Billing supports integration with VPS services to include hosting costs.
- Invoice generation leverages PDFBox; can be extended to other PDF libraries or formats.
- All operations are transactional to ensure data integrity.