<?php
date_default_timezone_set('Asia/Makassar');

$servername = "localhost";
$username = "bangalmy_tripay_payment_gateway";
$password = "89^E6Ha5rHhU"; 
$database = "bangalmy_tripay_payment_gateway";

$db = new mysqli($servername, $username, $password, $database);

if ($db->connect_error) {
    die("Koneksi database gagal: " . $db->connect_error);
}

//===============Payment metod==================

//ENDPOINT sandbox
//Sandbox URL 	https://tripay.co.id/api-sandbox/transaction/create
$Endpoint     = 'https://tripay.co.id/api-sandbox/transaction/create'; 
$apiKey       = 'DEV-U030UvOLkJKoNF6tpieRjZfa65Std4fksJSXsvIa';
$privateKey   = '7KFvx-e5cLt-v545A-BXnZd-H65zZ';
$merchantCode = 'T32842';

//ENDPOINT transaction real
//Production URL 	https://tripay.co.id/api/transaction/create
// $Endpoint     = 'https://tripay.co.id/api/transaction/create'; 
// $apiKey       = 'vbn0blv2iTpBhiswjaGq39w9yrdO2bLuUy1RJvLF';
// $privateKey   = 'BtS9c-S3XMp-OM3Aj-oj10E-NSUqp';
// $merchantCode = 'T32319';

$dns = 'wifi.konut';
$user_mikrotik = "risdian";
$password_mikrotik = "molawe123";
$ip_mikrotik = "sg15.gukotunnel.net";
$mikrotik_port = "9480";

include('routeros_api.class.php');

function verify_signature($data, $callback_signature, $private_key) {
    $signature = hash_hmac('sha256', json_encode($data), $private_key);
    return hash_equals($signature, $callback_signature);
}

$data = json_decode(file_get_contents('php://input'), true);
        if ($API->connect($ip_mikrotik, $user_mikrotik, $password_mikrotik, $mikrotik_port)) {
if (verify_signature($data, $_SERVER['HTTP_X_CALLBACK_SIGNATURE'], $privateKey)) {
    $merchant_ref = $data['merchant_ref'];
    $status = $data['status'];

    $API = new routeros_api();
    $API->debug = false;

    if ($status === 'PAID') {
            try {
                $get_profile_query = "SELECT profile, type FROM transaction WHERE merchant_ref = '$merchant_ref'";
                $result = $db->query($get_profile_query);
                $row = $result->fetch_assoc();
                $profile = $row['profile'];
                $type = $row['type'];

                if ($type === 'secret') {
                    $API->comm('/ppp/secret/set', array(
                        ".id" => $merchant_ref,
                        "profile" => $profile,
                        "comment"   => "lunas | pelanggan: " . $profile,
                    ));

                    $arrID2 = $API->comm("/ppp/active/getall", array(
                        ".proplist" => ".id",
                        "?name" => $merchant_ref,
                    ));
                    if (!empty($arrID2)) {
                        $API->comm("/ppp/active/remove", array(
                            ".id" => $arrID2[0][".id"],
                        ));
                    }
                } elseif ($type === 'hotspot') {
                    $API->comm("/ip/hotspot/user/add", array(
                        "server"    => "all",
                        "profile"	=> "$profile",
                        "name"      => $merchant_ref,
                        "password"  => $merchant_ref,
                        "comment"   => "vc-tripay",
                    ));
                }

                $update_query = "UPDATE transaction SET status = 'PAID' WHERE merchant_ref = '$merchant_ref'";
                if ($db->query($update_query) === TRUE) {
                    echo json_encode(['success' => true, 'message' => 'Transaksi berhasil diperbarui dan profil pengguna diubah.']);
                } else {
                    echo json_encode(['success' => false, 'message' => 'Gagal memperbarui status transaksi di database.']);
                }

                $API->disconnect();
            } catch (Exception $ex) {
                echo json_encode(['success' => false, 'message' => 'Caught exception: ' . $ex->getMessage()]);
            }
        } else {
            echo json_encode(['success' => false, 'message' => 'Status pembayaran tidak valid.']);
        }
    } else {
        echo json_encode(['success' => false, 'message' => 'Signature tidak valid.']);
    }
} else {
    echo json_encode(['success' => false, 'message' => 'Mikrotik Offline.']);
}
?>
