Files
2026-10-19 15:22:52 -07:00

108 lines
3.1 KiB
PHP

<?php
$ValidAccess = 2010;
require_once("../include/globals.php");
if(session_status() !== PHP_SESSION_ACTIVE) session_start();
if (SessionActive() != 1 || ! UserLoggedIn()) {
header("Location: {$FormTarget}");
exit();
}
$AllowedUserTypes = array('SuperUser','System Administrator');
if (checkPermission($AllowedUserTypes) != 1) {
$error_message = getLanguageText('ERRORMSG-ACCESS-DENIED');
$naction = '';
header("Location: {$FormTarget}?action=$action&naction=$naction$headerArgs&form_message=" . urlencode($form_message) . "&error_message=" . urlencode($error_message));
exit();
}
/**
* Simple helper to get merchant IDs for schools from database
*/
function getDbConnection() {
$dbConfig = require __DIR__ . '/db_config.php';
$conn = new mysqli(
$dbConfig['host'],
$dbConfig['username'],
$dbConfig['password'],
$dbConfig['database']
);
if ($conn->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()]);
}
}