<?php

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

//ini_set('display_errors', 0);

//if (version_compare(PHP_VERSION, '5.3', '>=')) {
//    error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED);
//}
//else {
//    error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE);
//}

require APPPATH . '/libraries/REST_Controller.php';

use Restserver\Libraries\REST_Controller;

class Iak extends REST_Controller
{
    function __construct($config = 'rest')
    {
        parent::__construct($config);
        $this->load->database();
        $this->load->library("Curl");
    }
    function test_get(){
        echo "masuk get";
    }
    function payment_post(){
        $arr = $_REQUEST;
        $arrDigi = $this->db->get_where('produk', array("id"=>$arr['produk']['id']))->result();
        $digi_blob = json_decode(base64_decode($arrDigi[0]->digi_blob),true);
        $cek = $this->prepaid($digi_blob);
        $result = array(
            "status" => 1,
            "digi_blob" => $digi_blob,
            "cek" => $cek,
            "arr" => $arr,
            "arrDigi" => $arrDigi[0],
            "preview" => $arr,
        );
        $this->response($result, 200);
    }
    function prepaid($arr){
        $username   = "08985193131";
        $sign       = "88764186e71f199d";
        $addit      = "pl";
        $code_transaksi = $arr['code_transaksi'];
        $nomor = $arr['no_hp'];
        $nomor_ref = $arr['nomor_ref'];
        $md5an = md5("$username$sign$nomor_ref");
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://prepaid.iak.dev/api/top-up', //sandbox
//            CURLOPT_URL => 'https://prepaid.iak.id/api/top-up', // production
            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_POSTFIELDS =>'{
                "customer_id": "'.$nomor.'",
                "product_code": "'.$code_transaksi.'",
                "ref_id": "'.$nomor_ref.'",
                "username": "'.$username.'",
                "sign": "'.$md5an.'"
            }',
            CURLOPT_HTTPHEADER => array(
                'Content-Type: application/json',
            ),
        ));
        $response = curl_exec($curl);
        curl_close($curl);

        $res = json_decode($response, true);
        $status=0;
        $dataInsert=array();
        if( isset($res['data']['status']) && $res['data']['status']!=2 ){
            $dataInsert = $res['data'];
            $dataInsert['oleh_id'] = $this->session->userdata['login']['id'];
            $dataInsert['oleh_nama'] = $this->session->userdata['login']['nama'];
            $dataInsert['datetime'] = date('Y-m-d H:i:s');
            $dataInsert['shoppingcart_order_id'] = $this->session->userdata['_TR_582']['main']['shoppingcart_order_id'];
            $cekTr = $this->db->get_where('', array("ref_id"=>$dataInsert['ref_id'],"tr_id"=>$dataInsert['tr_id']))->result();
            if(count($cekTr)==0){
                $this->db->insert('transaksi_digital', $dataInsert);
            }
            $status = $res['data']['status'];
        }
        else{

            $status = $res['data']['status'];
        }

        $result = array(
            "status" => $status,
            "response" => $dataInsert,
        );

        return $result;
    }
}

?>