<?php
// Start session
session_start();
// Check if user has admin privileges
if (!isset($_SESSION['role']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
echo json_encode([
'success' => false,
'message' => 'Unauthorized access'
]);
exit;
}
// Include database configuration
require_once '../../admin/database/db_config.php';
// Check if enrollment ID is provided
if (!isset($_GET['enrollment_id']) || empty($_GET['enrollment_id'])) {
echo json_encode([
'success' => false,
'message' => 'Enrollment ID is required'
]);
exit;
}
$enrollment_id = intval($_GET['enrollment_id']);
try {
// First get the user_id and course_id from the enrollment
$query = "SELECT user_id, course_id FROM enrollments WHERE id = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $enrollment_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
throw new Exception('Enrollment not found');
}
$enrollment = $result->fetch_assoc();
$user_id = $enrollment['user_id'];
$course_id = $enrollment['course_id'];
// Get the course details (for price)
$course_query = "SELECT title, price, discount_percent FROM courses WHERE id = ?";
$stmt = $conn->prepare($course_query);
$stmt->bind_param("i", $course_id);
$stmt->execute();
$course_result = $stmt->get_result();
$course = $course_result->fetch_assoc();
// Calculate total price
$price = $course['price'];
if ($course['discount_percent'] > 0) {
$discount = ($price * $course['discount_percent']) / 100;
$price = $price - $discount;
}
// Get student details
$user_query = "SELECT first_name, last_name, email FROM users WHERE id = ?";
$stmt = $conn->prepare($user_query);
$stmt->bind_param("i", $user_id);
$stmt->execute();
$user_result = $stmt->get_result();
$user = $user_result->fetch_assoc();
// Get all payments for this user and course
$payments_query = "SELECT p.id, p.amount, p.payment_date, p.payment_method,
p.status, p.transaction_id, p.payment_details,
a.first_name as admin_first_name, a.last_name as admin_last_name
FROM payments p
LEFT JOIN users a ON p.verified_by = a.id
WHERE p.user_id = ? AND p.course_id = ?
ORDER BY p.payment_date DESC";
$stmt = $conn->prepare($payments_query);
$stmt->bind_param("ii", $user_id, $course_id);
$stmt->execute();
$payments_result = $stmt->get_result();
// Calculate total payments
$total_paid = 0;
$payment_rows = [];
while ($payment = $payments_result->fetch_assoc()) {
$payment_rows[] = $payment;
if ($payment['status'] === 'verified') {
$total_paid += $payment['amount'];
}
}
// Calculate payment progress
$payment_progress = 0;
if ($price > 0) {
$payment_progress = min(100, round(($total_paid / $price) * 100));
}
// Define status badges
$status_badges = [
'pending' => '<span class="badge bg-warning text-dark">Pending</span>',
'verified' => '<span class="badge bg-success">Verified</span>',
'rejected' => '<span class="badge bg-danger">Rejected</span>'
];
// Print payment information header
echo '<div class="card mb-3">
<div class="card-header bg-primary text-white">
<h5 class="mb-0">Payment Information</h5>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<p><strong>Student:</strong> ' . htmlspecialchars($user['first_name'] . ' ' . $user['last_name']) . '</p>
<p><strong>Email:</strong> ' . htmlspecialchars($user['email']) . '</p>
</div>
<div class="col-md-6">
<p><strong>Course:</strong> ' . htmlspecialchars($course['title']) . '</p>
<p><strong>Course Price:</strong> $' . number_format($price, 2) . '</p>
<p><strong>Total Paid:</strong> $' . number_format($total_paid, 2) . '</p>
</div>
</div>
<div class="progress mb-3">
<div class="progress-bar bg-success" role="progressbar" style="width: ' . $payment_progress . '%;"
aria-valuenow="' . $payment_progress . '" aria-valuemin="0" aria-valuemax="100">
' . $payment_progress . '%
</div>
</div>
</div>
</div>';
if (count($payment_rows) > 0) {
echo '<div class="table-responsive">
<table class="table table-striped table-bordered">
<thead class="table-dark">
<tr>
<th>Payment Date</th>
<th>Amount</th>
<th>Method</th>
<th>Transaction ID</th>
<th>Status</th>
<th>Verified By</th>
<th>Details</th>
<th>Actions</th>
</tr>
</thead>
<tbody>';
foreach ($payment_rows as $payment) {
$status = isset($status_badges[$payment['status']])
? $status_badges[$payment['status']]
: '<span class="badge bg-secondary">' . ucfirst($payment['status']) . '</span>';
$verified_by = !empty($payment['admin_first_name'])
? htmlspecialchars($payment['admin_first_name'] . ' ' . $payment['admin_last_name'])
: 'N/A';
$payment_details = !empty($payment['payment_details'])
? htmlspecialchars($payment['payment_details'])
: 'No details provided';
echo '<tr>
<td>' . date('M d, Y H:i', strtotime($payment['payment_date'])) . '</td>
<td>$' . number_format($payment['amount'], 2) . '</td>
<td>' . htmlspecialchars($payment['payment_method']) . '</td>
<td>' . htmlspecialchars($payment['transaction_id']) . '</td>
<td>' . $status . '</td>
<td>' . $verified_by . '</td>
<td>' . $payment_details . '</td>
<td>';
if ($payment['status'] === 'pending') {
echo '<button type="button" class="btn btn-sm btn-success verify-payment" data-payment-id="' . $payment['id'] . '" data-status="verified">
<i class="fas fa-check"></i> Verify
</button>
<button type="button" class="btn btn-sm btn-danger verify-payment" data-payment-id="' . $payment['id'] . '" data-status="rejected">
<i class="fas fa-times"></i> Reject
</button>';
}
echo '</td>
</tr>';
}
echo '</tbody>
</table>
</div>';
// Add JavaScript for payment verification
echo '<script>
$(document).ready(function() {
$(".verify-payment").click(function() {
var paymentId = $(this).data("payment-id");
var status = $(this).data("status");
var adminNotes = "";
if (status === "rejected") {
adminNotes = prompt("Please provide a reason for rejection:");
if (adminNotes === null) return; // User canceled
}
$.ajax({
url: "admin/ajax/verify_payment.php",
type: "POST",
data: {
payment_id: paymentId,
status: status,
admin_notes: adminNotes
},
beforeSend: function() {
$("#paymentLoader").show();
},
success: function(response) {
try {
var data = JSON.parse(response);
if (data.success) {
showAlert("success", "Payment " + (status === "verified" ? "verified" : "rejected") + " successfully");
// Refresh payment list
loadPaymentHistory(' . $enrollment_id . ');
} else {
showAlert("danger", "Error: " + data.message);
}
} catch (e) {
showAlert("danger", "Error processing response");
}
},
error: function() {
showAlert("danger", "Server error while processing request");
},
complete: function() {
$("#paymentLoader").hide();
}
});
});
});
</script>';
} else {
echo '<div class="alert alert-info">No payment records found for this enrollment.</div>';
}
} catch (Exception $e) {
echo '<div class="alert alert-danger">Error: ' . $e->getMessage() . '</div>';
}
?>