Spaces:
Running
Running
File size: 10,558 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Receipt API Tester</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 900px;
margin: 0 auto;
background: white;
border-radius: 12px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
padding: 40px;
}
h1 {
color: #333;
margin-bottom: 10px;
text-align: center;
}
.subtitle {
text-align: center;
color: #666;
margin-bottom: 30px;
}
.form-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
color: #333;
font-weight: 600;
}
input[type="text"] {
width: 100%;
padding: 12px;
border: 2px solid #ddd;
border-radius: 6px;
font-size: 16px;
transition: border-color 0.3s;
}
input[type="text"]:focus {
outline: none;
border-color: #667eea;
}
.btn {
width: 100%;
padding: 14px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 6px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: transform 0.3s;
margin-bottom: 10px;
}
.btn:hover {
transform: translateY(-2px);
}
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.btn-secondary {
background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
}
.response-section {
margin-top: 30px;
display: none;
}
.response-section.show {
display: block;
}
.receipt-container {
background: #f8f9fa;
border-radius: 8px;
padding: 20px;
margin-top: 15px;
text-align: center;
}
.receipt-container img {
max-width: 100%;
border: 2px solid #ddd;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.error-box {
background: #f8d7da;
border: 1px solid #f5c6cb;
border-radius: 8px;
padding: 20px;
margin-top: 15px;
color: #721c24;
}
.info-box {
background: #e7f3ff;
border-left: 4px solid #2196F3;
padding: 15px;
margin-bottom: 20px;
border-radius: 4px;
}
.info-box p {
margin: 5px 0;
color: #333;
}
.checkbox-group {
margin-bottom: 20px;
}
.checkbox-group label {
display: flex;
align-items: center;
font-weight: normal;
}
.checkbox-group input[type="checkbox"] {
margin-right: 10px;
width: 18px;
height: 18px;
}
.loading {
text-align: center;
padding: 20px;
color: #667eea;
font-weight: 600;
}
</style>
</head>
<body>
<div class="container">
<h1>📄 Receipt API Tester</h1>
<p class="subtitle">Test the Last Payment Receipt endpoint</p>
<div class="info-box">
<p><strong>API Endpoint:</strong> <code id="apiEndpoint"></code></p>
<p><strong>Method:</strong> GET</p>
<p><strong>Response:</strong> PNG Image</p>
</div>
<form id="receiptForm">
<div class="form-group">
<label for="studentId">Student ID *</label>
<input type="text" id="studentId" name="student_id" required placeholder="e.g., 000001234567890123">
</div>
<div class="checkbox-group">
<label>
<input type="checkbox" id="useAuth" name="use_auth">
Use API Key Authentication
</label>
</div>
<div class="form-group" id="apiKeyGroup" style="display: none;">
<label for="apiKey">API Key</label>
<input type="text" id="apiKey" name="api_key" placeholder="Enter your API key">
</div>
<button type="submit" class="btn" id="submitBtn">Get Receipt</button>
<button type="button" class="btn btn-secondary" id="downloadBtn" style="display: none;">Download
Receipt</button>
</form>
<div class="response-section" id="responseSection">
<div id="loadingIndicator" class="loading" style="display: none;">
Loading receipt...
</div>
<div id="receiptContainer" class="receipt-container" style="display: none;">
<h3>Receipt Image</h3>
<img id="receiptImage" src="" alt="Receipt">
</div>
<div id="errorContainer" class="error-box" style="display: none;">
<h3>Error</h3>
<p id="errorMessage"></p>
</div>
</div>
</div>
<script>
// Set API endpoint
const apiEndpoint = window.location.origin + '/easypay/api/students/last_receipt';
document.getElementById('apiEndpoint').textContent = apiEndpoint;
let currentReceiptBlob = null;
// Toggle API key field
document.getElementById('useAuth').addEventListener('change', function () {
document.getElementById('apiKeyGroup').style.display = this.checked ? 'block' : 'none';
});
// Handle form submission
document.getElementById('receiptForm').addEventListener('submit', async function (e) {
e.preventDefault();
const submitBtn = document.getElementById('submitBtn');
const downloadBtn = document.getElementById('downloadBtn');
const responseSection = document.getElementById('responseSection');
const loadingIndicator = document.getElementById('loadingIndicator');
const receiptContainer = document.getElementById('receiptContainer');
const errorContainer = document.getElementById('errorContainer');
// Reset UI
submitBtn.disabled = true;
submitBtn.textContent = 'Loading...';
downloadBtn.style.display = 'none';
responseSection.classList.add('show');
loadingIndicator.style.display = 'block';
receiptContainer.style.display = 'none';
errorContainer.style.display = 'none';
// Prepare URL
const studentId = document.getElementById('studentId').value;
const url = `${apiEndpoint}?student_id=${encodeURIComponent(studentId)}`;
// Prepare headers
const headers = {};
if (document.getElementById('useAuth').checked) {
const apiKey = document.getElementById('apiKey').value;
if (apiKey) {
headers['Authorization'] = 'Bearer ' + apiKey;
}
}
try {
// Send request
const response = await fetch(url, {
method: 'GET',
headers: headers
});
loadingIndicator.style.display = 'none';
if (response.ok) {
// Success - display image
const blob = await response.blob();
currentReceiptBlob = blob;
const imageUrl = URL.createObjectURL(blob);
document.getElementById('receiptImage').src = imageUrl;
receiptContainer.style.display = 'block';
downloadBtn.style.display = 'block';
} else {
// Error - try to parse JSON error
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
const errorData = await response.json();
displayError(errorData.message || 'Unknown error occurred');
} else {
displayError(`HTTP ${response.status}: ${response.statusText}`);
}
}
} catch (error) {
loadingIndicator.style.display = 'none';
displayError('Request failed: ' + error.message);
} finally {
submitBtn.disabled = false;
submitBtn.textContent = 'Get Receipt';
}
});
// Handle download button
document.getElementById('downloadBtn').addEventListener('click', function () {
if (currentReceiptBlob) {
const url = URL.createObjectURL(currentReceiptBlob);
const a = document.createElement('a');
a.href = url;
a.download = 'receipt_' + document.getElementById('studentId').value + '.png';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
});
function displayError(message) {
const errorContainer = document.getElementById('errorContainer');
const errorMessage = document.getElementById('errorMessage');
errorMessage.textContent = message;
errorContainer.style.display = 'block';
}
</script>
</body>
</html> |