Path : /home/vishqocm/pcib.in/admin/ajax/
File Upload :
Current File : //home/vishqocm/pcib.in/admin/ajax/get_payment_info.php

<?php
// Start session
session_start();

// Check if user has admin privileges
if (!isset($_SESSION['role']) || ($_SESSION['role'] !== 'admin' && $_SESSION['role'] !== 'director')) {
    echo '<div class="alert alert-danger">Unauthorized access</div>';
    exit;
}

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

// Check if application ID is provided
if (!isset($_GET['application_id']) || empty($_GET['application_id'])) {
    echo '<div class="alert alert-danger">Invalid request</div>';
    exit;
}

$application_id = intval($_GET['application_id']);

// Get payment information for this user
$query = "SELECT p.*, c.title as course_title, c.price, c.discount_price
          FROM payments p
          JOIN users u ON p.user_id = u.id
          JOIN enrollments e ON u.id = e.user_id
          JOIN courses c ON e.course_id = c.id
          WHERE p.user_id = (SELECT user_id FROM enrollment_applications WHERE id = ?)
          ORDER BY p.payment_date DESC";
$stmt = $conn->prepare($query);
$stmt->bind_param("i", $application_id);
$stmt->execute();
$result = $stmt->get_result();

// Status badge classes
$status_badges = [
    'pending' => 'warning',
    'completed' => 'success',
    'failed' => 'danger',
    'refunded' => 'info'
];

// Display payment information
if ($result && $result->num_rows > 0) {
    $payment = $result->fetch_assoc();
    
    $status_badge = $status_badges[$payment['status']] ?? 'secondary';
    $payment_details = json_decode($payment['payment_details'], true);
    
    echo '<div class="card">';
    echo '<div class="card-body">';
    
    echo '<div class="d-flex justify-content-between align-items-center mb-3">';
    echo '<h6 class="card-title">Payment Details</h6>';
    echo '<span class="badge badge-' . $status_badge . '">' . ucfirst($payment['status']) . '</span>';
    echo '</div>';
    
    echo '<table class="table table-sm">';
    echo '<tr><th width="40%">Amount</th><td>₹' . number_format($payment['amount'], 2) . '</td></tr>';
    echo '<tr><th>Payment Method</th><td>' . ucwords(str_replace('_', ' ', $payment['payment_method'])) . '</td></tr>';
    echo '<tr><th>Transaction ID</th><td>' . $payment['transaction_id'] . '</td></tr>';
    echo '<tr><th>Payment Date</th><td>' . date('M d, Y h:i A', strtotime($payment['payment_date'])) . '</td></tr>';
    
    // Display payment details based on method
    if ($payment['payment_method'] === 'credit_card' || $payment['payment_method'] === 'debit_card') {
        if (isset($payment_details['card_number'])) {
            echo '<tr><th>Card Number</th><td>XXXX XXXX XXXX ' . $payment_details['card_number'] . '</td></tr>';
        }
        if (isset($payment_details['card_holder'])) {
            echo '<tr><th>Card Holder</th><td>' . htmlspecialchars($payment_details['card_holder']) . '</td></tr>';
        }
    } else if ($payment['payment_method'] === 'upi') {
        if (isset($payment_details['upi_id'])) {
            echo '<tr><th>UPI ID</th><td>' . htmlspecialchars($payment_details['upi_id']) . '</td></tr>';
        }
    } else if ($payment['payment_method'] === 'cash') {
        echo '<tr><th>Payment Location</th><td>' . ($payment_details['payment_location'] ?? 'Institute Office') . '</td></tr>';
    }
    
    echo '</table>';
    
    // Add manual verification button for cash payments
    if ($payment['payment_method'] === 'cash' && $payment['status'] === 'pending') {
        echo '<div class="mt-3">';
        echo '<button type="button" class="btn btn-success btn-sm verify-payment-btn" data-payment-id="' . $payment['id'] . '">';
        echo '<i class="fas fa-check-circle"></i> Verify Cash Payment</button>';
        echo '</div>';
        
        // Add inline JavaScript for verification
        echo '<script>
            $(document).ready(function() {
                $(".verify-payment-btn").click(function() {
                    var paymentId = $(this).data("payment-id");
                    
                    $.ajax({
                        url: "ajax/verify_payment.php",
                        type: "POST",
                        data: {payment_id: paymentId},
                        success: function(response) {
                            if (response === "success") {
                                alert("Payment verified successfully!");
                                location.reload();
                            } else {
                                alert("Failed to verify payment: " + response);
                            }
                        },
                        error: function() {
                            alert("An error occurred. Please try again.");
                        }
                    });
                });
            });
        </script>';
    }
    
    echo '</div>';
    echo '</div>';
} else {
    // Get application info to show course price
    $app_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 = ?";
    $stmt = $conn->prepare($app_query);
    $stmt->bind_param("i", $application_id);
    $stmt->execute();
    $app_result = $stmt->get_result();
    $application = $app_result->fetch_assoc();
    
    if ($application) {
        $price = $application['discount_price'] > 0 && $application['discount_price'] < $application['price'] 
                ? $application['discount_price'] : $application['price'];
        
        echo '<div class="alert alert-info">';
        echo '<h6>No Payment Recorded</h6>';
        echo '<p>Course: ' . htmlspecialchars($application['course_title']) . '</p>';
        echo '<p>Price: ₹' . number_format($price, 2) . '</p>';
        
        if ($application['status'] === 'payment_pending') {
            echo '<p>Status: Awaiting payment from student</p>';
        } else if ($application['status'] === 'pending') {
            echo '<p>Status: Application still under review</p>';
        } else if ($application['status'] === 'rejected') {
            echo '<p>Status: Application rejected</p>';
        }
        
        echo '</div>';
    } else {
        echo '<div class="alert alert-warning">No payment information available for this application</div>';
    }
}
?>