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

class PaypalPayment 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 paypal_success($donation_id) {

        if (isset($_REQUEST['paymentId']) && $_REQUEST['paymentId']!="") {

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

                $update_donation['payment_status']='success';
                $update_donation['transaction_id']=$_REQUEST['paymentId'];
                $update_donation['transaction_response']=json_encode($_REQUEST);

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

                $this->session->set_userdata("donation_id",$donation_id);
                $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/'));
            }

        } else {

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

    public function paypal_cancel($donation_id) {
        $this->db->where('id',$donation_id);
        $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/'));
    }

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

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

        if (isset($action['event_type']) && $action['event_type']=='PAYMENTS.PAYMENT.CREATED'){
            if (isset($action['resource'])){
                $payment_id=isset($action['resource']['id'])?$action['resource']['id']:0;
                if (isset($action['resource']['transactions'][0]['invoice_number'])){
                 $donation_id=$action['resource']['transactions'][0]['invoice_number'];


                    $this->db->select('*');
                    $this->db->where('id',$donation_id);
                    $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']=$payment_id;
                        $update_donation['transaction_response']=$json;

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