<?php
// require_once __DIR__ . "/vendor/autoload.php";
function EGetData($data,$PayID){
  global $dbo;
  $dataarr = $dbo->DataArray($data,"`",$keyItem = "~");
  if(isset($dataarr[''.$PayID]))return $dataarr[''.$PayID];
  if(isset($dataarr['-1']))return $dataarr['-1'];
  return "";
}

function EInit($param){
 
  global $dbo;
  $transnums = generateTransInfoOnly();
  return ["Ref"=>$transnums[1]];



}

function ICurlPost($url,$data){
  $datastr = http_build_query($data);
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_POST, count($data));
  curl_setopt($ch, CURLOPT_POSTFIELDS, $datastr);
  // Disable SSL verification
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  // Will return the response, if false it print the response
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  // Set the url
  curl_setopt($ch, CURLOPT_URL, $url);
  // Execute
  $rst = curl_exec($ch);
  //return ["Status"=>0,"DateTime"=>$params['RegDate'],"Ref"=>$params['Ref'],"Amt"=>$params['Amt'],"Message"=>$rst];			
  $rst = rtrim($rst, "</html>");
  $rst = trim($rst);
  curl_close($ch);

  return $rst;
}

function InitPayment($param = [])
{
  return ["Markup"=>__DIR__ . "/vendor/autoload.php","Data"=>__DIR__ . "/vendor/autoload.php","Url"=>""];

  $data = [];
  $email = $data['email'] = $param['Email'];
  $accountCode = $data['accountCode'] = $param["ACCOUNTCODE"];
  $secretKey = $param['SECRETKEY'];
  $phone = $data['phone'] = $param['Phone'];
  $amount = $data['amount'] = $param['Amt'];
  $ref = $data['ref'] = $param['Ref'];
  $callbackUrl = $data['callbackUrl'] = $param['CALLBACKURL'];
  $checkSum = $data['checkSum'] = md5($email.$ref.$accountCode.$amount.$callbackUrl.$secretKey);
  $initUrl = $param['GATEWAYINITPAYMENTURL'];
  //print_r($param);exit();
  // die(json_encode($data));
  $curl = ICurlPost($initUrl, $data);
   //print_r($param);
   //die($curl);

  $res = json_decode($curl, true);
  if ($res['status'] == false) {
    return ["Error"=>$res['message']];
  }

  // $mk = $res['data']['payUrl'];

  $mk = '<script language="javascript">window.location = "'.$res['data']['payUrl'].'"</script>';
  return ["Markup"=>$mk,"Data"=>$data,"Url"=>$initUrl];
  
}

function FinishPayment($param = [])
{
//   exit(json_encode($param));
  return [1,$param['Ref'],"Successful",0,["DateTime"=>date("Y-m-d")]];
}

function RequeryPayment($param = [])
{
  $data = [];

  $ref = $data['ref'] = $param['REF'];
  $accountCode = $data['accountCode'] = EGetData($param["ACCOUNTCODE"],$param["PayID"]);
  $secretKey = $data['secretKey'] = $param['SECRETKEY'];
  $checkSum = $data['checkSum'] = md5($ref.$accountCode.$secretKey);
  $requeryUrl = $data['requeryUrl'] = $param['GATEWAYREQUERYURL'];

  $form =  "<form method='POST' action='".$requeryUrl."'>
  <input type='hidden' name = 'accountCode' value='".$accountCode."'>
  <input type='hidden' name = 'ref' value='".$ref."'>
  <input type='hidden' name = 'checkSum' value='".$checkSum."'>
  </form>
  <script language='javascript'>
  var form = document.forms[0];
  form.submit()</script>";

  return ["Markup"=>$form,"Data"=>$data,"Url"=>$requeryUrl];
}

?>