PNG IHDR x sBIT|d pHYs + tEXtSoftware www.inkscape.org< ,tEXtComment
<?php
require_once "../config.php";
session_start();
// ✅ Check login session
if (!isset($_SESSION['auth'])) {
header("Location: ../login.php");
exit;
}
$user_id = $_SESSION['auth'];
// ✅ Get user details
$stmt = $pdo->prepare("SELECT id, pin_hash FROM users WHERE id = ?");
$stmt->execute([$user_id]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$user) die("User not found.");
// ✅ Get all user accounts
$stmt = $pdo->prepare("SELECT HEX(id) AS id_hex, id, account_number, balance, currency
FROM accounts WHERE user_id = ?");
$stmt->execute([$user_id]);
$accounts = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (!$accounts) die("No account found for this user.");
$message = "";
$success = false;
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$account_id_hex = $_POST['account_id'] ?? '';
$billType = trim($_POST['bill_type'] ?? '');
$refNumber = trim($_POST['ref_number'] ?? '');
$amount = floatval($_POST['amount'] ?? 0);
$pin = trim($_POST['pin'] ?? '');
if ($account_id_hex && $billType && $refNumber && $amount > 0 && $pin) {
$account = null;
foreach ($accounts as $acc) {
if ($acc['id_hex'] === $account_id_hex) {
$account = $acc;
break;
}
}
if (!$account) {
$message = "❌ Invalid account selected.";
} elseif (!password_verify($pin, $user['pin_hash'])) {
$message = "❌ Invalid Transaction PIN.";
} elseif ($account['balance'] < $amount) {
$message = "❌ Insufficient balance.";
} else {
try {
$pdo->beginTransaction();
// Deduct balance
$stmt = $pdo->prepare("UPDATE accounts SET balance = balance - ? WHERE id = UNHEX(?)");
$stmt->execute([$amount, $account_id_hex]);
// Insert transaction record
$txId = bin2hex(random_bytes(16));
$metadata = json_encode([
"type" => "bill_payment",
"bill_type" => $billType,
"ref_number" => $refNumber
]);
$stmt = $pdo->prepare("
INSERT INTO transactions (id, status, amount, from_account_id, to_account_id, metadata, created_by)
VALUES (UNHEX(?), 'completed', ?, UNHEX(?), NULL, ?, ?)
");
$stmt->execute([$txId, $amount, $account_id_hex, $metadata, $user_id]);
$pdo->commit();
$success = true;
$message = "✅ Bill payment of " . $account['currency'] . " " . number_format($amount, 2) . " for $billType successful!";
} catch (Exception $e) {
$pdo->rollBack();
$message = "❌ Transaction failed: " . $e->getMessage();
}
}
} else {
$message = "❌ Please fill all fields correctly.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pay Bills</title>
<link rel="stylesheet" href="../style.css">
<style>
body { font-family: Arial, sans-serif; background:#f8f9fb; margin:0; display:flex; justify-content:center; }
.container { max-width:450px; width:100%; background:#fff; min-height:100vh; padding:20px; }
h2 { color:#0C4DA2; }
form { margin-top:20px; display:flex; flex-direction:column; gap:15px; }
input, select { padding:12px; border:1px solid #ccc; border-radius:6px; font-size:14px; }
button { background:#0C4DA2; color:#fff; border:none; padding:12px; border-radius:6px; cursor:pointer; font-size:15px; }
button:hover { background:#094080; }
/* Modal Styles */
.modal-overlay {
display:none;
position:fixed; top:0; left:0; width:100%; height:100%;
background:rgba(0,0,0,0.5);
justify-content:center; align-items:center;
z-index:1000;
}
.modal-box {
background:#fff;
border-radius:16px;
padding:30px;
max-width:400px;
width:90%;
text-align:center;
box-shadow:0 10px 30px rgba(0,0,0,0.2);
animation: fadeIn 0.3s ease;
}
.modal-box.success { border-top:5px solid #0a8a4c; }
.modal-box.error { border-top:5px solid #c0392b; }
.modal-box h3 { margin:0; font-size:20px; }
.modal-box p { margin:15px 0; font-size:15px; }
.modal-box button {
background:#0C4DA2;
padding:10px 20px;
border:none;
border-radius:8px;
color:#fff;
cursor:pointer;
font-size:14px;
}
.modal-box button:hover { background:#094080; }
.checkmark {
font-size:40px;
margin-bottom:10px;
}
@keyframes fadeIn { from {opacity:0; transform:scale(0.95);} to {opacity:1; transform:scale(1);} }
</style>
</head>
<body>
<div class="container">
<h2>Pay Bills</h2>
<form method="POST">
<label>Select Account</label>
<select name="account_id" required>
<option value="">-- Choose Account --</option>
<?php foreach ($accounts as $acc): ?>
<option value="<?= htmlspecialchars($acc['id_hex']) ?>">
<?= htmlspecialchars($acc['currency']) ?> - <?= htmlspecialchars($acc['account_number']) ?>
(Bal: <?= number_format($acc['balance'],2) ?>)
</option>
<?php endforeach; ?>
</select>
<label>Bill Type</label>
<select name="bill_type" required>
<option value="">-- Select Bill --</option>
<option value="Electricity">Electricity</option>
<option value="Water">Water</option>
<option value="Internet">Internet</option>
<option value="TV Subscription">TV Subscription</option>
<option value="Other">Other</option>
</select>
<label>Reference / Account Number</label>
<input type="text" name="ref_number" placeholder="e.g. Meter No / Customer ID" required>
<label>Amount</label>
<input type="number" step="0.01" name="amount" placeholder="Enter amount" required>
<label>Transaction PIN</label>
<input type="password" name="pin" placeholder="Enter your PIN" required>
<button type="submit">Pay Now</button>
</form>
<p style="margin-top:20px;"><a href="pay.php">← Back to Pay & Transfer</a></p>
</div>
<!-- ✅ Modal -->
<?php if ($message): ?>
<div class="modal-overlay" id="alertModal" style="display:flex;">
<div class="modal-box <?= strpos($message,'✅')!==false ? 'success':'error' ?>">
<div class="checkmark"><?= strpos($message,'✅')!==false ? '✔️' : '❌' ?></div>
<h3><?= strpos($message,'✅')!==false ? 'Success' : 'Error' ?></h3>
<p><?= htmlspecialchars($message) ?></p>
<button onclick="closeModal()">OK</button>
</div>
</div>
<?php endif; ?>
<script>
function closeModal() {
document.getElementById('alertModal').style.display = 'none';
}
<?php if ($success): ?>
// Redirect to dashboard after success
setTimeout(() => { window.location.href = 'index.php'; }, 3000);
<?php endif; ?>
</script>
</body>
</html>
b IDATxytVսϓ22 A@IR:hCiZ[v*E:WũZA ^dQeQ @ !jZ'>gsV仿$|?g)&x-E