128 lines
6.3 KiB
PHP
128 lines
6.3 KiB
PHP
<?php
|
|
$title = "Purchase Simulation";
|
|
require_once __DIR__ . '/includes/header.php';
|
|
|
|
// Check if user is logged in
|
|
if (!isset($_SESSION['user_id'])) {
|
|
$_SESSION['redirect_after_login'] = "purchase.php?id=" . ($_GET['id'] ?? 0);
|
|
header("Location: login.php?msg=Please login to purchase");
|
|
exit;
|
|
}
|
|
|
|
$car_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
|
|
$db = db();
|
|
|
|
// Fetch Car Details
|
|
$stmt = $db->prepare("SELECT * FROM cars WHERE id = ? AND status = 'Available' AND is_deleted = 0 LIMIT 1");
|
|
$stmt->execute([$car_id]);
|
|
$car = $stmt->fetch();
|
|
|
|
if (!$car) {
|
|
echo "<div class='container' style='padding: 5rem 0; text-align: center;'><h1 class='hero'>Not Available</h1><p style='color: var(--text-muted);'>This car is no longer available for purchase.</p><a href='cars.php' class='btn btn-primary' style='margin-top: 2rem;'>Back to Marketplace</a></div>";
|
|
require_once __DIR__ . '/includes/footer.php';
|
|
exit;
|
|
}
|
|
|
|
$success = false;
|
|
$error = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$bank = $_POST['bank'] ?? '';
|
|
$account = $_POST['account'] ?? '';
|
|
|
|
if (empty($bank) || empty($account)) {
|
|
$error = "Please fill in all simulation details.";
|
|
} else {
|
|
// Start simulated transaction
|
|
try {
|
|
$db->beginTransaction();
|
|
|
|
// 1. Record Purchase
|
|
$pStmt = $db->prepare("INSERT INTO purchases (user_id, car_id, bank_name, transaction_id, amount) VALUES (?, ?, ?, ?, ?)");
|
|
$trans_id = "AFG-" . strtoupper(uniqid());
|
|
$pStmt->execute([$_SESSION['user_id'], $car_id, $bank, $trans_id, $car['price']]);
|
|
|
|
// 2. Update Car Status
|
|
$uStmt = $db->prepare("UPDATE cars SET status = 'SOLD' WHERE id = ?");
|
|
$uStmt->execute([$car_id]);
|
|
|
|
// 3. Create Notification
|
|
$nStmt = $db->prepare("INSERT INTO notifications (user_id, title, message) VALUES (?, ?, ?)");
|
|
$nStmt->execute([$_SESSION['user_id'], "Purchase Successful", "Congratulations! You have successfully reserved " . $car['title'] . ". Transaction ID: " . $trans_id]);
|
|
|
|
$db->commit();
|
|
$success = true;
|
|
} catch (Exception $e) {
|
|
$db->rollBack();
|
|
$error = "Simulation error: " . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<div class="container" style="padding: 5rem 0; max-width: 600px;">
|
|
<?php if ($success): ?>
|
|
<div class="glass-card" style="padding: 3rem; text-align: center;">
|
|
<div style="font-size: 5rem; margin-bottom: 2rem; color: var(--success);">🎉</div>
|
|
<h2 style="margin-bottom: 1rem;">Purchase Successful!</h2>
|
|
<p style="color: var(--text-muted); margin-bottom: 2rem;">Your simulated payment has been processed. The car is now reserved in your name.</p>
|
|
<div style="background: rgba(16, 185, 129, 0.1); padding: 1.5rem; border-radius: var(--radius-md); border: 1px dashed var(--success); margin-bottom: 2rem;">
|
|
<p><strong>Transaction ID:</strong> <?php echo $trans_id; ?></p>
|
|
<p><strong>Amount:</strong> $<?php echo number_format($car['price'], 2); ?></p>
|
|
</div>
|
|
<a href="user/dashboard.php" class="btn btn-primary" style="width: 100%;">Go to My Dashboard</a>
|
|
</div>
|
|
<?php else: ?>
|
|
<div class="glass-card" style="padding: 3rem;">
|
|
<h2 style="margin-bottom: 0.5rem; text-align: center;">Bank Payment Simulation</h2>
|
|
<p style="color: var(--text-muted); text-align: center; margin-bottom: 2.5rem;">Secure Offline Transaction System</p>
|
|
|
|
<?php if ($error): ?>
|
|
<div style="background: rgba(239, 68, 68, 0.1); color: var(--danger); padding: 1rem; border-radius: var(--radius-sm); margin-bottom: 1.5rem;">
|
|
<?php echo $error; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div style="margin-bottom: 2rem; padding: 1.5rem; background: var(--bg-glass); border-radius: var(--radius-md);">
|
|
<div style="display: flex; justify-content: space-between; margin-bottom: 0.5rem;">
|
|
<span>Car</span>
|
|
<strong><?php echo htmlspecialchars($car['title']); ?></strong>
|
|
</div>
|
|
<div style="display: flex; justify-content: space-between;">
|
|
<span>Price</span>
|
|
<strong>$<?php echo number_format($car['price'], 2); ?></strong>
|
|
</div>
|
|
</div>
|
|
|
|
<form action="purchase.php?id=<?php echo $car_id; ?>" method="POST">
|
|
<div class="form-group" style="margin-bottom: 1.5rem;">
|
|
<label>Select Afghanistan Bank</label>
|
|
<select name="bank" class="form-control" required>
|
|
<option value="">Choose Bank...</option>
|
|
<option value="Da Afghanistan Bank">Da Afghanistan Bank</option>
|
|
<option value="Azizi Bank">Azizi Bank</option>
|
|
<option value="New Kabul Bank">New Kabul Bank</option>
|
|
<option value="Pashtany Bank">Pashtany Bank</option>
|
|
<option value="Islamic Bank of Afghanistan">Islamic Bank of Afghanistan</option>
|
|
</select>
|
|
</div>
|
|
|
|
<div class="form-group" style="margin-bottom: 2rem;">
|
|
<label>Account Number (Simulation)</label>
|
|
<input type="text" name="account" class="form-control" placeholder="E.g. AF74001000..." required>
|
|
<p style="font-size: 0.75rem; color: var(--text-muted); margin-top: 0.5rem;">Enter any mock account number for this demonstration.</p>
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary" style="width: 100%; padding: 1rem; font-size: 1.1rem;">Confirm Simulated Payment</button>
|
|
<a href="car_detail.php?id=<?php echo $car_id; ?>" class="btn btn-outline" style="width: 100%; margin-top: 1rem;">Cancel</a>
|
|
</form>
|
|
|
|
<div style="margin-top: 2rem; text-align: center; color: var(--text-muted); font-size: 0.8rem;">
|
|
🔒 This is an offline simulation. No real money will be transferred.
|
|
</div>
|
|
</div>
|
|
<?php endif; ?>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/includes/footer.php'; ?>
|