Fix Fast API check_bill: Handle TIMO API response same as TagihanController
This commit is contained in:
@@ -118,24 +118,50 @@ 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']);
|
||||
}
|
||||
|
||||
// Get data - bisa dari 'data' key atau langsung dari response (sama seperti TagihanController)
|
||||
$data = $timoResponse['data'] ?? $timoResponse;
|
||||
|
||||
// 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 {
|
||||
// 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
|
||||
];
|
||||
}
|
||||
|
||||
// Normal response with errno
|
||||
if (isset($timoResponse->errno)) {
|
||||
// Log API usage
|
||||
$this->apiKeyModel->logApiUsage($apiKey->id, 'check_bill',
|
||||
$timoResponse->errno == 0 ? 'success' : 'api_error', [
|
||||
$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
|
||||
@@ -143,21 +169,9 @@ class FastController
|
||||
|
||||
return ResponseHelper::json($response, [
|
||||
'status' => 'success',
|
||||
'data' => $timoResponse,
|
||||
'data' => $responseData,
|
||||
'message' => 'Tagihan berhasil dicek'
|
||||
], 200);
|
||||
} 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);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
error_log("Error in checkBill: " . $e->getMessage());
|
||||
return ResponseHelper::json($response, [
|
||||
|
||||
Reference in New Issue
Block a user