Path : /home/vishqocm/pcib.in/student/
File Upload :
Current File : /home/vishqocm//pcib.in/student/exam_payment.php

<?php
$pageTitle = "Exam Re-attempt Payment";
include_once('includes/header.php');

// Check if student is logged in
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
    header("Location: ../login.php");
    exit();
}

$userId = $_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']);
}

// Check if payment data is provided
if (!isset($_POST['exam_id']) || !isset($_POST['payment_type']) || !isset($_POST['amount'])) {
    $_SESSION['error_message'] = "Invalid payment request.";
    header("Location: scheduled-exams.php");
    exit();
}

$exam_id = intval($_POST['exam_id']);
$payment_type = $_POST['payment_type'];
$amount = floatval($_POST['amount']);

// Validate that this is a reattempt payment
if ($payment_type !== 'reattempt_fee' || $amount != 50) {
    $_SESSION['error_message'] = "Invalid payment details.";
    header("Location: scheduled-exams.php");
    exit();
}

// Get exam details
$exam_query = "
    SELECT es.*, c.title as course_title
    FROM exam_schedules es
    JOIN courses c ON es.course_id = c.id
    WHERE es.id = ?
";

$stmt = $conn->prepare($exam_query);
$stmt->bind_param("i", $exam_id);
$stmt->execute();
$exam_result = $stmt->get_result();

if ($exam_result->num_rows === 0) {
    $_SESSION['error_message'] = "Exam not found.";
    header("Location: scheduled-exams.php");
    exit();
}

$exam = $exam_result->fetch_assoc();

// Generate a unique payment reference
$payment_reference = 'REATTEMPT-' . date('YmdHis') . '-' . $userId . '-' . $exam_id;

// Process payment submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['process_payment'])) {
    // In a real application, this would connect to a payment gateway
    // For this implementation, we'll simulate a successful payment
    
    // Create payment record
    $payment_query = "
        INSERT INTO payments (
            user_id, amount, payment_type, payment_reference, 
            status, exam_id, created_at
        ) VALUES (?, ?, ?, ?, 'completed', ?, NOW())
    ";
    
    $stmt = $conn->prepare($payment_query);
    $stmt->bind_param("idssi", $userId, $amount, $payment_type, $payment_reference, $exam_id);
    
    if ($stmt->execute()) {
        $_SESSION['success_message'] = "Payment successful! You can now re-attempt the exam.";
        header("Location: scheduled-exams.php");
        exit();
    } else {
        $error_message = "Payment processing failed. Please try again.";
    }
}
?>

<div class="container py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><?php echo $pageTitle; ?></h2>
        <a href="exam-results.php?id=<?php echo $exam_id; ?>" class="btn btn-outline-primary">
            <i class="fas fa-arrow-left me-2"></i>Back to Exam Results
        </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; ?>
    
    <div class="row">
        <div class="col-md-8 mx-auto">
            <div class="card shadow">
                <div class="card-header bg-primary text-white">
                    <h5 class="mb-0"><i class="fas fa-credit-card me-2"></i>Exam Re-attempt Payment</h5>
                </div>
                <div class="card-body">
                    <div class="alert alert-info mb-4">
                        <div class="d-flex">
                            <div class="me-3">
                                <i class="fas fa-info-circle fa-2x"></i>
                            </div>
                            <div>
                                <h5 class="alert-heading">Payment Information</h5>
                                <p class="mb-0">You are about to make a payment of ₹50 to re-attempt the exam "<?php echo htmlspecialchars($exam['title']); ?>" for the course "<?php echo htmlspecialchars($exam['course_title']); ?>". After successful payment, you will be able to re-attempt the exam.</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-4">
                        <h5>Payment Details</h5>
                        <table class="table">
                            <tr>
                                <th style="width: 40%">Exam</th>
                                <td><?php echo htmlspecialchars($exam['title']); ?></td>
                            </tr>
                            <tr>
                                <th>Course</th>
                                <td><?php echo htmlspecialchars($exam['course_title']); ?></td>
                            </tr>
                            <tr>
                                <th>Payment Type</th>
                                <td>Re-attempt Fee</td>
                            </tr>
                            <tr>
                                <th>Amount</th>
                                <td>₹<?php echo number_format($amount, 2); ?></td>
                            </tr>
                            <tr>
                                <th>Payment Reference</th>
                                <td><code><?php echo $payment_reference; ?></code></td>
                            </tr>
                        </table>
                    </div>
                    
                    <form method="post" action="">
                        <input type="hidden" name="exam_id" value="<?php echo $exam_id; ?>">
                        <input type="hidden" name="payment_type" value="<?php echo $payment_type; ?>">
                        <input type="hidden" name="amount" value="<?php echo $amount; ?>">
                        
                        <div class="mb-4">
                            <h5>Payment Method</h5>
                            <div class="form-check mb-2">
                                <input class="form-check-input" type="radio" name="payment_method" id="upi" value="upi" checked>
                                <label class="form-check-label" for="upi">UPI / Google Pay / PhonePe</label>
                            </div>
                            <div class="form-check mb-2">
                                <input class="form-check-input" type="radio" name="payment_method" id="card" value="card">
                                <label class="form-check-label" for="card">Credit / Debit Card</label>
                            </div>
                            <div class="form-check">
                                <input class="form-check-input" type="radio" name="payment_method" id="netbanking" value="netbanking">
                                <label class="form-check-label" for="netbanking">Net Banking</label>
                            </div>
                        </div>
                        
                        <div class="d-grid gap-2">
                            <button type="submit" name="process_payment" class="btn btn-success">
                                <i class="fas fa-money-bill-wave me-2"></i>Pay ₹<?php echo number_format($amount, 2); ?>
                            </button>
                            <a href="exam-results.php?id=<?php echo $exam_id; ?>" class="btn btn-outline-secondary">
                                Cancel
                            </a>
                        </div>
                    </form>
                </div>
                <div class="card-footer bg-light">
                    <small class="text-muted">
                        <i class="fas fa-shield-alt me-1"></i> Your payment information is secure and encrypted.
                    </small>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include_once('includes/footer.php'); ?>