<?php
set_time_limit(240);
defined("BASEPATH") or exit("No direct script access allowed.");

$VR_R = mysqli_query($db, "SELECT * FROM pembelian_guest WHERE  provider='VR' AND sn='Transaksi Pending' AND  status IN ('Processing', ' ', '') ORDER BY id DESC");
$g = date('Y-m-d H:i:s');
while($VR_games = mysqli_fetch_assoc($VR_R)) {
    $VRS = $db->query("select * from provider where code='VR'");
    $VR = $VRS->fetch_assoc();
    $vr_id = $VR['api_id'];
    $vr_key = $VR['api_key'];
    $result = $curl->connectPost($VR['link'].'game-feature',['key' => $vr_key,'sign' => md5($vr_id.$vr_key),'type' => 'status','trxid' => 	$VR_games['provider_order_id']]);
    if(isset($result)){
        if($result['result'] == true){
            $statuss = $result['data'][0]['status'];
            if($statuss == 'success'){
                $status = 'Success';
                $notea = strtr($note12, array(
                     'Transaksi Sukses' => ' ',
                 ));
                $note = $result['data'][0]['note'] !== '' ? $result['data'][0]['note'] : $result['message'];
                mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$VR_games['id']."'");
                echo $status;
            }else if($statuss == 'error'){
                $status = 'Canceled';
                $saldo = $VR_games['harga'];
                $note = "Silahkan Hubungi Admin";
                echo $status;
                mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$VR_games['id']."'");
                if($VR_games['username'] !== "" || $VR_games['username'] !== "none"){
                    $db->query("UPDATE users SET saldo=saldo+{$VR_games['harga']} WHERE username='{$VR_games['username']}'");
                    $db->query("INSERT INTO history_balance VALUES ('','{$VR_games['username']}','{$VR_games['harga']}','+','$g')");
                }
            }else{
                $status = 'Processing';
                $note = 'Transaksi Pending';
                mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$VR_games['id']."'");
                echo $status;
            }
        }else{
            echo 'Gagal Terkoneksi';
        }
        if ($status == "Success") {
            if(!$VR_games['additional_target']){
                $targets = $VR_games['target'];
            }else{
                $targets = $VR_games['target'].'('.$VR_games['additional_target'].')';
            }         
            $WhatsApp->sendMessage('https://wabot.myrigaming.my.id/',opt_get('dHAta2V5'),opt_get('dHAtcGhvbmU='),$VR_games['phone'],"*Pembelian ".$VR_games['layanan']." ".$status."*\n- User ID: *".$targets."*\n- Layanan: *".$VR_games['games']."*\n- Produk: *".$VR_games['layanan']."*\n- SN/Catatan: *".$note."*\nTanggal: *".$g."*\n\n*BY LOOKIT STORE*");
        }
    }
}

