<?php
//$host = '127.0.0.1';//'localhost';
//$user = 'itimetable2_rit-Admin';
//$pass = 'Kiwide4116*';
//$db = 'itimetable2_i_table';

$host = '127.0.0.1';
$user = 'stmulumba_sadmin';
$pass = 'Kiwide4116*RiT';
$db = 'stmulumba_ichurch';

$conn = mysqli_connect($host, $user, $pass, $db);

$isAPI=''; $isSenda=''; $isAPIrst='';
$QAva = "SELECT * FROM a_college";
$RAva = mysqli_query($conn, $QAva);
if (mysqli_num_rows($RAva) > 0) {
    if ($row = mysqli_fetch_object($RAva)) {

        $BNm     = $row->CName;
        $isAPI   = $row->API;
        $isSenda = $row->isSender;

        // If RestAPI is NULL or empty, use API
        $isAPIrst = (!empty($row->RestAPI)) ? $row->RestAPI : $row->API;
    }
}


$timezone = '+4:00';
$timezone = preg_replace('/[^0-9]/', '', $timezone) * 36;
$timezone_name = timezone_name_from_abbr(null, $timezone, true);
date_default_timezone_set($timezone_name);
$dt = date('Y-m-d');

    //Get input stream data and log it in a file
    $payload = file_get_contents('php://input');
    
        $callbackData=json_decode($payload);
        
        $transactionType=$callbackData->TransactionType;
        $transID=$callbackData->TransID;
        $TransTime=$callbackData->TransTime;
        $TransAmount=$callbackData->TransAmount;
        $SKod=$callbackData->BusinessShortCode;
        $ThirdP=$callbackData->ThirdPartyTransID;
        $MSISDN=$callbackData->MSISDN;
        $FirstName=$callbackData->FirstName;
        //$MiddleName=$callbackData->MiddleName;
        //$LastName=$callbackData->LastName;
        
        $FirstName = str_replace("'", "`", $FirstName);
        //$LastName = str_replace("'", "`", $LastName);
        
        $BRef=$callbackData->BillRefNumber; 
        
         /* $sql = "INSERT INTO payment_mpesa ".
	   "(TransactionType,TransID,TransTime,TransAmount,BusinessShortCode,BillRefNumber,ThirdPartyTransID,MSISDN,FirstName,SaveDate) "."VALUES ".
	   "('$transactionType','$transID','$TransTime','$TransAmount','$SKod','$BRef','$ThirdP','$MSISDN','$FirstName','$dt')";
	    $conn->query($sql); */
		
		$MSISDN="2931438ffe4906206caea642f4b44fd2acdf80cdc852520f56059290f4aee21e";
		
		$api_key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJib25uaWVuanVndW5hMTA2QGdtYWlsLmNvbSIsImV4cCI6MTc5NTAwOTExOSwibmJmIjoxNzY5MDg5MTE5fQ.sar4_hCCEXZod4olfT5Tk2NuT607FZ3GNbo8p9O2Ub8";
		$url = "https://decodehash.com/app/api/v1/decode-hash/freemium/" . $MSISDN;
		
		// Make the API request
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			"Authorization: Bearer " . $api_key,
			"Accept: application/json"
		));
		
		$response = curl_exec($ch);
		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		
		// Parse the response
		$data = json_decode($response, true);
		
		// Display the result
		$num = $data['msisdn'];
		
		$Jumbe="Shalom $FirstName, with gratitude we have received your tithe of Ksh $TransAmount. May the Lord open the windows of heaven for you and pour down overflowing blessings. Fr. in-charge";
		
		$url = "https://api2.uwaziimobile.com/send"; 
		$data = array( 
			'token' => $isAPI,
			'phone' => $num,
			'senderID' => $isSenda,
			'text' => $Jumbe,
			'type' => 'sms',
			'delivery' => 'TRUE'
		);

		$url = 'https://api2.uwaziimobile.com/send?' . http_build_query($data);

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('accept: */*'));

		$response = curl_exec($ch);
		$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		
		
		
        
        
       // $queryin = "INSERT INTO payment_voi_cbd (TransactionType,TransID,TransTime,TransAmount,BusinessShortCode,ThirdPartyTransID,MSISDN,FirstName,SaveDate) VALUES ('$transactionType','$transID','$TransTime','$TransAmount','$SKod','$ThirdP','$MSISDN','$FirstName','$dt')"; 
        //$conn->query($queryin);



    $file = 'confirm_log.txt'; //Please make sure that this file exists and is writable
    ///$fh = fopen($file, 'a'); review purpose
    $fh = fopen($file, 'w');
    fwrite($fh, "\n====".date("d-m-Y H:i:s")."====\n");
    fwrite($fh, $payload."\n");
    fclose($fh);
    
    header("Content-type: application/json; charset=utf-8");

 $response = ' {"ResultCode": 0,"ResultDesc":"Accepted"} ';
    //$response->contentType('application/json');
   // echo json_encode($response);
    echo ($response);
    

    
////}



////else{
//    echo "Authentication failed";

////////////////header("Content-type: application/json; charset=utf-8");
//////////// $response = ' {"ResultCode": 0,"ResultDesc":"Accepted"} ';
////////    echo ($response);
    
////}
?>