Files
manjapro_v6/load/cronjob/flip_auto_inactive.php
T
2025-11-11 12:57:39 +07:00

226 lines
7.3 KiB
PHP

<?php
// /**
// * File: /www/wwwroot/bilspro/cron/flip_auto_inactive.php
// * Jalankan via cron: */10 * * * * php /www/wwwroot/bilspro/cron/flip_auto_inactive.php >/dev/null 2>&1
// */
ini_set('max_execution_time', 60);
ini_set('memory_limit', '256M');
date_default_timezone_set('Asia/Jakarta');
require "/www/wwwroot/bilspro/config/connect.php";
require "/www/wwwroot/bilspro/payment/flip/Flip/Transaction.php";
// try {
// // Ambil maksimum 5 data agar ringan
// $sql = "
// SELECT
// a.id,
// a.transaction_id,
// b.apikey_payment_gateway,
// b.url_payment_gateway
// FROM
// tagihan a
// LEFT JOIN
// data_server b ON a.id_data_server = b.id
// WHERE
// a.status = 'lewat'
// AND a.transaction_id != ''
// AND a.transaction_status = 'ACTIVE'
// AND b.payment_gateway = 'flip'
// AND b.status_payment_gateway = 'ACTIVE'
// AND (
// YEAR(a.limit_pembayaran) < YEAR(NOW())
// OR (
// YEAR(a.limit_pembayaran) = YEAR(NOW())
// AND MONTH(a.limit_pembayaran) < MONTH(NOW())
// )
// )
// ORDER BY a.limit_pembayaran ASC
// LIMIT 5
// ";
// $stmt = $pdo->prepare($sql);
// $stmt->execute();
// $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
// if (empty($data)) {
// exit; // tidak ada data, langsung keluar biar hemat resource
// }
// $logFile = "/www/wwwroot/bilspro/load/cronjob/flip_auto_inactive.log";
// if (!file_exists(dirname($logFile))) {
// mkdir(dirname($logFile), 0777, true);
// }
// $results = [];
// foreach ($data as $row) {
// try {
// $flip = new FlipPaymentGateway($row['apikey_payment_gateway'], $row['url_payment_gateway']);
// $response = $flip->getBillStatus($row['transaction_id']);
// if (!is_array($response) || empty($response['link_id'])) {
// $results[] = [
// 'id' => $row['id'],
// 'transaction_id' => $row['transaction_id'],
// 'status' => 'error',
// 'message' => 'Invalid or missing response from Flip API'
// ];
// continue;
// }
// $delete = $flip->editBill(
// $response['link_id'],
// 'INACTIVE',
// $response['title'] ?? '',
// $response['amount'] ?? 0
// );
// $results[] = [
// 'id' => $row['id'],
// 'transaction_id' => $row['transaction_id'],
// 'flip_response' => $delete
// ];
// if ($delete) {
// $update = $pdo->prepare("UPDATE tagihan SET transaction_status = 'INACTIVE' WHERE id = :id");
// $update->execute([':id' => $row['id']]);
// }
// // Delay 1 detik antar request agar tidak membanjiri Flip API
// sleep(1);
// } catch (Exception $e) {
// $results[] = [
// 'id' => $row['id'],
// 'transaction_id' => $row['transaction_id'],
// 'status' => 'error',
// 'message' => $e->getMessage()
// ];
// }
// }
// // Simpan hasil ke log file, bukan echo ke output
// file_put_contents(
// $logFile,
// "[" . date('Y-m-d H:i:s') . "] " . json_encode($results, JSON_UNESCAPED_SLASHES) . PHP_EOL,
// FILE_APPEND
// );
// } catch (Throwable $e) {
// // Tangkap semua error (PDO/Exception)
// file_put_contents(
// "/www/wwwroot/bilspro/load/cronjob/flip_auto_inactive_error.log",
// "[" . date('Y-m-d H:i:s') . "] ERROR: " . $e->getMessage() . PHP_EOL,
// FILE_APPEND
// );
// }
try {
$sql = "
SELECT
a.id,
a.transaction_id,
a.transaction_data,
b.apikey_payment_gateway,
b.url_payment_gateway
FROM
tagihan a
LEFT JOIN
data_server b
ON a.id_data_server = b.id
WHERE
a.status = 'lunas'
AND a.transaction_id != ''
AND a.transaction_status = 'ACTIVE'
AND a.transaction_data != ''
AND b.payment_gateway = 'flip'
AND b.status_payment_gateway = 'ACTIVE'
AND YEAR(a.limit_pembayaran) = YEAR(NOW())
AND MONTH(a.limit_pembayaran) = MONTH(NOW())
ORDER BY a.id ASC
LIMIT 15
";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($data)) exit;
$logFile = "/www/wwwroot/bilspro/load/cronjob/flip_auto_inactive.log";
if (!file_exists(dirname($logFile))) mkdir(dirname($logFile), 0777, true);
$results = [];
$no = 1;
foreach ($data as $row) {
try {
$data2 = json_decode($row['transaction_data'], true);
if (json_last_error() !== JSON_ERROR_NONE || empty($data2)) {
$results[] = [
'nomor' => $no++,
'id' => $row['id'],
'transaction_id' => $row['transaction_id'],
'title' => '(invalid JSON)',
'error' => 'transaction_data tidak valid'
];
continue;
}
if (empty($data2['link_id'])) {
$results[] = [
'nomor' => $no++,
'id' => $row['id'],
'transaction_id' => $row['transaction_id'],
'status' => 'error',
'message' => 'link_id kosong di transaction_data'
];
continue;
}
$flip = new FlipPaymentGateway($row['apikey_payment_gateway'], $row['url_payment_gateway']);
$delete = $flip->editBill(
$data2['link_id'],
'INACTIVE',
$data2['title'] ?? '',
$data2['amount'] ?? 0
);
if ($delete) {
$update = $pdo->prepare("UPDATE tagihan SET transaction_status = 'INACTIVE' WHERE id = :id");
$update->execute([':id' => $row['id']]);
}
$results[] = [
'nomor' => $no++,
'id' => $row['id'],
'transaction_id' => $row['transaction_id'],
'title' => is_array($delete) ? ($delete['title'] ?? '(tidak ada title)') : '(tidak ada title)'
];
sleep(1);
} catch (Exception $e) {
$results[] = [
'id' => $row['id'],
'transaction_id' => $row['transaction_id'],
'status' => 'error',
'message' => $e->getMessage()
];
}
}
file_put_contents(
$logFile,
"[" . date('Y-m-d H:i:s') . "] " . json_encode($results, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL,
FILE_APPEND
);
} catch (Throwable $e) {
file_put_contents(
"/www/wwwroot/bilspro/load/cronjob/flip_auto_inactive_error.log",
"[" . date('Y-m-d H:i:s') . "] ERROR: " . $e->getMessage() . PHP_EOL,
FILE_APPEND
);
}