-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment.php
More file actions
150 lines (137 loc) · 5.86 KB
/
payment.php
File metadata and controls
150 lines (137 loc) · 5.86 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
<?php
session_start();
require_once __DIR__ . '/razorpay-php-2.9.1/Razorpay.php';
require_once 'includes/config.php';
require_once 'includes/auth.php';
use Razorpay\Api\Api;
if (!is_logged_in()) {
header('Location: login.php');
exit;
}
if (!isset($_GET['booking_id'])) {
header('Location: my_bookings.php');
exit;
}
$booking_id = $_GET['booking_id'];
// Fetch booking details
$stmt = $pdo->prepare("SELECT b.*, s.*, r.*, bu.*
FROM bookings b
JOIN schedules s ON b.schedule_id = s.schedule_id
JOIN routes r ON s.route_id = r.route_id
JOIN buses bu ON s.bus_id = bu.bus_id
WHERE b.booking_id = :booking_id AND b.user_id = :user_id");
$stmt->execute([
':booking_id' => $booking_id,
':user_id' => $_SESSION['user_id']
]);
$booking = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$booking) {
header('Location: my_bookings.php');
exit;
}
// Fetch wallet balance
$stmt = $pdo->prepare("SELECT wallet_balance FROM users WHERE user_id = :user_id");
$stmt->execute([':user_id' => $_SESSION['user_id']]);
$wallet_balance = $stmt->fetchColumn();
// Razorpay credentials
$keyId = 'rzp_test_8D84TQdTgvZcoq';
$keySecret = 'ifKuK9iDVmbRaG0CAaRpchUb';
$api = new Api($keyId, $keySecret);
// Create Razorpay Order
$orderData = [
'receipt' => 'rcptid_' . $booking_id,
'amount' => $booking['total_amount'] * 100, // in paise
'currency' => 'INR',
'payment_capture' => 1 // auto capture
];
$razorpayOrder = $api->order->create($orderData);
$razorpayOrderId = $razorpayOrder['id'];
$page_title = "Payment";
include('includes/header.php');
?>
<div class="container">
<div class="row">
<div class="col-md-8">
<div class="card mb-4">
<div class="card-header bg-primary text-white">
<h3 class="mb-0">Payment</h3>
</div>
<div class="card-body">
<div class="booking-summary mb-4">
<h4>Booking Summary</h4>
<div class="row">
<div class="col-md-6">
<p><strong>Bus:</strong> <?php echo htmlspecialchars($booking['bus_name']); ?></p>
<p><strong>Type:</strong> <?php echo htmlspecialchars($booking['bus_type']); ?></p>
<p><strong>Route:</strong> <?php echo htmlspecialchars($booking['departure_city']); ?> to <?php echo htmlspecialchars($booking['arrival_city']); ?></p>
</div>
<div class="col-md-6">
<p><strong>Departure:</strong> <?php echo date('M d, Y h:i A', strtotime($booking['departure_time'])); ?></p>
<p><strong>Seats:</strong> <?php echo htmlspecialchars($booking['seat_numbers']); ?></p>
<p><strong>Total Amount:</strong> ₹<?php echo $booking['total_amount']; ?></p>
</div>
</div>
</div>
<!-- Wallet Payment Option -->
<?php if ($wallet_balance >= $booking['total_amount']): ?>
<form method="post" action="wallet_pay.php" style="display:inline;">
<input type="hidden" name="booking_id" value="<?php echo $booking_id; ?>">
<button type="submit" class="btn btn-success btn-lg mb-2">Pay with Wallet (₹<?php echo $booking['total_amount']; ?>)</button>
</form>
<span class="mx-2">or</span>
<?php endif; ?>
<!-- Razorpay Payment Button -->
<button id="rzp-button1" class="btn btn-primary btn-lg mb-2">Pay Now with Razorpay (₹<?php echo $booking['total_amount']; ?>)</button>
</div>
</div>
</div>
<div class="col-md-4">
<!-- ...existing right column code if any... -->
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var options = {
"key": "<?php echo $keyId; ?>",
"amount": "<?php echo $booking['total_amount'] * 100; ?>",
"currency": "INR",
"name": "Bus Booking System",
"description": "Bus Ticket Payment",
"order_id": "<?php echo $razorpayOrderId; ?>",
"handler": function (response){
// Send payment_id, order_id, signature, and booking_id to server for verification and booking confirmation
$.ajax({
url: 'verify_payment.php',
type: 'POST',
data: {
razorpay_payment_id: response.razorpay_payment_id,
razorpay_order_id: response.razorpay_order_id,
razorpay_signature: response.razorpay_signature,
booking_id: "<?php echo $booking_id; ?>"
},
success: function(res) {
if(res.trim() === "success") {
window.location.href = "booking_confirmation.php?booking_id=<?php echo $booking_id; ?>";
} else {
alert("Payment verification failed: " + res);
}
}
});
},
"prefill": {
"name": "<?php echo htmlspecialchars($_SESSION['user_name'] ?? ''); ?>",
"email": "<?php echo htmlspecialchars($_SESSION['user_email'] ?? ''); ?>"
},
"theme": {
"color": "#3399cc"
}
};
var rzp1 = new Razorpay(options);
document.getElementById('rzp-button1').onclick = function(e){
rzp1.open();
e.preventDefault();
}
</script>
<?php include('includes/footer.php'); ?>