<?php
require "vendor/autoload.php";
use Dompdf\Dompdf;
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Flutterwave extends CI_Controller
{

    public $theme;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('model_customer');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        dt_set_time_zone();
        dt_run_default_query();
        $this->theme = "theme1";
    }

    public function payment(){
        $status=isset($_REQUEST['status'])?$_REQUEST['status']:"";
        $tx_ref=isset($_REQUEST['tx_ref'])?$_REQUEST['tx_ref']:0;
        $transaction_id=isset($_REQUEST['transaction_id'])?$_REQUEST['transaction_id']:"";

        $this->db->select('*');
        $this->db->where('id',$tx_ref);
        $donation_data=$this->db->get('app_donation')->row_array();

        if (isset($donation_data) && $donation_data!=null){
            if (isset($status) && $status=='completed'){

                if (isset($donation_data['cause_id']) && $donation_data['cause_id']>0){
                    dt_get_cause_and_update_staus($donation_data['cause_id']);
                }

                if ($donation_data['receipt_sent']==false){
                    $this->sendreceipt->generate($tx_ref);
                    $update_donation['receipt_sent']=true;
                }

                $update_donation['payment_status']='success';
                $update_donation['transaction_id']=$transaction_id;
                $update_donation['transaction_response']=null;

                $this->model_customer->update('app_donation', $update_donation, "id=".$tx_ref);

                $this->session->set_userdata("donation_id",$tx_ref);
                $this->session->set_flashdata('msg',dt_translate('transaction_success'));
                $this->session->set_flashdata('msg_class', 'success');

                redirect(base_url('success/'));

            }elseif (isset($status) && $status=='cancelled'){

                $this->db->where('id',$tx_ref);
                $this->db->delete('app_donation');

                $this->session->set_flashdata('msg', dt_translate('transaction_fail'));
                $this->session->set_flashdata('msg_class', 'failure');
                redirect(base_url('donate/'));

            }else{
                $this->session->set_flashdata('msg', dt_translate('transaction_fail'));
                $this->session->set_flashdata('msg_class', 'failure');
                redirect(base_url('donate/'));
            }
        }else{
            $this->session->set_flashdata('msg', dt_translate('transaction_fail'));
            $this->session->set_flashdata('msg_class', 'failure');
            redirect(base_url('donate/'));
        }
    }

    public function webhook(){
        $json = file_get_contents('php://input');
        $action = json_decode($json, true);

        $data['details']=$json;
        $data['type']="flutterwave";
        $this->model_customer->insert('app_webhook', $data);

        if (isset($action['txRef']) && $action['txRef']>0){
            $this->db->select('*');
            $this->db->where('id',$action['txRef']);
            $donation_data=$this->db->get('app_donation')->row_array();

            if (isset($donation_data) && $donation_data!=null){

                if (isset($donation_data['cause_id']) && $donation_data['cause_id']>0){
                    dt_get_cause_and_update_staus($donation_data['cause_id']);
                }

                if ($donation_data['receipt_sent']==false){
                    $this->sendreceipt->generate($donation_data['id']);
                    $update_donation['receipt_sent']=true;
                }

                $update_donation['payment_status']='success';
                $update_donation['transaction_id']=isset($action['id'])?$action['id']:"";
                $update_donation['transaction_response']=json_encode($action);

                $this->model_customer->update('app_donation', $update_donation, "id=".$donation_data['id']);
            }
        }
    }
}