<?php
defined('BASEPATH') or exit('Sorry request could not be completed');
class Egift
{
    protected $ci;
    public $im;

    public function __construct()
    {
        $this->ci = &get_instance();
        $this->ci->load->model('auditmodel');
        $this->ci->load->model('apimodel');
        $this->ci->load->model('vouchersmodel');
    }

    function egift($dmoid, $amount)
    {
        $curl_post_data = array(
            'client_id' =>  client_id,
            'client_secret' =>  client_secret,
            'grant_type' =>  grant_type,
            'scope' =>  scope
        );

        $data_string = "";
        foreach ($curl_post_data as $item => $value) {
            $data_string .= $item . "=" . $value . "&";
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, token_url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, rtrim($data_string, "&"));
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt(
            $curl,
            CURLOPT_HTTPHEADER,
            array(
                'Content-Type: application/x-www-form-urlencoded',
                "cache-control: no-cache",
            )
        );

        $curl_response = curl_exec($curl);
        if ($curl_response === false)
            return json_encode(array('error'=>'error','data'=>curl_error($curl)));
        else {
            $resp = json_decode($curl_response);
            $token = $resp->access_token;
            return $this->buyCard($token, $dmoid, $amount);
        }
        curl_close($curl);
    }

    private function buyCard($token, $dmoid, $amount)
    {
        $curl_post_data = array(
            'dmo_id' =>  $dmoid,
            'amount' =>  $amount,
            'delivery_type' =>  'api',
            'delivery_data_rname' =>  '',
            'delivery_data_rmail' => '',
            'delivery_data_msg' => 'Your Redemption is Successful',
            'delivery_data_smail' => 'mzawadi@giift.com',
            'delivery_data_sname' => 'MZAWADI',
            'state' => 'MZAWADI_INTERNAL_ORDER_ID'
        );

        $data_string = "";
        foreach ($curl_post_data as $item => $value) {
            $data_string .= $item . "=" . $value . "&";
        }

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, purchase_card_url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, rtrim($data_string, "&"));
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt(
            $curl,
            CURLOPT_HTTPHEADER,
            array(
                'Content-Type: application/x-www-form-urlencoded',
                "cache-control: no-cache",
                "Authorization: Bearer " . $token
            )
        );

        $resp = curl_exec($curl);
        if ($resp === false)
            $resp = json_encode(array('error'=>'error','data'=>curl_error($curl)));
        
        curl_close($curl);
        return $resp;
    } 
}
