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

$private_sign = "LynchStore";

if ($_GET) {
    if (isset($_GET['pkg']) && isset($_GET['text']) && isset($_GET['sign'])) {
        $pkg = $_GET['pkg'];
        $bigtext = $_GET['text'];
        $sign = $_GET['sign'];
        if ($pkg == 'com.gojek.resto' && $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);
            
            $wapi = $db->query("SELECT * FROM options WHERE opt_name='tp-key'")->fetch_assoc();
            $wap = $db->query("SELECT * FROM options WHERE opt_name='tp-phone'")->fetch_assoc();
            
            $check_deposit = $db->query("SELECT * FROM deposit WHERE metode='E-QRIS' AND status='Pending' AND amount='$amount'");
            $check_order = $db->query("SELECT * FROM pembelian_guest WHERE metode='E-QRIS' AND status='Pending' AND harga='$amount'");
            if($check_order->num_rows !== 0){
                if (mysqli_num_rows($check_order) == 0) echo "[QRIS] DATA PEMBELIAN WITH METHOD DANA NOT FOUND";
                    while ($data_order = $check_order->fetch_assoc()) {
                        $order_id = $data_order['order_id'];
                        if($data['username'] !== 'none'){
                        $user = $data_order['username'];
                        }else{
                            $user = $data_order['phone'];
                        }
                        $total_amount = $data_order['harga'];
                        if ($total_amount == $amount) {
                            $date = date("Y-m-d H:i:s");
                            $WhatsApp->sendMessage('https://wabot.myrigaming.my.id/',$wapi['opt_value'],$wap['opt_value'],$data_order['phone'],"*Terima Kasih, Pembayaran Pesanan Kamu Telah Berhasil Dibayar, Pesanan Akan Diproses!*\n\n*BY ©LOOKIT STORE*");
                            
                            $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','-' '$date','Unread','QRISM')");
                   
                            echo "[QRIS] DATA PEMBELIAN WITH AMOUNT RP. $amount SUCCESS";
                        } else {
                            echo "[QRIS] DATA PEMBELIAN WITH AMOUNT RP. $amount NOT FOUND";
                        }
                }
            }else if ($check_deposit->num_rows !== 0){
                if (mysqli_num_rows($check_deposit) == 0) echo "[QRIS] DATA DEPOSIT WITH METHOD DANA 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','QRIS')");
                   
                            echo "[QRIS] DATA DEPOSIT WITH AMOUNT RP. $amount SUCCESS";
                        } else {
                            echo "[QRIS] DATA DEPOSIT WITH AMOUNT RP. $amount NOT FOUND";
                        }
                }
            }
        } else {
            echo "[QRIS] INVALID SIGN OR PKG ID";
        }
    } else {
        echo "[QRIS] BAD REQUEST";
    }
} else {
    exit(http_response_code(404));
}
