# RadiusCloud Billing

ISP billing for **PPPoE, Hotspot and Static-IP customers on MikroTik** with **M-Pesa (STK Push and Paybill account numbers)**, SMS reminders, ONU monitoring through GenieACS and one-paste router provisioning over WireGuard. Plain PHP 8.1+ and MySQL/MariaDB, one ISP per installation.

**Hosted version:** one server, many ISPs, each with its own database, with signup, a console and M-Pesa auto-renewal of the monthly fee: [`docs/SAAS.md`](docs/SAAS.md).

Design reasoning: [`docs/DESIGN.md`](docs/DESIGN.md) · Install: [`docs/INSTALL.md`](docs/INSTALL.md)

## Features
**Customers and money**
- PPPoE customers (monthly), hotspot packages (time-based) and static-IP plans, with automatic enable/disable on MikroTik
- M-Pesa **STK Push** and **Paybill (C2B) with account numbers**: payments are matched to the customer, credited and the plan renewed automatically; unmatched payments wait for staff to assign
- Customer accounts (**creditors and debtors**): manual credit and debit, refunds, write-offs, statements, CSV export, renew from balance, optional credit limit
- Reports and an income dashboard: today / yesterday / week / month bar graphs, paid vs online vs paid-but-offline
- Branded captive portal with a **Hotspot page builder** (colours, shapes, logo, texts)

**Messaging**
- **Plan changes**: an upgrade takes effect immediately and the customer pays the price difference for the time left (rounded up to whole KES, from the account balance or as cash/M-Pesa received now); a downgrade is scheduled and applied at the next renewal, which is billed at the new price. Router is updated first, so an unreachable router changes nothing. Full history per customer
- **Customer search** on every page (press `/`): name (any word order), phone with or without 0 / +254, account number, username, IP, ONU serial, address, M-Pesa receipt or Paybill transaction
- **SMS** through Africa's Talking or any HTTP gateway: expiry reminders, expired notices, payment receipts, debt reminders, bulk messages to a chosen audience. Queued, with retries, a daily limit, quiet hours and one automatic message per event

**Network**
- Routers: add by hand or **via WireGuard** (server-generated keys), live status (CPU, memory, traffic, tunnel handshake), router health graphs and offline alerts
- **Router provisioning wizard**: paste one command on the router; the app reads its real interfaces, you assign each port (WAN / PPPoE / Hotspot / Static IP), and the router fetches a generated `.rsc` over the tunnel using a one-time security token
- FUP / data caps with automatic throttling, live sessions with disconnect
- GenieACS ONU monitoring: RX and TX signal, status, reboot, **WiFi name/password change** (2.4/5 GHz), **ONU admin password change** and a "Detect parameters" helper for any vendor's TR-069 paths

**Staff**
- Editable roles (Admin, Technician, Cashier and custom) with per-permission checkboxes, audit log
- Sign-in page in your brand colour with a trace unique to your business name, works on phones, light and dark; failed sign-ins are throttled (per email and per address), the audit log records sign-ins, and `php bin/reset_password.php` recovers a lost administrator password

(details: [`docs/NETWORK_FEATURES.md`](docs/NETWORK_FEATURES.md), [`docs/PROVISIONING_WIREGUARD.md`](docs/PROVISIONING_WIREGUARD.md))

## Install
Full step-by-step guide (Ubuntu/Debian + Apache, TLS, cron, WireGuard hub, updating, backups, troubleshooting): **[`docs/INSTALL.md`](docs/INSTALL.md)**.

Short version:
```bash
git clone https://github.com/dkaranj/radiuscloud.git /var/www/billing && cd /var/www/billing
cp config/config.sample.php config/config.php                  # DB, app_key, public_url
mysql billing < database/schema.sql && php bin/migrate.php --baseline
php bin/create_admin.php "Acme Internet" admin@acme.co.ke 'StrongPass123'
```
Ready-made server files in `deploy/`: `apache-app.conf` (site), `apache-tunnel.conf` / `nginx-tunnel.conf` (router tunnel), `billing.cron`, `wireguard-hub.sh`.

Coming from the old multi-tenant version? `php bin/convert_to_single.php` converts the database (see INSTALL.md).

## MikroTik setup (per router)
**Recommended:** use *Routers → Add router (WireGuard)* and follow the wizard; it needs the WireGuard hub set up once on the server (`deploy/wireguard-hub.sh`, `deploy/nginx-tunnel.conf` or `deploy/apache-tunnel.conf`, root cron for `bin/wg_sync.php`; see the provisioning doc). RouterOS 7 is required for that path.

Manual alternative:
```
/user group add name=billing policy=api,read,write,ppp,test
/user add name=billing group=billing password=STRONG
/ip service set api disabled=no address=<server-or-vpn-ip>
```
Hotspot: add the portal domain to the walled garden, e.g. `/ip hotspot walled-garden add dst-host=billing.example.com` (and `*.safaricom.co.ke` if needed). Replace the router's `hotspot/login.html` with:
```html
<html><head><meta http-equiv="refresh" content="0;url=https://billing.example.com/portal/?mac=$(mac)&ip=$(ip)"></head></html>
```
Set **Hotspot login URL** in Settings to the router's login address, e.g. `http://192.168.88.1/login`.

## First steps in the panel
1. Settings: branding and M-Pesa (sandbox first).
2. Routers: add and press Test.
3. Plans: add; this creates the PPP/hotspot profile on the router.
4. Staff: add technicians; adjust role permissions.

## GitHub workflow
- `main` = production, `develop` = integration, work in `feature/<name>` branches and open PRs.
- CI (`.github/workflows/ci.yml`) lints all PHP and loads the schema on every push/PR.
- Never commit `config/config.php`; it is git-ignored.
- Schema changes: add `database/migrations/YYYYMMDD_description.sql` (and the same change to `database/schema.sql`); servers apply it with `php bin/migrate.php`.

```bash
git checkout -b develop            # integration branch; main stays what is deployed
```

## Status
Checked by automated functional runs against MariaDB with a mock RouterOS API, mock GenieACS and a mock SMS gateway (about 250 checks): ledger, Paybill matching and C2B endpoints, SMS queue and reminders, provisioning logic and token endpoint, WireGuard config generation, GenieACS actions, the Apache vhosts (Apache 2.4 + PHP-FPM), and the multi-tenant to single-ISP conversion (data kept, structure identical to a fresh install).

**Not yet exercised on real equipment:** the generated RouterOS scripts, real `wg syncconf`, vendor-specific TR-069 paths, the Daraja and SMS gateway live calls. Trial on one router, one test ONU, the Daraja sandbox and a test SMS before production.
