Repository navigation
Expand file tree
/
Copy pathcheckout.html
More file actions
360 lines (322 loc) · 18.2 KB
/
Copy pathcheckout.html
File metadata and controls
360 lines (322 loc) · 18.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Checkout — L'Essence Luxury Perfumes">
<title>Checkout — L'Essence</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
<link rel="icon" type="image/svg+xml" href="./assets/favicon.svg">
<link rel="stylesheet" href="./css/styles.css">
<link rel="stylesheet" href="./css/responsive.css">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-luxury fixed-top" id="main-navbar"></nav>
<section class="checkout-section">
<div class="container">
<div class="section-title" style="margin-bottom:20px;">
<h2>Checkout</h2>
</div>
<div class="checkout-progress" id="checkout-progress">
<div class="step active" data-step="1">
<span class="step-number">1</span>
<span>Shipping</span>
</div>
<div class="step-line"></div>
<div class="step" data-step="2">
<span class="step-number">2</span>
<span>Payment</span>
</div>
<div class="step-line"></div>
<div class="step" data-step="3">
<span class="step-number">3</span>
<span>Review</span>
</div>
</div>
<div class="row g-4">
<div class="col-lg-8">
<div class="checkout-form">
<div class="checkout-step active" id="step-1">
<h4 class="mb-4" style="font-family:var(--font-heading);">Shipping Information</h4>
<div class="row g-3">
<div class="col-md-6">
<label for="ship-name" class="form-label">Full Name</label>
<input type="text" class="form-control" id="ship-name" required>
</div>
<div class="col-md-6">
<label for="ship-email" class="form-label">Email</label>
<input type="email" class="form-control" id="ship-email" required>
</div>
<div class="col-md-6">
<label for="ship-phone" class="form-label">Phone</label>
<input type="tel" class="form-control" id="ship-phone" required>
</div>
<div class="col-md-6">
<label for="ship-zip" class="form-label">ZIP Code</label>
<input type="text" class="form-control" id="ship-zip" required>
</div>
<div class="col-12">
<label for="ship-address" class="form-label">Street Address</label>
<input type="text" class="form-control" id="ship-address" required>
</div>
<div class="col-md-6">
<label for="ship-city" class="form-label">City</label>
<input type="text" class="form-control" id="ship-city" required>
</div>
<div class="col-md-6">
<label for="ship-country" class="form-label">Country</label>
<select class="form-select" id="ship-country" required>
<option value="">Select country</option>
<option value="US">United States</option>
<option value="CA">Canada</option>
<option value="UK">United Kingdom</option>
<option value="FR">France</option>
<option value="DE">Germany</option>
<option value="IT">Italy</option>
<option value="AE">UAE</option>
<option value="EG">Egypt</option>
<option value="SA">Saudi Arabia</option>
</select>
</div>
<div class="col-12 mt-4">
<button class="btn btn-gold" id="btn-to-step2"><span>Continue to Payment <i
class="bi bi-arrow-right ms-2"></i></span></button>
</div>
</div>
</div>
<div class="checkout-step" id="step-2">
<h4 class="mb-4" style="font-family:var(--font-heading);">Payment Method</h4>
<div class="payment-method" id="payment-methods">
<div class="method-option active" data-method="card">
<i class="bi bi-credit-card"></i>
<span>Credit Card</span>
</div>
<div class="method-option" data-method="cod">
<i class="bi bi-cash-stack"></i>
<span>Cash on Delivery</span>
</div>
</div>
<div id="card-fields">
<div class="row g-3">
<div class="col-12">
<label for="card-number" class="form-label">Card Number</label>
<input type="text" class="form-control" id="card-number"
placeholder="1234 5678 9012 3456" maxlength="19">
</div>
<div class="col-md-6">
<label for="card-expiry" class="form-label">Expiry Date</label>
<input type="text" class="form-control" id="card-expiry" placeholder="MM/YY"
maxlength="5">
</div>
<div class="col-md-6">
<label for="card-cvv" class="form-label">CVV</label>
<input type="text" class="form-control" id="card-cvv" placeholder="123"
maxlength="4">
</div>
</div>
</div>
<div class="d-flex gap-3 mt-4">
<button class="btn btn-outline-gold" id="btn-back-step1"><i
class="bi bi-arrow-left me-2"></i>Back</button>
<button class="btn btn-gold" id="btn-to-step3"><span>Review Order <i
class="bi bi-arrow-right ms-2"></i></span></button>
</div>
</div>
<div class="checkout-step" id="step-3">
<h4 class="mb-4" style="font-family:var(--font-heading);">Order Review</h4>
<div id="review-items" class="mb-4"></div>
<div class="row g-4 mb-4">
<div class="col-md-6">
<h6 style="font-family:var(--font-heading);">Shipping Address</h6>
<div id="review-address" class="text-muted"
style="font-size:0.9rem;line-height:1.8;"></div>
</div>
<div class="col-md-6">
<h6 style="font-family:var(--font-heading);">Payment Method</h6>
<div id="review-payment" class="text-muted" style="font-size:0.9rem;"></div>
</div>
</div>
<div class="d-flex gap-3">
<button class="btn btn-outline-gold" id="btn-back-step2"><i
class="bi bi-arrow-left me-2"></i>Back</button>
<button class="btn btn-gold" id="btn-place-order"><span><i
class="bi bi-check-circle me-2"></i>Place Order</span></button>
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="cart-summary" id="checkout-summary"></div>
</div>
</div>
</div>
</section>
<footer class="footer-luxury" id="main-footer"></footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script type="module">
import { Auth, Cart, UI, Products, Orders } from './js/app.js';
Auth.protect();
if (Cart.getItems().length === 0) {
window.location.href = 'cart.html';
}
let currentStep = 1;
let paymentMethod = 'card';
function renderSummary() {
let items = Cart.getItems();
let itemsHtml = '';
items.forEach(item => {
let p = Products.getById(item.productId);
if (!p) return;
itemsHtml += `
<div class="d-flex justify-content-between mb-2" style="font-size:0.85rem;">
<span>${p.name} (${item.volume}ml) × ${item.qty}</span>
<span>${UI.formatPrice(p.price * item.qty)}</span>
</div>`;
});
let subtotal = Cart.getSubtotal();
let shipping = Cart.getShipping();
let total = Cart.getTotal();
document.getElementById('checkout-summary').innerHTML = `
<h4>Order Summary</h4>
${itemsHtml}
<hr>
<div class="summary-row"><span>Subtotal</span><span>${UI.formatPrice(subtotal)}</span></div>
<div class="summary-row"><span>Shipping</span><span>${shipping === 0 ? '<span class="text-gold">Free</span>' : UI.formatPrice(shipping)}</span></div>
<div class="summary-row total"><span>Total</span><span>${UI.formatPrice(total)}</span></div>`;
}
renderSummary();
function goToStep(step) {
currentStep = step;
document.querySelectorAll('.checkout-step').forEach(s => s.classList.remove('active'));
document.getElementById(`step-${step}`).classList.add('active');
document.querySelectorAll('.checkout-progress .step').forEach(s => {
let sNum = parseInt(s.dataset.step);
s.classList.toggle('active', sNum === step);
s.classList.toggle('completed', sNum < step);
});
document.querySelectorAll('.checkout-progress .step-line').forEach((line, i) => {
line.classList.toggle('active', i < step - 1);
});
window.scrollTo({ top: 0, behavior: 'smooth' });
}
document.getElementById('btn-to-step2').addEventListener('click', () => {
let fields = ['ship-name', 'ship-email', 'ship-phone', 'ship-address', 'ship-city', 'ship-country', 'ship-zip'];
let valid = true;
fields.forEach(id => {
let el = document.getElementById(id);
el.classList.remove('is-invalid');
if (!el.value.trim()) {
el.classList.add('is-invalid');
valid = false;
}
});
let emailEl = document.getElementById('ship-email');
if (emailEl.value && !/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(emailEl.value)) {
emailEl.classList.add('is-invalid');
valid = false;
}
if (valid) goToStep(2);
});
document.querySelectorAll('.method-option').forEach(opt => {
opt.addEventListener('click', () => {
document.querySelectorAll('.method-option').forEach(o => o.classList.remove('active'));
opt.classList.add('active');
paymentMethod = opt.dataset.method;
document.getElementById('card-fields').style.display = paymentMethod === 'card' ? 'block' : 'none';
});
});
document.getElementById('btn-to-step3').addEventListener('click', () => {
if (paymentMethod === 'card') {
let cardNum = document.getElementById('card-number');
let cardExp = document.getElementById('card-expiry');
let cardCvv = document.getElementById('card-cvv');
let valid = true;
[cardNum, cardExp, cardCvv].forEach(el => el.classList.remove('is-invalid'));
if (!cardNum.value || cardNum.value.replace(/\s/g, '').length < 13) {
cardNum.classList.add('is-invalid');
valid = false;
}
if (!cardExp.value || !/^\d{2}\/\d{2}$/.test(cardExp.value)) {
cardExp.classList.add('is-invalid');
valid = false;
}
if (!cardCvv.value || cardCvv.value.length < 3) {
cardCvv.classList.add('is-invalid');
valid = false;
}
if (!valid) return;
}
let items = Cart.getItems();
let reviewHtml = '';
items.forEach(item => {
let p = Products.getById(item.productId);
if (!p) return;
reviewHtml += `
<div class="order-review-item">
<img src="${p.images[0]}" alt="${p.name}">
<div class="item-details">
<h6>${p.name}</h6>
<small>${item.volume}ml — Qty: ${item.qty}</small>
</div>
<div class="item-price">${UI.formatPrice(p.price * item.qty)}</div>
</div>`;
});
document.getElementById('review-items').innerHTML = reviewHtml;
let name = document.getElementById('ship-name').value;
let address = document.getElementById('ship-address').value;
let city = document.getElementById('ship-city').value;
let country = document.getElementById('ship-country');
let countryName = country.options[country.selectedIndex].text;
let zip = document.getElementById('ship-zip').value;
document.getElementById('review-address').innerHTML = `
<strong>${name}</strong><br>
${address}<br>
${city}, ${countryName} ${zip}`;
if (paymentMethod === 'card') {
let cardNum = document.getElementById('card-number').value;
let last4 = cardNum.replace(/\s/g, '').slice(-4);
document.getElementById('review-payment').innerHTML = `<i class="bi bi-credit-card me-2"></i>Credit Card ending in ${last4}`;
} else {
document.getElementById('review-payment').innerHTML = `<i class="bi bi-cash-stack me-2"></i>Cash on Delivery`;
}
goToStep(3);
});
document.getElementById('btn-back-step1').addEventListener('click', () => goToStep(1));
document.getElementById('btn-back-step2').addEventListener('click', () => goToStep(2));
document.getElementById('btn-place-order').addEventListener('click', () => {
let orderData = {
orderId: 'LE-' + Math.random().toString(36).substr(2, 9).toUpperCase(),
items: Cart.getItems().map(item => {
let p = Products.getById(item.productId);
return { name: p.name, volume: item.volume, qty: item.qty, price: p.price, image: p.images[0] };
}),
shippingAddress: {
name: document.getElementById('ship-name').value,
address: document.getElementById('ship-address').value,
city: document.getElementById('ship-city').value,
country: document.getElementById('ship-country').options[document.getElementById('ship-country').selectedIndex].text,
zip: document.getElementById('ship-zip').value
},
paymentMethod: paymentMethod === 'card' ? 'Credit Card' : 'Cash on Delivery',
subtotal: Cart.getSubtotal(),
shipping: Cart.getShipping(),
total: Cart.getTotal()
};
localStorage.setItem('perfume_last_order', JSON.stringify(orderData));
Orders.save(orderData);
window.location.href = 'confirmation.html';
});
document.getElementById('card-number').addEventListener('input', function () {
let val = this.value.replace(/\D/g, '');
val = val.match(/.{1,4}/g)?.join(' ') || val;
this.value = val;
});
document.getElementById('card-expiry').addEventListener('input', function () {
let val = this.value.replace(/\D/g, '');
if (val.length >= 2) val = val.substring(0, 2) + '/' + val.substring(2);
this.value = val;
});
</script>
</body>
</html>