Path : /home/vishqocm/pcib.in/enroll/
File Upload :
Current File : /home/vishqocm//pcib.in/enroll/check_payment_status.php

<?php
// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', 'C:/xampp/logs/php_error.log');

// Log page access
error_log('Payment status check page accessed - ' . date('Y-m-d H:i:s'));

// Start session
session_start();

// Include database configuration
require_once '../admin/database/db_config.php';

// Include email functions
require_once __DIR__ . '/includes/email_functions.php';

// Include Razorpay configuration
require_once __DIR__ . '/includes/razorpay_config.php';

// If user is not logged in, redirect to login page
if (!isset($_SESSION['user_id'])) {
    header('Location: ../login.php');
    exit;
}

// Get parameters
$payment_id = isset($_GET['payment_id']) ? trim($_GET['payment_id']) : '';
$application_id = isset($_GET['application_id']) ? intval($_GET['application_id']) : 0;

// Validate parameters
if (empty($payment_id) || $application_id <= 0) {
    $error = "Missing payment details. Please go back and try again.";
}

// Get user ID
$user_id = $_SESSION['user_id'];

// Check if the application belongs to the current user
$application_query = "SELECT ea.*, c.title as course_title, c.price, c.discount_price 
                      FROM enrollment_applications ea 
                      JOIN courses c ON ea.course_id = c.id 
                      WHERE ea.id = ? AND ea.user_id = ?";
$stmt = $conn->prepare($application_query);
$stmt->bind_param("ii", $application_id, $user_id);
$stmt->execute();
$application_result = $stmt->get_result();
$application = $application_result->fetch_assoc();

// If application not found or doesn't belong to the user, redirect
if (!$application) {
    header('Location: ../courses.php');
    exit;
}

// Initialize variables
$error = '';
$payment_status = '';
$payment_data = null;
$success = false;
$processed = false;

// Check if application is already completed
$check_completed = "SELECT status FROM enrollment_applications WHERE id = ? AND status = 'completed'";
$stmt = $conn->prepare($check_completed);
$stmt->bind_param("i", $application_id);
$stmt->execute();
$completed_result = $stmt->get_result();

if ($completed_result->num_rows > 0) {
    // Application is already completed, redirect to success page
    header("Location: enrollment_success.php?application_id=$application_id");
    exit;
}

// Get user information for completing process if needed
$user_query = "SELECT * 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();

