-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.php
More file actions
162 lines (140 loc) · 6.65 KB
/
checkout.php
File metadata and controls
162 lines (140 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
session_start();
include "db.php";
include "header.php";
// Check if the user is logged in
if (!isset($_SESSION["user_id"])) {
header("Location: login.php");
exit();
}
// Calculate total price from the cart
$total_price = 0;
if (isset($_SESSION['cart']) && !empty($_SESSION['cart'])) {
foreach ($_SESSION['cart'] as $book_id => $qty) {
$result = $conn->query("SELECT price FROM books WHERE id = $book_id");
if ($row = $result->fetch_assoc()) {
$total_price += $row["price"] * $qty;
}
}
} else {
echo "<p class='alert alert-warning text-center'>Your cart is empty!</p>";
include "footer.php";
exit();
}
// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$full_name = $_POST["full_name"];
$phone = $_POST["phone"];
$division = $_POST["division"];
$city = $_POST["city"];
$address_detail = $_POST["address"];
$payment_method = $_POST["payment_method"];
$full_address = "$full_name, $phone, $address_detail, $city, $division";
$user_id = $_SESSION["user_id"];
if ($payment_method === "cod") {
// Place order immediately for Cash on Delivery
$stmt = $conn->prepare("INSERT INTO orders (user_id, address, total_price, payment_method, status) VALUES (?, ?, ?, ?, 'pending')");
$stmt->bind_param("ssds", $user_id, $full_address, $total_price, $payment_method);
$stmt->execute();
$order_id = $conn->insert_id;
foreach ($_SESSION['cart'] as $book_id => $qty) {
$stmt = $conn->prepare("SELECT price FROM books WHERE id = ?");
$stmt->bind_param("i", $book_id);
$stmt->execute();
$result = $stmt->get_result();
$book = $result->fetch_assoc();
$price = $book['price'];
$stmt = $conn->prepare("INSERT INTO order_items (order_id, book_id, quantity, price) VALUES (?, ?, ?, ?)");
$stmt->bind_param("iiii", $order_id, $book_id, $qty, $price);
$stmt->execute();
}
unset($_SESSION['cart']);
header("Location: orders.php");
exit();
} else {
// Redirect to dummy_payment.php for online payment
$_SESSION['checkout_data'] = [
'user_id' => $user_id,
'full_address' => $full_address,
'total_price' => $total_price,
'payment_method' => $payment_method,
'cart' => $_SESSION['cart']
];
header("Location: gateway_payment.php");
exit();
}
}
?>
<!-- Checkout Form UI -->
<div class="container py-5">
<div class="row justify-content-center">
<div class="col-lg-8">
<div class="card shadow-lg border-0">
<div class="card-header bg-success text-white text-center py-3 rounded-top">
<h3 class="mb-0">Checkout</h3>
</div>
<div class="card-body bg-light">
<form method="post" action="checkout.php" onsubmit="return validateForm();">
<h5 class="mb-4 text-success fw-bold">Shipping Address</h5>
<div class="row mb-3">
<div class="col-md-6">
<label for="full_name" class="form-label">Full Name</label>
<input type="text" name="full_name" id="full_name" class="form-control" required placeholder="Enter your full name">
</div>
<div class="col-md-6">
<label for="phone" class="form-label">Phone Number</label>
<input type="text" name="phone" id="phone" class="form-control" required placeholder="e.g. 01XXXXXXXXX">
</div>
</div>
<div class="mb-3">
<label for="division" class="form-label">Division</label>
<select name="division" id="division" class="form-select" required>
<option value="">Select Division</option>
<option value="Dhaka">Dhaka</option>
<option value="Chattogram">Chattogram</option>
<option value="Khulna">Khulna</option>
<option value="Rajshahi">Rajshahi</option>
<option value="Barishal">Barishal</option>
<option value="Sylhet">Sylhet</option>
<option value="Rangpur">Rangpur</option>
<option value="Mymensingh">Mymensingh</option>
</select>
</div>
<div class="mb-3">
<label for="city" class="form-label">City / District</label>
<input type="text" name="city" id="city" class="form-control" required placeholder="e.g. Dhaka">
</div>
<div class="mb-3">
<label for="address" class="form-label">Full Address</label>
<textarea name="address" id="address" rows="3" class="form-control" required placeholder="House no, Road no, Area"></textarea>
</div>
<h5 class="mb-3 text-success fw-bold">Payment Method</h5>
<div class="mb-4">
<select name="payment_method" id="payment_method" class="form-select" required>
<option value="">Choose Payment Method</option>
<option value="bkash">bKash</option>
<option value="nagad">Nagad</option>
<option value="cod">Cash on Delivery</option>
</select>
</div>
<input type="hidden" name="total_price" value="<?= $total_price ?>">
<button type="submit" class="btn btn-success btn-lg w-100">✅ Confirm & Place Order (৳<?= $total_price ?>)</button>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
function validateForm() {
const name = document.getElementById('full_name').value.trim();
const phone = document.getElementById('phone').value.trim();
const address = document.getElementById('address').value.trim();
if (name.length < 2 || phone.length < 11 || address.length < 10) {
alert("Please fill in all fields correctly.");
return false;
}
return true;
}
</script>
<?php include "footer.php"; ?>