-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet_pay.php
More file actions
35 lines (32 loc) · 1.62 KB
/
wallet_pay.php
File metadata and controls
35 lines (32 loc) · 1.62 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
<?php
session_start();
require_once 'includes/config.php';
if (!isset($_SESSION['user_id']) || !isset($_POST['booking_id'])) {
header('Location: login.php');
exit;
}
$user_id = $_SESSION['user_id'];
$booking_id = $_POST['booking_id'];
// Fetch booking and user wallet
$stmt = $pdo->prepare("SELECT total_amount FROM bookings WHERE booking_id = :booking_id AND user_id = :user_id");
$stmt->execute([':booking_id' => $booking_id, ':user_id' => $user_id]);
$booking = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt = $pdo->prepare("SELECT wallet_balance FROM users WHERE user_id = :user_id");
$stmt->execute([':user_id' => $user_id]);
$wallet_balance = $stmt->fetchColumn();
if ($booking && $wallet_balance >= $booking['total_amount']) {
$pdo->beginTransaction();
$stmt = $pdo->prepare("UPDATE users SET wallet_balance = wallet_balance - :amount WHERE user_id = :user_id");
$stmt->execute([':amount' => $booking['total_amount'], ':user_id' => $user_id]);
$stmt = $pdo->prepare("UPDATE bookings SET payment_status = 'Paid', status = 'Confirmed' WHERE booking_id = :booking_id");
$stmt->execute([':booking_id' => $booking_id]);
$stmt = $pdo->prepare("INSERT INTO wallet_transactions (user_id, amount, type, description) VALUES (:user_id, :amount, 'debit', 'Bus Ticket Payment')");
$stmt->execute([':user_id' => $user_id, ':amount' => $booking['total_amount']]);
$pdo->commit();
header("Location: booking_confirmation.php?booking_id=$booking_id");
exit;
} else {
$pdo->rollBack();
header("Location: payment.php?booking_id=$booking_id&error=Insufficient+wallet+balance");
exit;
}