// Process status check
if (!empty($payment_id)) {
    // Check if payment already exists in database
    $check_payment = "SELECT * FROM payments WHERE transaction_id = ? AND user_id = ?";
    $stmt = $conn->prepare($check_payment);
    $stmt->bind_param("si", $payment_id, $user_id);
    $stmt->execute();
    $payment_result = $stmt->get_result();
    
    if ($payment_result->num_rows > 0) {
        $existing_payment = $payment_result->fetch_assoc();
        $payment_status = $existing_payment['status'];
        $processed = true;
        
        if ($payment_status === 'completed') {
            $success = true;
        } else {
            $error = "Payment exists but is not completed. Current status: " . $payment_status;
        }
    } else {
        // Fetch payment status from Razorpay
        $payment_data = get_razorpay_payment($payment_id);
        
        if (!$payment_data) {
            $error = "Failed to retrieve payment information from Razorpay.";
            error_log("Failed to retrieve payment: $payment_id");
        } else {
            $payment_status = $payment_data['status'];
            
            // Check payment status
            if ($payment_status === 'captured' || $payment_status === 'authorized') {
                error_log("Payment verified with Razorpay - Status: " . $payment_status);
                
                // Get payment plan and amount from session or default to full payment
                $payment_plan = 'full'; // Default
                $order_id = '';
                
                // Try to get from payment_error session
                if (isset($_SESSION['payment_error']['payment_plan'])) {
                    $payment_plan = $_SESSION['payment_error']['payment_plan'];
                    $order_id = isset($_SESSION['payment_error']['order_id']) ? $_SESSION['payment_error']['order_id'] : '';
                    error_log("Using payment plan from error session: $payment_plan");
                }
                // Try to get from payment_plan_data session
                else if (isset($_SESSION['payment_plan_data']['plan'])) {
                    $payment_plan = $_SESSION['payment_plan_data']['plan'];
                    $order_id = isset($_SESSION['payment_plan_data']['order_id']) ? $_SESSION['payment_plan_data']['order_id'] : '';
                    error_log("Using payment plan from data session: $payment_plan");
                }
                
                // Calculate the final price
                $price = $application['discount_price'] > 0 && $application['discount_price'] < $application['price'] 
                        ? $application['discount_price'] 
                        : $application['price'];
                
                // Set payment amount based on plan
                $payment_amount = $price; // Default to full payment
                if ($payment_plan === 'monthly') {
                    $payment_amount = round($price / 12);
                } else if ($payment_plan === 'six_month') {
                    $payment_amount = round($price / 6);
                }
                
                // Start transaction
                $conn->begin_transaction();
                
                try {
                    // Record payment in database
                    $payment_details = [
                        'payment_plan' => $payment_plan,
                        'razorpay_payment_id' => $payment_id,
                        'razorpay_order_id' => $order_id,
                        'payment_status' => $payment_status
                    ];
                    
                    // Convert payment details to JSON
                    $payment_details_json = json_encode($payment_details);
                    if ($payment_details_json === false) {
                        throw new Exception("Failed to encode payment details as JSON: " . json_last_error_msg());
                    }
                    
                    // Insert payment record
                    $payment_query = "INSERT INTO payments (user_id, course_id, amount, payment_method, transaction_id, status, payment_details) 
                                     VALUES (?, ?, ?, ?, ?, ?, ?)";
                    $stmt = $conn->prepare($payment_query);
                    if (!$stmt) {
                        throw new Exception("Failed to prepare payment statement: " . $conn->error);
                    }
                    
                    $payment_method = 'razorpay';
                    $payment_status = 'completed';
                    $stmt->bind_param("iidssss", $user_id, $application['course_id'], $payment_amount, $payment_method, $payment_id, $payment_status, $payment_details_json);
                    
                    if (!$stmt->execute()) {
                        throw new Exception("Failed to insert payment record: " . $stmt->error);
                    }
                    
                    error_log("Razorpay payment record inserted successfully");
                    
                    // Update application status to completed
                    $update_query = "UPDATE enrollment_applications SET status = 'completed' WHERE id = ?";
                    $stmt = $conn->prepare($update_query);
                    if (!$stmt) {
                        throw new Exception("Failed to prepare update statement: " . $conn->error);
                    }
                    
                    $stmt->bind_param("i", $application_id);
                    if (!$stmt->execute()) {
                        throw new Exception("Failed to update application status: " . $stmt->error);
                    }
                    
                    error_log("Application status updated to completed");
                    
                    // Check if enrollment already exists
                    $check_enrollment = "SELECT id FROM enrollments WHERE course_id = ? AND user_id = ?";
                    $stmt = $conn->prepare($check_enrollment);
                    if (!$stmt) {
                        throw new Exception("Failed to prepare enrollment check statement: " . $conn->error);
                    }
                    
                    $stmt->bind_param("ii", $application['course_id'], $user_id);
                    $stmt->execute();
                    $result = $stmt->get_result();
                    
                    if ($result->num_rows == 0) {
                        // Create actual enrollment record if it doesn't exist
                        $enrollment_query = "INSERT INTO enrollments (course_id, user_id, status, verification_token, payment_plan) 
                                          VALUES (?, ?, 'active', ?, ?)";
                        $stmt = $conn->prepare($enrollment_query);
                        if (!$stmt) {
                            throw new Exception("Failed to prepare enrollment statement: " . $conn->error);
                        }
                        
                        $stmt->bind_param("iiss", $application['course_id'], $user_id, $application['verification_token'], $payment_plan);
                        if (!$stmt->execute()) {
                            throw new Exception("Failed to create enrollment record: " . $stmt->error);
                        }
                        
                        error_log("New enrollment record created successfully");
                    } else {
                        // Update existing enrollment
                        $enrollment_query = "UPDATE enrollments SET status = 'active', payment_plan = ? 
                                          WHERE course_id = ? AND user_id = ?";
                        $stmt = $conn->prepare($enrollment_query);
                        if (!$stmt) {
                            throw new Exception("Failed to prepare enrollment update statement: " . $conn->error);
                        }
                        
                        $stmt->bind_param("sii", $payment_plan, $application['course_id'], $user_id);
                        if (!$stmt->execute()) {
                            throw new Exception("Failed to update enrollment record: " . $stmt->error);
                        }
                        
                        error_log("Existing enrollment record updated successfully");
                    }
                    
                    // Send confirmation email
                    if (function_exists('send_enrollment_confirmation_email')) {
                        try {
                            send_enrollment_confirmation_email($user['email'], $user['first_name'], $application['course_title'], $application['verification_token']);
                            error_log("Confirmation email sent successfully");
                        } catch (Exception $emailEx) {
                            // Log the error but don't stop the transaction
                            error_log("Error sending confirmation email: " . $emailEx->getMessage());
                        }
                    }
                    
                    // Log activity
                    $activity_query = "INSERT INTO activities (user_id, user_type, activity_type, activity_description, ip_address) 
                                     VALUES (?, 'student', 'enrollment', ?, ?)";
                    $stmt = $conn->prepare($activity_query);
                    if (!$stmt) {
                        throw new Exception("Failed to prepare activity statement: " . $conn->error);
                    }
                    
                    $activity_description = "Enrolled in course: " . $application['course_title'] . " via manual payment verification";
                    $ip_address = $_SERVER['REMOTE_ADDR'];
                    $stmt->bind_param("iss", $user_id, $activity_description, $ip_address);
                    if (!$stmt->execute()) {
                        throw new Exception("Failed to log activity: " . $stmt->error);
                    }
                    
                    // Commit transaction
                    $conn->commit();
                    error_log("Payment transaction committed successfully - payment verification complete");
                    
                    // Clear error session
                    if (isset($_SESSION['payment_error'])) {
                        unset($_SESSION['payment_error']);
                    }
                    
                    $success = true;
                    $processed = true;
                    
                } catch (Exception $e) {
                    // Rollback transaction
                    $conn->rollback();
                    $error = "An error occurred while processing your payment: " . $e->getMessage();
                    error_log("Razorpay payment error: " . $e->getMessage());
                    error_log("Trace: " . $e->getTraceAsString());
                }
            } else {
                $error = "Payment status is not completed. Current status: " . $payment_status;
                error_log("Invalid payment status for ID: $payment_id, Status: $payment_status");
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Status - <?php echo htmlspecialchars($application['course_title']); ?></title>
    
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    
    <!-- Font Awesome -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    
    <!-- Google Fonts -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    
    <!-- Modern UI CSS -->
    <link rel="stylesheet" href="css/modern-ui.css">
</head>
<body>
<!-- Logo Container -->
<div class="logo-container">
    <?php 
    // Get school settings from site_settings table
    $school_name = 'Popular Computer Institute';
    $school_logo = '../assets/img/logo.png';
    
    $site_settings_query = "SELECT setting_key, setting_value FROM site_settings WHERE setting_key IN ('site_name', 'site_logo')";
    try {
        $site_settings_result = $conn->query($site_settings_query);
        if ($site_settings_result && $site_settings_result->num_rows > 0) {
            while ($row = $site_settings_result->fetch_assoc()) {
                if ($row['setting_key'] == 'site_name') {
                    $school_name = $row['setting_value'];
                } else if ($row['setting_key'] == 'site_logo') {
                    $school_logo = $row['setting_value'];
                    // Add path prefix if not already present
                    if ($school_logo && substr($school_logo, 0, 1) !== '/' && substr($school_logo, 0, 4) !== 'http') {
                        $school_logo = '../' . $school_logo;
                    }
                }
            }
        }
    } catch (Exception $e) {
        // Silently handle the error - use default values
    }
    ?>
    <img src="<?php echo $school_logo; ?>" alt="<?php echo htmlspecialchars($school_name); ?>" class="logo" onerror="this.src='../assets/img/logo.png'; this.onerror='';">
    <h2><?php echo htmlspecialchars($school_name); ?></h2>
</div>

<!-- Animated Background -->
<div class="animated-bg">
    <div class="animated-shape shape-1"></div>
    <div class="animated-shape shape-2"></div>
    <div class="animated-shape shape-3"></div>
    <div class="animated-shape shape-4"></div>
</div>

<!-- Begin Page Content -->
<div class="container mt-5 mb-5">
    <div class="row justify-content-center">
        <div class="col-lg-8">
            <div class="glass-card shadow fade-in">
                <div class="modern-card-header">
                    <div class="glow-effect glow-1"></div>
                    <div class="glow-effect glow-2"></div>
                    <div class="modern-card-header-content">
                        <i class="fas fa-money-check-alt"></i>
                        <h4 class="mb-0">Payment Status Verification</h4>
                    </div>
                </div>
                <div class="card-body">
                    <div class="text-center mb-4">
                        <?php if ($success): ?>
                            <div class="success-icon mx-auto mb-4">
                                <i class="fas fa-check-circle fa-4x text-success"></i>
                            </div>
                            <h3 class="text-white">Payment Successful!</h3>
                            <p class="text-white-50">Your payment has been processed successfully and your enrollment is now complete.</p>
                            <a href="enrollment_success.php?application_id=<?php echo $application_id; ?>" class="btn btn-3d btn-success-3d mt-3">
                                <i class="fas fa-graduation-cap me-2"></i>View Enrollment Details
                            </a>
                        <?php elseif ($processed && !$success): ?>
                            <div class="warning-icon mx-auto mb-4">
                                <i class="fas fa-exclamation-circle fa-4x text-warning"></i>
                            </div>
                            <h3 class="text-white">Payment Processing</h3>
                            <p class="text-white-50">Your payment is being processed. Current status: <strong><?php echo htmlspecialchars($payment_status); ?></strong></p>
                            <p class="text-white-50"><?php echo $error; ?></p>
                            <div class="mt-3">
                                <a href="?payment_id=<?php echo urlencode($payment_id); ?>&application_id=<?php echo $application_id; ?>" class="btn btn-3d btn-warning-3d me-2">
                                    <i class="fas fa-sync-alt me-2"></i>Check Again
                                </a>
                                <a href="payment.php?application_id=<?php echo $application_id; ?>" class="btn btn-3d btn-outline-3d">
                                    <i class="fas fa-arrow-left me-2"></i>Back to Payment
                                </a>
                            </div>
                        <?php else: ?>
                            <?php if ($error): ?>
                                <div class="error-icon mx-auto mb-4">
                                    <i class="fas fa-times-circle fa-4x text-danger"></i>
                                </div>
                                <h3 class="text-white">Payment Error</h3>
                                <p class="text-white-50"><?php echo $error; ?></p>
                            <?php else: ?>
                                <div class="info-icon mx-auto mb-4">
                                    <i class="fas fa-info-circle fa-4x text-info"></i>
                                </div>
                                <h3 class="text-white">Checking Payment Status</h3>
                                <p class="text-white-50">We're verifying your payment status with Razorpay.</p>
                            <?php endif; ?>
                            <div class="mt-3">
                                <a href="payment.php?application_id=<?php echo $application_id; ?>" class="btn btn-3d btn-primary-3d">
                                    <i class="fas fa-arrow-left me-2"></i>Back to Payment
                                </a>
                            </div>
                        <?php endif; ?>
                    </div>
                    
                    <?php if ($payment_data && !empty($payment_data)): ?>
                    <div class="payment-details-section mt-4 pt-4 border-top border-white border-opacity-10">
                        <h5 class="text-white mb-3">Payment Details</h5>
                        <div class="bg-dark bg-opacity-50 p-3 rounded">
                            <div class="row g-2">
                                <div class="col-6">
                                    <p class="mb-1"><span class="text-white-50">Payment ID:</span></p>
                                    <p class="text-white"><?php echo htmlspecialchars($payment_id); ?></p>
                                </div>
                                <div class="col-6">
                                    <p class="mb-1"><span class="text-white-50">Status:</span></p>
                                    <p class="text-white"><?php echo htmlspecialchars($payment_data['status']); ?></p>
                                </div>
                                <div class="col-6">
                                    <p class="mb-1"><span class="text-white-50">Amount:</span></p>
                                    <p class="text-white">₹<?php echo number_format($payment_data['amount'] / 100, 2); ?></p>
                                </div>
                                <div class="col-6">
                                    <p class="mb-1"><span class="text-white-50">Date:</span></p>
                                    <p class="text-white"><?php echo date('d M Y H:i', $payment_data['created_at']); ?></p>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endif; ?>
                </div>
            </div>
            
            <div class="text-center mt-4">
                <a href="../index.php" class="btn btn-link text-white">
                    <i class="fas fa-home me-2"></i>Return to Homepage
                </a>
            </div>
        </div>
    </div>
</div>

<!-- Bootstrap JS Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>