<?php
require_once("../../config.php");
$date = date('Y-m-d H:i:s');
 
$cek = $db->query("SELECT * FROM pembelian_guest WHERE status='Waiting' AND sn='Transaksi Pending'");
    
while($data_depo = $cek->fetch_assoc()){
    $db->query("UPDATE pembelian_guest SET date_update='$date' WHERE status='Processing' AND sn='Transaksi Pending' AND harga='{$data_depo['harga']}' AND fee='{$data_depo['fee']}'");
    if ($data_depo['metode'] == 'GOPAY'){
        $db->query("UPDATE mutasi SET status='Read' WHERE code='GOPAY' AND amount='{$data_depo['total_harga']}'");
    } else if ($data_depo['metode'] == 'OVO'){
        $db->query("UPDATE mutasi SET status='Read' WHERE code='OVO' AND amount='{$data_depo['total_harga']}'");
    }else if ($data_depo['metode'] == 'DANA'){
        $db->query("UPDATE mutasi SET status='Read' WHERE code='DANA' AND amount='{$data_depo['total_harga']}'");
    }else if ($data_depo['metode'] == 'SHOPEEPAY'){
        $db->query("UPDATE mutasi SET status='Read' WHERE code='SHOPEEPAY' AND amount='{$data_depo['total_harga']}'");
    }else if ($data_depo['metode'] == 'BANK BCA'){
        $db->query("UPDATE mutasi SET status='Read' WHERE code='BCA' AND amount='{$data_depo['total_harga']}'");
    }
    if(mysqli_num_rows($cek) !== 0){
        $fetch_pesanan = $data_depo;
        $query_layanan = mysqli_query($db, "SELECT * FROM layanan_pulsa WHERE id = '".$fetch_pesanan['layanan_id']."'");
        if (mysqli_num_rows($query_layanan) !== 0) {
                    $fetch_layanan = mysqli_fetch_assoc($query_layanan);
                    if ($fetch_layanan['provider'] == 'DF') {
                        $DFS = mysqli_query($db, "SELECT * FROM provider WHERE code = 'DF'");
                        $DF = mysqli_fetch_array($DFS);
                        $reff = $fetch_pesanan['order_id'];
                        $df_user = $DF['api_id'];
                        $df_key = $DF['api_key'];
                        if($fetch_pesanan['games'] == "Genshin Impact"){
                            if($fetch_pesanan['additional_target'] == "Asia"){
                                $targets = $fetch_pesanan['target'].'|os_asia';
                            } else if($fetch_pesanan['additional_target'] == "America"){
                                $targets = $fetch_pesanan['target'].'|os_usa';
                            } else if($fetch_pesanan['additional_target'] == "Europe"){
                                $targets = $fetch_pesanan['target'].'|os_euro';
                            } else if($fetch_pesanan['additional_target'] == "China"){
                                $targets = $fetch_pesanan['target'].'|os_cht';
                            }
                        }else{
                            $targets = $fetch_pesanan['target'].$fetch_pesanan['additional_target'];
                        }
                        $result = $curl->connectPost($DF['link'].'transaction',json_encode(['username' => $df_user,'buyer_sku_code' => $fetch_pesanan['sku'],'customer_no' => $targets,'ref_id' => $reff,'sign' => md5($df_user.$df_key.$reff)]));
                        if (isset($result['data'])) {
                            if ($result['data']['status'] == 'Gagal') {
                                mysqli_query($db, "UPDATE pembelian_guest SET error_msg = '".$result['data']['message']."', status = 'Canceled' WHERE id = '".$fetch_pesanan['id']."'");
                                if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                        $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                        $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                                    }
                            } else if($result['data']['status'] == 'Sukses'){
                                $sn = $result['data']['sn'] !== '' ? $result['data']['sn'] : $result['data']['message'];
                                mysqli_query($db, "UPDATE pembelian_guest SET status = 'Success', sn = 'Transaksi Pending' WHERE id = '".$fetch_pesanan['id']."'");
                                echo json_encode(['success' => true]);
                            } else {
                                mysqli_query($db, "UPDATE pembelian_guest SET status = 'Processing', sn = 'Transaksi Pending' WHERE id = '".$fetch_pesanan['id']."'");
                            }
                        } else {
                            mysqli_query($db, "UPDATE pembelian_guest SET error_msg = 'DigiFlazz tidak mereponse' WHERE id = '".$fetch_pesanan['id']."'");
                            if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                        $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                        $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                                    }
                        }
                    } else if($fetch_layanan['provider'] == 'AG'){
                            $check_data = $db->query("SELECT * FROM provider WHERE code = 'AG'");
                            $AG = $check_data->fetch_assoc();
                            $resultS = $curl->connectPost($AG['link'].'transaksi',["ref_id" => $fetch_pesanan['order_id'], "merchant_id" => $AG['merchant_id'],"produk" => $fetch_pesanan['sku'],"tujuan" => $fetch_pesanan['target'].$fetch_pesanan['additional_target'],"signature" => md5($AG['api_key'].$AG['merchant_id'].$fetch_pesanan['order_id'].$fetch_pesanan['sku'].$fetch_pesanan['target'].$fetch_pesanan['additional_target'])]);
                            if (isset($resultS['data'])) {
                                
                                if ($resultS['data']['status'] === 'Gagal') {
                                    mysqli_query($db, "UPDATE pembelian_guest SET error_msg = '".$resultS['data']['message']."', status = 'Canceled' WHERE id = '".$fetch_pesanan['id']."'");
                                    if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                        $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                        $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                                    }
                                } else if ($resultS['data']['status'] === 'Sukses') {
                                    $sn = $resultS['data']['sn'] !== '' ? $resultS['data']['sn'] : $resultS['data']['message'];
                                
                                    mysqli_query($db, "UPDATE pembelian_guest SET status = 'Success', sn = '$sn' WHERE id = '".$fetch_pesanan['id']."'");
                                
                                    echo json_encode(['success' => true]);
                                }else{
                                    mysqli_query($db, "UPDATE pembelian_guest SET status = 'Processing', sn = 'Transaksi Pending' WHERE id = '".$fetch_pesanan['id']."'");
                                }
                            }else{
                                mysqli_query($db, "UPDATE pembelian_guest SET error_msg = 'ApiGames tidak mereponse' WHERE id = '".$fetch_pesanan['id']."'");
                                if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                        $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                        $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                                    }
                            }
                    }else if($fetch_layanan['provider'] == 'VR'){
                        $check_data = $db->query("SELECT * FROM provider WHERE code = 'VR'");
                        $vip = $check_data->fetch_assoc();
                        $result = $curl->connectPost($vip['link'].'game-feature',['key' => $vip['api_key'],'sign' => md5($vip['api_id'] . $vip['api_key']),'type' => 'order','service' => $fetch_pesanan['sku'],'data_no' => $fetch_pesanan['target'],'data_zone' => $fetch_pesanan['additional_target']]);
                        if(isset($result)){
                            if($result['result'] == true){
                                $poi = $result['data']['trxid'];
                                $note = $result['data']['note'];
                                $status = $result['data']['status'];
                                if($status == 'success'){
                                    $statuss = 'Success';
                                    $db->query("UPDATE pembelian_guest SET status='$statuss', sn='Transaksi Pending', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                                    
                                }else if($status == 'error'){
                                    $statuss = 'Canceled';
                                    $notes = $result['message'];
                                    $db->query("UPDATE pembelian_guest SET status='$statuss', sn='$notes', date_update='$date' WHERE id='".$fetch_pesanan['id']."'");
                                    if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                        $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                        $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                                    }
                                }else{
                                    $statuss = 'Processing';
                                    $db->query("UPDATE pembelian_guest SET status='$statuss', sn='Transaksi Pending', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                                }
                            }else{
                                $notes2 = $result['message'];
                                $db->query("UPDATE pembelian_guest SET status='Canceled', sn='$notes2', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                                if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                        $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                        $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                                    }
                            }
                        }
                     }else if($fetch_layanan['provider'] == 'VR1'){
                        $check_data = $db->query("SELECT * FROM provider WHERE code = 'VR1'");
                        $vr = $check_data->fetch_assoc();
                        $result = $curl->connectPost($vr['link'].'prepaid',['key' => $vr['api_key'],'sign' => md5($vr['api_id'] . $vr['api_key']),'type' => 'order','service' => $fetch_pesanan['sku'],'data_no' => $fetch_pesanan['target']]);
                            if(isset($result)){
                            if($result['result'] == true){
                                $poi = $result['data']['trxid'];
                                $note = $result['data']['note'];
                                $status = $result['data']['status'];
                                if($status == 'success'){
                                    $statuss = 'Success';
                                    $db->query("UPDATE pembelian_guest SET status='$statuss', sn='Transaksi Pending', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                                    
                                }else if($status == 'error'){
                                    $statuss = 'Canceled';
                                    $notes = $result['message'];
                                    $db->query("UPDATE pembelian_guest SET status='$statuss', sn='$notes', date_update='$date' WHERE id='".$fetch_pesanan['id']."'");
                                    if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                        $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                        $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                                    }
                                }else{
                                    $statuss = 'Processing';
                                    $db->query("UPDATE pembelian_guest SET status='$statuss', sn='Transaksi Pending', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                                }
                            }else{
                                $notes2 = $result['message'];
                                $db->query("UPDATE pembelian_guest SET status='Canceled', sn='$notes2', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                                if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                        $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                        $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                                    }
                            }
                        }
                    } else if($fetch_layanan['provider'] == "SO"){
                        $check_data = $db->query("SELECT * FROM provider WHERE code = 'SO'");
                        $SO = $check_data->fetch_assoc();
                        $Smile = new SmileOne();
                        $result = $Smile->order($fetch_pesanan['target'],$fetch_pesanan['additional_target'],$fetch_pesanan['sku']);
                        $result = json_decode($result);
                        if(isset($result)){
                            $status = $result->status;
                            $poi = $result->trxId;
                            if($status == true){
                                $statuss = 'Processing';
                                $db->query("UPDATE pembelian_guest SET status='$statuss', sn='Transaksi Pending', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                            }else{
                                $statuss = 'Canceled';
                                $db->query("UPDATE pembelian_guest SET status='$statuss', sn='{$result['message']}', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                                if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                    $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                    $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                                }
                            }
                        }else{
                            $statuss = 'Canceled';
                            $db->query("UPDATE pembelian_guest SET status='Canceled', sn='Harap menghubungi Admin', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                            if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                            }
                        }
                    } else if($fetch_layanan['provider'] == 'MP'){
                        $check_data = $db->query("SELECT * FROM provider WHERE code = 'MP'");
                        $MP = $check_data->fetch_assoc();
                        $result = $curl->connectPost($vip['link'].'order',['api_key' => $MP['api_key'],'api_id' => $MP['api_id'],'service' => $fetch_pesanan['sku'],'target' => $fetch_pesanan['target'],'quantity' => $fetch_pesanan['quantity']]);
                            if(isset($result)){
                                $status = $result['status'];
                                $poi = $result['data']['id'];
                                if($status == true){
                                    if($result['data']['status'] == 'Pending'){
                                    $statuss = 'Waiting';
                                    $db->query("UPDATE pembelian_guest SET status='$statuss', sn='Transaksi Pending', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                                    }else{
                                    $statuss = 'Canceled';
                                    $db->query("UPDATE pembelian_guest SET status='$statuss', sn='-', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                                    }
                                }else{
                                    $statuss = 'Canceled';
                                    $db->query("UPDATE pembelian_guest SET status='Canceled', sn='{$result['data']}', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                                    if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                        $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                        $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                                    }
                                }
                            }else{
                                $db->query("UPDATE pembelian_guest SET status='Canceled', sn='Silahkan Hubungi Admin', date_update='$date', provider_order_id='$poi' WHERE id='".$fetch_pesanan['id']."'");
                                if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                                    $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                                    $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                                }
                            }
                        }
                } else {
                    mysqli_query($db, "UPDATE pembelian_guest SET error_msg = 'Layanan tidak ditemukan' WHERE id = '".$fetch_pesanan['id']."'");
                    if($fetch_pesanan['username'] !== "" || $fetch_pesanan['username'] !== "none"){
                        $db->query("UPDATE users SET saldo=saldo+{$fetch_pesanan['harga']} WHERE username='{$fetch_pesanan['username']}'");
                        $db->query("INSERT INTO history_balance VALUES ('','{$fetch_pesanan['username']}','{$fetch_pesanan['harga']}','+','$date')");
                    }
                }
            } else {
                $query_pesanan = mysqli_query($db, "SELECT * FROM pembelian_guest WHERE order_id = '$merchantOrderId' AND status = 'Pending'");
    }
}