<?php
require('../../../config.php');
$apii = $db->query("SELECT * FROM payment WHERE code='Mutasi'")->fetch_assoc();
$cekmutasi = [
    'api_signature' => $apii['api_id'],
];
$incomingApiSignature = isset($_SERVER['HTTP_API_SIGNATURE']) ? $_SERVER['HTTP_API_SIGNATURE'] : '';

if (! hash_equals($cekmutasi['api_signature'], $incomingApiSignature)) {
    echo 'Invalid Signature';
}

$post = file_get_contents('php://input');
$json = json_decode($post);

if (json_last_error() !== JSON_ERROR_NONE) {
    echo 'Invalid JSON';
}
if ($json->action === 'payment_report') {
    foreach ($json->content->data as $data) {
        $time = $data->unix_timestamp;
        $type = $data->type;
        $amount = $data->amount;
        $amountt = (int)$amount;
        $description = $data->description;
        $balance = $data->balance;

        if ($type === 'credit') {
            $pembelian = $db->query("SELECT * FROM pembelian_guest WHERE total_harga='$amountt' AND status = 'Pending' ");
            $deposit = $db->query("SELECT * FROM deposit WHERE amount='$amountt' AND status = 'Pending'");
            if (mysqli_num_rows($pembelian) !== 0) {
                $db->query("UPDATE pembelian_guest SET status = 'Waiting',sn='Transaksi Pending' WHERE total_harga='$amountt' AND status='Pending'");
            }else if (mysqli_num_rows($deposit) !== 0) {
                $depo = $deposit->fetch_assoc();
                $db->query("UPDATE deposit SET status = 'Success' WHERE amount='$amountt'");
                $db->query("UPDATE users SET saldo=saldo+{$depo['get_saldo']} WHERE username='{$depo['username']}'");
            }
        }
    }
}