# Project Roadmap: Link Shortener with Paywall

## Overview
The project aims to create a simple link shortener where users are redirected to a specific website after passing a paywall. We will integrate a payment gateway (PhonePe or Stripe) to process payments, and use `better-sqlite3` with `quick.db` for an efficient local database solution to store transaction information, including the user's email.

---

## 1. Project Setup

### 1.1 Initial Setup
- **Set up a Git repository**: Create a repository for version control.
- **Set up Node.js and Express**: Use Node.js as the server environment and Express.js for the backend framework.
- **Install required packages**: Install the following packages:
  - `express` - for creating the server and handling routes.
  - `better-sqlite3` and `quick.db` - for database management.
  - `stripe` or PhonePe SDK (depending on the chosen payment gateway) - for handling payments.

```bash
npm init -y
npm install express better-sqlite3 quick.db stripe
```

### 1.2 Configure Environment Variables
- Create a `.env` file to store sensitive information such as API keys and database URIs.
- Add the following environment variables:
  - `STRIPE_API_KEY` or `PHONEPE_API_KEY` for the payment gateway.
  - `PORT` for the server port.

---

## 2. Database Setup

### 2.1 Database Configuration
- Set up a local SQLite database with `better-sqlite3`.
- Use `quick.db` for simple database manipulation.
  
### 2.2 Database Schema
- **Links Table**: Store the original URL, shortened URL, access status (paid or unpaid), and the user who created it.
- **Transactions Table**: Store transaction details such as `email`, `transaction_id`, `payment_status`, and `date`.
- **Earnings Table**: Track daily earnings for each user and specific links they shortened, subtracting a platform fee.

---

## 3. Link Shortening and Redirection

### 3.1 Link Shortening API
- Create an endpoint to generate a short link.
  - **POST /shorten**: Accepts an original URL and creator’s email, generates a unique shortened link, and stores it in the `Links` table with `is_paid` set to `0`.

### 3.2 Redirection Logic
- Create an endpoint to handle redirection.
  - **GET /:shortUrl**: Checks if the short link exists and if `is_paid` is `1`.
    - If paid, redirect to the original URL.
    - If not paid, redirect to the payment page.

---

## 4. Payment Integration

### 4.1 Payment Gateway Configuration
- Set up either the Stripe or PhonePe payment gateway using their respective APIs.
- Store payment gateway configurations in the `.env` file.

### 4.2 Payment API
- Create an endpoint to initiate the payment process.
  - **POST /pay**: Accepts `email` and `shortUrl`, initiates payment, and returns a payment link to the user.
  - Once payment is confirmed, set `is_paid` to `1` in the `Links` table and save the transaction details in the `Transactions` table.
  - Add earnings to the `Earnings` table for tracking purposes.

### 4.3 Webhook for Payment Confirmation
- Set up a webhook endpoint for the payment gateway to receive payment status updates.
  - Update `payment_status` in the `Transactions` table once payment is confirmed.
  - Update earnings in the `Earnings` table, subtracting a certain percentage (e.g., 10%) as the platform fee.

---

## 5. Frontend

### 5.1 Link Shortening Form
- A simple form for users to input the original URL and generate a shortened link.

### 5.2 Payment Page
- A paywall page that explains the need for payment and redirects users to the payment link.

---

## 6. Earnings and Reporting

### 6.1 Daily Earnings Calculation
- Calculate daily earnings for each registered user.
- Subtract a fixed platform fee (e.g., 10%) from each payment and record it in the `Earnings` table.

### 6.2 Reporting API
- **GET /earnings/daily**: Returns the daily earnings for the logged-in user, with a breakdown by individual link and total daily earnings after the platform fee.
- **GET /earnings/:linkId**: Returns the total earnings generated by a specific shortened link.

---

## 7. Error Handling and Validation

- **Input validation**: Ensure that all inputs, especially URLs and email addresses, are sanitized.
- **Error handling**: Implement error handling for issues such as invalid URLs, payment failures, or database errors.
- **Security**: Ensure sensitive data (such as API keys) is stored securely and never exposed.

---

## 8. Testing

- **Unit testing**: Write tests for each endpoint and function to verify their behavior.
- **Payment testing**: Use sandbox/testing modes of the payment gateway to simulate payments.
- **Database testing**: Verify data is correctly saved and retrieved.

---

## 9. Deployment

### 9.1 Hosting
- Host the application on a cloud provider (e.g., DigitalOcean, Heroku, Vercel) or a dedicated server.

### 9.2 Environment Configuration
- Set environment variables on the server to securely store API keys and database configuration.

### 9.3 Database Backup and Recovery
- Set up regular backups for the SQLite database.

---

## 10. Future Improvements

- **Analytics**: Track click-through rates and conversions for each shortened link.
- **Custom Short URLs**: Allow users to create custom shortened URLs.
- **User Accounts**: Enable user registration to track their links and payments.

---

This roadmap should guide you from setup to deployment. Adjust each section as per specific requirements or improvements during the development process.