150 lines
6.0 KiB
PHP
150 lines
6.0 KiB
PHP
<?php
|
|
session_start();
|
|
if (!isset($_SESSION['user_id'])) {
|
|
header('Location: login.php');
|
|
exit();
|
|
}
|
|
|
|
require_once 'db/config.php';
|
|
$db = db();
|
|
|
|
$user_id = $_SESSION['user_id'];
|
|
$message = '';
|
|
$error = '';
|
|
|
|
// Fetch wallet balance
|
|
$stmt = $db->prepare("SELECT wallet_balance FROM users WHERE id = ?");
|
|
$stmt->execute([$user_id]);
|
|
$wallet_balance = $stmt->fetchColumn();
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['amount'])) {
|
|
$amount = filter_input(INPUT_POST, 'amount', FILTER_VALIDATE_FLOAT);
|
|
|
|
if ($amount === false || $amount <= 0) {
|
|
$error = 'Please enter a valid withdrawal amount.';
|
|
} elseif ($amount > $wallet_balance) {
|
|
$error = 'Withdrawal amount cannot exceed your wallet balance.';
|
|
} else {
|
|
try {
|
|
$db->beginTransaction();
|
|
|
|
// 1. Insert into withdrawals table
|
|
$stmt = $db->prepare("INSERT INTO withdrawals (user_id, amount) VALUES (?, ?)");
|
|
$stmt->execute([$user_id, $amount]);
|
|
$withdrawal_id = $db->lastInsertId();
|
|
|
|
// 2. Deduct from user's wallet
|
|
$stmt = $db->prepare("UPDATE users SET wallet_balance = wallet_balance - ? WHERE id = ?");
|
|
$stmt->execute([$amount, $user_id]);
|
|
|
|
// 3. Record the transaction
|
|
$stmt = $db->prepare("INSERT INTO transactions (user_id, amount, type, description, related_withdrawal_id) VALUES (?, ?, 'withdrawal_request', 'Withdrawal request initiated', ?)");
|
|
$stmt->execute([$user_id, -$amount, $withdrawal_id]);
|
|
|
|
$db->commit();
|
|
$message = 'Your withdrawal request has been submitted successfully. It will be processed shortly.';
|
|
// Refresh wallet balance
|
|
$wallet_balance -= $amount;
|
|
} catch (PDOException $e) {
|
|
$db->rollBack();
|
|
$error = 'An error occurred. Please try again.';
|
|
// For debugging: error_log($e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
// Fetch recent withdrawals
|
|
$stmt = $db->prepare("SELECT * FROM withdrawals WHERE user_id = ? ORDER BY created_at DESC LIMIT 10");
|
|
$stmt->execute([$user_id]);
|
|
$withdrawals = $stmt->fetchAll();
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Request Withdrawal</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="assets/css/custom.css">
|
|
</head>
|
|
<body>
|
|
|
|
<div class="container mt-5">
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<h2>Request a Withdrawal</h2>
|
|
<a href="dashboard.php" class="btn btn-secondary">Back to Dashboard</a>
|
|
</div>
|
|
|
|
<?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">
|
|
<div class="col-md-6">
|
|
<div class="card mb-4">
|
|
<div class="card-body">
|
|
<h5 class="card-title">Available Balance</h5>
|
|
<p class="card-text fs-3">₹<?php echo number_format($wallet_balance, 2); ?></p>
|
|
<form method="POST" action="withdraw.php">
|
|
<div class="mb-3">
|
|
<label for="amount" class="form-label">Withdrawal Amount</label>
|
|
<div class="input-group">
|
|
<span class="input-group-text">₹</span>
|
|
<input type="number" step="0.01" class="form-control" id="amount" name="amount" placeholder="Enter amount" required>
|
|
</div>
|
|
</div>
|
|
<button type="submit" class="btn btn-primary w-100">Submit Request</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
Recent Withdrawal History
|
|
</div>
|
|
<div class="card-body">
|
|
<table class="table table-sm">
|
|
<thead>
|
|
<tr>
|
|
<th>Date</th>
|
|
<th>Amount</th>
|
|
<th>Status</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($withdrawals)): ?>
|
|
<tr><td colspan="3" class="text-center">No recent withdrawals.</td></tr>
|
|
<?php else: ?>
|
|
<?php foreach ($withdrawals as $w): ?>
|
|
<tr>
|
|
<td><?php echo date('d M Y', strtotime($w['created_at'])); ?></td>
|
|
<td>₹<?php echo number_format($w['amount'], 2); ?></td>
|
|
<td>
|
|
<span class="badge bg-<?php
|
|
switch ($w['status']) {
|
|
case 'pending': echo 'warning'; break;
|
|
case 'approved': echo 'success'; break;
|
|
case 'rejected': echo 'danger'; break;
|
|
}
|
|
?>"><?php echo ucfirst($w['status']); ?></span>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|