<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Lipisha_callback extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
	}
	
	//this is the callback function by Lipisha API after a payment has been made
	//replaced function name: receive_payment_notification
	function index()
	{
		// Check if Lipisha IPN server has made a callback
	  if(isset($_POST)) 
	  {
		  // Your API details for authentication
		  $api_key       = "7c487439a94da7437bcc0122735efbc8";        // # Check under Settings->API in dashboard
		  $api_signature = "1h2v9z0VNF0Ku/cc6rjUJe41OgcsrFXBrELw/yeebT56+LNB+yGgYPKnk+zytyI1zrLvCpPf4SOi/M8wcqnF+a8m0T7Sfs4U5RodXykp5CNozKZrMPK3iephwKg6XOShMR/h24O4U3fx+JoIUxugdmVs1cW+dFVXfvc4oKRqF2g=";  // # Check under Settings->API in dashboard
	  
		  // Confirm if authentication details are genuine else log a fraud attempt
		  if (($_POST["api_key"] == $api_key) && ($_POST["api_signature"] == $api_signature)) 
		  {

			// Process Initiate
			if($_POST["api_type"] == "Initiate")
			{
				
					// Perform action e.g save data to database or log file
					$myFile = "transaction.log";
					$fh = fopen($myFile, 'a') or die("can't open file");
					fwrite($fh, "\r\n============= Initiate ================\r\n");
					fwrite($fh, "Logged at " . date("Y-m-d H:i:s") . "\r\n");

					foreach ($_POST as $key => $value) 
					{
						fwrite($fh, "$key = $value\r\n");
					}
					fclose($fh);		
					
					// Acknowledge API call
					$response= array();
					$response["api_key"] = $_POST["api_key"];
					$response["api_signature"] = $_POST["api_signature"];
					$response["api_version"] = $_POST["api_version"];
					$response["api_type"] = "Receipt";
					$response["transaction_reference"] = $_POST["transaction_reference"];
					$response["transaction_status_code"] = "001";
					$response["transaction_status"] = "Success";
					$response["transaction_status_description"] = "Transaction received successfully.";
					$response["transaction_custom_sms"] = "Payout completed. Asante sana sand box!"; // Send a custom sms

					$myFile = "lipisha.log";
					$fh = fopen($myFile, 'a') or die("can't open file");
					fwrite($fh, "\r\n============= Response ================\r\n");
					fwrite($fh, "Logged at " . date("Y-m-d H:i:s") . "\r\n");

					foreach ($response as $key => $value) 
					{
						fwrite($fh, "$key = $value\r\n");
					}
					fclose($fh);

					$json_response = json_encode($response);
					header("Content-Type: application/json"); 
					echo $json_response; // This should be the only printed out put of this page else session already sent error will occur				
			}
			
			// Process Acknowledge
			if($_POST["api_type"] == "Acknowledge")
			{
				// Perform action e.g update data to database or log file
				$myFile = "transaction.log";
				$fh = fopen($myFile, 'a') or die("can't open file");
				fwrite($fh, "\r\n============= Acknowledge ================\r\n");
				fwrite($fh, "Logged at " . date("Y-m-d H:i:s") . "\r\n");

				foreach ($_POST as $key => $value) 
				{
					fwrite($fh, "$key = $value\r\n");
				}
				fclose($fh);						
			}
			
		}
		  else 
		  {
			// Log attempted fraud or hacking
			$myFile = "fraud_log.txt";
			$fh = fopen($myFile, 'a') or die("can't open file");
			fwrite($fh, "\r\n============= Possible Fraud ================\r\n");
			fwrite($fh, "Logged at " . date("Y-m-d H:i:s") . "\r\n");

			foreach ($_POST as $key => $value) 
			{
				fwrite($fh, "$key = $value\r\n");
			}
			fclose($fh);
		}           
	  }
		
		
	}
	

	
}

?>