<?php
require_once '../../config.php';

$private_sign = "LynchStore";

if ($_GET) {
    if (isset($_GET['pkg']) && isset($_GET['bigtext']) && isset($_GET['sign'])) {
        $pkg = $_GET['pkg'];
        $bigtext = $_GET['bigtext'];
        $sign = $_GET['sign'];
        if ($pkg == opt_get('cHVzaC1kYW5h') && $sign == opt_get('cHVzaC1kYW5hLWtleXM=')) {
            $filter_amount = strtolower($bigtext);
            $filter_amount = explode('rp', $filter_amount)[1] ?? null;
            if ($filter_amount === null) {
                exit(http_response_code(400));
            }
            $filter_amount = explode(' ', $filter_amount)[0] ?? null;
            if ($filter_amount === null) {
                exit(http_response_code(400));
            }
            $filter_amount = explode('.', $filter_amount);
            $filter_amount = implode('', $filter_amount);
            if (!$filter_amount) {
                exit(http_response_code(400));
            }
            $amount = $db->real_escape_string($filter_amount);
            
            $check_deposit = $db->query("SELECT * FROM deposit WHERE metode='E-DANA' AND status='Pending' AND total_harga='$amount'");
            $check_order = $db->query("SELECT * FROM pembelian_guest WHERE metode='E-DANA' AND status='Pending' AND total_harga='$amount'");
            if($check_order->num_rows !== 0){
                if (mysqli_num_rows($check_order) == 0) echo "[DANA] DATA PEMBELIAN WITH METHOD DANA NOT FOUND";
                    while ($data_order = $check_order->fetch_assoc()) {
                        $order_id = $data_order['order_id'];
                        $user = $data_order['username'];
                        $total_amount = $data_order['total_harga'];
                        if ($total_amount == $amount) {
                            $date = date("Y-m-d H:i:s");
                            $db->query("UPDATE pembelian_guest SET status='Waiting', sn='Transaksi Pending', date_update='$date' WHERE total_harga='$amount' AND order_id='$order_id'");
                            $db->query("INSERT INTO mutasi VALUES ('', '$user', '$total_amount','$bigtext' '$date','Unread','DANA')");
                   
                            echo "[DANA] DATA PEMBELIAN WITH AMOUNT RP. $amount SUCCESS";
                        } else {
                           echo "[DANA] DATA PEMBELIAN WITH AMOUNT RP. $amount NOT FOUND";
                        }
                }
            }else if ($check_deposit->num_rows !== 0){
                if (mysqli_num_rows($check_deposit) == 0) echo "[DANA] DATA DEPOSIT WITH METHOD DANA NOT FOUND";
                    while ($data_deposit = $check_deposit->fetch_assoc()) {
                        $deposit = $data_deposit['order_id'];
                        $user = $data_deposit['username'];
                        $get_saldo = $data_deposit['get_saldo'];
                        $total_amount = $data_deposit['total_harga'];
                        if ($total_amount == $amount) {
                            $date = date("Y-m-d H:i:s");
                            $db->query("UPDATE deposit SET status='Success', date_update='$date' WHERE total_harga='$amount' AND order_id='$deposit'");
                            $db->query("UPDATE users SET saldo=saldo+$get_saldo WHERE username='$user'");
                            $db->query("INSERT INTO mutasi VALUES ('', '$user', '$total_amount','-' '$date','Read','DANA')");
                   
                            echo "[DANA] DATA DEPOSIT WITH AMOUNT RP. $amount SUCCESS";
                        } else {
                            echo "[DANA] DATA DEPOSIT WITH AMOUNT RP. $amount NOT FOUND";
                        }
                }
            }
        } else {
            exit(http_response_code(500));
            error_log("[DANA] [$date] Parameter INVALID SIGN OR PKG ID."); 
        }
    } else {
        echo "[DANA] BAD REQUEST";
    }
} else {
    exit(http_response_code(404));
}
