From 3f7f6e1be99d913836fb34e087e1279d4ecbc730 Mon Sep 17 00:00:00 2001 From: mwpn Date: Wed, 21 Jan 2026 11:36:28 +0700 Subject: [PATCH] Add endpoint aktifkan_wipay untuk aktivasi akun WIPAY dari mobile app --- public/index.php | 1 + src/Controllers/WipayController.php | 116 ++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/public/index.php b/public/index.php index 0fc3771..d1c9d65 100644 --- a/public/index.php +++ b/public/index.php @@ -162,6 +162,7 @@ $app->post('/timo/history_gangguan', [$laporanController, 'historyGangguan']); // WIPAY routes $app->post('/timo/cek_wipay', [$wipayController, 'cekWipay']); +$app->post('/timo/aktifkan_wipay', [$wipayController, 'aktifkanWipay']); // Other routes $app->post('/timo/promo', [$otherController, 'promo']); diff --git a/src/Controllers/WipayController.php b/src/Controllers/WipayController.php index 85fe40e..e95d4fc 100644 --- a/src/Controllers/WipayController.php +++ b/src/Controllers/WipayController.php @@ -63,6 +63,122 @@ class WipayController return ResponseHelper::custom($response, $responseData, 404); } + /** + * Aktifkan akun WIPAY (create WIPAY account) + * POST /timo/aktifkan_wipay + */ + public function aktifkanWipay(Request $request, Response $response): Response + { + $data = $request->getParsedBody(); + $token = $data['token'] ?? ''; + + if (empty($token)) { + return ResponseHelper::error($response, 'Token harus diisi', 400); + } + + $pengguna = $this->userModel->findById($token); + if (!$pengguna) { + return ResponseHelper::error($response, 'Token tidak valid. Silahkan login ulang.', 401); + } + + // Cek apakah sudah punya akun WIPAY + if ($pengguna->wipay && $pengguna->wipay > 0) { + $existingWipay = $this->db->fetchOne( + "SELECT * FROM wipay_pengguna WHERE id_wipay = :id_wipay", + ['id_wipay' => $pengguna->wipay] + ); + + if ($existingWipay) { + return ResponseHelper::error($response, 'Akun WIPAY sudah aktif', 400); + } + } + + // Cek apakah nomor HP sudah digunakan untuk akun WIPAY lain + if (!empty($pengguna->no_hp)) { + $existingWipayByHp = $this->db->fetchOne( + "SELECT * FROM wipay_pengguna WHERE no_hp = :no_hp", + ['no_hp' => $pengguna->no_hp] + ); + + if ($existingWipayByHp) { + return ResponseHelper::error($response, 'Nomor HP sudah terdaftar untuk akun WIPAY lain', 400); + } + } + + try { + // Generate no_reg (8 karakter alphanumeric) + $noReg = $this->generateRandomString(8); + + // Pastikan no_reg unik + $counter = 1; + while ($this->db->fetchOne("SELECT id_wipay FROM wipay_pengguna WHERE no_reg = :no_reg", ['no_reg' => $noReg])) { + $noReg = $this->generateRandomString(8); + $counter++; + if ($counter > 10) { + // Fallback: gunakan timestamp + $noReg = substr(str_replace(['-', ':', ' '], '', date('YmdHis')), -8); + break; + } + } + + // Insert WIPAY user + $wipayData = [ + 'no_reg' => $noReg, + 'nama_lengkap' => $pengguna->nama_lengkap ?? '', + 'email' => $pengguna->email ?: ($pengguna->username . '@timo.local'), + 'no_hp' => $pengguna->no_hp ?? '', + 'pin' => 123456, // PIN default + 'saldo' => 0, + ]; + + $idWipay = $this->db->insert('wipay_pengguna', $wipayData); + + if ($idWipay) { + // Update pengguna_timo dengan id_wipay + $this->db->update( + 'pengguna_timo', + ['wipay' => $idWipay], + 'id_pengguna_timo = :id', + ['id' => $pengguna->id_pengguna_timo] + ); + + // Get created WIPAY data + $wipayCreated = $this->db->fetchOne( + "SELECT * FROM wipay_pengguna WHERE id_wipay = :id_wipay", + ['id_wipay' => $idWipay] + ); + + return ResponseHelper::success($response, 'Akun WIPAY berhasil diaktifkan', [ + 'wipay' => $wipayCreated, + 'username' => $wipayCreated->no_hp ?? '', // Username untuk login WIPAY adalah nomor HP + 'pin' => 123456, // PIN default + 'pesan' => 'Username: ' . ($wipayCreated->no_hp ?? '') . ', PIN: 123456', + ]); + } else { + return ResponseHelper::error($response, 'Gagal membuat akun WIPAY', 500); + } + } catch (\Exception $e) { + error_log("Error in aktifkanWipay: " . $e->getMessage()); + return ResponseHelper::error($response, 'Terjadi kesalahan: ' . $e->getMessage(), 500); + } + } + + /** + * Generate random string untuk no_reg + */ + private function generateRandomString($length = 8) + { + $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $charactersLength = strlen($characters); + $randomString = ''; + + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[rand(0, $charactersLength - 1)]; + } + + return $randomString; + } + // Note: buat_kode, cek_kode, reset_kode sudah digunakan untuk reset password // Kode unik pembayaran otomatis di-generate saat request_pembayaran }