false, "msg" => "Action tidak dikenal"]); break; } /** * Fungsi untuk membuat order_id unik */ function generateOrderId($pdo) { do { $order_id = sprintf('%013d', mt_rand(0, 9999999999999)); $stmt = $pdo->prepare(" SELECT (SELECT COUNT(*) FROM apk_payment WHERE order_id = :order_id) + (SELECT COUNT(*) FROM client_payment WHERE order_id = :order_id) "); $stmt->bindParam(':order_id', $order_id); $stmt->execute(); $exists = $stmt->fetchColumn(); } while ($exists > 0); return $order_id; } /** * Fungsi untuk membuat instruksi pembayaran */ function bayar($pdo) { $idTagihan = intval($_POST['idTagihan'] ?? 0); $metode = strtolower($_POST['paymentMethod'] ?? ''); // Cek tagihan $stmt = $pdo->prepare("SELECT id_data_server, verifikasi_tagihan, keterangan, nomor_whatsapp, nama FROM v_tagihan WHERE id = :id"); $stmt->bindParam(':id', $idTagihan, PDO::PARAM_INT); $stmt->execute(); $dataTagihan = $stmt->fetch(PDO::FETCH_ASSOC); if (!$dataTagihan) { echo json_encode(["success" => false, "msg" => "Tagihan tidak ditemukan"]); return; } $idServer = $dataTagihan['id_data_server']; $amount = $dataTagihan['verifikasi_tagihan']; $jumlah = $dataTagihan['verifikasi_tagihan']; $note = $dataTagihan['keterangan']; $nama = $dataTagihan['nama']; $ewallet_phone = isset($dataTagihan['nomor_whatsapp']) ? preg_replace('/\D/', '', $dataTagihan['nomor_whatsapp']) : ''; $stmt = $pdo->prepare("SELECT biaya_payment_gateway FROM data_server WHERE id = :id_data_server"); $stmt->bindParam(':id_data_server', $idServer, PDO::PARAM_INT); $stmt->execute(); $dataBiaya = $stmt->fetch(PDO::FETCH_ASSOC); //perhitungan biaya payment gateway $jenisBiaya = $dataBiaya['biaya_payment_gateway']; switch ($metode) { case 'qris': $biaya = $jumlah * 0.01; break; case 'mandiri':$biaya = 4500; break; default: $biaya = 2950; break; } if ($metode == 'qris') { if ($jenisBiaya == 2) { $saldo = $jumlah - $biaya; $amount = $jumlah; } else { $saldo = $jumlah; $amount = $jumlah + $biaya; } } else { if ($jenisBiaya == 2) { $saldo = $jumlah - $biaya; $amount = $jumlah; } else { $saldo = $jumlah; $amount = $jumlah + $biaya; } } if ($amount <= 0) { echo json_encode(["success" => false, "msg" => "Nominal tagihan tidak valid"]); return; } $valid_time = ($metode === 'qris') ? 300 : 3600; $new_order_id = generateOrderId($pdo); $data = [ 'unique_code' => $new_order_id, 'service' => $metode, 'amount' => $amount, 'note' => $note, 'valid_time' => $valid_time, 'ewallet_phone' => $ewallet_phone, ]; $response = CreateTransaction($data); $dataCreate = json_decode($response, true); $jenis = $dataCreate['data']['service_name']; if (!empty($dataCreate['success']) && $dataCreate['success'] === true) { $stmt = $pdo->prepare(" INSERT INTO client_payment (id_data_server, id_tagihan, order_id, data_get, status, jumlah, saldo, biaya, jenis, keterangan, nama, input_time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()) "); $stmt->execute([ $idServer, $idTagihan, $new_order_id, json_encode($dataCreate), 'PENDING', $amount, $saldo, $biaya, $jenis, $note, $nama ]); echo json_encode([ "success" => true, "msg" => "Berhasil membuat pembayaran", "data" => $dataCreate ]); } else { echo json_encode([ "success" => false, "msg" => "Gagal membuat pembayaran", "data" => $dataCreate, "metode" => $metode ]); } } function status($pdo) { $unique_code = $_POST['idTagihan'] ?? ''; $data = [ 'unique_code' => $unique_code, ]; $response = statusTransaction($data); $dataStatus = json_decode($response, true); if (!empty($dataStatus['success']) && $dataStatus['success'] === true) { if ($dataStatus['data']['status'] == "Success") { $stmt = $pdo->prepare(" UPDATE client_payment SET status = :status, update_time = :update_time WHERE order_id = :order_id "); $stmt->bindParam(':status', strtoupper($dataStatus['data']['status'])); $stmt->bindParam(':update_time', date('Y-m-d H:i:s')); $stmt->bindParam(':order_id', $unique_code); $stmt->execute(); } echo json_encode([ "success" => true, "msg" => "Berhasil cek status", "data" => $dataStatus ]); } else { echo json_encode([ "success" => false, "msg" => "Gagal cek status", "data" => $dataStatus ]); } }