37684-vm/admin_payments.php
2026-03-01 22:19:37 +00:00

188 lines
10 KiB
PHP

<?php
session_start();
// Check if user is Super User
if (!isset($_SESSION["user_id"]) || ($_SESSION["user_role"] ?? '') !== 'Super User') {
header("Location: login.php");
exit;
}
require_once 'db/config.php';
$project_name = $_SERVER['PROJECT_NAME'] ?? 'LPA Online';
$db = db();
// Handle form submissions
$message = '';
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (isset($_POST['update_stripe'])) {
try {
$stmt = $db->prepare("UPDATE stripe_config SET setting_value = ? WHERE setting_key = ?");
$stmt->execute([$_POST['stripe_publishable_key'], 'stripe_publishable_key']);
$stmt->execute([$_POST['stripe_secret_key'], 'stripe_secret_key']);
$stmt->execute([$_POST['stripe_webhook_secret'], 'stripe_webhook_secret']);
$stmt->execute([$_POST['currency'], 'currency']);
$message = 'Stripe configuration updated successfully.';
} catch (PDOException $e) {
$error = 'Error updating Stripe configuration: ' . $e->getMessage();
}
} elseif (isset($_POST['add_package'])) {
try {
$stmt = $db->prepare("INSERT INTO credit_packages (name, description, credits, price_amount, price_currency) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$_POST['name'], $_POST['description'], $_POST['credits'], $_POST['price_amount'], $_POST['price_currency']]);
$message = 'Credit package added successfully.';
} catch (PDOException $e) {
$error = 'Error adding credit package: ' . $e->getMessage();
}
} elseif (isset($_POST['delete_package'])) {
try {
$stmt = $db->prepare("DELETE FROM credit_packages WHERE id = ?");
$stmt->execute([$_POST['package_id']]);
$message = 'Credit package deleted.';
} catch (PDOException $e) {
$error = 'Error deleting credit package: ' . $e->getMessage();
}
}
}
// Fetch configuration
$stripe_config = $db->query("SELECT setting_key, setting_value FROM stripe_config")->fetchAll(PDO::FETCH_KEY_PAIR);
$packages = $db->query("SELECT * FROM credit_packages ORDER BY price_amount ASC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Payment Settings — <?php echo htmlspecialchars($project_name); ?></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/css/custom.css" rel="stylesheet">
</head>
<body class="bg-light">
<nav class="navbar navbar-expand-lg bg-white border-bottom shadow-sm">
<div class="container">
<a class="navbar-brand d-flex align-items-center" href="/admin_dashboard.php">
<img src="assets/pasted-20260228-235417-eedda424.png" alt="<?php echo htmlspecialchars($project_name); ?>" height="40">
</a>
<div class="d-flex align-items-center">
<a href="/admin_dashboard.php" class="btn btn-outline-primary btn-sm px-3 rounded-pill me-2">Main Dashboard</a>
<a href="/logout.php" class="btn btn-outline-secondary btn-sm px-3 rounded-pill">Logout</a>
</div>
</div>
</nav>
<div class="container py-5">
<h1 class="h3 fw-bold mb-4">Payment & Package Settings</h1>
<?php if ($message): ?>
<div class="alert alert-success"><?php echo $message; ?></div>
<?php endif; ?>
<?php if ($error): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<div class="row g-4">
<!-- Stripe Config -->
<div class="col-lg-6">
<div class="card border-0 shadow-sm p-4 h-100">
<h5 class="fw-bold mb-4">Stripe Configuration</h5>
<form method="POST">
<div class="mb-3">
<label class="form-label small fw-bold">Publishable Key</label>
<input type="text" name="stripe_publishable_key" class="form-control" value="<?php echo htmlspecialchars($stripe_config['stripe_publishable_key'] ?? ''); ?>" placeholder="pk_test_...">
</div>
<div class="mb-3">
<label class="form-label small fw-bold">Secret Key</label>
<input type="password" name="stripe_secret_key" class="form-control" value="<?php echo htmlspecialchars($stripe_config['stripe_secret_key'] ?? ''); ?>" placeholder="sk_test_...">
</div>
<div class="mb-3">
<label class="form-label small fw-bold">Webhook Secret</label>
<input type="password" name="stripe_webhook_secret" class="form-control" value="<?php echo htmlspecialchars($stripe_config['stripe_webhook_secret'] ?? ''); ?>" placeholder="whsec_...">
<div class="form-text small">Endpoint: <code><?php echo (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]/api/stripe_webhook.php"; ?></code></div>
</div>
<div class="mb-3">
<label class="form-label small fw-bold">Currency</label>
<select name="currency" class="form-select">
<option value="GBP" <?php echo ($stripe_config['currency'] ?? '') === 'GBP' ? 'selected' : ''; ?>>GBP (£)</option>
<option value="USD" <?php echo ($stripe_config['currency'] ?? '') === 'USD' ? 'selected' : ''; ?>>USD ($)</option>
<option value="EUR" <?php echo ($stripe_config['currency'] ?? '') === 'EUR' ? 'selected' : ''; ?>>EUR (€)</option>
</select>
</div>
<div class="d-grid">
<button type="submit" name="update_stripe" class="btn btn-primary rounded-pill py-2 fw-bold">Save Settings</button>
</div>
</form>
</div>
</div>
<!-- Packages -->
<div class="col-lg-6">
<div class="card border-0 shadow-sm p-4 h-100">
<h5 class="fw-bold mb-4">Credit Packages</h5>
<div class="table-responsive mb-4">
<table class="table table-hover align-middle small">
<thead>
<tr>
<th>Name</th>
<th>Credits</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($packages as $pkg): ?>
<tr>
<td><?php echo htmlspecialchars($pkg['name']); ?></td>
<td><?php echo (int)$pkg['credits']; ?></td>
<td><?php echo htmlspecialchars($pkg['price_currency']) . ' ' . number_format($pkg['price_amount'], 2); ?></td>
<td>
<form method="POST" onsubmit="return confirm('Delete this package?');">
<input type="hidden" name="package_id" value="<?php echo $pkg['id']; ?>">
<button type="submit" name="delete_package" class="btn btn-sm btn-link text-danger p-0">Delete</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<h6 class="fw-bold mb-3">Add New Package</h6>
<form method="POST">
<div class="row g-2 mb-3">
<div class="col-md-8">
<input type="text" name="name" class="form-control form-control-sm" placeholder="Package Name" required>
</div>
<div class="col-md-4">
<input type="number" name="credits" class="form-control form-control-sm" placeholder="Credits" required>
</div>
</div>
<div class="mb-3">
<textarea name="description" class="form-control form-control-sm" placeholder="Description" rows="2"></textarea>
</div>
<div class="row g-2 mb-3">
<div class="col-md-8">
<div class="input-group input-group-sm">
<span class="input-group-text">Price</span>
<input type="number" step="0.01" name="price_amount" class="form-control" placeholder="0.00" required>
</div>
</div>
<div class="col-md-4">
<select name="price_currency" class="form-select form-select-sm">
<option value="GBP">GBP</option>
<option value="USD">USD</option>
<option value="EUR">EUR</option>
</select>
</div>
</div>
<div class="d-grid">
<button type="submit" name="add_package" class="btn btn-outline-primary btn-sm rounded-pill fw-bold">Add Package</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>