<?php
include 'remita_constants.php';
include 'includes/functions.php';
$json = file_get_contents('php://input');
$arr=json_decode($json,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);
						$response_code = $response['status'];
						$response_reason = $response['message'];
						$rrr = $response['RRR'];
					    echo $orderId = $response['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
							echo $iid = get_invoice_id_using_txnref($orderId);
							
							
							
							
							
							
							
							
							
							
							
						}//end of if response code is successull
					}//end of for each
				exit('OK');
			}
		
		}
		catch (Exception $e) {
			exit('Not OK');
		}
?>		
		
<?php 
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/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;
}
?>