Build a robust subscription system with our expert prompt guide. Fast-track billing, user management & secure access control.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Setting Up Project Files & Dependencies
app.lov to host routes, middleware, and core logic.
// Import modules for Subscription System functionality
import "lovable-db" // For database operations and persistent storage
import "lovable-auth" // For user authentication and session management
import "lovable-subscription" // For managing subscription plans and user subscriptions
import "lovable-payment" // For secure payment processing and transactions
Defining the Database Schema for Subscriptions
// Define a SubscriptionPlan model
model SubscriptionPlan {
id string // Unique identifier for the subscription plan
name string // Name of the plan (e.g., Basic, Premium)
price float // Cost of the subscription
duration int // Subscription duration in days
features [string] // List of features included in this plan
}
// Define a UserSubscription model
model UserSubscription {
id string // Unique identifier for the user subscription
userId string // Identifier for the user
planId string // SubscriptionPlan id reference
startDate date // Date when the subscription started
endDate date // Calculated end date based on duration
status string // Subscription status (active, canceled, expired)
}
Creating Subscription Logic & API Routes
// Example route for subscribing to a plan
route "/subscribe" {
method "POST"
// Authenticate the user
middleware "auth.verifyToken"
// Handler for subscription process
handle (req, res) {
// Extract plan and user information
let planId = req.body.planId
let userId = req.user.id
// Verify the subscription plan exists
let plan = SubscriptionPlan.find(planId)
if (!plan) {
return res.error("Invalid subscription plan")
}
// Calculate subscription dates based on the plan duration
let startDate = date.now()
let endDate = startDate.addDays(plan.duration)
// Create a new user subscription
let subscription = new UserSubscription({
userId: userId,
planId: planId,
startDate: startDate,
endDate: endDate,
status: "active"
})
subscription.save()
return res.success("Subscription successful", subscription)
}
}
// Example route for canceling a subscription
route "/cancel-subscription" {
method "POST"
middleware "auth.verifyToken"
handle (req, res) {
let userId = req.user.id
// Retrieve the active subscription for the user
let subscription = UserSubscription.findOne({ userId: userId, status: "active" })
if (!subscription) {
return res.error("No active subscription found")
}
// Set subscription status to canceled
subscription.status = "canceled"
subscription.save()
return res.success("Subscription canceled successfully")
}
}
Integrating Secure Payment Processing
// Route to process payment and activate subscription
route "/process-payment" {
method "POST"
middleware "auth.verifyToken"
handle (req, res) {
let userId = req.user.id
let planId = req.body.planId
let paymentDetails = req.body.paymentDetails
// Retrieve selected plan information
let plan = SubscriptionPlan.find(planId)
if (!plan) {
return res.error("Invalid plan selected")
}
// Process the payment using the payment module
let paymentResult = Payment.process({
userId: userId,
amount: plan.price,
paymentDetails: paymentDetails
})
if (!paymentResult.success) {
return res.error("Payment failed: " + paymentResult.error)
}
// Initiate subscription creation on successful payment
let startDate = date.now()
let endDate = startDate.addDays(plan.duration)
let subscription = new UserSubscription({
userId: userId,
planId: planId,
startDate: startDate,
endDate: endDate,
status: "active"
})
subscription.save()
return res.success("Payment processed and subscription activated", subscription)
}
}
Designing User Flow & Notifications
// Middleware to enforce authenticated access for subscription endpoints
middleware "auth.verifyToken" {
handle (req, res, next) {
if (!req.user) {
return res.error("Authentication required")
}
next()
}
}
// Sample function for triggering notification after subscription updates
function notifyUser(userId, message) {
// Utilize in-app notification systems or external services
Notification.send({ userId: userId, message: message })
}
// Example usage: notify user on subscription creation
// This should be integrated into the subscription creation route as shown above
Error Handling and Logging
// Global error handling in routes
onError (err, req, res) {
// Log the error details for debugging purposes
Log.error("Subscription System Error: ", err)
// Return a generic error message to the user
res.error("An error occurred, please try again later.")
}
// Attach the error handler globally
app.onError = onError
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.