Spaces:
Running
Running
| /** | |
| * API Endpoint: Fetch Student Outstanding Balance | |
| * | |
| * Returns the total outstanding balance and a breakdown of fees | |
| * for all terms and sessions. | |
| * | |
| * Method: GET | |
| * URL: /api/students/balance?student_id=... | |
| */ | |
| require_once '../../db_config.php'; | |
| require_once '../../includes/PaymentProcessor.php'; | |
| require_once '../../includes/ApiValidator.php'; | |
| require_once '../../config/api_config.php'; | |
| // Initialize core classes | |
| $validator = new ApiValidator($pdo, defined('API_KEYS') ? API_KEYS : []); | |
| $processor = new PaymentProcessor($pdo); | |
| // 1. Validate Request (Method & Auth) | |
| $validation = $validator->validateRequest(['GET']); | |
| if (!$validation['valid']) { | |
| http_response_code($validation['http_code']); | |
| echo json_encode(['status' => 'error', 'message' => $validation['error']]); | |
| exit; | |
| } | |
| // 2. Validate Student ID | |
| $studentId = trim($_GET['student_id'] ?? ''); | |
| if (empty($studentId)) { | |
| http_response_code(400); | |
| echo json_encode(['status' => 'error', 'message' => 'Student ID is required']); | |
| exit; | |
| } | |
| $studentCheck = $validator->validateStudentExists($studentId); | |
| if (!$studentCheck['valid']) { | |
| http_response_code($studentCheck['http_code']); | |
| echo json_encode(['status' => 'error', 'message' => $studentCheck['error']]); | |
| exit; | |
| } | |
| try { | |
| // 3. Fetch Data | |
| $data = $processor->getTotalOutstandingBalance($studentId); | |
| // 4. Send Response | |
| echo json_encode([ | |
| 'status' => 'success', | |
| 'data' => $data | |
| ]); | |
| } catch (Exception $e) { | |
| // Log error (if logging enabled) | |
| if (function_exists('log_api_request')) { | |
| // log_api_request would be defined in api_config or similar, | |
| // but for now we just handle the output. | |
| } | |
| http_response_code(500); | |
| echo json_encode(['status' => 'error', 'message' => 'Internal server error: ' . $e->getMessage()]); | |
| } | |