34968-vm/checkout.php
Flatlogic Bot 369f552c9f V6
2025-10-15 00:49:03 +00:00

81 lines
3.1 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
require_once 'includes/api_keys.php'; // Include Stripe API keys
// Redirect to login if user is not logged in
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
$userId = $_SESSION['user_id'];
$pdoconnection = db();
// Fetch cart items
$stmt = $pdoconnection->prepare("SELECT c.id, mi.name, mi.price, c.quantity, r.name as restaurant_name, r.id as restaurant_id FROM cart c JOIN menu_items mi ON c.menu_item_id = mi.id JOIN restaurants r ON mi.restaurant_id = r.id WHERE c.user_id = :user_id");
$stmt->bindParam(':user_id', $userId);
$stmt->execute();
$cartItems = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($cartItems)) {
header("Location: cart.php");
exit();
}
$totalPrice = 0;
$restaurantId = $cartItems[0]['restaurant_id'];
$restaurantName = $cartItems[0]['restaurant_name'];
foreach ($cartItems as $item) {
$totalPrice += $item['price'] * $item['quantity'];
}
include 'header.php';
?>
<script src="https://js.stripe.com/v3/"></script>
<div class="container mt-5">
<h2 class="text-center mb-4">Checkout</h2>
<div class="row">
<div class="col-md-8">
<h4>Delivery Information</h4>
<form action="create_stripe_session.php" method="POST">
<div class="mb-3">
<label for="name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="mb-3">
<label for="address" class="form-label">Address</label>
<input type="text" class="form-control" id="address" name="address" required>
</div>
<div class="mb-3">
<label for="phone" class="form-label">Phone Number</label>
<input type="text" class="form-control" id="phone" name="phone" required>
</div>
<input type="hidden" name="restaurant_id" value="<?php echo $restaurantId; ?>">
<input type="hidden" name="total_price" value="<?php echo $totalPrice; ?>">
<button type="submit" class="btn btn-primary">Proceed to Payment</button>
</form>
</div>
<div class="col-md-4">
<h4>Order Summary</h4>
<h5><?php echo htmlspecialchars($restaurantName); ?></h5>
<ul class="list-group mb-3">
<?php foreach ($cartItems as $item):
?>
<li class="list-group-item d-flex justify-content-between align-items-center">
<?php echo htmlspecialchars($item['name']); ?> (x<?php echo $item['quantity']; ?>)
<span>$<?php echo number_format($item['price'] * $item['quantity'], 2); ?></span>
</li>
<?php endforeach; ?>
<li class="list-group-item d-flex justify-content-between align-items-center fw-bold">
Total
<span>$<?php echo number_format($totalPrice, 2); ?></span>
</li>
</ul>
</div>
</div>
</div>
<?php include 'footer.php'; ?>