Fix Fast API check_bill: Handle TIMO API response same as TagihanController

This commit is contained in:
mwpn
2026-01-26 10:14:23 +07:00
parent 67f947a7e1
commit 748e06427e

View File

@@ -118,46 +118,60 @@ class FastController
], 500);
}
// Check if it's an HTTP error response
if (is_object($timoResponse) && isset($timoResponse->status) && $timoResponse->status != 200) {
// Convert to array untuk konsistensi (sama seperti TagihanController)
if (is_object($timoResponse)) {
$timoResponse = (array)$timoResponse;
}
// Check for HTTP error status
if (isset($timoResponse['status']) && $timoResponse['status'] != 200) {
$this->apiKeyModel->logApiUsage($apiKey->id, 'check_bill', 'failed', [
'no_sl' => $no_sl,
'error' => $timoResponse->error ?? 'HTTP Error'
'error' => $timoResponse['error'] ?? 'HTTP Error'
]);
return ResponseHelper::json($response, [
'status' => 'error',
'message' => 'Gagal cek tagihan: ' . ($timoResponse->error ?? 'HTTP Error')
], $timoResponse->status);
'message' => 'Gagal cek tagihan: ' . ($timoResponse['error'] ?? 'HTTP Error')
], $timoResponse['status']);
}
// Normal response with errno
if (isset($timoResponse->errno)) {
// Log API usage
$this->apiKeyModel->logApiUsage($apiKey->id, 'check_bill',
$timoResponse->errno == 0 ? 'success' : 'api_error', [
'no_sl' => $no_sl,
'timo_user_id' => $timoUser->id_pengguna_timo,
'wipay_user_id' => $wipayUser ? $wipayUser->id_wipay : null
]);
// Get data - bisa dari 'data' key atau langsung dari response (sama seperti TagihanController)
$data = $timoResponse['data'] ?? $timoResponse;
return ResponseHelper::json($response, [
'status' => 'success',
'data' => $timoResponse,
'message' => 'Tagihan berhasil dicek'
], 200);
// Format response untuk Fast API (sesuai format API lama)
// Response TIMO API langsung berupa array data, perlu di-wrap dengan errno, error, recordsTotal, data
if (is_array($data) && isset($data[0])) {
// Data adalah array tagihan
$responseData = [
'errno' => 0,
'error' => '',
'recordsTotal' => count($data),
'data' => $data
];
} else {
// Log failed API call
$this->apiKeyModel->logApiUsage($apiKey->id, 'check_bill', 'failed', [
'no_sl' => $no_sl,
'error' => 'Failed to connect to TIMO API'
]);
return ResponseHelper::json($response, [
'status' => 'error',
'message' => 'Gagal cek tagihan: Tidak dapat menghubungi server TIMO'
], 500);
// Data mungkin sudah dalam format yang benar atau single object
$responseData = [
'errno' => isset($timoResponse['errno']) ? $timoResponse['errno'] : 0,
'error' => $timoResponse['error'] ?? '',
'recordsTotal' => isset($timoResponse['recordsTotal']) ? $timoResponse['recordsTotal'] : (is_array($data) ? count($data) : 1),
'data' => $data
];
}
// Log API usage
$this->apiKeyModel->logApiUsage($apiKey->id, 'check_bill',
$responseData['errno'] == 0 ? 'success' : 'api_error', [
'no_sl' => $no_sl,
'timo_user_id' => $timoUser->id_pengguna_timo,
'wipay_user_id' => $wipayUser ? $wipayUser->id_wipay : null
]);
return ResponseHelper::json($response, [
'status' => 'success',
'data' => $responseData,
'message' => 'Tagihan berhasil dicek'
], 200);
} catch (\Exception $e) {
error_log("Error in checkBill: " . $e->getMessage());
return ResponseHelper::json($response, [