<?php
include_once 'includes/functions.php';
include_once 'sch_fees_remita_constants.php';
include_once 'includes/sms.php';

/*$json = file_get_contents('php://input');
$arr=json_decode($json,true);*/

							
	$pay_count = 0;
	$trans_type = 1; //applicants
	$trans_for = 2; //registration fee
	$response_code = '';
	$rrr = '';
	$dt = '';
	$sn = 1;
	$sn1 = 1;
    /*	$qall = "SELECT DISTINCT
            (rem.transaction_reference) AS transaction_reference
        FROM
            remita_webpay_transaction_log rem,
            invoices invs
        WHERE
            rem.transaction_type = '1' AND rem.transaction_for = '2' AND rem.payment_status = 1 
            AND invs.invoice_id = rem.invoice_id AND invs.session = '4' 
            AND NOT EXISTS( SELECT sch.invoice_id FROM school_fee_payments_nce sch 
            WHERE invs.invoice_id = sch.invoice_id AND session = 4)"; */
            
            
    	$qall = "SELECT DISTINCT
                (rem.transaction_reference) AS transaction_reference
                FROM
                    remita_webpay_transaction_log rem,
                    invoices invs
                WHERE
                rem.transaction_type = '1' AND rem.transaction_for = '2' 
                AND invs.invoice_id = rem.invoice_id AND invs.session = '4' 
                AND rem.`date_added` BETWEEN '2019-01-08 00:00:00.000000' AND '2019-01-09 00:00:00.000000' "; 
		  
	$rall = mysqli_query($dbc,$qall);
	

	if(mysqli_num_rows($rall) > 0)
	{
	
		while($row = mysqli_fetch_array($rall))
		{
		    $response_code = '';
		    $rrr = '';
			$orderRef = $row['transaction_reference'];
			//Confirm transaction Status to be sure it is coming from Remita
			$response =  remita_transaction_details($orderRef);
			$response_code = $response['status'];
			$response_reason = $response['message'];
			$rrr = $response['RRR'];
		    $orderId =$response['orderId'];
		  
		    $iid = get_invoice_id_using_txnref($orderId);
		    
			if($response_code == '01' || $response_code == '00' )
			{

					//Payment Successful, You can Update Status to Paid here on Database;
							
					$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_type  = get_invoice_type($iid);//get invoice type
					 $invoice_to = get_invoice_invoice_to($iid);
			
			 
			 
				  //collect matric number an dother student info
				  $appno = get_user_application_number($invoice_to);
				 
				  $stud_id = get_student_id_application_number($appno, $level = 'I');
			
					$pay_count++;
				
				if( $stud_id != ''){
				    echo  $sn.'----'.$orderId.'----'.$stud_id.'----'.$response_code.'<br>';
				}else{
				    echo  $sn.'----'.$orderId.'----'.$appno.'----'.$response_code.'<br>';
				}
				
					$sn++;
				
						
					
						
								
			}//end of if response code is successull
			else{
			    
			    			
					$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_type  = get_invoice_type($iid);//get invoice type
					 $invoice_to = get_invoice_invoice_to($iid);
			     $dt .=  $sn1.'----NOT----'.$orderId.'----'.$appno.'----'.$response_code.'<br>';
			      $sn1++;
			}
			

		}
		
		if ($pay_count != '') {
		    echo 'Successful payment = '.$pay_count;
		    echo  '<br>'.$dt;
		}else{
			echo 'None';
		}	
	}else
	{
		echo 'no records';
	}

		




function remita_transaction_details($orderId){
	$mert =  "2815618357";//2815618357
	$api_key = "527461";//527461
	$mode = "Live";
	$hash_string = $orderId . $api_key . $mert;
	$hash = hash('sha512', $hash_string);
	if( $mode == 'Test' ){
		$query_url = 'http://www.remitademo.net/remita/ecomm';
		}
	else if( $mode == 'Live' ){
		$query_url = 'https://login.remita.net/remita/ecomm';
		}
	$url 	= $query_url . '/' . $mert  . '/' . $orderId . '/' . $hash . '/' . 'orderstatus.reg';
	$result = file_get_contents($url);
    $response = json_decode($result, true);
    return $response;
}
?>