connect_error) { return null; } $conn->set_charset($dbConfig['charset']); return $conn; } function getMerchantId(string $schoolName): ?string { $db = getDbConnection(); if (!$db) { // Fallback to JSON file $file = __DIR__ . '/data/school_merchants.json'; if (!file_exists($file)) return null; $merchants = json_decode(file_get_contents($file), true) ?: []; return $merchants[$schoolName]['merchant_id'] ?? null; } $stmt = $db->prepare("SELECT paypal_merchant_id FROM schools WHERE school_name = ? AND paypal_merchant_id IS NOT NULL"); $stmt->bind_param('s', $schoolName); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_assoc(); $stmt->close(); $db->close(); return $row['paypal_merchant_id'] ?? null; } function getAllMerchants(): array { $db = getDbConnection(); if (!$db) { // Fallback to JSON file $file = __DIR__ . '/data/school_merchants.json'; if (!file_exists($file)) return []; return json_decode(file_get_contents($file), true) ?: []; } $result = $db->query("SELECT school_name, paypal_merchant_id, paypal_connected_at FROM schools WHERE paypal_merchant_id IS NOT NULL"); $merchants = []; while ($row = $result->fetch_assoc()) { $merchants[$row['school_name']] = [ 'merchant_id' => $row['paypal_merchant_id'], 'connected_at' => strtotime($row['paypal_connected_at']) ]; } $db->close(); return $merchants; } // Return JSON when accessed directly writeLog(basename(__FILE__)); writeLog(basename($_SERVER['SCRIPT_FILENAME'])); if (basename(__FILE__) === basename($_SERVER['SCRIPT_FILENAME'])) { header('Content-Type: application/json'); $school = $_GET['school'] ?? ''; writeLog($school); if ($school) { echo json_encode([ 'school' => $school, 'merchant_id' => getMerchantId($school) ]); } else { echo json_encode(['merchants' => getAllMerchants()]); } }