<?php


//includes functions and classes for remita constants
// $entry_mode= get_user_mode_of_entry($user_id);
// $ser_type_id = get_service_type_id_by_entry_mode($entry_mode);
/*echo $ser_type_id;
exit();*/

include_once 'Rremita_constants.php'; //include som other constants

$orderID = "";
if( isset( $_GET['txnref'] )) {
$orderID = $_GET["txnref"];
}
$response_code ="";
$rrr = "";
$response_message = "";
//Verify Transaction
function remita_transaction_details($orderId){
		$mert =  MERCHANTID;
		$api_key =  APIKEY;
		$concatString = $orderId . $api_key . $mert;
		$hash = hash('sha512', $concatString);
		$url 	= CHECKSTATUSURL . '/' . $mert  . '/' . $orderId . '/' . $hash . '/' . 'orderstatus.reg';
		//  Initiate curl
		$ch = curl_init();
		// Disable SSL verification
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		// Will return the response, if false it print the response
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		// Set the url
		curl_setopt($ch, CURLOPT_URL,$url);
		// Execute
		$result=curl_exec($ch);
		// Closing
		curl_close($ch);
		$response = json_decode($result, true);
		return $response;
	}
	if($orderID !=null){
		$response = remita_transaction_details($orderID);
		$response_code = $response['status'];
		if (isset($response['RRR']))
			{
			$rrr = $response['RRR'];
			}
		$response_message = $response['message'];
}




?>





<ul class="breadcrumb">

			<li><a href="dashboard.php?act=payments&qlk=<?php echo md5(8); ?>">Payments</a> <span class="divider"></span> </li>

			<li class="active" > Transaction Status <span class="divider"></span> </li>



	</ul>



<div class="well"><!-- well starts -->


      <div class="row" style="padding-left:0px; padding-right:0px;">

                      <ul class="list-group">
                          <li class="list-group-item list-group-item-info">
                              <h5>PAYMENT STATUS</h5>
                          </li>
                      </ul>

                      <div class="col-md-12">


