-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuy_ticket.php
More file actions
446 lines (395 loc) · 15.7 KB
/
Copy pathbuy_ticket.php
File metadata and controls
446 lines (395 loc) · 15.7 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
session_start();
include 'config/db.php';
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
die("Please <a href='login.php'>login</a> to buy tickets.");
}
$user_id = $_SESSION['user_id'];
// Handle both POST (from seat selection) and GET (direct link)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Multiple seats from seat selection form
$event_id = isset($_POST['event_id']) ? intval($_POST['event_id']) : 0;
$seats_json = isset($_POST['seats']) ? $_POST['seats'] : '';
if (!$event_id || empty($seats_json)) {
die("Invalid event or seat selection.");
}
$selected_seats = json_decode($seats_json, true);
if (!is_array($selected_seats) || empty($selected_seats)) {
die("Invalid seat selection.");
}
$seat_numbers = $selected_seats;
} else {
// Single seat from GET (legacy support)
$event_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$seat_number = isset($_GET['seat']) ? htmlspecialchars($_GET['seat']) : '';
if ($event_id <= 0 || empty($seat_number)) {
die("Invalid event or seat selection.");
}
$seat_numbers = [$seat_number];
}
// Fetch event details
$stmt = $conn->prepare("SELECT id, title, price FROM events WHERE id = ?");
$stmt->execute([$event_id]);
$event = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$event) {
die("Event not found.");
}
$price_per_seat = $event['price'];
$total_price = $price_per_seat * count($seat_numbers);
// Mock Razorpay Key for testing
$razorpay_key = "rzp_test_mock_test_key_12345";
$use_mock_payment = true;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="assets/css/style.css">
<title>Buy Tickets - Checkout</title>
<style>
body {
font-family: sans-serif;
background-color: #111827;
color: #e5e7eb;
padding: 20px;
}
.container {
max-width: 600px;
margin: 0 auto;
}
h1 {
color: #e5e7eb;
margin-bottom: 20px;
text-align: center;
}
.checkout-details {
background-color: #1f2937;
padding: 20px;
border-radius: 8px;
margin-bottom: 20px;
}
.detail-row {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 0;
border-bottom: 1px solid #374151;
}
.detail-row:last-child {
border-bottom: none;
}
.detail-label {
color: #9CA3AF;
font-weight: 600;
}
.detail-value {
color: #e5e7eb;
text-align: right;
}
.seats-list {
background: #111827;
padding: 12px;
border-radius: 4px;
margin-top: 8px;
word-break: break-word;
}
.price-section {
background: #064e3b;
padding: 20px;
border-radius: 8px;
margin-bottom: 20px;
}
.price-row {
display: flex;
justify-content: space-between;
margin-bottom: 10px;
color: #86efac;
}
.price-row:last-child {
border-top: 1px solid #10b981;
padding-top: 10px;
font-size: 18px;
font-weight: bold;
}
.button-group {
display: flex;
gap: 10px;
justify-content: center;
flex-wrap: wrap;
}
button {
padding: 12px 30px;
border: none;
border-radius: 6px;
cursor: pointer;
font-weight: bold;
font-size: 16px;
transition: all 0.3s;
}
#rzp-button1 {
background: linear-gradient(135deg, #6366f1, #22d3ee);
color: #040025;
flex: 1;
min-width: 200px;
}
#rzp-button1:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(99, 102, 241, 0.4);
}
.back-btn {
background: #6b7280;
color: white;
text-decoration: none;
flex: 1;
min-width: 150px;
text-align: center;
display: inline-block;
}
.back-btn:hover {
background: #4b5563;
}
.info-box {
background: #0c4a6e;
border-left: 4px solid #0284c7;
padding: 12px;
margin-bottom: 20px;
border-radius: 4px;
color: #7dd3fc;
}
</style>
</head>
<body>
<div class="container">
<h1>🎫 Checkout</h1>
<div class="info-box">
ℹ️ Review your booking details below and proceed to payment
</div>
<div class="checkout-details">
<div class="detail-row">
<span class="detail-label">Event</span>
<span class="detail-value"><?php echo htmlspecialchars($event['title']); ?></span>
</div>
<div class="detail-row">
<span class="detail-label">Number of Seats</span>
<span class="detail-value"><?php echo count($seat_numbers); ?></span>
</div>
<div class="detail-row">
<span class="detail-label">Selected Seats</span>
<span class="detail-value" style="text-align: left;">
<div class="seats-list">
<?php echo implode(', ', array_map(function($s) { return "Seat $s"; }, $seat_numbers)); ?>
</div>
</span>
</div>
<div class="detail-row">
<span class="detail-label">Price per Seat</span>
<span class="detail-value">₹<?php echo number_format($price_per_seat, 2); ?></span>
</div>
</div>
<div class="price-section">
<div class="price-row">
<span>Subtotal (<?php echo count($seat_numbers); ?> seat<?php echo count($seat_numbers) !== 1 ? 's' : ''; ?>)</span>
<span>₹<?php echo number_format($total_price, 2); ?></span>
</div>
<div class="price-row">
<span>Processing Fee</span>
<span>₹0.00</span>
</div>
<div class="price-row">
<span>Total Amount</span>
<span>₹<?php echo number_format($total_price, 2); ?></span>
</div>
</div>
<div class="button-group">
<button id="rzp-button1">💳 Pay with Razorpay</button>
<a href="select_seat.php?id=<?php echo $event_id; ?>" class="back-btn">← Back to Seats</a>
</div>
</div>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var useMockPayment = <?php echo $use_mock_payment ? 'true' : 'false'; ?>;
var eventId = <?php echo $event_id; ?>;
var totalPrice = <?php echo $total_price; ?>;
var seats = <?php echo json_encode($seat_numbers); ?>;
document.getElementById('rzp-button1').onclick = function(e){
e.preventDefault();
if (useMockPayment) {
mockPaymentPopup();
} else {
realRazorpayPayment();
}
}
function mockPaymentPopup() {
var modal = document.createElement('div');
modal.style.cssText = `
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,0.7);
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
font-family: sans-serif;
`;
var popup = document.createElement('div');
popup.style.cssText = `
background: white;
padding: 30px;
border-radius: 10px;
width: 90%;
max-width: 400px;
box-shadow: 0 10px 40px rgba(0,0,0,0.3);
`;
var seatsText = seats.map(s => 'Seat ' + s).join(', ');
popup.innerHTML = `
<div style="text-align: center;">
<h2 style="color: #1f2937; margin-bottom: 10px;">🏦 Mock Payment Gateway</h2>
<p style="color: #6b7280; margin-bottom: 20px;">Testing Mode - No Real Payment</p>
<div style="background-color: #f3f4f6; padding: 15px; border-radius: 5px; margin-bottom: 20px; text-align: left;">
<p style="color: #374151; margin: 5px 0;"><strong>Event:</strong> <?php echo htmlspecialchars($event['title']); ?></p>
<p style="color: #374151; margin: 5px 0;"><strong>Seats:</strong> ` + seatsText + `</p>
<p style="color: #374151; margin: 5px 0;"><strong>Amount:</strong> ₹` + totalPrice.toFixed(2) + `</p>
</div>
<p style="color: #d97706; font-size: 14px; margin-bottom: 20px;">
⚠️ This is a test payment. Select an option below:
</p>
<button id="mock-pay-success" style="
width: 100%;
padding: 12px;
margin-bottom: 10px;
background-color: #10b981;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
">✓ Simulate Success</button>
<button id="mock-pay-failure" style="
width: 100%;
padding: 12px;
margin-bottom: 10px;
background-color: #ef4444;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
">✗ Simulate Failure</button>
<button id="mock-pay-cancel" style="
width: 100%;
padding: 12px;
background-color: #6b7280;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
">Cancel</button>
</div>
`;
modal.appendChild(popup);
document.body.appendChild(modal);
document.getElementById('mock-pay-success').onclick = function() {
var mockPaymentId = "pay_mock_" + generateRandomId();
modal.remove();
// Submit form to save booking
var form = document.createElement('form');
form.method = 'POST';
form.action = 'save_booking.php';
var eventInput = document.createElement('input');
eventInput.type = 'hidden';
eventInput.name = 'event_id';
eventInput.value = eventId;
form.appendChild(eventInput);
var seatsInput = document.createElement('input');
seatsInput.type = 'hidden';
seatsInput.name = 'seats';
seatsInput.value = JSON.stringify(seats);
form.appendChild(seatsInput);
var paymentInput = document.createElement('input');
paymentInput.type = 'hidden';
paymentInput.name = 'payment_id';
paymentInput.value = mockPaymentId;
form.appendChild(paymentInput);
var statusInput = document.createElement('input');
statusInput.type = 'hidden';
statusInput.name = 'status';
statusInput.value = 'Paid';
form.appendChild(statusInput);
document.body.appendChild(form);
form.submit();
};
document.getElementById('mock-pay-failure').onclick = function() {
modal.remove();
alert("❌ Payment Failed - Test Mode\n\nThis is a simulated failure for testing purposes.");
};
document.getElementById('mock-pay-cancel').onclick = function() {
modal.remove();
alert("Payment cancelled.");
};
}
function realRazorpayPayment() {
if (typeof Razorpay === 'undefined') {
alert("Error: Razorpay SDK not loaded. Check internet connection.");
return;
}
var razorpayKey = "<?php echo $razorpay_key; ?>";
var options = {
"key": razorpayKey,
"amount": Math.round(totalPrice * 100),
"currency": "INR",
"name": "Event Booking",
"description": "Tickets for <?php echo htmlspecialchars($event['title']); ?>",
"handler": function (response){
if(response.razorpay_payment_id) {
var form = document.createElement('form');
form.method = 'POST';
form.action = 'save_booking.php';
var eventInput = document.createElement('input');
eventInput.type = 'hidden';
eventInput.name = 'event_id';
eventInput.value = eventId;
form.appendChild(eventInput);
var seatsInput = document.createElement('input');
seatsInput.type = 'hidden';
seatsInput.name = 'seats';
seatsInput.value = JSON.stringify(seats);
form.appendChild(seatsInput);
var paymentInput = document.createElement('input');
paymentInput.type = 'hidden';
paymentInput.name = 'payment_id';
paymentInput.value = response.razorpay_payment_id;
form.appendChild(paymentInput);
var statusInput = document.createElement('input');
statusInput.type = 'hidden';
statusInput.name = 'status';
statusInput.value = 'Paid';
form.appendChild(statusInput);
document.body.appendChild(form);
form.submit();
}
},
"prefill": {
"name": "<?php echo isset($_SESSION['username']) ? htmlspecialchars($_SESSION['username']) : ''; ?>",
"email": "<?php echo isset($_SESSION['email']) ? htmlspecialchars($_SESSION['email']) : ''; ?>"
},
"theme": {
"color": "#6366f1"
}
};
var rzp1 = new Razorpay(options);
rzp1.open();
}
function generateRandomId() {
return Math.random().toString(36).substr(2, 9);
}
</script>
</body>
</html>