<?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")
			  {
				$status    = "OK"; 				  
				$form_data = array(
								  'date' =>  $_POST["transaction_date"],
								  'amount' => $_POST["transaction_amount"],
								  'type' => $_POST["transaction_type"],
								  'method' => $_POST["transaction_method"],
								  'name' => $_POST["transaction_name"],
								  'mobile' => $_POST["transaction_mobile"],
								  'email' => $_POST["transaction_email"],
								  'paybill' => $_POST["transaction_paybill"],
								  'account_number' => $_POST["transaction_account_number"],
								  'account_name' => $_POST["transaction_account_name"],
								  'reference' => $_POST["transaction_reference"],            
								  'status' => $status 							  
								  );          
				  
				  // Perform action e.g update order/invoice as paid, save to database or log
				  $status    = "OK";
				  //process_payment($_POST, $status); // replace with your own actions
				  $this->main_model->insert_data('lipisha_payments', $form_data);
	  
				  // 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"]         = "Your order is paid for! Mpesa " . $code . " confirmed."; // Customize SMS. *Premium 
				  $json_response                              = json_encode($response);
				 // header("Content-Type: application/json"); 
				  echo $json_response; // This should be the only printed output of this page 
									   // to prevent header session already sent error occuring              
			  }
			  // Process Acknowledge
			  if($_POST["api_type"] == "Acknowledge")
			  {
				  // Perform action e.g update your transaction table as confirmed
				  $status    = "Acknowledged"; 
				  //process_payment($_POST, $status);  // replace with your own actions                     
			  }
		  }
		  else 
		  {    
			  // Log attempted fraud if api credentials are wrong
			  $status    = "Fraud"; 
			  $date = date('Y-m-d H:i:s');
			  $form_data = array(
									'date' => $date,
									'amount' => 500,
									'type' => "Test",
									'method' => "Test",
									'name' => "Test",
									'mobile' => "1234",
									'email' => "Test",
									'paybill' => "1234",
									'account_number' => "1234",
									'account_name' => "Test",
									'reference' => "Test",
									'merchant_reference' => "1234",            
									'code' => "Test",            
									'status' => $status  							  
									);           
				  
				  $this->main_model->insert_data('lipisha_payments', $form_data);
			  
			      
			  //process_payment($_POST, $status); // replace with your own actions
			  //print_r($_POST);
//			  echo "<hr>";
			  print_r($form_data); 
			  echo "<hr>";
			  $json_data = json_encode($form_data);
			  print_r($json_data); 
		  }           
	  }
		
		
	}
	

	
}

?>