Repository navigation
Expand file tree
/
Copy pathCart.php
More file actions
103 lines (88 loc) · 3.27 KB
/
Copy pathCart.php
File metadata and controls
103 lines (88 loc) · 3.27 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
<?php
// Include database connection
include 'db_connection.php';
require 'vendor/autoload.php';
header("Access-Control-Allow-Origin: http://localhost");
header("Content-Type: application/json");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE");
use \Firebase\JWT\JWT;
// Authenticate user using JWT
function authenticate() {
$headers = getallheaders();
if (isset($headers['Authorization'])) {
$token = str_replace('Bearer ', '', $headers['Authorization']);
try {
$key = "YOUR_SECRET_KEY";
$decoded = JWT::decode($token, new \Firebase\JWT\Key($key, 'HS256'));
return $decoded;
} catch (Exception $e) {
http_response_code(401);
echo json_encode(["message" => "Access denied. Invalid token."]);
exit();
}
} else {
http_response_code(401);
echo json_encode(["message" => "Access denied. No token provided."]);
exit();
}
}
$user = authenticate();
// Handle cart operations
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'POST': // Add to cart or update quantity
$product_id = intval($_POST['product_id']);
$quantity = intval($_POST['quantity']);
$user_id = $user->id;
if ($quantity <= 0) {
echo json_encode(["message" => "Quantity must be greater than 0."]);
exit();
}
$sql = "INSERT INTO Cart (user_id, product_id, quantity)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE quantity = quantity + ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("iiii", $user_id, $product_id, $quantity, $quantity);
if ($stmt->execute()) {
echo json_encode(["message" => "Product added to cart."]);
} else {
echo json_encode(["message" => "Error: " . $stmt->error]);
}
$stmt->close();
break;
case 'GET': // View cart items
$user_id = $user->id;
$sql = "SELECT Cart.id, Products.name, Products.description, Products.price, Cart.quantity
FROM Cart
JOIN Products ON Cart.product_id = Products.id
WHERE Cart.user_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$cartItems = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($cartItems);
$stmt->close();
break;
case 'DELETE': // Remove item from cart
parse_str(file_get_contents("php://input"), $delete_vars);
$product_id = intval($delete_vars['product_id']);
$user_id = $user->id;
$sql = "DELETE FROM Cart WHERE user_id = ? AND product_id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $user_id, $product_id);
if ($stmt->execute()) {
echo json_encode(["message" => "Product removed from cart."]);
} else {
echo json_encode(["message" => "Error: " . $stmt->error]);
}
$stmt->close();
break;
default:
http_response_code(405);
echo json_encode(["message" => "Method not allowed."]);
break;
}
mysqli_close($conn);
?>