<?php 
defined('BASEPATH') OR exit('No direct script access allowed');

class Qrcode extends CI_Controller {

    public function index()
    {
        if($this->session->userdata('level') != null)
        {
            $this->load->view('Header');
            $this->load->view('Qrcode');
            $this->load->view('Footer');
        }else{
            redirect(base_url(),'refresh');
        }

    }

    public function onSentQrcode()
    {
            $amount = $this->input->post('amount');
            $backgroundUrl = 'https://fbgray.com/Qrcode/onCallbackQrcode';
            $tokenKey = rawurlencode('kYrzhIsG4s+e9Awx6BZlgT24Thu5Yvd24R7Erx2Pt5r5GBEZRGlK7YXLKlCnf6E5+bsI3FW4H8hwVD0hM/wFHRCouvmn1O6ZSxCLQm6ulNS4Ilfsj/LT8wGl/qPzuvVBBqcu379xkn4Qg2eVSEkMJbO2OVxNruIJHQsD2w3+CgKAdtkJ');
            $this->db->order_by('id', 'desc');
            $query = $this->db->get('qrcode_refill', 1);
            $data = $query->row_array();
 
            $ref = $data['id']+1;
            $object = array(
                    'id_user' => $this->session->userdata('id_users'),
                    'amount'  =>  $amount,
                    'ref'  =>  $ref,
            );

            $this->db->insert('qrcode_refill', $object);
            
        
            $curl = curl_init();

            curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.gbprimepay.com/v3/qrcode',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_HTTPHEADER => array(
                'content-type: application/x-www-form-urlencoded'
            ),
            ));
            $field = 'token='.$tokenKey.'&referenceNo='.$ref.'&amount='.$amount.'&backgroundUrl='.$backgroundUrl;
            curl_setopt($curl, CURLOPT_POSTFIELDS, $field);

            $response = curl_exec($curl);
            curl_close($curl);
 

            $image = 'data:image/png;base64,' . base64_encode($response) . '';
            echo json_encode(['status' => 1, 'image' => $image, 'ref'=> $ref]);
    }

    public function checkStatusQR()
    {
        $ref = $this->input->post('ref');
        $this->db->where('ref', $ref);
        $query = $this->db->get('qrcode_refill', 1);
        $data = $query->row_array();

        if($data['status'] == 1)
        {
            echo json_encode(['status' => 1]);
        }else{
            echo json_encode(['status' => 0]);
        }
    }


    public function onCallbackQrcode()
    {
         $white_list = [
            '203.151.205.45',
            '203.151.205.33',
            '18.143.213.62',
            '13.215.225.183',
            '54.254.171.101',
            '18.141.54.201',
            '54.151.232.117',
            '54.255.79.153'
        ];

        if (in_array($_SERVER['REMOTE_ADDR'], $white_list)) {
            $respFile = fopen("resp-log.json", "w") or die("Unable to open file!");
            $json_str = file_get_contents('php://input');
            fwrite($respFile, $json_str . "nn");
            $json_obj = json_decode($json_str);
            if($json_obj->resultCode == 00)
            {
            $object = array(
                'status' => 1,
            );

            $this->db->where('ref', $json_obj->referenceNo);
            $this->db->update('qrcode_refill', $object);
            
            $this->db->where('ref', $json_obj->referenceNo);
            $query = $this->db->get('qrcode_refill', 1);
            $data = $query->row_array();
            $id = $data['id_user'];
            
            $this->db->query("UPDATE users SET credit = credit +'$json_obj->amount' WHERE id_users = $id");
            $object = array(
                'id_users'  =>  $id,
                'amount'    =>  $json_obj->amount,
                'by'        =>  'QR Code'
            );
            $this->db->insert('logrefill', $object);


            }
           
            fwrite($respFile, $json_obj);
            fclose($respFile);
        } else {
            die('Access Denied');
        }
    }
}

/* End of file Login.php */
