<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use Razorpay\Api as RazorpayApi;
class Payment extends CI_Controller {
    function __construct()
 	{
	    parent::__construct();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('session');
		$this->load->database();
		$this->load->library(array('form_validation','template'));
	}
	public function index()   
	{
		
		$organization_id=$this->session->userdata['session_data']['organization_id'];

		if($organization_id == "0"){
		 $data['payment'] =  $this->db->select('*')->from('trip_payments')->order_by('tp_id','desc')->get()->result_array();
		}else{
			  $this->db->select("*");
			  $this->db->from('trip_payments');
			  $this->db->join('trips', 'trips.t_id=trip_payments.tp_trip_id');
			  $this->db->join('customers','customers.c_id=trips.t_customer_id');	
			  $this->db->join('drivers', 'drivers.d_id=trips.t_driver');	
			 $this->db->where('drivers.organization_id', $organization_id);			  
			  $this->db->order_by('tp_id','desc');
			  $query = $this->db->get();
			   $data['payment']=$query->result_array();
		}

		$this->template->template_render('payments_management',$data); 
	}


		public function viewpaymentdetails()   
	{
		$tp_id = $this->uri->segment(3);
		$data['online'] = $this->db->select('*')->from('trip_payments')->where('tp_id',$tp_id)->get()->result_array();
		$this->template->template_render('payment_view',$data); 
	}
	// public function viewpaymentdetails()   
	// {
	// 	// die('hi');
	// 	$MerchantAuthenticationKey='a54f43e205754e03bcc34cc680f54318';
	// 	$MerchantAuthenticationToken='4d282ce823c146589ec2c2ad5d7a58ac';
	// 	$OrderKeyId=$this->uri->segment(3);
	// 	$MerchantKeyId='8340';
	// 	$PaymentType='CreditCard'; 

	// 	$arrData = array('OrderKeyId' =>$OrderKeyId,'MerchantKeyId'=>$MerchantKeyId); 
	// 	$arrDatajson = json_encode($arrData);
	// 	$header = array(		    
	// 			'Content-Type: application/json',
	// 			'Authorization: Basic '. base64_encode($MerchantAuthenticationKey.":".$MerchantAuthenticationToken.":M:".$MerchantKeyId)
	// 	);
	// 	$curl = curl_init();
	// 	curl_setopt_array($curl, array(
	// 	  CURLOPT_URL =>'https://uatapi.payg.in/payment/api/order/Detail',
	// 	  CURLOPT_RETURNTRANSFER => true,
	// 	  CURLOPT_ENCODING => "",
	// 	  CURLOPT_MAXREDIRS => 10,
	// 	  CURLOPT_TIMEOUT => 0,
	// 	  CURLOPT_FOLLOWLOCATION => true,
	// 	  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	// 	  CURLOPT_CUSTOMREQUEST => "POST",
	// 	  CURLOPT_POSTFIELDS =>$arrDatajson,
	// 	  CURLOPT_HTTPHEADER => $header,
	// 	)); 

	// 	$response = curl_exec($curl);
	// 	$data['the_json'] = json_decode($response, true);
	// 		if(curl_errno($curl)){
 //       		throw new Exception(curl_error($curl));
 //   		}
	// 	curl_close($curl);
	// 	//return  $response;
	// 	$this->template->template_render('payment_view',$data); 
	// }

	public function test_fcm_post(){
        $API_SERVER_KEY = 'AAAALej8XAk:APA91bHzSYZTdhG-2GUFcuI21fh2-0-jx1s5I2YDc7jV5igThcQqzbuXzTixT8TxTdtPmL1BZCnM4ZpVysWiGxZtlW43_cvAQXf2YdwkL60nONF37sDyj_YIo2qabmqTE1RlX2A7OYPf';
		$path_to_firebase_cm = 'https://fcm.googleapis.com/fcm/send';
        $title="Hi this shekhar";
        $message="shekhar message";
        $device_id="dEeEZVnQnPg:APA91bEXszRHGRKZR3MjVkZNjBqbdX3m0MzQ6UKOAj2IEtk4Lt3QLsELEPbrDo4oPWIfJIGPzKz7zuB5e273y_I1P2NrvPEOt6Qc3gpcoOJiDP59LgM3C49avuG75HRLNH1Mw6yFhGTd";
       /*  $fields = array(
            "registration_ids" =>"eQ_Oos5xTSo:APA91bFjMhQF4bHy4D-wcH5mQuo9THxZNXW9W1du-b6IN0Pk7RK4WcpS1ea8GQCDwN_eE22pdnzQ5ch_r3ZrXYM1Ccm_gkK3jxrP70QlOCTgykxLs8emNzZV4n2J4sAW5nkzlBGA78gm",  
			"content_available"=> true,
            'notification' => array('title' => 'Notice-'.$title,'body' => $message)   
            );  */

        $fields = array (
            'registration_ids' => array ($device_id),			
			"content_available"=> true,
            'data' => array (
                    "message" => $message
            )
        );
		$title="shekhr";
		$message="test shekhar 12345";
		$fields = array(
            "registration_ids" =>array($device_id),  
			"content_available"=> true,
            'notification' => array('title' => 'Notice-'.$title,'body' => $message)   
            ); 

        $headers = array(
            'Authorization:key=' .$API_SERVER_KEY,	
            'Content-Type:application/json'
        );

        // Open connection
        $ch = curl_init();
        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $path_to_firebase_cm);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        // Execute post
        $result = curl_exec($ch);
        if ($result === FALSE) {
            die('FCM Send Error: ' . curl_error($ch));
        }
        curl_close($ch);
        echo $result;

		//$this->setSuccessMessage("Message Sent Succesfully");
    }

	public function  test(){
	
	$API_SERVER_KEY = 'AAAALej8XAk:APA91bHzSYZTdhG-2GUFcuI21fh2-0-jx1s5I2YDc7jV5igThcQqzbuXzTixT8TxTdtPmL1BZCnM4ZpVysWiGxZtlW43_cvAQXf2YdwkL60nONF37sDyj_YIo2qabmqTE1RlX2A7OYPf';
	$fcm_token="dEeEZVnQnPg:APA91bEXszRHGRKZR3MjVkZNjBqbdX3m0MzQ6UKOAj2IEtk4Lt3QLsELEPbrDo4oPWIfJIGPzKz7zuB5e273y_I1P2NrvPEOt6Qc3gpcoOJiDP59LgM3C49avuG75HRLNH1Mw6yFhGTd";
	$title="shekhr";
	$message="test shekhar 12345";
		$path_to_firebase_cm = 'https://fcm.googleapis.com/fcm/send';
        $fields = array(
            "registration_ids" =>array($fcm_token),  
			"content_available"=> true,
            'notification' => array('title' => 'Notice-'.$title,'body' => $message)   
            ); 
        $headers = array(
            'Authorization:key=' .$API_SERVER_KEY,
            'Content-Type:application/json'
        );

        // Open connection
        $ch = curl_init();
        // Set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_URL, $path_to_firebase_cm);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
        // Execute post
        $result = curl_exec($ch);
        // Close connection
        curl_close($ch);
		echo $result;

	}
	public function cashdetails()
	{ 
		$tp_id = $this->uri->segment(3);
		$data['cash'] = $this->db->select('*')->from('trip_payments')->where('tp_id',$tp_id)->get()->result_array();
		$this->template->template_render('payment_cash_view',$data);
	}
	
}