$VR1 = mysqli_query($db, "SELECT * FROM pembelian_guest WHERE  provider='VR1' AND sn='Transaksi Pending' AND  status IN ('Processing', ' ', '') ORDER BY id DESC");
while($VR_prepaid = mysqli_fetch_assoc($VR1)) {
    $VRSC = $db->query("select * from provider where code='VR1'");
    $VRC = $VRSC->fetch_assoc();
    $vr_id = $VRC['api_id'];
    $vr_key = $VRC['api_key'];
    $result = $curl->connectPost($VRC['link'].'prepaid',['key' => $vr_key,'sign' => md5($vr_id.$vr_key),'type' => 'status','trxid' => 	$VR_prepaid['provider_order_id']]);
    if(isset($result)){
        if($result['result'] == true){
            $statuss = $result['data'][0]['status'];
            if($statuss == 'success'){
                $status = 'Success';
                $note1 = $result['data'][0]['note'] !== '' ? $result['data'][0]['note'] : $result['message'];
                $note = strtr($note1, array(
                     'Transaksi Sukses' => ' ',
                 ));
                mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$VR_prepaid['id']."'");
                echo $status;
            }else if($statuss == 'error'){
                $status = 'Canceled';
                $saldo = $VR_prepaid['harga'];
                $note = "Silahkan Hubungi Admin";
                echo $status;
                mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$VR_prepaid['id']."'");
                if($VR_games['username'] !== "" || $VR_prepaid['username'] !== "none"){
                    $db->query("UPDATE users SET saldo=saldo+{$VR_prepaid['harga']} WHERE username='{$VR_prepaid['username']}'");
                    $db->query("INSERT INTO history_balance VALUES ('','{$VR_prepaid['username']}','{$VR_prepaid['harga']}','+','$g')");
                }
            }else{
                $status = 'Processing';
                $note = 'Transaksi Pending';
                mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$VR_prepaid['id']."'");
                echo $status;
            }
        }else{
            echo 'Gagal Terkoneksi';
        }
        if ($status == "Success") {
            if(!$VR_prepaid['additional_target']){
                $targets = $VR_prepaid['target'];
            }else{
                $targets = $VR_prepaid['target'].'('.$VR_prepaid['additional_target'].')';
            }         
            $WhatsApp->sendMessage('https://wabot.myrigaming.my.id/',opt_get('dHAta2V5'),opt_get('dHAtcGhvbmU='),$VR_prepaid['phone'],"*Pembelian ".$VR_prepaid['layanan']." ".$status."*\n- User ID: *".$targets."*\n- Layanan: *".$VR_prepaid['games']."*\n- Produk: *".$VR_prepaid['layanan']."*\n- SN: ".$note."\nTanggal: *".$g."*\n\n*BY ".config('web','title')."*");
            
        }
    }
}

$MPP = mysqli_query($db, "SELECT * FROM pembelian_guest WHERE  provider='MP' AND  status IN ('Processing', ' ', '') ORDER BY id DESC");
while($MP_games = mysqli_fetch_assoc($MPP)) {
    $MPS = $db->query("select * from provider where code='MP'");
    $MP = $MPS->fetch_assoc();
    $MP_id = $MP['api_id'];
    $MP_key = $MP['api_key'];
    $result = $curl->connectPost($MP['link'].'status',['api_key' => $MP_key,'api_id' => $MP_id,'id' => 	$MP_games['provider_order_id']]);
    if($result['status'] == true){
        $count = $result['data']['start_count'];
        $statuss = $result['data']['status'];
        if($statuss == 'Success'){
            $status = 'Success';
            $note = 'Pesanan selesai dan berhasil.';
            echo $status;
            mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$MP_games['id']."'");
        }else if($statuss == 'Error'){
            $status = 'Canceled';
            $sn = 'Pesanan gagal diproses.';
            mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$MP_games['id']."'");
            echo $status;
            $saldo = $MP_games['harga'];
            if($MP_games['username'] !== "" || $MP_games['username'] !== "none"){
                $db->query("UPDATE users SET saldo=saldo+{$MP_games['harga']} WHERE username='{$MP_games['username']}'");
                $db->query("INSERT INTO history_balance VALUES ('','{$MP_games['username']}','{$MP_games['harga']}','+','$g')");
            }
        }else{
            $status = 'Processing';
            $note = 'Pesanan sedang diproses.';
            mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$MP_games['id']."'");
            echo $status;
        }
        if ($status == "Success") {
            if(!$MP_games['additional_target']){
                $targets = $MP_games['target'];
            }else{
               $targets = $MP_games['target'].'('.$MP_games['additional_target'].')';
            }         
            $WhatsApp->sendMessage('https://waysender-v2.ridped.com/',opt_get('dHAta2V5'),opt_get('dHAtcGhvbmU='),$MP_games['phone'],"*Pembelian ".$MP_games['layanan']." ".$status."*\n- User ID: *".$targets."*\n- Layanan: *".$MP_games['games']."*\n- Produk: *".$MP_games['layanan']."*\n- SN: ".$note."\nTanggal: *".$g."*\n\n*BY ".config('web','title')."*");
            
        }
    }
}


