5.8 KiB
5.8 KiB
Billing System Migration Summary
Files Modified
1. module.php - Database Schema
Changes:
- Removed all legacy
ALTER TABLEmigration queries (db_version reset to 1) - Updated to single clean install with current schema
- Added
ogp_billing_invoicestable definition - Added missing columns to
billing_orders:order_date,payment_txid,paid_ts - Changed
end_datefrom VARCHAR to DATETIME - Removed obsolete columns:
cart_id,extended - Removed
billing_cartstable (replaced by invoices) - Added proper indexes for performance
2. cron-shop.php - Server Lifecycle Automation
Fixed Logic Errors:
- OLD BUG: Was deleting servers with
status='paid'orstatus='installed'if end_date was close - NEW: Only processes servers based on invoice payment status, not just order status
- Now uses
billing_invoicestable to determine if payment is due
New 3-Step Process:
-
Create Renewal Invoices (7 days before expiration)
- Find
installedservers expiring soon - Check if unpaid invoice exists
- If not, create renewal invoice
- Send email reminder
- Find
-
Suspend Servers (on expiration with unpaid invoice)
- Find
installedservers past end_date - Check if they have unpaid invoices
- Stop server, disable FTP, unassign from user
- Status →
suspended
- Find
-
Delete Servers (7 days after suspension)
- Find
suspendedservers 7+ days past end_date - Still have unpaid invoices
- Permanently delete files and database
- Status →
deleted
- Find
New Files Created
1. migration_to_invoices.sql
Purpose: Upgrade existing installations
What it does:
- Adds new columns to
billing_orders - Creates
billing_invoicestable - Migrates existing paid orders to have invoice records
- Removes obsolete
billing_cartstable - Adds performance indexes
2. INVOICE_SYSTEM.md
Purpose: Documentation
Contents:
- Table schemas explained
- Workflow diagrams
- Status field definitions
- Cron automation logic
- Migration instructions
SQL for Fresh Install
The module.php now contains clean CREATE TABLE statements for:
ogp_billing_services
CREATE TABLE `ogp_billing_services` (
service_id INT AUTO_INCREMENT PRIMARY KEY,
service_name VARCHAR(255),
remote_server_id VARCHAR(255),
price_monthly FLOAT(15,4),
enabled INT DEFAULT 1,
... [other fields]
);
ogp_billing_orders
CREATE TABLE `ogp_billing_orders` (
order_id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
service_id INT NOT NULL,
home_name VARCHAR(255),
home_id VARCHAR(255),
status VARCHAR(16) DEFAULT 'in-cart',
order_date DATETIME DEFAULT CURRENT_TIMESTAMP,
end_date DATETIME NULL,
payment_txid VARCHAR(255) NULL,
paid_ts DATETIME NULL,
... [other fields]
KEY (user_id),
KEY (status),
KEY (home_id)
);
ogp_billing_invoices (NEW)
CREATE TABLE `ogp_billing_invoices` (
invoice_id INT AUTO_INCREMENT PRIMARY KEY,
order_id INT NOT NULL,
user_id INT NOT NULL,
customer_name VARCHAR(255),
customer_email VARCHAR(255),
amount FLOAT(15,2),
currency VARCHAR(3) DEFAULT 'USD',
status VARCHAR(16) DEFAULT 'unpaid',
invoice_date DATETIME DEFAULT CURRENT_TIMESTAMP,
due_date DATETIME NULL,
paid_date DATETIME NULL,
payment_txid VARCHAR(255),
payment_method VARCHAR(50),
description VARCHAR(500),
invoice_duration VARCHAR(16),
qty INT DEFAULT 1,
KEY (order_id),
KEY (user_id),
KEY (status),
KEY (due_date)
);
Migration Steps for Existing Installations
-
Backup Database
mysqldump -u root -p ogp_panel > backup_before_invoice_migration.sql -
Run Migration Script
mysql -u root -p ogp_panel < modules/billing/migration_to_invoices.sql -
Verify Tables
SHOW TABLES LIKE 'ogp_billing%'; -- Should show: billing_services, billing_orders, billing_invoices DESCRIBE ogp_billing_orders; -- Should have: order_date, payment_txid, paid_ts, end_date (DATETIME) DESCRIBE ogp_billing_invoices; -- Should exist with all invoice fields -
Test Cron Job
cd /path/to/ogp/web php modules/billing/cron-shop.php -
Check Logs
SELECT * FROM ogp_logger WHERE type LIKE '%BILLING-CRON%' ORDER BY date DESC LIMIT 20;
Key Improvements
-
Accurate Server Management
- Servers only suspended if they have unpaid invoices
- Active paid servers are never touched
- Clear separation between order state and payment state
-
Audit Trail
- Every payment creates an invoice record
- Can track payment history per server
- Know exactly when/why server was suspended
-
Flexible Pricing
- Each renewal can have different price
- Support for discounts and promotions
- Currency per invoice (multi-currency support ready)
-
Better Customer Experience
- Clear invoice emails with due dates
- 7-day warning before expiration
- 7-day grace period before deletion
Status Field Values Reference
billing_orders.status
in-cart- Initial state, unpaidpaid- Payment received, awaiting provisioninginstalled- Server active and running ✅suspended- Stopped due to non-paymentdeleted- Permanently removedexpired- Service endedrenew- Renewal in cart (legacy, now uses invoices)
billing_invoices.status
unpaid- Invoice created, awaiting paymentpaid- Invoice paid successfully
Next Steps for Implementation
- Update cart.php to show invoices instead of orders
- Update my_account.php "Renew" button to create invoices
- Update payment success flow to mark invoices paid
- Add invoice viewing page
- Test full workflow: order → pay → renew → pay renewal