<?php
	error_reporting(0);
	define('INCLUDE_CHECK',true);
	session_start();
	require ('classes/config.php');
	//print_r($_SESSION);
    require('includes/initialize.php');
    
    
    global $database;
	$subpdtid = 6925; // your product ID
	$submittedamt = $_POST["amount"];
	$submittedref = $_POST['txnref'];
        $_SESSION['txn_ref'] = $submittedref;
	
	$nhash = "4D7826760C772D0AEBFA4141ECAD80E73B74347E017F6FDCF18E0AD8C68D91DFA7131319D072709A44ABFCB74E4CFC0B6D6E4E82C9C0D0D35CDE4ABC48E80C27" ; // the mac key sent to by them

$parami = array(
        "productid"=>$subpdtid,
        "transactionreference"=>$submittedref,
        "amount"=>$submittedamt
);
$payparams = http_build_query($parami);

$url = "https://webpay.interswitchng.com/paydirect/api/v1/gettransaction.json?$payparams"; // json
//FROM OUTSIUDE (NOTE SSL) = "https://stageserv.interswitchng.com/test_paydirect/api/v1/gettransaction.json?$ponmo"; // json
//stageserv.interswitchng.com stageserv.interswitchng.com note the variables appended to the url as get values for these parameters

echo "<br/>";
/*echo "GET TRANSACTION URL";
echo "<br/>";
echo $url; */
echo "<br/>";

$hashv = $subpdtid.$submittedref.$nhash;  // concatenate the strings for hash again
$securityHash = hash('sha512',$hashv); 

