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


include("config.php");

$transdate=date("Y-m-d");


$query="SELECT sum(userCom) as totalUcom FROM transactions where userID='$userID' and status='pending'";
$data=mysqli_query($conn,$query);
while($row=mysqli_fetch_assoc($data)){
  $totalUcom=$row["totalUcom"];
}


$query1="SELECT * FROM users where userID='$userID' and grplevel='200'";
$data1=mysqli_query($conn,$query1);
while($row1=mysqli_fetch_assoc($data1)){
    $fname=$row1["fname"];
    $lname=$row1["lname"];
    $phone=$row1["phone"];
    $email=$row1["email"];   
    $telco=$row1["telco"];
}
$fullName="$fname $lname";


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

///////Send money hubtel api /// sending money to customer

$recipient="MCI";

// $number2="$recipientID";
$code2="233";
$rest2 = substr("$phone", 1);
$number_sneder="$code2$rest2";
 
    
    // replace with equivaleng amount $recipient_amount
// $equiAmount1=$recipient_amount;
$payload = array( "RecipientName" => "$recipient",
"RecipientMsisdn" => "$number_sneder",
"CustomerEmail" => "info@mobilemoneycad.net",
"Channel" => "$channel",
"Amount" => $totalUcom,
"PrimaryCallbackUrl" => "https://mobilemoneycad.net/callback_momoCAD.php",
"Description" => "Withdrawal",
"ClientReference" => "$token");

$payload_json=json_encode($payload);

// echo $payload_json;
// exit;


$user_name = "qQ8My7G";
$password = "4daa91de2db94928824c19392030ca63";
$auth= "$user_name:$password";
$auth_64 = base64_encode($auth);
// $url = "https://webhook.site/5177a04b-f436-4194-b690-532ec6b9f41c";
$url = "https://smp.hubtel.com/api/merchants/2017123/send/mobilemoney/";
$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);
$response = curl_exec($ch);

curl_close($ch);

// echo $response;
// exit;


/////send sms

if($data1){
    $token=mt_rand(100000,999999);
    $recipient="MCI";
    
    // $number2="$recipientID";
    $code2="233";
    $rest2 = substr("$phone", 1);
    $number_sneder="$code2$rest2";
    $recipient_amount=0.10;
        // replace with equivaleng amount $recipient_amount
    // $equiAmount1=$recipient_amount;
    $payload = array( "RecipientName" => "$recipient",
    "RecipientMsisdn" => "$number_sneder",
    "CustomerEmail" => "info@mobilemoneycad.net",
    "Channel" => "$channel",
    "Amount" => $recipient_amount,
    "PrimaryCallbackUrl" => "https://mobilemoneycad.net/callback_momoCAD.php",
    "Description" => "Withdrawal",
    "ClientReference" => "$token");
    
    $payload_json=json_encode($payload);
    
    // echo $payload_json;
    // exit;
    
    
    $user_name = "qQ8My7G";
    $password = "4daa91de2db94928824c19392030ca63";
    $auth= "$user_name:$password";
    $auth_64 = base64_encode($auth);
    // $url = "https://webhook.site/5177a04b-f436-4194-b690-532ec6b9f41c";
    $url = "https://smp.hubtel.com/api/merchants/2017123/send/mobilemoney/";
    $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);
    $response = curl_exec($ch);
    
    curl_close($ch);
    
  
    $number="$phone";
    $code="233";
    $rest = substr("$number", 1);
    $number_recipient="$code$rest";
    $text="Hello $fullName your total earnings of [Ghc $totalUcom] has been sent to you.Keep up the good work.";
    $from="momoCAD";
    $sender2="MCI";
    
    // SEND SMS
    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => "https://sms.hubtel.com/v1/messages/send?clientsecret=kzgcubkj&clientid=ityrcnbz&from=CAD&to=$number_recipient&content=$text",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 10,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'GET', ));
    $response = curl_exec($curl);
    curl_close($curl);
    // echo $response;



    $query5="UPDATE transactions set status='paid' where userID='$userID'";
    $data5=mysqli_query($conn,$query5);

    $error="Earnings paid. Thank you";
    header("location:earnings.php?message=$error");
    exit;
      } 


?>
