<?php
ob_start(); // Start output buffering to prevent headers already sent error
$pageTitle = "Certificate Payments";
include_once('includes/header.php');
// Check if user is logged in
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
header("Location: ../login.php");
exit();
}
// Get student ID
$student_id = $_SESSION['user_id'];
$success_message = '';
$error_message = '';
// Handle flash messages
if (isset($_SESSION['success_message'])) {
$success_message = $_SESSION['success_message'];
unset($_SESSION['success_message']);
}
if (isset($_SESSION['error_message'])) {
$error_message = $_SESSION['error_message'];
unset($_SESSION['error_message']);
}
// Get fee amount from settings
$certification_fee = 0;
try {
$fee_query = "SELECT setting_value FROM settings WHERE setting_key = 'certificate_fee'";
$fee_result = $conn->query($fee_query);
if ($fee_result && $fee_result->num_rows > 0) {
$certification_fee = (float)$fee_result->fetch_assoc()['setting_value'];
} else {
// Default fee if not found in settings
$certification_fee = 700;
}
} catch (Exception $e) {
error_log("Error fetching certificate fee: " . $e->getMessage());
// Default fee if query fails
$certification_fee = 700;
}
// Get all pending certificate payments for this student
$pending_query = "
SELECT uc.id, uc.certificate_number, uc.issue_date, uc.payment_status,
c.id as course_id, c.title as course_title, c.image as course_image
FROM unified_certificates uc
JOIN courses c ON uc.course_id = c.id
WHERE uc.user_id = ? AND (uc.payment_status = 'pending' OR uc.payment_status = 'verification_pending')
ORDER BY uc.issue_date DESC
";
$stmt = $conn->prepare($pending_query);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result = $stmt->get_result();
$pending_certificates = [];
while ($row = $result->fetch_assoc()) {
$pending_certificates[] = $row;
}
// Get eligible passed exams for certificate generation
$eligible_exams_query = "
SELECT se.id as student_exam_id, se.exam_id, se.status,
CASE WHEN se.percentage IS NOT NULL THEN se.percentage ELSE se.total_score END as exam_score,
es.title as exam_title, es.passing_percentage,
c.id as course_id, c.title as course_title, c.image as course_image,
e.id as enrollment_id, e.completion_date
FROM student_exams se
JOIN exam_schedules es ON se.exam_id = es.id
JOIN courses c ON es.course_id = c.id
JOIN enrollments e ON e.course_id = c.id AND e.user_id = se.user_id
LEFT JOIN unified_certificates uc ON uc.course_id = c.id AND uc.user_id = se.user_id
WHERE se.user_id = ?
AND se.status = 'passed'
AND e.status = 'completed'
AND uc.id IS NULL
ORDER BY se.created_at DESC
";
$stmt = $conn->prepare($eligible_exams_query);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result = $stmt->get_result();
$eligible_exams = [];
while ($row = $result->fetch_assoc()) {
$eligible_exams[] = $row;
}
// Get all completed certificate payments for this student
$completed_query = "
SELECT uc.id, uc.certificate_number, uc.issue_date, uc.payment_status,
uc.payment_date, uc.payment_reference,
c.id as course_id, c.title as course_title, c.image as course_image,
p.transaction_id, p.payment_method
FROM unified_certificates uc
JOIN courses c ON uc.course_id = c.id
LEFT JOIN payments p ON p.user_id = uc.user_id AND p.course_id = uc.course_id
WHERE uc.user_id = ? AND (uc.payment_status = 'paid' OR uc.payment_status = 'completed')
ORDER BY uc.payment_date DESC
";
$stmt = $conn->prepare($completed_query);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result = $stmt->get_result();
$completed_certificates = [];
while ($row = $result->fetch_assoc()) {
$completed_certificates[] = $row;
}
// Process certificate generation request
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['generate_certificate'])) {
$enrollment_id = isset($_POST['enrollment_id']) ? intval($_POST['enrollment_id']) : 0;
$student_exam_id = isset($_POST['student_exam_id']) ? intval($_POST['student_exam_id']) : 0;
$course_id = isset($_POST['course_id']) ? intval($_POST['course_id']) : 0;
$redirect_after_success = false;
if ($enrollment_id > 0 && $course_id > 0 && $student_exam_id > 0) {
try {
// Check if enrollment belongs to this student and is completed
$check_query = "
SELECT e.id, e.status, se.id as exam_id, se.status as exam_status
FROM enrollments e
LEFT JOIN student_exams se ON se.user_id = e.user_id AND se.id = ?
WHERE e.id = ? AND e.user_id = ? AND e.status = 'completed'
";
$stmt = $conn->prepare($check_query);
$stmt->bind_param("iii", $student_exam_id, $enrollment_id, $student_id);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 1) {
$check_data = $result->fetch_assoc();
// Get student details
$student_query = "SELECT first_name, last_name FROM users WHERE id = ?";
$stmt = $conn->prepare($student_query);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$student_result = $stmt->get_result();
$student = $student_result->fetch_assoc();
// Generate certificate number
$certificate_number = 'CERT-' . date('Y') . '-' . $enrollment_id . '-' . mt_rand(1000, 9999);
// Generate verification code
$verification_code = 'VC-' . strtoupper(substr(md5(uniqid()), 0, 8));
// Get institute information
$institute_name = "Popular Computer Institute";
$institute_address = "Bhimpura No.1,Ballia,UP";
// Create certificate data JSON
$certificate_data = json_encode([
'student_id' => $student_id,
'student_name' => $student['first_name'] . ' ' . $student['last_name'],
'enrollment_id' => $enrollment_id,
'student_exam_id' => $student_exam_id,
'course_id' => $course_id,
'exam_status' => $check_data['exam_status'] ?? null,
'issue_date' => date('Y-m-d'),
'institute_name' => $institute_name,
'institute_address' => $institute_address
]);
// Insert certificate record with pending payment status
$insert_query = "
INSERT INTO unified_certificates
(enrollment_id, student_exam_id, course_id, user_id, certificate_number,
verification_code, certificate_data, issue_date, payment_status)
VALUES (?, ?, ?, ?, ?, ?, ?, CURDATE(), 'pending')
";
$stmt = $conn->prepare($insert_query);
$stmt->bind_param("iiissss", $enrollment_id, $student_exam_id, $course_id, $student_id,
$certificate_number, $verification_code, $certificate_data);
if ($stmt->execute()) {
$cert_id = $conn->insert_id;
$success_message = "Certificate generated successfully! Please complete the payment to download it.";
$redirect_after_success = true;
} else {
$error_message = "Error generating certificate. Please try again later.";
}
} else {
$error_message = "Invalid enrollment or exam data.";
}
} catch (Exception $e) {
$error_message = "Error: " . $e->getMessage();
error_log("Certificate generation error: " . $e->getMessage());
}
} else {
$error_message = "Invalid request parameters.";
}
}
// Handle success messages from URL
if (isset($_GET['success']) && $_GET['success'] === 'generated') {
$success_message = "Certificate generated successfully! Please complete the payment to download it.";
}
?>
<div class="container py-4">
<div class="d-flex justify-content-between align-items-center mb-4">
<h2 class="mb-0">Certificate Payments</h2>
<a href="certificates.php" class="btn btn-outline-primary">
<i class="fas fa-arrow-left me-1"></i> Back to Certificates
</a>
</div>
<?php if ($success_message): ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<i class="fas fa-check-circle me-2"></i> <?php echo $success_message; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if ($error_message): ?>
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="fas fa-exclamation-circle me-2"></i> <?php echo $error_message; ?>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php endif; ?>
<?php if (isset($redirect_after_success) && $redirect_after_success): ?>
<script>
// Refresh the page to display the newly generated certificate
window.location.href = 'certificate_payments.php?success=generated';
</script>
<?php endif; ?>
<!-- Pending Payments -->
<div class="card mb-4">
<div class="card-header bg-warning text-dark">
<h5 class="mb-0">Pending Certificate Payments</h5>
</div>
<div class="card-body">
<?php if (empty($pending_certificates)): ?>
<div class="alert alert-info mb-0">
<i class="fas fa-info-circle me-2"></i> You don't have any pending certificate payments.
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Course</th>
<th>Certificate Number</th>
<th>Issue Date</th>
<th>Fee</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($pending_certificates as $cert): ?>
<tr>
<td>
<div class="d-flex align-items-center">
<img src="<?php echo !empty($cert['course_image']) ? '../' . $cert['course_image'] : '../assets/img/course-placeholder.jpg'; ?>"
class="rounded me-2" alt="" style="width: 40px; height: 40px; object-fit: cover;">
<div><?php echo htmlspecialchars($cert['course_title']); ?></div>
</div>
</td>
<td><?php echo htmlspecialchars($cert['certificate_number']); ?></td>
<td><?php echo date('d M Y', strtotime($cert['issue_date'])); ?></td>
<td>₹<?php echo number_format($certification_fee, 2); ?></td>
<td>
<?php if ($cert['payment_status'] === 'pending'): ?>
<a href="certificate_payment.php?course_id=<?php echo $cert['course_id']; ?>&amount=<?php echo $certification_fee; ?>&cert_id=<?php echo $cert['id']; ?>" class="btn btn-primary btn-sm">
<i class="fas fa-credit-card me-1"></i> Pay Now
</a>
<?php elseif ($cert['payment_status'] === 'verification_pending'): ?>
<span class="badge bg-warning d-inline-block p-2">
<i class="fas fa-clock me-1"></i> Awaiting Verification
</span>
<small class="d-block text-muted mt-1">Cash payment at institute</small>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
<!-- Eligible Exams for Certificate Generation -->
<?php if (!empty($eligible_exams)): ?>
<div class="card mb-4" id="eligible-exams">
<div class="card-header bg-info text-white">
<h5 class="mb-0"><i class="fas fa-graduation-cap me-2"></i>Generate Unified Certificates</h5>
</div>
<div class="card-body">
<div class="alert alert-info">
<i class="fas fa-info-circle me-2"></i> You have <strong><?php echo count($eligible_exams); ?></strong> completed course(s) with passed exams eligible for unified certificate generation. Generate a certificate to proceed with payment.
</div>
<div class="row mb-4">
<div class="col-md-12">
<div class="unified-certificate-info p-3 bg-light rounded">
<h6><i class="fas fa-certificate text-warning me-2"></i>What is a Unified Certificate?</h6>
<p class="mb-2">A unified certificate combines your course completion and exam achievements into a single official document. It includes:</p>
<div class="row">
<div class="col-md-6">
<ul class="mb-0">
<li>Course completion details</li>
<li>Exam scores and grades</li>
</ul>
</div>
<div class="col-md-6">
<ul class="mb-0">
<li>Digital verification technology</li>
<li>Official seal and signatures</li>
</ul>
</div>
</div>
<div class="mt-2">
<span class="badge bg-primary">Certificate Fee: ₹700.00</span>
</div>
</div>
</div>
</div>
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Course</th>
<th>Exam</th>
<th>Score</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($eligible_exams as $exam): ?>
<tr>
<td>
<div class="d-flex align-items-center">
<img src="<?php echo !empty($exam['course_image']) ? '../' . $exam['course_image'] : '../assets/img/course-placeholder.jpg'; ?>"
class="rounded me-2" alt="" style="width: 40px; height: 40px; object-fit: cover;">
<div><?php echo htmlspecialchars($exam['course_title']); ?></div>
</div>
</td>
<td><?php echo htmlspecialchars($exam['exam_title']); ?></td>
<td>
<span class="badge bg-success">
<?php echo is_numeric($exam['exam_score']) ? number_format($exam['exam_score'], 1) : $exam['exam_score']; ?>%
</span>
</td>
<td>
<span class="badge bg-success">PASSED</span>
</td>
<td>
<form method="POST" action="">
<input type="hidden" name="enrollment_id" value="<?php echo $exam['enrollment_id']; ?>">
<input type="hidden" name="student_exam_id" value="<?php echo $exam['student_exam_id']; ?>">
<input type="hidden" name="course_id" value="<?php echo $exam['course_id']; ?>">
<button type="submit" name="generate_certificate" class="btn btn-success btn-sm">
<i class="fas fa-certificate me-1"></i> Generate Unified Certificate
</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
<!-- Payment History -->
<div class="card">
<div class="card-header bg-success text-white">
<h5 class="mb-0">Payment History</h5>
</div>
<div class="card-body">
<?php if (empty($completed_certificates)): ?>
<div class="alert alert-info mb-0">
<i class="fas fa-info-circle me-2"></i> You don't have any completed certificate payments yet.
</div>
<?php else: ?>
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Course</th>
<th>Certificate Number</th>
<th>Payment Date</th>
<th>Amount</th>
<th>Payment Method</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php foreach ($completed_certificates as $cert): ?>
<tr>
<td>
<div class="d-flex align-items-center">
<img src="<?php echo !empty($cert['course_image']) ? '../' . $cert['course_image'] : '../assets/img/course-placeholder.jpg'; ?>"
class="rounded me-2" alt="" style="width: 40px; height: 40px; object-fit: cover;">
<div><?php echo htmlspecialchars($cert['course_title']); ?></div>
</div>
</td>
<td><?php echo htmlspecialchars($cert['certificate_number']); ?></td>
<td><?php echo !empty($cert['payment_date']) ? date('d M Y', strtotime($cert['payment_date'])) : 'N/A'; ?></td>
<td>₹<?php echo number_format($certification_fee, 2); ?></td>
<td><?php echo !empty($cert['payment_method']) ? ucfirst(htmlspecialchars($cert['payment_method'])) : 'N/A'; ?></td>
<td>
<a href="../verify-certificate.php?cert=<?php echo urlencode($cert['certificate_number']); ?>" class="btn btn-success btn-sm" target="_blank">
<i class="fas fa-download me-1"></i> Download
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php endif; ?>
</div>
</div>
<!-- Help Section -->
<div class="card mt-4">
<div class="card-header bg-light">
<h5 class="mb-0">Certificate Information</h5>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<h6>About Unified Certificates</h6>
<p>Our unified certificates combine your course completion and exam achievements into a single document. Benefits include:</p>
<ul>
<li>Official certification with digital verification</li>
<li>Course completion details on first page</li>
<li>Exam scorecard on second page</li>
<li>Unique verification code for authenticity</li>
</ul>
<p><strong>Certificate Fee:</strong> ₹700.00 (one-time payment per certificate)</p>
</div>
<div class="col-md-6">
<h6>Payment Methods</h6>
<p>We accept the following payment methods:</p>
<ul>
<li><strong>Razorpay Online Payment:</strong> Credit/Debit Cards, UPI, Net Banking, Wallets</li>
<li><strong>Cash Payment:</strong> Visit our institute to pay in person</li>
</ul>
<div class="alert alert-info">
<i class="fas fa-info-circle me-1"></i> For cash payments, your certificate will be available after verification by our staff.
</div>
<p class="mb-0">If you're experiencing any payment issues, please contact our support team.</p>
</div>
</div>
</div>
</div>
</div>
<?php include_once('includes/footer.php'); ?>
<?php ob_end_flush(); // End output buffering and send all buffered content to the browser ?>