first commit
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
/*******************************************************************
|
||||
|
||||
PHP Class : /Utils/PPOBClient
|
||||
Description : Class khusus untuk Client PPOB
|
||||
Created By : Siau Pheng ([email protected]).
|
||||
Created Date : February 10th, 2012.
|
||||
Last Modified : October 22nd, 2026.
|
||||
|
||||
(c) 3FONIA Software; WWW.3FONIA.COM.
|
||||
|
||||
*******************************************************************/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
use flight\Engine;
|
||||
use flight\Session;
|
||||
use flight\database\SimplePdo;
|
||||
|
||||
final class PPOBClient {
|
||||
|
||||
private string $ppob_server = "https://api.trifonia.com/";
|
||||
private array $agen;
|
||||
private int $id_user;
|
||||
private int $id_server;
|
||||
private int $id_cabang;
|
||||
protected SimplePdo $db;
|
||||
|
||||
public function __construct(Engine $app) {
|
||||
$this->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 '<i class="bi-'. $aicon[$id] .'"></i>';
|
||||
}
|
||||
|
||||
public static function Badge_Status(?int $id): string {
|
||||
$hasil = "";
|
||||
if ($id <= 1) $hasil = '<span class="badge rounded-pill bg-warning"><i class="bi-upload me-1"></i>Proses</span>';
|
||||
if ($id == 2) $hasil = '<span class="badge rounded-pill bg-danger"><i class="bi-x-lg me-1"></i>Gagal</span>';
|
||||
if ($id == 3) $hasil = '<span class="badge rounded-pill bg-secondary"><i class="bi-arrow-clockwise me-1"></i>Refund</span>';
|
||||
if ($id == 4) $hasil = '<span class="badge rounded-pill bg-success"><i class="bi-check-all me-1"></i>Sukses</span>';
|
||||
return $hasil;
|
||||
}
|
||||
|
||||
public static function makeWAText(?string $msg): string {
|
||||
$styles = [ '*' => 'strong', '_' => 'i', '~' => 'strike' ];
|
||||
return preg_replace_callback('/(?<!\w)([*~_])(.+?)\1(?!\w)/',
|
||||
function($m) use($styles) {
|
||||
return '<'. $styles[$m[1]]. '>'. $m[2]. '</'. $styles[$m[1]]. '>';
|
||||
},
|
||||
$msg);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user