160 lines
7.2 KiB
PHP
160 lines
7.2 KiB
PHP
<?php
|
|
// Callback Paydisini
|
|
$secret = '4f120457653057e4362b56639478af67';
|
|
$key = $_POST['key'] ?? '';
|
|
$order_id = $_POST['unique_code'] ?? '';
|
|
$status = $_POST['status'] ?? '';
|
|
$signature = $_POST['signature'] ?? '';
|
|
$sign = md5($secret . $order_id . 'CallbackStatus');
|
|
|
|
$result = ['success' => false, 'message' => 'Invalid request'];
|
|
|
|
// --- Fungsi logging ---
|
|
function writeLog($message) {
|
|
$dir = __DIR__ . "/logs";
|
|
if (!is_dir($dir)) {
|
|
mkdir($dir, 0755, true);
|
|
}
|
|
$file = $dir . '/callback_' . date('Y-m-d') . '.log';
|
|
$log = "[" . date('Y-m-d H:i:s') . "] " . $message . PHP_EOL;
|
|
file_put_contents($file, $log, FILE_APPEND);
|
|
}
|
|
|
|
try {
|
|
if ($key !== $secret) {
|
|
throw new Exception("Invalid key");
|
|
}
|
|
|
|
include '../../../config/connect.php';
|
|
include '../../../config/whatsapp_api.php';
|
|
|
|
// Cari data payment
|
|
$stmt = $pdo->prepare("SELECT * FROM apk_payment WHERE order_id = :order_id");
|
|
$stmt->execute([':order_id' => $order_id]);
|
|
$apk = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($apk) {
|
|
$apk['table_name'] = 'apk_payment';
|
|
$data = $apk;
|
|
} else {
|
|
$stmt = $pdo->prepare("SELECT * FROM client_payment WHERE order_id = :order_id");
|
|
$stmt->execute([':order_id' => $order_id]);
|
|
$client = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
if ($client) {
|
|
$client['table_name'] = 'client_payment';
|
|
$data = $client;
|
|
}
|
|
}
|
|
|
|
|
|
if (!$data) {
|
|
throw new Exception("Order not found: $order_id");
|
|
}
|
|
|
|
// Log awal callback
|
|
// writeLog("Callback received | Order=$order_id | Status=$status | Signature=$signature | Expected=$sign");
|
|
|
|
if ($signature !== $sign) {
|
|
throw new Exception("Signature mismatch");
|
|
}
|
|
|
|
// === APK_PAYMENT ===
|
|
if ($data['table_name'] === 'apk_payment') {
|
|
if ($status === 'Success') {
|
|
if ($data['jenis'] === 'PPPOE') {
|
|
// TODO: Update sukses PPPOE (kosong dulu)
|
|
// writeLog("Order $order_id: PPPOE payment success");
|
|
} elseif ($data['jenis'] === 'Hotspot') {
|
|
$stmt = $pdo->prepare("SELECT * FROM v_voucher_paygw WHERE order_id = :order_id");
|
|
$stmt->execute([':order_id' => $order_id]);
|
|
$vcr = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
if (!$vcr) throw new Exception("Voucher tidak ditemukan | Order=$order_id");
|
|
|
|
require('../../../config/routeros_api.class.php');
|
|
$API = new RouterosAPI();
|
|
$API->debug = false;
|
|
|
|
if ($API->connect2($vcr['ip_address'], $vcr['username'], $vcr['password'], $vcr['port_api'])) {
|
|
try {
|
|
$API->comm('/ip/hotspot/user/add', [
|
|
"name" => $vcr['voucher'],
|
|
"password" => $vcr['voucher'],
|
|
"profile" => $vcr['nama_profile'],
|
|
"server" => "all"
|
|
]);
|
|
$pdo->prepare("UPDATE ganerate_voucher SET status_create = '1' WHERE id = ?")
|
|
->execute([$vcr['id']]);
|
|
|
|
$nomorList = array_filter(array_map('trim', explode(',', $vcr['nomor_whatsapp'])));
|
|
if ($nomorList) {
|
|
$Pesan = "Pembelian voucher kamu *BERHASIL!*\n\n"
|
|
. "Tanggal : {$vcr['waktu_transaksi']}\n"
|
|
. "Pembayaran : QRIS\n\n"
|
|
. "Paket : {$vcr['nama_paket']}\n"
|
|
. "Harga : Rp " . number_format($vcr['harga']) . "\n"
|
|
. "Kode Voucher : *{$vcr['voucher']}*\n\n"
|
|
. "_Voucher hanya bisa digunakan pada 1 perangkat / device._\n\n"
|
|
. "Terima kasih telah menggunakan layanan kami.";
|
|
(new PesanWA())->Send_Pesan($vcr['id_data_server'], 'sendMessage', $nomorList, $Pesan);
|
|
}
|
|
// writeLog("Voucher {$vcr['voucher']} sukses ditambahkan di MikroTik & WA dikirim | Order=$order_id");
|
|
} catch (Exception $e) {
|
|
$pdo->prepare("UPDATE ganerate_voucher SET status_create = '0' WHERE id = ?")
|
|
->execute([$vcr['id']]);
|
|
// writeLog("Gagal tambah user hotspot | Order=$order_id | Error=" . $e->getMessage());
|
|
} finally {
|
|
$API->disconnect();
|
|
}
|
|
} else {
|
|
$pdo->prepare("UPDATE ganerate_voucher SET status_create = '0' WHERE id = ?")
|
|
->execute([$vcr['id']]);
|
|
// writeLog("Gagal koneksi MikroTik | IP={$vcr['ip_address']} | Order=$order_id");
|
|
}
|
|
} else {
|
|
$ids = array_map('intval', explode(',', $data['id_data_server']));
|
|
if ($ids) {
|
|
$in = implode(',', array_fill(0, count($ids), '?'));
|
|
$sql = "UPDATE data_server SET status='Aktif', expaired_date=? WHERE id IN ($in)";
|
|
$params = array_merge([$data['expaired_time']], $ids);
|
|
$pdo->prepare($sql)->execute($params);
|
|
}
|
|
// writeLog("Server status diupdate aktif | Order=$order_id");
|
|
}
|
|
|
|
$pdo->prepare("UPDATE apk_payment SET status='LUNAS' WHERE order_id=?")->execute([$order_id]);
|
|
$result = ['success' => true, 'message' => "Payment success, updated to LUNAS"];
|
|
// writeLog("Order $order_id status updated to LUNAS");
|
|
} elseif ($status === 'Canceled') {
|
|
$pdo->prepare("UPDATE apk_payment SET status='CANCELED' WHERE order_id=?")->execute([$order_id]);
|
|
$result = ['success' => true, 'message' => "Payment canceled"];
|
|
// writeLog("Order $order_id status updated to CANCELED");
|
|
} else {
|
|
throw new Exception("Invalid status for apk_payment | Order=$order_id");
|
|
}
|
|
}
|
|
|
|
// === CLIENT_PAYMENT ===
|
|
elseif ($data['table_name'] === 'client_payment') {
|
|
if ($status === 'Success') {
|
|
$pdo->prepare("UPDATE client_payment SET status='SUCCESS', update_time=NOW() WHERE order_id=?")
|
|
->execute([$order_id]);
|
|
$result = ['success' => true, 'message' => "Client payment success"];
|
|
// writeLog("Client payment sukses | Order=$order_id");
|
|
} elseif ($status === 'Canceled') {
|
|
$pdo->prepare("UPDATE client_payment SET status='CANCELED', update_time=NOW() WHERE order_id=?")
|
|
->execute([$order_id]);
|
|
$result = ['success' => true, 'message' => "Client payment canceled"];
|
|
// writeLog("Client payment canceled | Order=$order_id");
|
|
} else {
|
|
throw new Exception("Invalid status for client_payment | Order=$order_id");
|
|
}
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
$result = ['success' => false, 'message' => $e->getMessage()];
|
|
writeLog("ERROR | Order=$order_id | " . $e->getMessage());
|
|
}
|
|
|
|
header('Content-Type: application/json');
|
|
echo json_encode($result);
|