<?php
//////////////////////////////////////
header("Access-Control-Allow-Origin: *");
include_once('OAuth.php');
//////////// SAAS ENV ////////////////////////////////////////////////////
require_once("class.restframework.php");
require_once("class.saas.php");
include_once('class.pesapal.php');
////////////////////////////////////////////////////////////////////////
$restapi = new RestFrameWork();
// only allow get method
$saas = new SaaS($restapi);
$auth = $saas->_initAuth();
////////////////////////////////////////////////////////////////////////
$kpesapal = new Pesapal($auth, $restapi);
$settings = $kpesapal->settings;
////////////////////////////////////////////////////////////////////////////////////
$consumer_key = $settings["PESAPAL_CONSUMER_KEY"]["entry_value"]; // Register a merchant account on
// demo.pesapal.com and use the merchant key for testing.
// When you are ready to go live make sure you change the key to the live account
// registered on www.pesapal.com!
$consumer_secret = $settings["PESAPAL_CONSUMER_SECRET"]["entry_value"]; // Use the secret from your test
// account on demo.pesapal.com. When you are ready to go live make sure you 
// change the secret to the live account registered on www.pesapal.com!
$statusrequestAPI = $settings["PESAPAL_API_URL"]["entry_value"]; //change to      
// https://www.pesapal.com/api/querypaymentstatus' when you are ready to go live!
// Parameters sent to you by PesaPal IPN
$pesapalNotification = $_GET['pesapal_notification_type'];
$pesapalTrackingId = $_GET['pesapal_transaction_tracking_id'];
$pesapal_merchant_reference = $_GET['pesapal_merchant_reference'];
//////////////////////////////////////////////////////////////////////////
if ($pesapalNotification == "CHANGE" && $pesapalTrackingId != '') {
   $token = $params = NULL;
   $consumer = new OAuthConsumer($consumer_key, $consumer_secret);
   $signature_method = new OAuthSignatureMethod_HMAC_SHA1();
   //get transaction status
   $request_status = OAuthRequest::from_consumer_and_token($consumer, $token, "GET", $statusrequestAPI, $params);
   $request_status->set_parameter("pesapal_merchant_reference", $pesapal_merchant_reference);
   $request_status->set_parameter("pesapal_transaction_tracking_id", $pesapalTrackingId);
   $request_status->sign_request($signature_method, $consumer, $token);
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $request_status);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
   if (defined('CURL_PROXY_REQUIRED')) {
      $proxy_tunnel_flag = (defined('CURL_PROXY_TUNNEL_FLAG') && strtoupper(CURL_PROXY_TUNNEL_FLAG) == 'FALSE') ? false : true;
      curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, $proxy_tunnel_flag);
      curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
      curl_setopt($ch, CURLOPT_PROXY, CURL_PROXY_SERVER_DETAILS);
   }
   $response = curl_exec($ch);
   $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
   $raw_header  = substr($response, 0, $header_size - 4);
   $headerArray = explode("\r\n\r\n", $raw_header);
   $header      = $headerArray[count($headerArray) - 1];
   //transaction status
   $elements = preg_split("/=/", substr($response, $header_size));
   $status = $elements[1];
   curl_close($ch);
   //    //UPDATE YOUR DB TABLE WITH NEW STATUS FOR TRANSACTION WITH pesapal_transaction_tracking_id $pesapalTrackingId
   $DB_UPDATE_IS_SUCCESSFUL = $kpesapal->UpdateTransation($pesapalTrackingId);
   if ($DB_UPDATE_IS_SUCCESSFUL) {
      $resp = "pesapal_notification_type=$pesapalNotification&pesapal_transaction_tracking_id=$pesapalTrackingId&pesapal_merchant_reference=$pesapal_merchant_reference";
      ob_start();
      echo $resp;
      ob_flush();
      // exit;
   }
}