$SOO = mysqli_query($db, "SELECT * FROM pembelian_guest WHERE  provider='SO' AND  status IN ('Processing', ' ', '') ORDER BY id DESC");
while($SO_games = mysqli_fetch_assoc($SOO)) {
    $SOS = $db->query("select * from provider where code='SO'");
    $SO = $SOS->fetch_assoc();
    $Smile = new SmileOne();
    $result = json_decode($Smile->getTransaction($SO_games['target'],$SO_games['provider_order_id']));
        if(isset($result)){
            $statuss = $result->data->status;
            if($result->status == true){
                if($statuss == 'Sucesso'){
                    $status = 'Success';
                    $note = 'Pesanan selesai dan berhasil.';
                    mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$SO_games['id']."'");
                }else {
                    $status = 'Processing';
                    $note = 'Transaksi Pending';
                    mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$SO_games['id']."'");
                }
            }else{
                $status = 'Canceled';
                $note = "Silahkan Hubungi Admin";
                mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$SO_games['id']."'");
            }
        if ($status == "Success") {
            if(!$SO_games['additional_target']){
                $targets = $SO_games['target'];
            }else{
               $targets = $SO_games['target'].'('.$SO_games['additional_target'].')';
            }         
            $WhatsApp->sendMessage('https://waysender-v2.ridped.com/',opt_get('dHAta2V5'),opt_get('dHAtcGhvbmU='),$SO_games['phone'],"*Pembelian ".$SO_games['layanan']." ".$statuss."*\n- User ID: *".$targets."*\n- Layanan: *".$SO_games['games']."*\n- Produk: *".$SO_games['layanan']."*\n- SN: ".$note."\nTanggal: *".$g."*\n\n*BY ".config('web','title')."*");
            
        }
    }else{
        $req_message = 'Gagal Terkoneksi';
    }
}


$DFFF = mysqli_query($db, "SELECT * FROM pembelian_guest WHERE  provider='DF' AND sn='Transaksi Pending'AND  status IN ('Processing', ' ', '') ORDER BY id DESC");
while($DF_games = mysqli_fetch_assoc($DFFF)) {
    $harga = $DF_games['harga'];
    $DFs = $db->query("select * from provider where code='DF'");
    $DF = $DFs->fetch_assoc();
    if($DF_games['games'] == "Genshin Impact"){
        if($DF_games['additional_target'] == "Asia"){
            $targets = $DF_games['target'].'|os_asia';
        } else if($DF_games['additional_target'] == "America"){
            $targets = $DF_games['target'].'|os_usa';
        } else if($DF_games['additional_target'] == "Europe"){
            $targets = $DF_games['target'].'|os_euro';
        } else if($DF_games['additional_target'] == "China"){
            $targets = $DF_games['target'].'|os_cht';
        }
    }else{
        $targets = $DF_games['target'].$DF_games['additional_target'];
    }
    $post_data = json_encode([
        'username' => $DF['api_id'],
        'buyer_sku_code' => $DF_games['sku'],
        'customer_no' => $targets,
        'ref_id' => $DF_games['order_id'],
        'sign' => md5($DF['api_id'].$DF['api_key'].$DF_games['order_id']),
        ]);
    $result = $curl->connectPost($DF['link'].'transaction',$post_data);
    
    if (isset($result['data'])) {
        
        $statuss = $result['data']['status'];
        $note = $result['data']['sn'] !== '' ? $result['data']['sn'] : $result['data']['message'];
        
        if ($statuss === 'Gagal') {
            $status = 'Canceled';
            $note = "Silahkan Hubungi Admin";
            mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$DF_games['id']."'");
            echo $status;
             if($DF_games['username'] !== "" || $DF_games['username'] !== "none"){
               $db->query("UPDATE users SET saldo=saldo+{$DF_games['harga']} WHERE username='{$DF_games['username']}'");
               $db->query("INSERT INTO history_balance VALUES ('','{$DF_games['username']}','{$DF_games['harga']}','+','$g')");
             }
        } else if ($statuss === 'Sukses') {
            $status = 'Success';
            mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$DF_games['id']."'");
            echo $status;
        } else {
            $status = 'Processing';
            $note = "Transaksi Pending";
            mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$DF_games['id']."'");
            echo $status;
        }
        if ($status == "Success") {
            if(!$DF_games['additional_target']){
                $targets = $DF_games['target'];
            }else{
               $targets = $DF_games['target'].'('.$DF_games['additional_target'].')';
            }         
            $WhatsApp->sendMessage('https://waysender-v2.ridped.com/',opt_get('dHAta2V5'),opt_get('dHAtcGhvbmU='),$DF_games['phone'],"*Pembelian ".$DF_games['layanan']." ".$status."*\n- User ID: *".$targets."*\n- Layanan: *".$DF_games['games']."*\n- Produk: *".$DF_games['layanan']."*\n- SN: ".$note."\nTanggal: *".$g."*\n\n*BY ".config('web','title')."*");
            
        }
    } else {
        $req_message = 'Gagal melakukan koneksi server.';
    }
}


