<?php require_once("../includes/initialize.php");?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../favicon.ico">

    <title>Make Payment</title>

    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="../vendors/bootstrap/css/bootstrap.min.css">
    
  </head>

  <body>

    <div class="container">
      <div class="header clearfix">
        <nav>
          <ul class="nav nav-pills pull-right">
            <li role="presentation" class="active"><a href="#">Home</a></li>
            <li role="presentation"><a href="#">About</a></li>
            <li role="presentation"><a href="#">Contact</a></li>
          </ul>
        </nav>
        <h3 class="text-muted">Make Payment</h3>
      </div>
<?php
//echo '1';
// $txnref = uniqid('sbcstx');
 $txnref = $_SESSION['payment_no'];
$product_id = P_ID;
$mac_key = MAC_KEY;
 $total = $_SESSION['famount'];

// $base_url = "https://sandbox.interswitchng.com/webpay/pay/";
// $base_url = "https://webpay.interswitchng.com/paydirect/api/v1/gettransaction.json";
//$base_url = "https://stageserv.interswitchng.com/test_paydirect/api/v1/gettransaction.json";

$base_url = "https://sandbox.interswitchng.com/webpay/api/v1/gettransaction.json";
$url = $base_url . "?productid=$product_id&transactionreference=$txnref&amount=$total";


$hash = $product_id . $txnref . $mac_key;
$hash = hash("sha512", $hash);
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$request_headers = array();
// "GET /HTTP/1.1[]";
$request_headers[] = 'User-Agent: ' . $User_Agent;
$request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$request_headers[] = 'Hash: ' . $hash;

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$result = curl_exec($ch);
curl_close($ch);

$response = json_decode($result, true);
$response_code = $response['ResponseCode'];
$amount_paid = $response['Amount'] / 100;
$response_description = $response['ResponseDescription'];
$merchant_reference = $response['MerchantReference'];
$payment_reference = $response['PaymentReference'];
$retrieval_reference_number = $response['RetrievalReferenceNumber'];
$card_number = $response['CardNumber'];
$transaction_date = $response['TransactionDate'];

// $mysqli->query("UPDATE acceptance_tranzact SET amount = '" . $amount_paid . "', responsecode = '" . $response_code . "', status = '" . $response_description . "', payment_reference = '" . $payment_reference . "', merchant_reference = '" . $merchant_reference . "', retrieval_reference_number = '" . $retrieval_reference_number . "', card_number = '" . $card_number . "', transaction_date = '" . $transaction_date . "' WHERE transaction_no='" . $txnref . "';");
//$mysqli->query("UPDATE acceptance_tranzact SET amount = '" . $amount_paid . "', responsecode = '" . $response_code . "', status = '" . $response_description . "', , datetime = '" . $transaction_date . "' WHERE txt_ref='" . $txnref . "';");
//$responsedescription=$response_description;
//$responsecode=$responsecode;
//$txn_ref=$txnref;
Transaction::updateaccept($response_description,$response_code,$txnref);

if ($response_code == "00") {
    ?>
    <div class="note note-success">
        <h4 class="block">Your transaction is Approved Successfully on <?= $transaction_date ?></h4>
        <p>
            Your transaction reference number is : <?= $txnref ?>
        </p>
      <h1><a href="../.VSESC_EED_CLEARANCE.php?txn=<?= $txnref ?>">Print Receipt</a> </h1>
      (<strong>Pls Print Your Receipt/Clearance OR Login with your Matric No on Student Login and Print Latter</strong>) 
    </div>
<?php } else { ?>
    <div class="note note-warning">
        <h4 class="block">Your transaction was not successful</h4>
        <p>
            Reason : (<?= $response_code ?>) - <?= $response_description ?> <br />Transaction Reference : <?= $txnref ?><br/>Payment Reference: <?= $retrieval_reference_number ?>
        </p>
        <a href="../index.php">Try Again</a>
         <a href="?type="acceptance_payment" style="margin-left:30px; text-decoration:none">Print receipt</a>
<?php } ?>


	  <footer class="footer">
        <p>&copy; 2018 eportal.oscotechesaoke.edu.ng/student_portal</p>
      </footer>

    </div> <!-- /container -->
	
<script src="../vendors/jquery/jquery-3.2.1.min.js" type="text/javascript"></script>
<script src="../vendors/bootstrap/js/bootstrap.min.js"></script>

<img src ="../images/image001_1.png"/>
</body>
</html>