db = $app->db(); $this->agen = $app->session()->get('APP_PPOB'); $this->id_user = (int) $app->session()->get('WEB_USER', ['UserID'=>0])['UserID']; $this->id_server = (int) $app->session()->get('WEB_APP')['SERVER_ID']; $this->id_cabang = (int) $app->session()->get('APP_INV')['CABANGID']; } public function Depo(string $bank, float $jumlah): array { $sign = $this->CreateSign($bank, "", ""); $url = $this->ppob_server.$this->agen['API_AGENID']."/depo&bank=".$bank."&jumlah=".(string) $jumlah."&sign=".$sign; $tdata = file_get_contents($url); $adata = json_decode($tdata, true); if (count($adata) > 0 && (int) $adata['Status'] === 4) { $copy = ['No_Depo','Tgl_Depo','Tgl_Exp','Jumlah','Bank','Catatan']; $insert = array_intersect_key($adata, array_flip($copy)); $insert['UserID'] = $this->id_user; $this->db->insert("ppob_depo", $insert); } return $adata; } public function TopUp(string $kode, string $tujuan, string $no_tx, ?string $no_hp=""): array { $sign = $this->CreateSign($kode, $tujuan, $no_tx); $url = $this->ppob_server.$this->agen['API_AGENID']."/topup&kode=".$kode."&tujuan=".$tujuan."&agentx=".$no_tx."&hppel=".$no_hp."&sign=".$sign; $tdata = file_get_contents($url); $adata = json_decode($tdata, true); $this->db->update("ppob_tx", $this->Generate_SQL($adata), "No_TX=?", [$adata['No_TX']]); return ['Status'=>$adata['Status'], 'Message'=>$adata['Message']]; } public function CheckTrxByID(string $no_tx): array { $sign = $this->CreateSign("", "", $no_tx); $url = $this->ppob_server.$this->agen['API_AGENID']."/cek&agentx=".$no_tx."&sign=".$sign; $tdata = file_get_contents($url); $adata = json_decode($tdata, true); $this->Save_TX($adata); return ['Status'=>$adata['Status'], 'Message'=>$adata['Message']]; } public function CheckTrx(): array { $sign = $this->CreateSign("", "", ""); $url = $this->ppob_server.$this->agen['API_AGENID']."/cek-all&sign=".$sign; $tdata = file_get_contents($url); $adata = json_decode($tdata, true); if ((int) $adata['Status'] === 1) { if (count($adata['Depo']) > 0) { foreach ($adata['Depo'] as $_row) { $this->Save_Depo($_row); } } if (count($adata['TX']) > 0) { foreach ($adata['TX'] as $_row) { $this->Save_TX($_row); } } if ($adata['Saldo']) { $this->db->runQuery("UPDATE ppob_config SET API_SALDO=? WHERE REG_ID=?", [$adata['Saldo'], $this->agen['REG_ID']]); } } return ['Status'=>$adata['Status'], 'Message'=>$adata['Message']]; } public function Balance(): array { $sign = $this->CreateSign("", "", ""); $url = $this->ppob_server.$this->agen['API_AGENID']."/saldo&sign=".$sign; $tdata = file_get_contents($url); $adata = json_decode($tdata, true); if (count($adata) > 0 && (int) $adata['Status'] === 4) $this->db->runQuery("UPDATE ppob_config SET API_SALDO=? WHERE REG_ID=?", [$adata['Saldo'], $this->agen['REG_ID']]); return $adata; } public function Produk(string $kate, string $ope): array { $sign = $this->CreateSign("", "", ""); $url = $this->ppob_server.$this->agen['API_AGENID']."/produk&kategori=".$kate."&operator=".$ope."&sign=".$sign; $tdata = file_get_contents($url); $adata = json_decode($tdata, true); if (count($adata) > 0 && (int) $adata['Status'] === 4) { $this->db->runQuery("TRUNCATE TABLE import_produk"); foreach ($adata['Data'] as $_row) { $copy = ['Kode','Operator','Kategori','Nama_Produk','Harga_Pokok','Status']; $insert = array_intersect_key($_row, array_flip($copy)); $this->db->insert("import_produk", $insert); } $this->db->runQuery("UPDATE import_produk A, ppob_kategori B SET A.KateID=B.KateID WHERE A.Kategori=B.K_Kate"); $this->db->runQuery("UPDATE import_produk A, ppob_operator B SET A.OpeID=B.OpeID WHERE A.Operator=B.K_Ope"); $this->db->runQuery("INSERT IGNORE INTO ppob_produk (OpeID,KateID,Kode,Operator,Kategori,Nama_Produk,Harga_Pokok,Status) SELECT OpeID,KateID,Kode,Operator,Kategori,Nama_Produk,Harga_Pokok,Status FROM import_produk WHERE Kode NOT IN (SELECT Kode FROM ppob_produk)"); $this->db->runQuery("UPDATE ppob_produk A,import_produk B SET A.Harga_Pokok=B.Harga_Pokok,A.Status=B.Status WHERE A.Kode=B.Kode"); $this->db->runQuery("TRUNCATE TABLE import_produk"); } return ['Status'=>$adata['Status'], 'Message'=>$adata['Message']]; } public function Save_Depo(array $adata): void { if (!isset($adata['No_Depo'])) return; $this->db->runQuery("UPDATE ppob_depo SET Proses=? WHERE No_Depo=?", [$adata['Proses'], $adata['No_Depo']]); } public function Save_TX(array $adata): void { if (!isset($adata['No_TX'])) return; $this->db->update("ppob_tx", $this->Generate_SQL($adata), "No_TX=?", [$adata['No_TX']]); } private function Generate_SQL($adata = []): array { $asql = []; foreach ($adata as $_key => $_val) { if ($_key === "Svr_TX") $asql['Svr_TX'] = $_val; if ($_key === "Entri_Stamp") $asql['Svr_Entri'] = $_val; if ($_key === "Update_Stamp") $asql['Svr_Update'] = $_val; if ($_key === "Status") { $asql['Status'] = $_val; $asql['Proses'] = ((int) $_val > 0) ? 1 : 0; } if ($_key === "Catatan") $asql['Catatan'] = $_val; if ($_key === "SN") $asql['SN'] = $_val; if ($_key === "Harga") $asql['Harga_Beli'] = $_val; if ($_key === "Saldo") { $this->db->runQuery("UPDATE ppob_config SET API_SALDO=? WHERE API_AGENID=?", [$_val, $this->agen['API_AGENID']]); } } return $asql; } private function CreateSign(?string $kode, ?string $tujuan, ?string $no_tx): string { $templ = base64_encode(sha1("3FSP|".$this->agen['API_AGENID']."|".$kode."|".$tujuan."|".$no_tx."|".(string) $this->agen['API_PIN']."|".$this->agen['API_SECRET'], true)); $sign = str_replace("/", "_", str_replace("+", "-", rtrim($templ, "="))); return $sign; } public function Nomor_Baru(string $tipe = "P", string $tgl = ""): string { $atipe = ['P'=>["No_TX","ppob_tx"]]; if (empty($tgl)) { $tno = $tipe . date("y") . abjad((int) date("n")); } else { $tmp = explode("-", $tgl); $tno = $tipe . substr((string) $tmp[2],2,2) . abjad((int) $tmp[1]); } $tno .= $this->id_server . $this->id_cabang; $ino = (int) $this->db->fetchField("SELECT MAX(RIGHT(".$atipe[$tipe][0].",4)) Nomor FROM ".$atipe[$tipe][1]." WHERE ".$atipe[$tipe][0]." LIKE '".$tno."%'") ?? 0; $tno .= isi_nol(++$ino, 4); return $tno; } public static function SN_PLN(string $kode): array { $ahasil = ['Token'=>"", 'Nama'=>"", 'Daya'=>"", 'Kwh'=>""]; if (!empty($kode)) { $atmp = explode("/", $kode); $ahasil = ['Token'=>$atmp[0], 'Nama'=>$atmp[1], 'Daya'=>$atmp[2]." / ".$atmp[3], 'Kwh'=>$atmp[4]]; } return $ahasil; } public static function Icon_Kate(?int $id): string { $aicon = [0=>"question-circle", 1=>"phone", 2=>"tablet", 3=>"envelope", 4=>"phone", 5=>"lightbulb", 6=>"wallet", 7=>"wallet", 8=>"ticket"]; return ''; } public static function Badge_Status(?int $id): string { $hasil = ""; if ($id <= 1) $hasil = 'Proses'; if ($id == 2) $hasil = 'Gagal'; if ($id == 3) $hasil = 'Refund'; if ($id == 4) $hasil = 'Sukses'; return $hasil; } public static function makeWAText(?string $msg): string { $styles = [ '*' => 'strong', '_' => 'i', '~' => 'strike' ]; return preg_replace_callback('/(?'. $m[2]. ''; }, $msg); } }