<?php
ob_start();session_start();
require_once('../includes/initialize.php');

include 'remita_constants.php';

$stid = $_POST["stid"];
$stdacct= Biodata::getpaymenttogenrrr($stid);


$totalAmount = ($stdacct->amount+645); 
   $servicetypeid = Biodata::getcredentials(1);
  $servicetypeid=$servicetypeid->serviceTypeId;


$merchantId = MERCHANTID;
$timesammp=DATE("dmyHis");		
$orderID = $timesammp;
$payerName = $_POST["name"];
$payerEmail = $_POST["email"];
$payerPhone = $_POST["phone"];

$responseurl = "remitaListeningUrl.php";
echo "hash: ".$hash_string = MERCHANTID . $servicetypeid . $orderID . $totalAmount . APIKEY;
$hash = hash('sha512', $hash_string);
$itemtimestamp = $timesammp;
//The JSON data.
$content = '{"serviceTypeId":"'.$servicetypeid.'"'.",".'
  "amount":"'.$totalAmount.'"'.",".'
  "orderId":"'.$orderID.'"'.",".'
  "payerName":"'.$payerName.'"'.",".'
  "payerEmail":"'.$payerEmail.'"'.",".'
  "payerPhone":"'.$payerPhone.'"}';


$curl = curl_init();


curl_setopt_array($curl, array(
  CURLOPT_URL => GATEWAYURL,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_SSL_VERIFYPEER => false,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => $content,
  CURLOPT_HTTPHEADER => array(
    "Authorization: remitaConsumerKey=$merchantId,remitaConsumerToken=$hash",
    "Content-Type: application/json",
    "cache-control: no-cache"
  ),
));

$json_response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

$jsonData = substr($json_response, 7, -1);
$response = json_decode($jsonData, true);


var_dump($response);
// $paymentReference = $response['paymentReference'];
// $transactionId = $response['transactionId'];
// $amount = $response['amount'];
// if (!empty($paymentReference) and !empty($transactionId) and !empty($amount))
// {
//     $paydetails= StudentAcct::updatepayment($stid,$amount,$paymentReference,$transactionId );
  
// }
    
$statuscode = $response['statuscode'];
 $statusMsg = $response['status'];
if($statuscode=='025'){
$rrr = trim($response['RRR']);
$new_hash_string = MERCHANTID . $rrr . APIKEY;
$new_hash = hash('sha512', $new_hash_string);
 echo "RRR Generation Response - " .$rrr;
$prev_record = Biodata::getpaymenttogenrrr($stid);


if (empty($prev_record->orderId) or empty($prev_record->rrr)){
 echo "Order ID: ".$stid.'/'.$rrr.'/'.$orderID;
  $save=Biodata::updatejson($stid,$rrr,$orderID);
  
}

//if ($save){
 //redirect_to('pages/online-payment.html');
 redirect_to('http://odfel.oscotechesaoke.edu.ng/student_portal/dashboard.php?type=remitadetailsrrr');
//}

}else{
//echo "Error Generating RRR - " .$rrr;
$save=Biodata::updatejsonerror($stid,$rrr);
//echo $response['statuscode'];



//$arr=json_decode($jsonData,true);
// 	try {
// 		if($arr!=null)
// 			{
				// foreach($arr as $key => $value)
				// 	{
				// 		$orderRef = $value['orderRef'];
				// 		//Confirm transaction Status to be sure it is coming from Remita
				// 		$response =  remita_transaction_details($orderRef);
						
						
				// 		echo 'My response: '.$response;
				// 	echo	$response_code = $response['status'];
				// 	echo	$response_reason = $response['message'];
				// 		$rrr = $response['RRR'];
				// 	  $orderId = $response['orderId'];
				// 		if($response_code == '01' || $response_code == '00')
				// 		{
				// 			var_dump($arr);
				// 		}
				// 	}
				// exit('OK');
		//	}
		
	//	}
	//	catch (Exception $e) {
	//		exit('Not OK');
	//	}
// 		function remita_transaction_details($orderId){
// 	$mert =  "";
// 	$api_key = "";
// 	$mode = "Test";
// 	$hash_string = $orderId . $api_key . $mert;
// 	$hash = hash('sha512', $hash_string);
// 	if( $mode == 'Test' ){
// 		$query_url = 'http://www.remitademo.net/remita/exapp/api/v1/send/api/echannelsvc';
// 		}
// 	else if( $mode == 'Live' ){
// 		$query_url = 'https://login.remita.net/remita/exapp/api/v1/send/api/echannelsvc';
// 		}
// 	$url 	= $query_url . '/' . $mert  . '/' . $rrr . '/' . $hash . '/' . 'status.reg';
// 	$result = file_get_contents($url);
//     $response = json_decode($result, true);
//     return $response;
// }











}

























//var_dump($response);



?>