?>



            <div class="rightp">
            	<h1>TRANSACTION STATUS (Query Response)</h1>
                <div class="kontent">
         <?php
			$headers = array(
					"GET /HTTP/1.1",
					"Host: webpay.interswitchng.com", //stageserv.interswitchng.com"
					"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1",
					//"Content-type:  multipart/form-data",
					//"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
					"Accept-Language: en-us,en;q=0.5",
					//"Accept-Encoding: gzip,deflate",
					//"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
					"Keep-Alive: 300",
					"Connection: keep-alive",
					"Hash: " . $securityHash
				);        
			//print_r2($headers);
			//echo $url;
		 
			$ch = curl_init();  //INITIALIZE CURL///////////////////////////////
			//               
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
			curl_setopt($ch, CURLOPT_POST, false );
			//
			$data = curl_exec($ch);  //EXECUTE CURL STATEMENT///////////////////////////////
			$json = null;


			if (curl_errno($ch)) 
			{ 
                    echo 'REQUEST FAILED: THERE WAS AN ERROR' . '<br/>';
					print "Error: " . curl_error($ch) . "</br></br>";
			
					$errno = curl_errno($ch);
					$error_message = curl_strerror($errno);
					print $error_message . "</br></br>";;
			
					print_r($headers);
			
			}
			else 
			{  
					// Show me the result
					$json = json_decode($data, TRUE);
			
					curl_close($ch);    //END CURL SESSION///////////////////////////////
					
					//print_r(array_values($json));
					echo '<br/>';echo '<br/>';
					echo 'TRANSACTION SUMMARY';echo '<br/>';echo '--------------------------------------------------------------------------';echo '<br/>';
					if($json["ResponseCode"] != '00')
						{
							echo '<strong>YOUR TRANSACTION FAILED. FIND BELOW TRANSACTION DETAILS.</strong>';        echo '<br/>';
							echo "<b>Response Code: </b>".$json["ResponseCode"]."</br>";                  
							echo "<b>Response Description: </b>".$json["ResponseDescription"]."</br>";    
							echo "<b>Transaction Reference: </b>" . $_POST['txnref'];          echo '<br/>';
					}else{
							echo '<strong>YOUR TRANSACTION WAS SUCCESSFUL. FIND BELOW TRANSACTION DETAILS.</strong>';            echo '<br/>';
							echo "<b>Response Code: </b>".$json["ResponseCode"]."</br>";                   
							echo "<b>Response Description: </b>".$json["ResponseDescription"]."</br>";     
							echo "<b>Payment Reference: </b>".$json["PaymentReference"]."</br>";
							echo "<b>Transaction Reference: </b>" . $_POST['txnref'];          echo '<br/>';
                                                        echo "<b>Transaction Amount: </b>".$json["Amount"]."</br>";
                                                        echo "<b>Transaction Date: </b>".$json["TransactionDate"]."</br>";

                                                        $to = $_SESSION['email'];
							$subject="OSCOTECH ePayment System Details!"; 
							$body = "Dear ".$_SESSION['surname'].",";
							$body .=PHP_EOL.PHP_EOL ."Your ePayment Transaction was Successfull. Below is your payment details:."; echo '<br/><br/>';
							$body .=PHP_EOL . "Response Code: ".$json["ResponseCode"];                 
							$body .=PHP_EOL . "Response Description: ".$json["ResponseDescription"];      
							$body .=PHP_EOL . "Payment Reference: ".$json["PaymentReference"];
							$body .=PHP_EOL . "Transaction Reference: " . $_POST['txnref'];    
							$body .=PHP_EOL . "Transaction Amount: ".$json["Amount"]; 
							$body .=PHP_EOL . "Transaction Date: ".$json["TransactionDate"];
							$body .=PHP_EOL.PHP_EOL.PHP_EOL."------------------------".PHP_EOL."Thanks";
							$body = stripslashes($body);
							$headers = "From: no.reply@oscotechesaoke.edu.ng";
							$headers .= PHP_EOL;
							$headers .= "Return-Path: no.reply@oscotechesaoke.edu.ng";
							$headers .= PHP_EOL;
							$headers .= "Reply-To: no.reply@oscotechesaoke.edu.ng";
							mail($to,$subject,$body,$headers);


// ======== Omoluabi Integration ===========================================================================================
						$sqls = $database->query("SELECT * FROM tranzact_t WHERE txn_ref='$submittedref'") or die;			
							while($srows = mysqli_fetch_array($sqls)){
								$fullnameo = $srows['surname']. ' ' . $srows['othername']; 
								$facultycodeo = $srows['facultycode'];
								$facultyo = $srows['faculty'];
								$departmentcodeo = $srows['departmentcode'];
								$departmento = $srows['department'];
								$programmecodeo = $srows['programmecode'];
								$programmeo = $srows['programme'];
								$levelcodeo = $srows['levelcode'];
								$levelo = $srows['level'];
								$fnoo = $srows['fno'];
								$txn_refo = $srows['txn_ref'];
								$paymentdate = $srows['date_time'];								
							}
					$sqlin = $database->query("INSERT INTO student (transref,institutioncode,institutionname,matricnumber,fullname,facultycode,facultyname,departmentcode,departmentname,programmecode,programmename,levelcode,levelname,paymentdate,transDate) 
						VALUES('$txn_refo','OSCOTECH','Osun State College of Technology, Esa-Oke','$fnoo','$fullnameo','$facultycodeo','$facultyo','$departmentcodeo','$departmento','$programmecodeo','$programmeo','$levelcodeo','$levelo','$paymentdate','$paymentdate')") or die(mysql_error());
						
						$sqlss = $database->query("SELECT id FROM student WHERE transref='$submittedref'") or die;			
							while($srowss = mysqli_fetch_array($sqlss)){
								$stdid = $srowss['id'];
							}
						$sqlsi = $database->query("SELECT * FROM tranzact_t_items WHERE txn_ref='$submittedref'") or die;			
							while($isrows = mysqli_fetch_array($sqlsi)){
								$txn_refoo = $isrows['txn_ref'];
								$feecode = $isrows['feecode'];
								$feename = $isrows['fees_description'];
								$amountoo = $isrows['amount'];	
						$sqlins = $database->query("INSERT INTO studentfee (studentId,feecode,feename,amount,transref) 
						VALUES('$stdid','$feecode','$feename','$amountoo','$txn_refoo')") or die(mysql_error());

							}
	
//======== End Omoluabi Integration ==============================================================================

						}
					echo '-------------------------------------------------------------------------- <br>';
                                        echo '<b>NOTE: <em>Your ePayment Transaction details sent successfully to your email box. Thanks.</b></em> <br>';
					$responsecode = $json["ResponseCode"];
					$responsedesc = $json["ResponseDescription"];
					//print_r($json);
					// loop through the array nicely for your UI
				
				$sqlupdate=$database->query("UPDATE tranzact_t, webpaylog SET tranzact_t.status = '$responsedesc', tranzact_t.responsecode='$responsecode', webpaylog.responsedesc = '$responsedesc', webpaylog.responsecode='$responsecode' WHERE tranzact_t.txn_ref='$submittedref' AND webpaylog.txnref='$submittedref'")or die ('An error occured ' . mysql_error());
			
			}
			
			session_write_close();
			
		/*	//function print_r2($val)
			{
					echo '<pre>';
					print_r($val);
					echo  '</pre>';
			} */


?>		
<br><br>
<?php
   if ($responsecode =='00'){ ?>
    <a href="payment_notification_receipt.php" target="_new" class="btn btn-success">Print Receipt</a>
    <a href="index.php?action=go_out" class="btn btn-primary">Continue</a>
  <?php } ?>
                </div>
            </div>
        </div><!-- content -->    
        