<?php

            	//$response_code = '00';

				if($response_code == '01' || $response_code == '00')
				{

					//Payment Successful, You can Update Status to Paid here on Database;


					$user_id = get_invoice_to_using_txnref($orderID); //get the user id from

					//get the invoice details
						$iid = get_invoice_id_using_txnref($orderID);

						//invoice to
						 $invoice_type  = get_invoice_type($iid);//get invoice type
						 $invoice_to = get_invoice_invoice_to($iid);
						 $pay_to = get_invoice_pay_to($iid); //get who to pay the invoce to


						 $invoice_for = get_invoice_for($iid); //get what the invoice is made for
						 $title = get_invoice_title($iid); // get the invocie title
						 $description = get_invoice_desc($iid); //get the invoice desc

					   $invoice_amount =  $amt = get_invoice_total_amount($iid);

							//$user = get_user_id();//get user
						//$level = '100'; //get user level
						$level = get_user_level($use); //get user level
						$programme_type = get_user_programme_type_id($user_id); //programme type
						$department = get_user_department_id($user_id); //department
						$programme  = get_user_programme_id($user_id);//programme
						$school = get_user_school_id($invoice_to); //school

					    $mode = 1; //programme mode 1 -  full time, 2 part time


					 //get current session
					 $session =   get_current_session($id = 1); //get //current session
					 $session_title = get_session_title($session); //get session title

					 //get current semester
					$semester = get_current_semester($id = 1); //get current semester
					 //$semester_title = get_semester_title($semester); //get semester title
					/*
				    $full_name = get_user_fullname($invoice_to);
					$email = get_user_email($invoice_to);

					$phone_no = get_user_phone_no($invoice_to);*/
					//collect matric number an dother student info
				  //$appno = get_user_application_number($invoice_to);




				 	//

					if(!has_paid_summer_fee($user_id,$session, $semester)) //in future add session to match exactly the candidate
					{


						$payment_year = date('Y');//payment year
			 			// $qry44="INSERT INTO `applicants_nce_fee_payments`
						// 	(`programme_type_id`,`department_id`,`programme_id`,  `applicant_id`, `payment_method`, `invoice_id`, `amount`, `date_added`,added_by, session )
						// VALUES ('$programme_type','$department','$programme','$invoice_to','1','$iid','$amt',NOW(), 'onself','$session')";


						$qry44 = "INSERT INTO `summer_application_fee_payments`(`programme_type_id`, `department_id`, `programme_id`, `school_id`,`session`, `semester`, `student_id`,
							 `payment_status`, `invoice_id`, `title`, `description`, `amount`, `date_added`)
							VALUES ('$programme_type','$department','$programme','$school','$session','$semester','$invoice_to','1', '$iid','$invoice_title','$invoice_desc','$invoice_amount',NOW())";

						 $rst44 =  mysqli_query($dbc, $qry44);

						  if($rst44)//mysqli_affected_rows($dbc)
						  {
							//if done finally collect all info for receipt



							 //query
							  $q441 = "UPDATE invoices SET payment_status = 1 WHERE invoice_id = '$iid'"; //update invoice to paid where payment status is 1
							  $r441 = mysqli_query($dbc, $q441);

							  if(mysqli_affected_rows($dbc))
							  //if($r441)
							  {//end of if update is successful


								 //echo '<br/><br/> <a target="_blank" href="create_app_payment_slip.php?qlk='.md5(8).'&me='.$invoice_to.'" class="btn btn-info" data-toggle="tooltip" data-placement="top" title="Click here to Print / Download Payment Receipt"><i class="glyphicon glyphicon-print"> </i> Print / Download Receipt</a>';
								 //clear some sessions


								 //$applicant_fee_payments_entered = true;

							  }
							  else
							  {
								 /*$msg =   '&nbsp;&nbsp;SERVER ERROR: Please try again.';
								header("Location: dashboard.php?act=view_invoices&error=".urlencode($msg)."&qlk=".md5(8)."&auth=".sha1(md5(2))."&iid=".$iid);*/

							  }//end of if invoice is updated



						  }
						  else
						  {
							 //else tell the admin to reprint

							  $msg =  'System Error! We are sorry for inconvenience.Please Retry again. Thank you.';
							  header("Location: dashboard.php?act=view_tlog&error=".urlencode($msg)."&qlk=".md5(8)."&auth=".sha1(md5(2))."&iid=".$iid);
						  }

					}//end of if applicnt id exist in payment
				   else{
					 	//echo '<br/><br/> <a target="_blank" href="create_app_payment_slip.php?qlk='.md5(8).'&me='.$invoice_to.'" class="btn btn-info" data-toggle="tooltip" data-placement="top" title="Click here to Print / Download Payment Receipt"><i class="glyphicon glyphicon-print"> </i> Print / Download Receipt</a>';
				   		echo '<li class="list-group-item list-group-item-info"><h4>Payment Already Processed!</h4></li><br>';
					}


					$transaction_type = 22; //applicants
					$transaction_for = 22; //application fee
					$payment_method = 1; //3 done by transaction status update

					$iid2 = $iid;//pass invoice id
					$title2 = $title;
					$desc2 = $description;


					//get payment status using
					$payment_status = get_payment_status_by_resp_code($response_code);
					//$amt = $amt / 100;
					//$amt = $tot;

					//$rrr = '98908098';
					if($rrr != '')
					{


					  if($rrr != '' && $orderID != '')
					  {

							$qry23="UPDATE  `remita_webpay_transaction_log`  SET
							 `payment_method` = '$payment_method', `payment_status` = '$payment_status', `response_code` = '$response_code', `response_description` = '$response_message',
							 `total_paid_by_buyer` = '$invoice_amount',`extra_charges_by_merchant` = '$extra_tot_m',
							  `transaction_id` = '$rrr',  `email` = '$email', `transaction_date` = NOW()

							 WHERE transaction_reference = '$orderID'";

							$rst23 =  mysqli_query($dbc, $qry23);

							//if(mysqli_affected_rows($dbc))
							if($rst23)
							{//end of if update is successful



								   //clear some sessions

								   //unset($_SESSION['iid']);
								   //unset($_SESSION['amnt']);


							//do nothing for now

							}
							else
							{
								//echo $msg6 =   '&nbsp;&nbsp;SERVER ERROR: Transaction Log has issues, we are sorry for the incovenience. Error(06)';

							   //header("Location: dashboard.php?act=view_invoices3&error=".urlencode($msg)."&qlk=".md5(8)."&iid=".$iid);

							}


					  }//en dof if check

					}//end of RRR aint empty



				}//end of if response code is successull





            ?>

                   <?php echo remita_response_code($response_code, $rrr, $response_message, $orderID ); ?>



      </div><!-- /.col md 12 -->


      </div><!-- /row-->

      <div class="clearfix"></div>





</div><!-- /.well -->


 <br/><br/>

      <ul class="list-group">
          <li class="list-group-item list-group-item-info">
              <h5>TRANSACTION LOG</h5>
          </li>
      </ul>
      <div class="clearfix"></div>
      <div class="col-md-12">

      		<?php echo view_remita_pay_transaction_log($user_id, $transaction_type = 22, $transaction_for = 22);   ?>


      </div>

      <div class="col-md-12">

       <br/><br/><br/>

        <hr>

       </div>


        <div class="col-md-12">
              <div class="col-sm-6  help-block">





                     <img src="images/remita-payment-logo-horizonal-opt.png" class="img-responsive" />

                  </div>

                <div class="col-sm-6  help-block">
                    <h4>For Support, Email or Call : </h4>
                     <h5><span class="glyphicon glyphicon-phone"></span> +234- 08142411717  </h5>


                     <img src="images/support.png" class="pull-right" />

                  </div>
         </div>
