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