$AGG = mysqli_query($db, "SELECT * FROM pembelian_guest WHERE  provider='AG' AND sn='Transaksi Pending'AND  status IN ('Processing', ' ', '') ORDER BY id DESC");
while($AG_games = mysqli_fetch_assoc($AGG)) {
    $AG = $db->query("select * from provider where code='AG'")->fetch_assoc();
    $merchant_id = $AG['merchant_id'];
    $secret = $AG['api_key'];
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://v1.apigames.id/merchant/'.$merchant_id.'/cektrx/'.$AG_games['order_id'],
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,
));

$result = curl_exec($curl);
    
    if (isset($result['data'])) {
        
        $statuss = $result['data']['status'];
        $note = $result['data']['sn'] !== '' ? $result['data']['sn'] : $result['data']['message'];
        
        if ($status === 'Gagal') {
            $status = 'Canceled';
            $note = 'Silahkan Hubung Admin';
            mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$AG_games['id']."'");
            echo $status;
             if($AG_games['username'] !== "" || $AG_games['username'] !== "none"){
                $db->query("UPDATE users SET saldo=saldo+{$AG_games['harga']} WHERE username='{$AG_games['username']}'");
                $db->query("INSERT INTO history_balance VALUES ('','{$AG_games['username']}','{$AG_games['harga']}','+','$g')");
             }
        } else if ($status === 'Sukses') {
            $status = 'Success';
            $note = $result['data']['sn'] !== '' ? $result['data']['sn'] : $result['data']['message'];
            mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$AG_games['id']."'");
            echo $status;
        } else {
            $status = 'Processing';
            $note = 'Transaksi Pending';
            mysqli_query($db, "UPDATE pembelian_guest SET date_update='$g', status = '$status', sn = '$note' WHERE id = '".$AG_games['id']."'");
            echo $status;
        }
        
        if ($status == "Success") {
            if(!$AG_games['additional_target']){
                $targets = $AG_games['target'];
            }else{
               $targets = $AG_games['target'].'('.$AG_games['additional_target'].')';
            }         
            $WhatsApp->sendMessage('https://waysender-v2.ridped.com/',opt_get('dHAta2V5'),opt_get('dHAtcGhvbmU='),$AG_games['phone'],"*Pembelian ".$AG_games['layanan']." ".$status."*\n- User ID: *".$targets."*\n- Layanan: *".$AG_games['games']."*\n- Produk: *".$AG_games['layanan']."*\n- SN: ".$note."\nTanggal: *".$g."*\n\n*BY ".config('web','title')."*");
            
        }
    } else {
        $req_message = 'Gagal melakukan koneksi server.';
    }
}