<?php
require 'remita_constants.php';

$orderID = "";
if (isset($_GET['orderID'])) {
    $orderID = $_GET["orderID"];
}
$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'];
    /*print_r($response);
exit();*/
}
?>

<!--	<div style="text-align: center;">
		<?php //if($response_code == '01' || $response_code == '00') { ?>
		<h2>Transaction Successful</h2>
		<p><b>Remita Retrieval Reference: </b><?php //echo $rrr; ?><p>
		<?php // }else if($response_code == '021') { ?>
						<h2>RRR Generated Successfully</h2>
						<p><b>Remita Retrieval Reference: </b><?php //echo $rrr; ?><p>
		<?php //}    else{ ?>
						<h2>Your Transaction was not Successful</h2>
						<?php //if ($rrr !=null){ ?>
						 <p>Your Remita Retrieval Reference is <span><b><?php //echo $rrr; ?></b></span><br />
						<?php //} ?>
						  <p><b>Reason: </b><?php //echo $response_message; ?><p>
		 <?php //}?>
	</div>-->


<?php

//collect invoice id
$iid = $_SESSION['iidar'];

?>
<ul class="breadcrumb">

        <li><a href="dashboard.php?sms=payment&qlk=<?php echo md5(2); ?>">Payments</a> <span class="divider"></span> </li>
        <li><a href="dashboard.php?sms=view_invoices&qlk=<?php echo md5(2); ?>&auth=<?php echo sha1(md5(2)) ?>&iid=<?php echo $iid; ?>">View Invoice</a> <span class="divider"></span> </li>


        <li class="active" > Payment Response <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

//collect invoice id
//if(isset($_POST['iid'],$_POST['qlk']))
//{

//
//
// $level =  get_user_level($user_id); //get user level

$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
$invoice_title = get_invoice_title($iid); // get the invocie title
$invoice_desc = get_invoice_desc($iid); //get the invoice desc
//     $amount = get_invoice_amount($iid); //get the invocie amount
$invoice_amount = get_invoice_total_amount($iid); //get invoice total amount

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

//     $payment_status = get_invoice_payment_status($iid); //get the invoice payment status

//pass invoice to user id
$user_id = $invoice_to;

//$user = get_user_id();//get user
//$level = '100'; //get user level
$level = get_user_level($user_id); //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
$spt = get_user_sponsorship_type($user_id);

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

$remita_share_fix = '300';
$vat_fix = '15';

$extra_tot_m = $remita_share_fix + $vat_fix;

$full_name = get_user_fullname($invoice_to);
$email = get_user_email($invoice_to);
$phone_no = get_user_phone_no($invoice_to);

//collect matric number
$m_no = get_user_mat_no($user_id);

if (isset($_GET["orderID"])) { //begin if interswitch rep

    //finally if none of the above happens
    echo remita_response_code($response_code, $rrr, $response_message, $orderID);

    // $apprAmt = 3600000;

    $amt = $_SESSION['amnttr']; // amount passed from the confirmation page this for the interswitch collagepay

    //now if response code
    //check if the payment is succesful

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

        //if successful transaction enter the students and generate matric number

        ///check if he / she has paid fee
        if (has_paid_hostel_fee($user_id, $session)) {

            $msg = "&nbsp;&nbsp;&nbsp;&nbsp;Hostel fee payment for this session has been made already. Please click on payment tab to print receipt";
            header("Location: dashboard.php?sms=view_invoices&info=" . urlencode($msg) . "&qlk=" . md5(2) . "&auth=" . sha1(md5(2)) . "&iid=" . $iid);

        } else {
            $payment_year = date('Y'); //payment year

            $qqq1 = "INSERT INTO `hostel_fee_payments`(`id`, `programme_type_id`, `department_id`, `programme_id`, `school_id`,`session`, `semester`, `student_id`,`sponsorship_type`,
									 `payment_status`,`payment_method`,`payment_mode`, `invoice_id`,
								   `title`, `description`, `amount`, `date_added`)
									VALUES (NULL,'$programme_type','$department','$programme','$school','$session','0','$invoice_to','$spt','1','1', '2','$iid','$invoice_title','$invoice_desc','$invoice_amount',NOW())";

            $rrr1 = mysqli_query($dbc, $qqq1);

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

                $q441 = "UPDATE hostel_allocation SET `status` = 1 WHERE student_id = '$invoice_to' AND session_id = '$session'";
                $r441 = mysqli_query($dbc, $q441);

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

                if ($r442) { //end of if update is successful

                    //here the display is handled when its 00

                    echo '&nbsp;&nbsp;&nbsp;<a class="btn btn-success"  href="create_sf_payment_slip.php?me=' . $user_id . '&s=' . $session . '&l=' . $level . '&myid=' . md5('fuckusha1sha1') . '."   data-toggle="tooltip" data-placement="top" title="Download / Print  ">Click here to  DOWNLOAD / PRINT </a>';

                    //clear some sessions

                    $school_fee_payments_entered = true;

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

                } //end of if invoice is updated

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

                header("Location: dashboard.php?sms=view_invoices&error=" . urlencode($msg) . "&qlk=" . md5(2) . "&auth=" . sha1(md5(2)) . "&iid=" . $iid);

            } //end of if inserted

        } //end of if he / she has paid school

        ///end of if application is successful and insert into students table
    } //en dof json of response code

    $transaction_type = 2; //returning students
    $transaction_for = 2; //registration fee
    $payment_method = 1; //1 for remita pay

    $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;

    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` = '$amt',`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 {
                $msg = '&nbsp;&nbsp;SERVER ERROR: Transaction Log has issues, we are sorry for the incovenience.';

                header("Location: dashboard.php?sms=view_invoices&error=" . urlencode($msg) . "&qlk=" . md5(2) . "&iid=" . $iid);

            }

        } //en dof if check

    }

} //end of if we have  response fromvogu pay

?>







      </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($invoice_to, $transaction_type, $transaction_for); ?>

      </div>

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

       <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-envelope"></span> eportals@coeminna.edu.ng </h5>
                     <h5><span class="glyphicon glyphicon-phone"></span> +234- 08142411717 </h5>


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

                  </div>
         </div>
