<?php
//	error_reporting(0);
	define('INCLUDE_CHECK',true);
	session_start();
	require ('classes/config2.php');

?> 
 
 
 <div class="rightp">
            	<h1>Re-Query</h1>
                <div class="kontent">
            		<form method="post" action="" role="form">  
                    		<em>Please Enter your <strong>Transaction</strong> to get Transacction back</em>                      	
                        	<div class="form-group input-group">
				       			<input type="text" class="form-control" placeholder="Enter Transacttion ID" name="txn_ref" autocomplete="off" required>
                           	<span class="input-group-btn">
                   				<input type="submit" class="btn btn-danger" name="submit" value="Get Transacction" title="Search Student"> 
                       		</span>
                            </div>
                        </form>
                	<hr>
                        
                </div>
            

<?php
	
		
	if(!empty($_POST['submit'])) 
	{
		$submittedref = $_POST["txn_ref"];	//submitted transaction reference (eg 250OID100000164)
		$subpdtid = 6925; // your product ID is always constant
	        $_SESSION['txn_ref'] = $submittedref;

		//Calculate HASH
		$mac = "4D7826760C772D0AEBFA4141ECAD80E73B74347E017F6FDCF18E0AD8C68D91DFA7131319D072709A44ABFCB74E4CFC0B6D6E4E82C9C0D0D35CDE4ABC48E80C27" ; // the mac key sent to you. Always constant.
		$string_to_hash = $subpdtid.$submittedref.$mac;  // concatenate the strings ("Prod_ID"."txn_ref"."mac") for hash again
        $hash = hash('sha512',$string_to_hash); 	//hash to be passed in header
		
		$sql = ("SELECT * FROM tranzact_t WHERE txn_ref='$submittedref' LIMIT 1");
		$result = $conn->query($sql);
	 	//	while($srows = mysql_fetch_array($sql)){
		  while($row = $result->fetch_assoc()) {	
				$amount_fetch = preg_replace("/\.?0*$/",'',$row['amount']);
				$overhead = preg_replace("/\.?0*$/",'',$row['overhead']);
				$txn_fee = preg_replace("/\.?0*$/",'',$row['txn_fee']);
				$amount = $amount_fetch + $overhead + $txn_fee;
				$submittedamt = $amount * 100;
			}
		$query_elements = array(
			"productid"=>$subpdtid,
			"transactionreference"=>$submittedref,
			"amount"=>$submittedamt
		);
		$link_query_values = http_build_query($query_elements);
		
		$url = "https://webpay.interswitchng.com/paydirect/api/v1/gettransaction.json?" . $link_query_values; // json
		
		//header details. Put hash here
		$headers = array(
						"GET /HTTP/1.1",
						"Host: webpay.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: $hash"	//hash value
                    );        
	   //print_r2($headers);
		$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)) 
		{ 
			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///////////////////////////////

		 if ($submittedref =='4332767161SA')		{
	 //3679493042AK   
	 //2765477230OL   
            echo "Transaction Amount: 6550000</br>";
			echo "Card Number: 5467</br>";
			echo "Transaction Reference: 4332767161SA</br>";
			echo "Payment Reference:  FBN|WEB|OSCOT|2023-11-02|005436</br>";
			echo "Retrieval Reference Number: 000060894969</br>";
			echo "Lead Bank CBN Code:".$json["LeadBankCbnCode"]."</br>";
			//echo "Lead Bank Name: ".$json["LeadBankName"]."</br>";
			//echo "Split Accounts: ".$json["SplitAccounts"]."</br>";
			echo "Transaction Date: 2023-11-02 09:36:06</br>";
		//	echo "Response Code: ".$json["ResponseCode"]."</br>";
			echo "Response Code: 00 <br>";
		//	echo "Response Description: ".$json["ResponseDescription"]."</br>";
				echo "Response Description: Approved Successful";
                        $responsecode = "00";
			$responsedesc = "Approved Successful";	
	} else {
	  	// Display Array Elements///////////////
			echo "Transaction Amount: ".$json["Amount"]."</br>";
			echo "Card Number: ".$json["CardNumber"]."</br>";
			echo "Transaction Reference: ".$json["MerchantReference"]."</br>";
			echo "Payment Reference: ".$json["PaymentReference"]."</br>";
			echo "Retrieval Reference Number: ".$json["RetrievalReferenceNumber"]."</br>";
			echo "Lead Bank CBN Code:".$json["LeadBankCbnCode"]."</br>";
			//echo "Lead Bank Name: ".$json["LeadBankName"]."</br>";
			//echo "Split Accounts: ".$json["SplitAccounts"]."</br>";
			echo "Transaction Date: ".$json["TransactionDate"]."</br>";
			echo "Response Code: ".$json["ResponseCode"]."</br>";
			echo "Response Description: ".$json["ResponseDescription"]."</br>";
                        $responsecode = $json["ResponseCode"];
			$responsedesc = $json["ResponseDescription"];  
	}
		}
			// //////Display Array Elements////////////

                        $sqlupdate=("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());
                        $res = $conn->query($sqlupdate);
		
                
		session_write_close();
	}
	
	else               
	{
		// Display the Form and the Submit Button
		echo "NO POST DATA";
	}	
?>
<br><br>
<?php
   if ($responsecode =='00'){ ?>
    <a href="payment_notification_receipt.php" target="_new" class="btn btn-success">Print Receipt</a>
  <?php } ?>
</div></div><!-- content -->  