-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment.php
More file actions
65 lines (56 loc) · 2 KB
/
Copy pathpayment.php
File metadata and controls
65 lines (56 loc) · 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
<?php
require('includes/db.php');
session_start();
if (!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user_id = $_SESSION['user_id'];
$address = $_POST['address'] ?? '';
$cart_json = $_POST['cart_data'] ?? '[]';
$cart_items = json_decode($cart_json, true);
if (empty($cart_items)) {
die("Cart is empty.");
}
// Calculate total (Recalculate for security)
$total_amount = 0;
// We need to fetch current prices to be secure, or trust client for this simple demo.
// I'll trust client for simplicity but normally would query DB.
// Let's do a quick query for correctness.
$valid_items = [];
foreach ($cart_items as $item) {
$stmt = $pdo->prepare("SELECT price FROM products WHERE id = ?");
$stmt->execute([$item['id']]);
$product = $stmt->fetch();
if ($product) {
$price = $product['price'];
$qty = $item['quantity'];
$total_amount += $price * $qty;
$valid_items[] = [
'id' => $item['id'],
'price' => $price,
'qty' => $qty
];
}
}
if (count($valid_items) > 0) {
// Create Order
$stmt = $pdo->prepare("INSERT INTO orders (user_id, total_amount, status, address) VALUES (?, ?, 'pending', ?)");
$stmt->execute([$user_id, $total_amount, $address]);
$order_id = $pdo->lastInsertId();
// Create Order Items
$stmtObj = $pdo->prepare("INSERT INTO order_items (order_id, product_id, quantity, price) VALUES (?, ?, ?, ?)");
foreach ($valid_items as $vi) {
$stmtObj->execute([$order_id, $vi['id'], $vi['qty'], $vi['price']]);
}
// Redirect to success
header("Location: order_success.php?id=$order_id");
exit();
} else {
echo "Error: No valid items found.";
}
} else {
header("Location: index.php");
}
?>