35464-vm/api/dummy_endpoint.php
2025-11-04 16:06:10 +00:00

80 lines
2.1 KiB
PHP

<?php
header('Content-Type: application/json');
// --- Authentication ---
$authHeader = $_SERVER['HTTP_AUTHORIZATION'] ?? null;
$expectedToken = 'Bearer aSecretToken123'; // Dummy token
if ($authHeader !== $expectedToken) {
http_response_code(401);
echo json_encode([
'statusCode' => 401,
'statusMessage' => 'Unauthorized'
]);
exit;
}
// --- Request Method & Body ---
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode([
'statusCode' => 405,
'statusMessage' => 'Method Not Allowed'
]);
exit;
}
$json_payload = file_get_contents('php://input');
$data = json_decode($json_payload, true);
// --- Validation ---
$required_fields = ['name', 'accountNumber', 'accountName', 'amount'];
$missing_fields = [];
foreach ($required_fields as $field) {
if (empty($data[$field])) {
$missing_fields[] = $field;
}
}
if (!empty($missing_fields)) {
http_response_code(400);
echo json_encode([
'statusCode' => 400,
'statusMessage' => 'Bad Request',
'error' => 'Missing required fields: ' . implode(', ', $missing_fields)
]);
exit;
}
// --- Log Request (for debugging the dummy API itself) ---
// In a real app, you might not log to a file here, but it's useful for the dummy.
// The main app will log to its own DB.
$log_entry = [
'timestamp' => date('c'),
'request' => $data,
];
// --- Process and Respond ---
// Simulate some random failures for realism
$should_fail = rand(1, 10) > 8; // 20% chance of failure
if ($should_fail) {
http_response_code(500);
$response = [
'statusCode' => 500,
'statusMessage' => 'Internal Server Error',
'error' => 'A simulated random error occurred.'
];
} else {
http_response_code(200);
$response = [
'statusCode' => 200,
'statusMessage' => 'sukses',
'transactionId' => 'txn_' . uniqid()
];
}
$log_entry['response'] = $response;
file_put_contents('dummy_api_log.txt', json_encode($log_entry) . PHP_EOL, FILE_APPEND);
echo json_encode($response);