File size: 1,919 Bytes
e31284f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**

 * 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()]);
}