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

$data = json_decode(file_get_contents('php://input'), true);
if ($data) {
    if ($data['status'] == 'PAID') {
        
        $kode_deposit = $data['merchant_ref'];
        
        $query_deposit = mysqli_query($db, "SELECT * FROM deposit WHERE order_id = '$kode_deposit' AND status = 'Pending'");
        if (mysqli_num_rows($query_deposit) === 1) {
            $fetch_deposit = mysqli_fetch_assoc($query_deposit);
            
            $query_user = mysqli_query($db, "SELECT * FROM users WHERE username = '".$fetch_deposit['username']."'");
            
            if (mysqli_num_rows($query_user) === 1) {
                $fetch_user = mysqli_fetch_assoc($query_user);
                
                $get_saldo = $fetch_deposit['get_saldo'];
                $k_id = $fetch_deposit['order_id'];
                $tanggal = date('Y-m-d H:i:s');
                
                mysqli_query($db, "UPDATE deposit SET status = 'Success', date_update='$tanggal' WHERE username = '".$fetch_deposit['username']."' AND order_id='$kode_deposit'");
                mysqli_query($db, "UPDATE users SET saldo = saldo+$get_saldo WHERE username = '".$fetch_deposit['username']."'");
                
            }else{
                
            }
        } else {
            $query_pesanan = mysqli_query($db, "SELECT * FROM pembelian_guest WHERE order_id = '$kode_deposit' AND status = 'Pending'");
            if(mysqli_num_rows($query_pesanan) === 1){
                $db->query("UPDATE pembelian_guest SET status='Waiting',sn='Transaksi Pending' WHERE order_id='$kode_deposit' AND status='Pending'");
            }
        }
    }
}