php / easypay-api /api /test_receipt.html
kingkay000's picture
Upload 25 files
e31284f verified
<!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>