get($schoolName); return $conn['merchant_id'] ?? null; } /** * Return a usable (decrypted, non-expired) access token for a school, refreshing * it lazily via the refresh token if it is near expiry. Returns null if the * school is not connected. This is what the Phase 4 payments code calls. */ function getValidAccessToken(string $schoolName): ?string { $client = new SquareClient(squareConfig()); $result = squareStore()->ensureFreshToken($schoolName, $client); return $result['access_token'] ?? null; } function getAllMerchants(): array { $store = squareStore(); $merchants = []; foreach ($store->connectedSchools() as $school) { $rec = $store->get($school); if (!$rec) continue; $merchants[$school] = [ 'merchant_id' => $rec['merchant_id'], 'expires_at' => $rec['expires_at'], ]; } return $merchants; } // Return JSON when accessed directly (never expose tokens here). if (basename(__FILE__) === basename($_SERVER['SCRIPT_FILENAME'])) { header('Content-Type: application/json'); $school = $_GET['school'] ?? ''; if ($school) { echo json_encode([ 'school' => $school, 'merchant_id' => getMerchantId($school), 'connected' => getMerchantId($school) !== null, ]); } else { echo json_encode(['merchants' => getAllMerchants()]); } }