</<?php


//includes functions and classes for remita constants
require 'remita_constants_sf.php';

$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?sms=payment&qlk=<?php echo md5(2); ?>">Payments</a> <span class="divider"></span> </li>

			<li class="active" > Check 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 TRANSACTION 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);
						//collect matric number
					  if($type_id==1)
					  {
					    //collect matric number
					    $m_no = get_user_mat_no_prence($user_id);
								$mno = str_replace('/','',$m_no);

					    //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
					    $invoice_title = get_invoice_title($iid); // get the invocie title
					    $invoice_desc = get_invoice_desc($iid); //get the invoice desc
					    $invoice_amount = get_invoice_amount($iid); //get the invocie amount
					    $total_amount = get_invoice_total_amount($iid); //get invoice total amount
					    $credit = get_invoiced_credit($iid); //get invoice credit
					    $full_name= get_user_fullname_prence($invoice_to);
					    $U_addres = get_user_address_prence($invoice_to);
					    $U_state = show_state(get_user_state_id_prence($invoice_to));
					    $invoice_date= get_invoice_date($iid);
					    $invoice__due_date = get_invoice_due_date($iid);
					    $payment_status = get_invoice_payment_status($iid); //get the invoice payment status
					    $invoice_expire_date = get_invoice_exp_date($iid);
					    $email = get_user_email_prence($invoice_to);
					    $phone_no = get_user_phone_no_prence($invoice_to);
					    //pass invoice to user id
					    $user_id = $invoice_to;

					    $programme_type = get_user_programme_type_id_prence($user_id); //programme type
					    $department = get_user_department_id_prence($user_id); //department
					    $programme = get_user_programme_id_prence($user_id); //programme
					  }else if($type_id==2)
					  {
					    //collect matric number
					    $m_no = get_user_mat_no_nce($user_id);
								$mno = str_replace('/','',$m_no);

					    //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
					    $invoice_title = get_invoice_title($iid); // get the invocie title
					    $invoice_desc = get_invoice_desc($iid); //get the invoice desc
					    $invoice_amount = get_invoice_amount($iid); //get the invocie amount
					    $total_amount = get_invoice_total_amount($iid); //get invoice total amount
					    $credit = get_invoiced_credit($iid); //get invoice credit
					    $full_name= get_user_fullname_nce($invoice_to);
					    $U_addres = get_user_address_nce($invoice_to);
					    $U_state = show_state(get_user_state_id_nce($invoice_to));
					    $invoice_date= get_invoice_date($iid);
					    $invoice__due_date = get_invoice_due_date($iid);
					    $payment_status = get_invoice_payment_status($iid); //get the invoice payment status
					    $invoice_expire_date = get_invoice_exp_date($iid);
					    $email = get_user_email_nce($invoice_to);
					    $phone_no = get_user_phone_no_nce($invoice_to);
					    //pass invoice to user id
					    $user_id = $invoice_to;

					    $programme_type = get_user_programme_type_id_nce($user_id); //programme type
					    $department = get_user_department_id_nce($user_id); //department
					    $programme = get_user_programme_id_nce($user_id); //programme
					  }else if($type_id==3)
					  {
					    //collect matric number
					    $m_no = get_user_mat_no_duap($conn_2, $user_id);
								$mno = str_replace('/','',$m_no);

					    //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
					    $invoice_title = get_invoice_title($iid); // get the invocie title
					    $invoice_desc = get_invoice_desc($iid); //get the invoice desc
					    $invoice_amount = get_invoice_amount($iid); //get the invocie amount
					    $total_amount = get_invoice_total_amount($iid); //get invoice total amount
					    $credit = get_invoiced_credit($iid); //get invoice credit
					    $full_name= get_user_fullname_duap($conn_2, $invoice_to);
					    $U_addres = get_user_address_duap($conn_2, $invoice_to);
					    $U_state = show_state(get_user_state_id_duap($conn_2, $invoice_to));
					    $invoice_date= get_invoice_date($iid);
					    $invoice__due_date = get_invoice_due_date($iid);
					    $payment_status = get_invoice_payment_status($iid); //get the invoice payment status
					    $invoice_expire_date = get_invoice_exp_date($iid);
					    $email = get_user_email_duap($conn_2, $invoice_to);
					    $phone_no = get_user_phone_no_duap($conn_2, $invoice_to);
					    //pass invoice to user id
					    $user_id = $invoice_to;

					    $programme_type = get_user_programme_type_id_duap($conn_2, $user_id); //programme type
					    $department = get_user_department_id_duap($conn_2, $user_id); //department
					    $programme = get_user_programme_id_duap($conn_2, $user_id); //programme
					  }else if($type_id==4)
					  {
					    //collect matric number
					    $m_no = get_user_mat_no_duap_udus($conn_2, $user_id);
								$mno = str_replace('/','',$m_no);

					    //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
					    $invoice_title = get_invoice_title($iid); // get the invocie title
					    $invoice_desc = get_invoice_desc($iid); //get the invoice desc
					    $invoice_amount = get_invoice_amount($iid); //get the invocie amount
					    $total_amount = get_invoice_total_amount($iid); //get invoice total amount
					    $credit = get_invoiced_credit($iid); //get invoice credit
					    $full_name= get_user_fullname_duap_udus($conn_2, $invoice_to);
					    $U_addres = get_user_address_duap_udus($conn_2, $invoice_to);
					    $U_state = show_state(get_user_state_id_duap_udus($conn_2, $invoice_to));
					    $invoice_date= get_invoice_date($iid);
					    $invoice__due_date = get_invoice_due_date($iid);
					    $payment_status = get_invoice_payment_status($iid); //get the invoice payment status
					    $invoice_expire_date = get_invoice_exp_date($iid);
					    $email = get_user_email_duap_udus($conn_2, $invoice_to);
					    $phone_no = get_user_phone_no_duap_udus($conn_2, $invoice_to);
					    //pass invoice to user id
					    $user_id = $invoice_to;

					    $programme_type = get_user_programme_type_id_duap_udus($conn_2, $user_id); //programme type
					    $department = get_user_department_id_duap_udus($conn_2, $user_id); //department
					    $programme = get_user_programme_id_duap_udus($conn_2, $user_id); //programme
					  }else if($type_id==5)
					  {
					    //collect matric number
					    $m_no = get_user_mat_no_summer($user_id);
								$mno = str_replace('/','',$m_no);

					    //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
					    $invoice_title = get_invoice_title($iid); // get the invocie title
					    $invoice_desc = get_invoice_desc($iid); //get the invoice desc
					    $invoice_amount = get_invoice_amount($iid); //get the invocie amount
					    $total_amount = get_invoice_total_amount($iid); //get invoice total amount
					    $credit = get_invoiced_credit($iid); //get invoice credit
					    $full_name= get_user_fullname_summer($invoice_to);
					    $U_addres = get_user_address_summer($invoice_to);
					    $U_state = show_state(get_user_state_id_summer($invoice_to));
					    $invoice_date= get_invoice_date($iid);
					    $invoice__due_date = get_invoice_due_date($iid);
					    $payment_status = get_invoice_payment_status($iid); //get the invoice payment status
					    $invoice_expire_date = get_invoice_exp_date($iid);
					    $email = get_user_email_summer($invoice_to);
					    $phone_no = get_user_phone_no_summer($invoice_to);
					    //pass invoice to user id
					    $user_id = $invoice_to;

					    $programme_type = get_user_programme_type_id_summer($user_id); //programme type
					    $department = get_user_department_id_summer($user_id); //department
					    $programme = get_user_programme_id_summer($user_id); //programme
					  }else if($type_id==6)
					  {
					    //collect matric number
					    $m_no = get_user_mat_no_lvsp($user_id);
								$mno = str_replace('/','',$m_no);

					    //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
					    $invoice_title = get_invoice_title($iid); // get the invocie title
					    $invoice_desc = get_invoice_desc($iid); //get the invoice desc
					    $invoice_amount = get_invoice_amount($iid); //get the invocie amount
					    $total_amount = get_invoice_total_amount($iid); //get invoice total amount
					    $credit = get_invoiced_credit($iid); //get invoice credit
					    $full_name= get_user_fullname_lvsp($invoice_to);
					    $U_addres = get_user_address_lvsp($invoice_to);
					    $U_state = show_state(get_user_state_id_lvsp($invoice_to));
					    $invoice_date= get_invoice_date($iid);
					    $invoice__due_date = get_invoice_due_date($iid);
					    $payment_status = get_invoice_payment_status($iid); //get the invoice payment status
					    $invoice_expire_date = get_invoice_exp_date($iid);
					    $email = get_user_email_lvsp($invoice_to);
					    $phone_no = get_user_phone_no_lvsp($invoice_to);
					    //pass invoice to user id
					    $user_id = $invoice_to;

					    $programme_type = get_user_programme_type_id_lvsp($user_id); //programme type
					    $department = get_user_department_id_lvsp($user_id); //department
					    $programme = get_user_programme_id_lvsp($user_id); //programme
					  }


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

					 

				 	//

					//collect the commission
					$remita_share = '323';
					//$vat_fix = '15';

					$extra_tot_m = $remita_share;



				 if(has_paid_hostel_fee($user_id,$session))
				 {
					 //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>';

				 }//end of if applicnt id exist in payment
				 else
				 {

							 $payment_year = date('Y');//payment year  //payment method is 2

							 $qry44="INSERT INTO `hostel_fee_payments`(`id`, `programme_type_id`, `department_id`, `programme_id`,
								`session_id`, `semester_id`, `student_id`, `payment_status`, `invoice_id`,
								`title`, `description`, `amount`, `date_added`)
								 VALUES (NULL,'$type_id','$department','$programme',
								 '$session','0','$invoice_to','1', '$iid','$invoice_title','$invoice_desc','$invoice_amount',NOW())";


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


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



										//query
										$q442= "UPDATE hostel_invoices SET payment_status = 1 WHERE invoice_id = '$iid'"; //update invoice to paid where payment status is 1
										$r442 = mysqli_query($conn_1, $q442);

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

									  	//here the display is handled when its 00
									  	//$msg ="&nbsp;&nbsp;Payment has been successfully made, please click on payment history to access receipt.";
									  	//header("Location: gateway.php?act=school_fee_payments&is=".urlencode($msg)."&qlk=".md5(2)."");


									   	//clear some sessions
									   $school_fee_payments_entered = true;

									}
									else
									{
									   echo $msg =   '&nbsp;&nbsp;SERVER ERROR: Error(1).';

									}//end of if invoice is updated

								}
								else
								{
									$msg =   '&nbsp;&nbsp;SERVER ERROR: Error(2).';

									//header("Location: gateway.php?act=view_invoices3&error=".urlencode($msg)."&qlk=".md5(2)."&auth=".sha1(md5(2))."&iid=".$iid);

								}//end of if inserted

				 }


					$transaction_type = 5; //returning students
					$transaction_for = 5; //registration fee
					//$payment_method = 1; //1 for remita  epay, 2 - bank branch payment


					$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  `hostel_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($conn_1, $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_min($user_id, $transaction_type = 5, $transaction_for = 5);   ?>


      </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>
