Files
manjapro_v6/controller/testgetflip.php
T
2025-11-11 12:57:39 +07:00

97 lines
2.8 KiB
PHP

<?php
include '../config/connect.php';
require '../payment/flip/Flip/Transaction.php';
try {
// Query ambil data tagihan yang lunas & masih ACTIVE di bulan berjalan
$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 5
";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);
$results = [];
$no = 1;
foreach ($data as $row) {
// Decode JSON transaction_data
$data2 = json_decode($row['transaction_data'], true);
// Cek jika JSON tidak valid atau kosong
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'
];
$no++;
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']]);
}
// Tambahkan ke hasil
$results[] = [
'nomor' => $no,
'id' => $row['id'],
'transaction_id' => $row['transaction_id'],
'title' => $delete['title'] ?? '(tidak ada title)',
// 'flip_response' => $response // bisa diaktifkan nanti jika perlu request ke Flip API
];
$no++;
}
// Output hasil
header('Content-Type: application/json');
echo json_encode($results, JSON_PRETTY_PRINT);
} catch (PDOException $e) {
echo json_encode([
'status' => 'error',
'message' => 'Database Error: ' . $e->getMessage()
]);
} catch (Exception $e) {
echo json_encode([
'status' => 'error',
'message' => 'General Error: ' . $e->getMessage()
]);
}