<?php
require_once '../mainconfig.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 == 'com.shopee.id' && $sign == $private_sign) {
            $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='SHOPEEPAY' AND status='Pending' AND amount='$amount'");
            $check_order = $db->query("SELECT * FROM pembelian_guest WHERE metode='SHOPEEPAY' AND status='Pending' AND harga='$amount'");
            if($check_order->num_rows !== 0){
                if (mysqli_num_rows($check_order) == 0) echo "[SHOPEEPAY] DATA PEMBELIAN WITH METHOD SHOPEEPAY NOT FOUND";
                    while ($data_order = $check_order->fetch_assoc()) {
                        $order_id = $data_order['order_id'];
                        $user = $data_order['username'];
                        $total_amount = $data_order['harga'];
                        if ($total_amount == $amount) {
                            $date = date("Y-m-d H:i:s");
                            $db->query("UPDATE pembelian_guest SET status='Processing', sn='Transaksi Pending', date_update='$date' WHERE harga='$amount' AND order_id='$order_id'");
                            $db->query("INSERT INTO mutasi VALUES('', '$user', '-', '$total_amount', '$date','Unread','SHOPEEPAY')");
                   
                            echo "[SHOPEEPAY] DATA PEMBELIAN WITH AMOUNT RP. $amount SUCCESS";
                        } else {
                           echo "[SHOPEEPAY] DATA PEMBELIAN WITH AMOUNT RP. $amount NOT FOUND";
                        }
                }
            }else if ($check_deposit->num_rows !== 0){
                if (mysqli_num_rows($check_deposit) == 0) echo "[SHOPEEPAY] DATA DEPOSIT WITH METHOD SHOPEEPAY NOT FOUND";
                    while ($data_deposit = $check_deposit->fetch_assoc()) {
                        $deposit = $data_deposit['order_id'];
                        $user = $data_deposit['username'];
                        $total_amount = $data_deposit['amount'];
                        if ($total_amount == $amount) {
                            $date = date("Y-m-d H:i:s");
                            $db->query("UPDATE deposit SET status='Success', date_update='$date' WHERE amount='$amount' AND order_id='$deposit'");
                            $db->query("UPDATE users SET saldo=saldo+$total_amount WHERE username='$user'");
                            $db->query("INSERT INTO mutasi VALUES('', '$user', '$total_amount','-', '$date','Read','SHOPEEPAY')");
                   
                            echo "[SHOPEEPAY] DATA DEPOSIT WITH AMOUNT RP. $amount SUCCESS";
                        } else {
                            echo "[SHOPEEPAY] DATA DEPOSIT WITH AMOUNT RP. $amount NOT FOUND";
                        }
                }
            }
        } else {
            echo "[SHOPEEPAY] INVALID SIGN OR PKG ID";
        }
    } else {
        echo "[SHOPEEPAY] BAD REQUEST";
    }
} else {
    exit(http_response_code(404));
}
