forked from SAMBUDAR/Styleka
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcart.html
More file actions
363 lines (310 loc) · 9.68 KB
/
cart.html
File metadata and controls
363 lines (310 loc) · 9.68 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<!DOCTYPE html>
<html>
<head>
<title>My Cart</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f9f9f9;
}
h1 {
text-align: center;
color: #333;
margin-top: 20px;
}
#cart-list {
list-style: none;
padding: 0;
margin: 20px auto;
max-width: 800px;
background: #fff;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
overflow: hidden;
}
#cart-list li {
display: flex;
align-items: center;
padding: 15px 20px;
border-bottom: 1px solid #ddd;
}
#cart-list li:last-child {
border-bottom: none;
}
.product-image {
width: 60px;
height: 60px;
border-radius: 8px;
margin-right: 15px;
object-fit: cover;
}
.product-details {
flex: 1;
}
.product-title {
font-weight: bold;
color: #555;
margin-bottom: 5px;
}
.product-size {
font-size: 14px;
color: #888;
}
.product-price {
font-weight: bold;
color: #444;
}
.quantity-controls {
display: flex;
align-items: center;
gap: 10px;
}
.quantity-controls button {
padding: 5px 10px;
font-size: 14px;
border: 1px solid #ccc;
background-color: #f0f0f0;
cursor: pointer;
border-radius: 4px;
}
.quantity-controls button:hover {
background-color: #e0e0e0;
}
.quantity-controls input {
width: 40px;
text-align: center;
border: 1px solid #ccc;
border-radius: 4px;
}
#total {
text-align: center;
font-size: 20px;
color: #444;
margin: 20px 0;
}
#payBtn {
display: block;
margin: 20px auto;
padding: 10px 20px;
font-size: 16px;
color: #fff;
background-color: #28a745;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s ease;
}
#payBtn:disabled {
background-color: #ccc;
cursor: not-allowed;
}
#payBtn:hover:not(:disabled) {
background-color: #218838;
}
.empty-cart {
text-align: center;
color: #888;
font-size: 18px;
margin-top: 50px;
}
#coupon-section {
max-width: 800px;
margin: 20px auto;
display: flex;
justify-content: space-between;
align-items: center;
background: #fff;
padding: 15px 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
#coupon-section input {
flex: 1;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
margin-right: 10px;
}
#coupon-section button {
padding: 10px 20px;
background-color: #088178;
color: #fff;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease;
}
#coupon-section button:hover {
background-color: #066d5f;
}
.coupon-message {
text-align: center;
margin-top: 10px;
font-size: 14px;
color: #088178;
}
.coupon-message.error {
color: #d9534f;
}
</style>
<script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-app-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-auth-compat.js"></script>
<script src="https://www.gstatic.com/firebasejs/9.6.1/firebase-database-compat.js"></script>
<script src="https://js.stripe.com/v3/"></script>
</head>
<body>
<h1>My Cart</h1>
<ul id="cart-list"></ul>
<h3 id="total">Total: ₹0</h3>
<div id="coupon-section">
<input type="text" id="coupon-code" placeholder="Enter coupon code">
<button onclick="applyCoupon()">Apply Coupon</button>
</div>
<p id="coupon-message" class="coupon-message"></p>
<button id="payBtn" disabled>Pay Now</button>
<script>
const firebaseConfig = {
apiKey: "AIzaSyCidoh3aUKsFzamLCetZkNlvbw-q_icLTY",
authDomain: "aniket-f99f3.firebaseapp.com",
projectId: "aniket-f99f3",
storageBucket: "aniket-f99f3.appspot.com",
messagingSenderId: "640193454368",
appId: "1:640193454368:web:336be90f18f5278a16f745",
databaseURL: "https://aniket-f99f3-default-rtdb.firebaseio.com"
};
firebase.initializeApp(firebaseConfig);
const cartList = document.getElementById('cart-list');
const totalDisplay = document.getElementById('total');
const payBtn = document.getElementById('payBtn');
const couponMessage = document.getElementById('coupon-message');
let totalAmount = 0;
let discount = 0;
firebase.auth().onAuthStateChanged(user => {
if (user) {
const userId = user.uid;
const cartRef = firebase.database().ref('cart/' + userId);
cartRef.on('value', snapshot => {
cartList.innerHTML = '';
totalAmount = 0;
if (!snapshot.exists()) {
cartList.innerHTML = '<p class="empty-cart">Your cart is empty.</p>';
totalDisplay.textContent = 'Total: ₹0';
payBtn.disabled = true;
return;
}
snapshot.forEach(child => {
const item = child.val();
const price = parseInt(item.price.replace('₹', '').replace(',', '')) || 0;
const quantity = parseInt(item.quantity) || 1;
totalAmount += price * quantity;
const li = document.createElement('li');
li.innerHTML = `
<img src="${item.image}" alt="${item.title}" class="product-image">
<div class="product-details">
<div class="product-title">${item.title}</div>
<div class="product-size">Size: ${item.size}</div>
<div class="product-price">₹${price * quantity}</div>
<div class="quantity-controls">
<button onclick="updateQuantity('${child.key}', ${quantity - 1})">-</button>
<input type="number" value="${quantity}" min="1" readonly>
<button onclick="updateQuantity('${child.key}', ${quantity + 1})">+</button>
</div>
</div>
`;
cartList.appendChild(li);
});
updateTotal();
payBtn.disabled = false;
});
} else {
alert("User not logged in. Please log in.");
}
});
function updateQuantity(itemKey, newQuantity) {
if (newQuantity < 1) return;
const user = firebase.auth().currentUser;
if (user) {
const userId = user.uid;
const itemRef = firebase.database().ref(`cart/${userId}/${itemKey}`);
itemRef.update({ quantity: newQuantity });
}
}
function applyCoupon() {
const couponCode = document.getElementById('coupon-code').value.trim();
couponMessage.textContent = '';
couponMessage.classList.remove('error');
if (couponCode === 'DISCOUNT10') {
discount = 0.1; // 10% discount
couponMessage.textContent = 'Coupon applied! You get 10% off.';
} else if (couponCode === 'Aniket18') {
discount = 0.3; // 30% discount
couponMessage.textContent = 'Coupon applied! You get 30% off.';
} else {
discount = 0;
couponMessage.textContent = 'Invalid coupon code.';
couponMessage.classList.add('error');
}
updateTotal();
}
function updateTotal() {
const discountedTotal = totalAmount - totalAmount * discount;
totalDisplay.textContent = `Total: ₹${discountedTotal.toFixed(2)}`;
}
payBtn.addEventListener('click', async () => {
try {
const user = firebase.auth().currentUser;
if (!user) return alert("Please log in first.");
const cartRef = firebase.database().ref('cart/' + user.uid);
const snapshot = await cartRef.once('value');
const items = [];
snapshot.forEach(child => {
const item = child.val();
const price = parseInt(item.price.replace('₹', '').replace(',', '')) || 0;
const quantity = parseInt(item.quantity) || 1;
// Apply discount to each item's price
const discountedPrice = price - price * discount;
items.push({
title: item.title,
price: discountedPrice.toFixed(2), // Send discounted price
quantity: quantity
});
});
const response = await fetch('http://localhost:4242/create-checkout-session', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ items }) // Send discounted items
});
const data = await response.json();
if (!response.ok) {
console.error("Error response from server:", data);
alert("Something went wrong: " + data.error);
}
if (data.id) {
const stripe = Stripe("pk_test_51REOhFECJtYsBmLByllvEwttdOUt4mUF8PDMEivSWcEyS4yji8WecBezyZGBLc1mXywdqjE48Y1bwGpb3FXIW7FK00mG2XzHXK");
stripe.redirectToCheckout({
sessionId: data.id
}).then(result => {
if (result.error) {
alert("Payment failed: " + result.error.message);
} else {
emptyCart(user.uid); // Empty the cart after successful payment
window.location.href = "success.html"; // Redirect to success page
}
});
} else {
alert("No session id returned from server.");
}
} catch (err) {
console.error("Error during payment:", err);
alert("Payment failed: " + err.message);
}
});
function emptyCart(userId) {
const cartRef = firebase.database().ref('cart/' + userId);
cartRef.remove();
}
</script>
</body>
</html>