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

class RazorpayPayment 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";
    }

    /*Process Razor Pay Action*/
    public function razorpay_success(){

        $razorpay_merchant_key_id=dt_get_payment_setting('razorpay_merchant_key_id');
        $razorpay_merchant_key_secret=dt_get_payment_setting('razorpay_merchant_key_secret');

        if(isset($_REQUEST['razorpay_payment_id']) && $_REQUEST['razorpay_payment_id']!=""){
            $razorpay_payment_id=$_REQUEST['razorpay_payment_id'];
            $razorpay_order_id=$_REQUEST['razorpay_order_id'];
            $razorpay_signature=($_REQUEST['razorpay_signature']);

            $data=$razorpay_order_id."|".$razorpay_payment_id;
            $generated_signature = hash_hmac("sha256",$data,  $razorpay_merchant_key_secret);

            if ($generated_signature == $razorpay_signature) {
               // pay_Mcr09Itci42RWK---Payment
                $api = new Api($razorpay_merchant_key_id, $razorpay_merchant_key_secret);

                $payment_details=$api->payment->fetch($razorpay_payment_id);
                $order_details=$api->order->fetch($razorpay_order_id);

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

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

                            $update_donation['payment_status']='success';
                            $update_donation['transaction_id']=isset($payment_details->id)?$payment_details->id:"";
                            $update_donation['transaction_response']=isset($order_details->id)?$order_details->id:"";

                            $this->model_customer->update('app_donation', $update_donation, "id=".$order_details->receipt);
                            $this->session->set_userdata("donation_id",$order_details->receipt);
                            $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'));
                }

            }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 razorpay_cancel(){
        $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']="razorpay";
        $this->model_customer->insert('app_webhook', $data);


        $razorpay_merchant_key_id=dt_get_payment_setting('razorpay_merchant_key_id');
        $razorpay_merchant_key_secret=dt_get_payment_setting('razorpay_merchant_key_secret');

        if (isset($action['payload'])){
            if (isset($action['payload']['payment'])){
                if (isset($action['payload']['payment']['entity'])){

                    $razorpay_payment_id=isset($action['payload']['payment']['entity']['id'])?$action['payload']['payment']['entity']['id']:"";
                    $razorpay_order_id=isset($action['payload']['payment']['entity']['order_id'])?$action['payload']['payment']['entity']['order_id']:"";

                    $api = new Api($razorpay_merchant_key_id, $razorpay_merchant_key_secret);
                    $payment_details=$api->payment->fetch($razorpay_payment_id);
                    $order_details=$api->order->fetch($razorpay_order_id);

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

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

                            $update_donation['payment_status']='success';
                            $update_donation['transaction_id']=isset($payment_details->id)?$payment_details->id:"";
                            $update_donation['transaction_response']=isset($order_details->id)?$order_details->id:"";
                            $this->model_customer->update('app_donation', $update_donation, "id=".$order_details->receipt);
                        }

                    }

                }

            }

        }
    }
}