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

class PaystackPayment extends CI_Controller
{
    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();
    }

    public function payment() {
        $reference=isset($_REQUEST['reference'])?$_REQUEST['reference']:0;
        $transaction_id=isset($_REQUEST['trxref'])?$_REQUEST['trxref']:0;

        $this->db->select('*');
        $this->db->where('id',$reference);
        $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($reference);
                $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=".$reference);
            $this->session->set_userdata("donation_id",$reference);
            $this->session->set_flashdata('msg',dt_translate('transaction_success'));
            $this->session->set_flashdata('msg_class', 'success');
            redirect(base_url('success/'));
        }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']="paystack";
        $this->model_customer->insert('app_webhook', $data);

        if (isset($action['event']) && $action['event']=='charge.success'){
            if (isset($action['data']['reference']) && $action['data']['reference']>0){

                $this->db->select('*');
                $this->db->where('id',$action['data']['reference']);
                $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($action['data']['reference']);
                        $update_donation['receipt_sent']=true;
                    }

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

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