<?php 

//if ( ! defined('BASEPATH')) exit('No direct script access allowed');
namespace App\Controllers\User;
use App\Views;
use App\Models\CommonModel;
use App\Helpers\site_helper;
use App\Libraries\MyEncrypt;
use App\Libraries\WalletCoreAPI;
use App\Models\Rank_update;
use App\Models\Commissions;


class Ipn_verify extends BaseController {

    public function __construct() 
    {
        helper('site_helper');

        $this->db = db_connect();
        $myencrypt = new MyEncrypt(true);
        $this->common_model = model('CommonModel');
        $session = \Config\Services::session();
        $validation = \Config\Services::validation();
        $uri = service('uri');
        if(!$session->get('user_login')) 
        {
            return redirect()->to('login');
        }
        $status = site_info('site_status');
        if($status!='on')
        {
            return redirect()->to('offline');
        } 

        // user login only
        if($session->get('user_login') || $session->get('admin_login')) 
        {

            // form validation library
            // $this->load->library('form_validation');
            // $this->load->model('userdash_model');
            // $this->load->model('finance_model');
            $this->finance_model = model('finance_model');
            $this->userdash_model = model('userdash_model');

            
        } else 
        {
            throw new \CodeIgniter\Router\Exceptions\RedirectException('login');
        }

    }

    public function index(){
            
            $ipn_secret =    $this->db->query("SELECT * FROM `payment_controls` WHERE payment_name = 'walletcore' AND payment_key = 'ipn_secret'")->getRow();

            // Fill these in with the information from your WalletCore account. 
            // $ipn_secret = "TESTIPNSECRET"; // Same as WalletCore
            $raw_data = file_get_contents("php://input"); // Post Josn Data
            $request = json_decode($raw_data,true); // Decode Json Data


            // if ($request === FALSE || empty($request)) { 
            //     echo view('user/faild');
            //     exit();
            // } 

            // Data form WalletCore
            $address        = $request['address'];
            $recvied        = $request['amount'];
            $tnxid          = $request['tnxid']; 
            $IPNSECRET      = $request['IPNSECRET']; 
            $confirmations  = $request['confirmations']; 

            // $address        = '3Ca3TxAX7unvdPEKzFFWZR9tU6SJEDb1h8';
            // $recvied        = '120';
            // $tnxid          = 'testtran'; 
            // $IPNSECRET      = 'KIRHYIP'; 
            // $confirmations  = 4; 

            if ($confirmations < 3) {    
                echo view('user/faild');
                exit();
            }

            if ($IPNSECRET == $ipn_secret->payment_value) {


                $db      = \Config\Database::connect();
                $pay_address = $db->table('pay_address');
                $pay_address = $pay_address->where('address',$address)->get()->getRow();
               

                
                $data = array(
                    'status' => 2
                );

                $db      = \Config\Database::connect();
                $builder = $db->table('pay_address');



                $builder->where('address', $address);
                $update = $builder->update($data);
                
                $transactionid = "ACT-".$tnxid; 



                // print_r($transactionid);
                // exit();
                if ($update) {
                    
                     $historydata = array(
                        'uusersid' => $pay_address->userid,
                        'amount' => $recvied,
                        'btc_amount' => $recvied,
                        'type' => 'Activation',
                        'description' => 'Member pay for activation',
                        'date' => date('Y-m-d'),
                        'transactionid' => $transactionid,
                        'address' => $address,
                        
                    );

                    $db      = \Config\Database::connect();
                    $history = $db->table('history');
                    $history->insert($historydata);

                    // print_r($historydata);
                    // exit();

                    $rewardsettings = $this->db->table('rewardsettings');
                    $rewardsettings = $rewardsettings->where('id','4')->get()->getRow();

                    
                    $rank_id =  $rewardsettings->id;

                    $rank_data = array(
                        'rankid' => $rank_id
                    );

                    $uusers = $this->db->table('uusers_matrix');
                    $uusers = $uusers->where('uusersid',$pay_address->userid)->update($rank_data);

                    $uusers = $this->db->table('uusers');
                    $uusers = $uusers->where('uusersid',$pay_address->userid)->update($rank_data);
                    
                    if ($uusers) {
                        $Rank_update = new Rank_update();

                        $Rank_update = $Rank_update->rank_update($pay_address->userid,$recvied);

                        $Commissions = new Commissions();

                        $description = ' for activation';

                        $Commissions_provide = $Commissions->index($pay_address->userid,$recvied,$description);

                        // $description = ' for profit';

                        // $Teamreward_provide = $Commissions->teamreaward_check($pay_address->userid,$recvied,$description);
                    }
                }

                echo view('user/success');

            }else{
                echo view('user/faild');
                exit();
            }
    }

} 
?>