<?php
// var_dump($_POST);
// exit;
    
session_start();
$userID=$_SESSION["userID"];
if(empty($userID)){
  $error="You have no permission";
  header("location:login.php");
}

if(isset($_POST)){
    include("config.php");

    $fullname=mysqli_escape_string($conn,$_POST["fullname"]);
    $proprefID=mysqli_escape_string($conn,$_POST["proprefID"]);
    $amount=mysqli_escape_string($conn,$_POST["amount"]);
    $telco=mysqli_escape_string($conn,$_POST["telco"]);
    $phone=mysqli_escape_string($conn,$_POST["phone"]);


        $transdate=date("Y-m-d");
  // String of all alphanumeric character
$str_result = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$trandate1=date("Ymd");

  $token= substr(str_shuffle($str_result), 0, 6);

$paynow=5;
$user=30;
$GRArevenue=$amount-(($amount*$paynow)/100); //23.75
$paynowAmnt=$amount-$GRArevenue; //1.25
$userCom=($paynowAmnt*$user)/100; //0.375
$paynowCom=$paynowAmnt-$userCom; //0.875


        $query2="INSERT INTO transactions SET paynowCom='$paynowCom',userCom='$userCom',userID='$userID',propRefID='$proprefID',fullName='$fullname',amount='$amount',telco='$telco',phone='$phone',token='$token',date_created='$transdate'";
        $data2=mysqli_query($conn,$query2);
        
        
        /////////debit momo
        


$query7="SELECT * FROM transactions where token='$token'";
$data7=mysqli_query($conn,$query7);
while($row7=mysqli_fetch_assoc($data7)){
  $amount_sender=number_format($row7["amount"],2);
  $network=$row7["telco"];
  $senderID=$row7["phone"];
  $userID2=$row7["userID"];
 
}
$email="info@paynowgh.com";


if($network=="MTN"){
  $channel="mtn-gh";
  }
  if($network=="Telecel"){
  $channel="vodafone-gh";
  }
  if($network=="Airteltigo"){
  $channel="tigo-gh";
  }

///////Debit hubtel api /// sender must authorize and pay for transaction

$code2="233";
$number_sneder="$code2$senderID";

///// start engaging momo api
$phoneNumber="$number_sneder";
// $channel="vodafone-gh";
// $equiAmount=$amount_sender;
$equiAmount=0.1;
// $token=rand(20000000,999999999);

$payload = array( "CustomerName" => "$fullname",
"CustomerMsisdn" => "$phoneNumber",
"CustomerEmail" => "$email",
"Channel" => "$channel",
"Amount" => "$equiAmount",
"PrimaryCallbackUrl" => "https://mobilemoneycad.net/callback_momoCAD.php",
"Description" => "Deposit",
"ClientReference" => "$token");

$payload_json=json_encode($payload);

$user_name = "qQ8My7G";
$password = "4daa91de2db94928824c19392030ca63";
$auth= "$user_name:$password";
$auth_64 = base64_encode($auth);
// print($auth_64); 144.217.113.138
// exit;

$url = "https://rmp.hubtel.com/merchantaccount/merchants/2017419/receive/mobilemoney/";
// $url = "https://webhook.site/1b95b469-2b78-4524-b158-646e77ceefed";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept:application/json","Content-Type:application/json","Authorization:Basic cVE4TXk3Rzo0ZGFhOTFkZTJkYjk0OTI4ODI0YzE5MzkyMDMwY2E2Mw==","Cache-Control:no-cache"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload_json);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);

// echo $content;
// exit;

sleep(30);
$error="Success";
// header("location:momo_api.php?error=$error&token=$token");
header("location:status_api.php?error=$error&token=$token");

exit;

    }


else{
    $error="You have no right to be here";
    header("location:payProp_rate.php?message=$error");
    exit;
}





?>
