Files
verifikator/app/Services/TteService.php
2026-11-05 16:03:14 +07:00

98 lines
3.0 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Throwable;
class TteService
{
private string $baseUrl = 'http://118.98.229.146';
private string $username = '9702946';
private string $password = 'DsC1-G0cP-Pbk8-qYxE';
public function signDokumenUser(string $nik, string $passphrase, string $filePath, string $linkQr): string|false
{
try {
$query = http_build_query([
'nik' => $nik,
'passphrase' => $passphrase,
'tampilan' => 'invisible',
'halaman' => 'terakhir',
'image' => 'false',
'xAxis' => 43.63,
'yAxis' => 28.71,
'width' => 550.78,
'height' => 130.88,
'reason' => 'Dokumen ditandatangani secara elektronik Universitas Tanjungpura',
'location' => 'Universitas Tanjungpura',
'linkQR' => $linkQr,
]);
$response = Http::withBasicAuth($this->username, $this->password)
->timeout(60)
->attach('file', file_get_contents($filePath), basename($filePath))
->post("{$this->baseUrl}/api/sign/pdf?{$query}");
} catch (Throwable $th) {
Log::error('TTE signDokumenUser : '.$th->getMessage());
return false;
}
if (! $response->successful()) {
Log::error("TTE signDokumenUser gagal: HTTP {$response->status()} - {$response->body()}");
return false;
}
return $response->body();
}
/**
* Status pendaftaran pengguna di BSrE (GET /api/user/status/{nik}).
* Contoh: NOT_REGISTERED (2011) = belum punya sertifikat, ISSUE (1111) = sertifikat aktif.
*
* @return array{status_code?: int, status?: string, message?: string}|null Null kalau API gagal dihubungi.
*/
public function statusUser(string $nik): ?array
{
try {
$response = Http::withBasicAuth($this->username, $this->password)
->timeout(15)
->get("{$this->baseUrl}/api/user/status/{$nik}");
} catch (Throwable $th) {
Log::error('TTE statusUser : '.$th->getMessage());
return null;
}
$data = $response->json();
if (! is_array($data) || ! isset($data['status'])) {
Log::error("TTE statusUser gagal: HTTP {$response->status()} - {$response->body()}");
return null;
}
return $data;
}
/**
* Pengguna wajib tanda tangan elektronik kecuali BSrE menyatakan NIK-nya belum terdaftar.
* Kalau NIK tidak diketahui atau API gagal, dianggap wajib supaya tidak ada persetujuan yang lolos tanpa TTE.
*/
public function wajibTte(?string $nik): bool
{
if (! $nik) {
return true;
}
$status = $this->statusUser($nik);
return ($status['status'] ?? null) !== 'NOT_REGISTERED';
}
}