Repository navigation
Expand file tree
/
Copy pathcheckout.php
More file actions
332 lines (290 loc) · 15.2 KB
/
Copy pathcheckout.php
File metadata and controls
332 lines (290 loc) · 15.2 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
if (!isset($_SERVER['HTTP_REFERER'])){
header("Location: http://localhost/Freshcery/index.php");
exit();
}
?>
<?php
include_once "includes/header.php";
include_once "config/config.php";
?>
<?php
if (!isset($_SESSION['user'])){
header('Location: localhost/Freshcery/');
exit();
}
?>
<?php
if (isset($_POST['submit'])){
// Retrieve and sanitize input
$user_id = $_SESSION['user']['id']; // Getting user ID
$first_name = sanitizeInput($_POST['first_name']);
$last_name = sanitizeInput($_POST['last_name']);
$company_name = sanitizeInput($_POST['company_name']);
$address = sanitizeInput($_POST['address']);
$town_city = sanitizeInput($_POST['town_city']);
$state_country = sanitizeInput($_POST['state_country']);
$postcode_zip = sanitizeInput($_POST['postcode_zip']);
$email = sanitizeInput($_POST['email']);
$phone = sanitizeInput($_POST['phone']);
$order_notes = sanitizeInput($_POST['order_notes']);
if (isset($_SESSION['price'])){
$total_price = (float)$_SESSION['price'] + 20; // 20 USD for shipping
$_SESSION['total_price'] = $total_price;
}
// Validate required fields
if (
empty($first_name) || empty($last_name) ||
empty($address) || empty($town_city) ||
empty($state_country) || empty($postcode_zip) ||
empty($email) || empty($phone)
)
{
$errors[] = 'Please fill in all required fields.';
}
// Validate email
if (
!filter_var($email, FILTER_VALIDATE_EMAIL)
)
{
$errors[] = 'Invalid email format.';
}
// Validate phone number
if (!preg_match('/^[\d\s\+\-()]+$/', $phone)) {
$errors[] = 'Invalid phone number format.';
}
// If there are no errors, proceed to insert data
if (empty($errors)) {
try {
// Prepare the SQL statement
$stmt = $conn->prepare("
INSERT INTO orders
(user_id, first_name, last_name, company_name, address, town_city, state_country, postcode_zip, email, phone, order_notes, total_price)
VALUES
(:user_id, :first_name, :last_name, :company_name, :address, :town_city, :state_country, :postcode_zip, :email, :phone, :order_notes, :total_price)
");
// Bind parameters
$stmt->bindParam(':user_id', $user_id);
$stmt->bindParam(':first_name', $first_name);
$stmt->bindParam(':last_name', $last_name);
$stmt->bindParam(':company_name', $company_name);
$stmt->bindParam(':address', $address);
$stmt->bindParam(':town_city', $town_city);
$stmt->bindParam(':state_country', $state_country);
$stmt->bindParam(':postcode_zip', $postcode_zip);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':phone', $phone);
$stmt->bindParam(':order_notes', $order_notes);
$stmt->bindParam(':total_price', $total_price);
// Execute the statement
$stmt->execute();
// Set session variable to indicate success
$_SESSION['checkout_success'] = true;
// Redirect to the charge page
echo "<script> window.location.href = 'charge.php' </script>";
} catch (PDOException $e) {
// Handle SQL errors
$errors[] = 'Error: ' . $e->getMessage();
}
}
// If there are errors, join them into a single string for display
$error_messages = '';
if (!empty($errors)) {
foreach ($errors as $error) {
$error_messages .= '<div class="alert alert-danger">' . htmlspecialchars($error) . '</div>';
}
}
}
?>
<?php
$total_price = $_SESSION['price'];
$products = $_SESSION['products']
?>
<div id="page-content" class="page-content">
<div class="banner">
<div class="jumbotron jumbotron-bg text-center rounded-0" style="background-image: url('assets/img/bg-header.jpg');">
<div class="container">
<h1 class="pt-5">
Checkout
</h1>
<p class="lead">
Save time and leave the groceries to us.
</p>
</div>
</div>
</div>
<section id="checkout">
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-7">
<h5 class="mb-3">BILLING DETAILS</h5>
<!-- Bill Detail of the Page -->
<form action="checkout.php" method="POST" class="bill-detail">
<?php if (!empty($error_messages)): ?>
<?php echo $error_messages; ?>
<?php endif; ?>
<fieldset>
<div class="form-group row">
<div class="col">
<input
class="form-control" placeholder="Name"
type="text" name="first_name"
value="<?php echo htmlspecialchars($_POST['first_name'] ?? ''); ?>"
>
</div>
<div class="col">
<input
class="form-control" placeholder="Last Name"
type="text" name="last_name"
value="<?php echo htmlspecialchars($_POST['last_name'] ?? ''); ?>"
>
</div>
</div>
<div class="form-group">
<input
class="form-control" placeholder="Company Name"
type="text" name="company_name"
value="<?php echo htmlspecialchars($_POST['company_name'] ?? ''); ?>"
>
</div>
<div class="form-group">
<textarea
class="form-control" placeholder="Address"
name="address"
value="<?php echo htmlspecialchars($_POST['address'] ?? ''); ?>"
></textarea>
</div>
<div class="form-group">
<input
class="form-control" placeholder="Town / City"
type="text" name="town_city"
value="<?php echo htmlspecialchars($_POST['town_city'] ?? ''); ?>"
>
</div>
<div class="form-group">
<input
class="form-control" placeholder="State / Country"
type="text" name="state_country"
value="<?php echo htmlspecialchars($_POST['state_country'] ?? ''); ?>"
>
</div>
<div class="form-group">
<input
class="form-control" placeholder="Postcode / Zip"
type="text" name="postcode_zip"
value="<?php echo htmlspecialchars($_POST['postcode_zip'] ?? ''); ?>"
>
</div>
<div class="form-group row">
<div class="col">
<input
class="form-control" placeholder="Email Address"
type="email" name="email"
value="<?php echo htmlspecialchars($_POST['email'] ?? ''); ?>"
>
</div>
<div class="col">
<input
class="form-control" placeholder="Phone Number"
type="tel" name="phone"
value="<?php echo htmlspecialchars($_POST['phone'] ?? ''); ?>"
>
</div>
</div>
<div class="form-group">
<textarea
class="form-control" placeholder="Order Notes"
name="order_notes"
value="<?php echo htmlspecialchars($_POST['order_notes'] ?? ''); ?>"
>
</textarea>
</div>
</fieldset>
<div class="form-group">
<button
type="submit"
name="submit"
class="btn btn-primary float-left">
PROCEED TO CHECKOUT
<i class="fa fa-check"></i>
</button>
</div>
</form>
<!-- Bill Detail of the Page end -->
</div>
<div class="col-xs-12 col-sm-5">
<div class="holder">
<h5 class="mb-3">YOUR ORDER</h5>
<div class="table-responsive">
<table class="table table-striped table-hover mt-4">
<thead>
<tr>
<th>Products</th>
<th class="text-right">Subtotal</th>
</tr>
</thead>
<tbody>
<?php
$total = 0;
$shipping = 20;
foreach($products as $product):
$discount = $product['discount'];
$price = $product['price'];
$quantity = $product['product_quantity'];
// Discounted price
$discounted_price = $price - ($price * $discount / 100);
// Subtotal
$subtotal = $discounted_price * $quantity;
$total += $subtotal;
?>
<tr>
<td>
<?php echo $product['name']." x".$quantity?>
</td>
<td class="text-right">
USD <?=number_format($subtotal, 2, ',', '.')?>
</td>
</tr>
<?php endforeach?>
</tbody>
<tfooter>
<tr>
<td>
<strong>Cart Subtotal</strong>
</td>
<td class="text-right">
USD <?=number_format($total, 2, ',', '.')?>
</td>
</tr>
<tr>
<td>
<strong>Shipping</strong>
</td>
<td class="text-right">
USD <?=number_format($shipping, 2, ',', '.')?>
</td>
</tr>
<tr>
<td>
<strong>ORDER TOTAL</strong>
</td>
<td class="text-right">
<strong>USD <?=number_format($total+$shipping, 2, ',', '.')?></strong>
</td>
</tr>
</tfooter>
</table>
</div>
</div>
<p class="text-right mt-3">
<input checked="" type="checkbox"> I’ve read & accept the <a href="#">terms & conditions</a>
</p>
<div class="clearfix">
</div>
</div>
</div>
</section>
</div>
<?php
include_once "includes/footer.php";
?>