<?php
session_start();
error_reporting(0);
require_once("db.php");
$logPath = "log/kenya/safaricom/send_payment_".date("Ymd").".txt";
error_log("called url: ".date('Ymd His').": ".$_SERVER['REQUEST_URI']."\n", 3, $logPath);

$lastid = $_GET['ClientTransactionId'];
//$lastid = $_GET['lastid'];
//$lastidd = date("ymdHisu");
$msisdn = $_GET['msisdn'];
sleep(3);

error_log(date('Ymd His')." lastid=$lastid\n", 3, $logPath);
//error_log(date('Ymd His')." access_token=$access_token\n", 3, $logPath);
/////////////////////////////////////////////////////////////////////////

$url1="http://15.206.36.3/gamingbox/send_callback_request_sckesecured.php?msisdn=$msisdn&lastid=$lastid";
		error_log("Called url=".date('Ymd His')."".$url1."\n", 3, $logPath);
		$ch = curl_init();
				curl_setopt($ch,CURLOPT_URL,$url1);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
				$output=curl_exec($ch);
				curl_close($ch);
			error_log("Called url output=".date('Ymd His')."".$output."\n", 3, $logPath);
					
					error_log("final view:".date('Ymd His').": op we recieve $output \n", 3, $logPath);	




////////////////////////////////////////////////////////////////////////
$qry = "select token from tbl_access_token_gamingbox_enscke where token!='' order by id desc limit 1";
			error_log(date('Ymd His').":qry= $qry \n", 3, $logPath);
	$TotalActiveBaseResult=mysqli_query($con1,$qry);
	while($mis_array=mysqli_fetch_assoc($TotalActiveBaseResult)) {
		$access_token = $mis_array['token'];
	}
error_log(date('Ymd His')." access_token=$access_token\n", 3, $logPath);
////////////////////////////
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/

/*

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://hesdp.safaricom.com:1212/api/v1/charge');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n\t\"msisdn\":\"$msisdn\",\n\t\"offerCode\":\"001023834398\",\n\t\"CpId\":\"238\",\n    \"callBackUrl\": \"http://139.59.3.239:8080/lkgetme/cookery/smartfren/kenya\",\n    \"ChargeAmount\": \"15.00\"\n}");

$headers = array();
$headers[] = 'X-Api-Auth-Token: Bearer  '.$access_token.'';
$headers[] = 'Accept-Encoding: application/json';
$headers[] = 'Accept-Language: EN';
$headers[] = 'Content-Type: application/json';
$headers[] = 'X-App: ussd';
$headers[] = 'X-Correlation-Conversation-Id: '.$lastid.'';
$headers[] = 'X-Messageid: '.$lastid.'';
$headers[] = 'X-Source-Division: DIT';
$headers[] = 'X-Source-Countrycode: KE';
$headers[] = 'X-Source-Operator: Safaricom';
$headers[] = 'X-Source-System: web-portal';
$headers[] = 'X-Source-Timestamp: '.$lastid.'';
$headers[] = 'X-Version: 1.0.0';
$headers[] = 'Authorization: Basic ZGV2X3VzZXI6ZGV2X3Bhc3N3b3Jk';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
error_log(date('Ymd His').": headers =" .$headers."\n", 3, $logPath);
$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
error_log(" Response:".date('Ymd His').": " .$result."\n", 3, $logPath);

$data = json_decode($result,false);
$arr = (object) $data;
$status =$arr->body->status;
$statusCode = $arr->body->statusCode;
$description = $arr->body->description;
echo "$description";

*/



/////////////////////////////////////////////////////////////////////////////////////////////////////////////

$ch = curl_init();
//curl_setopt($ch, CURLOPT_URL, 'https://dtsvc.safaricom.com:8480/api/public/SDP/activate');
curl_setopt($ch, CURLOPT_URL, 'https://dsvc.safaricom.com:9480/api/public/SDP/paymentRequest');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n  \"requestId\": \"$lastid\",\n  \"channel\": \"APIGW\",\n  \"operation\": \"ACTIVATE\",\n  \"requestParam\": {\n    \"data\": [\n      {\n        \"name\": \"OfferCode\",\n        \"value\": \"001034001290\"\n      },\n      {\n        \"name\": \"Msisdn\",\n        \"value\": \"$msisdn\"\n      },\n      {\n        \"name\": \"CpId\",\n        \"value\": \"340\"\n      }\n    ]\n  }\n}");
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n  \"requestId\": \"$lastid\",\n  \"channel\": \"APIGW\",\n  \"operation\": \"Payment\",\n  \"requestParam\": {\n    \"data\": [\n      {\n        \"name\": \"OfferCode\",\n        \"value\": \"001023835437\"\n      },\n      {\n        \"name\": \"Msisdn\",\n        \"value\": \"$msisdn\"\n      },\n      {\n        \"name\": \"ChargeAmount\",\n        \"value\": \"15.00\"\n      },\n      {\n        \"name\": \"CpId\",\n        \"value\": \"238\"\n      }\n    ]\n  }\n}");

$headers = array();
$headers[] = 'Accept: */*';
$headers[] = 'Authorization: Basic ZGV2X3VzZXI6ZGV2X3Bhc3N3b3Jk';
//$headers[] = 'Content-Length: 248';
$headers[] = 'Content-Type: application/json';
//$headers[] = 'Sourceaddress:  '.$ip.'';
//$headers[] = 'User-Agent:  '.$UserAgent.'';
$headers[] = 'X-Authorization: Bearer  '.$access_token.'';
$headers[] = 'X-Requested-With: XMLHttpRequest';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

error_log(date('Ymd His').": headers =" .$headers."\n", 3, $logPath);
error_log(date('Ymd His') . ": headers = " . var_export($headers, true) . "\n", 3, $logPath);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

error_log(" ch:".date('Ymd His').": " .$ch."\n", 3, $logPath);
curl_close($ch);


error_log(" Response:".date('Ymd His').": " .$result."\n", 3, $logPath);

$data = json_decode($result,false);
$arr = (object) $data;
//echo $arr->requestId;
//echo $arr->responseParam->status;
//echo $arr->responseParam->statusCode;
$status =$arr->responseParam->status;
$statusCode = $arr->responseParam->statusCode;
$description = $arr->responseParam->description;

echo $description;







///////////////////////////////////////////////////////////////////////////////////////////////////////////////










?>

