<?php
//include "../../../config/connection.php";
//Array ( [message] => SUCCESS [balance] => 393354.7989 [local-trxn-code] => 59629038852110210728 [status-code] => 0 [trxn] => 4778545666 [status] => OK )
//include "../../config/init.php";
function buy_airtime($phoneNumber,$amount,$network,$transaction_uid)
{
    $myStatus = 'open';
   if($myStatus == 'open'){
       $url = "https://tpp.one4all.com.gh/TPP/TopUpApi?";
       $data = array();
       $info = "username=FUDX9ZHCCMOFZBG0BSPK&pin=V5ORQH5DML9YQHILTWAQ&service=001&retailer=FUDX9ZHCCMOFZBG0BSPK&recipient=" . $phoneNumber . "&amount=" . $amount . "&network=" . $network . "&trxn=" . $transaction_uid;
       $main_url = $url.$info;

       $ch = curl_init($main_url); // Set url to query

       curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); // Send via POST
       curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Set POST data
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response text
       curl_setopt($ch, CURLOPT_HEADER, "Content-Type: application/json"); // send POST data as form data

       $response = curl_exec($ch);


       curl_close($ch);
       $res = json_decode($response,true);

       return $res;
   } else{
       $res = array();
       return $res;
   }





}

//test
/*$senderData = array();
$transitData = array();
$senderData['id'] = "7854iuesgd";
$senderData['countryCode'] = 'gh';
$transitData['airtimePhoneNum'] = "0540431070";
$transitData['network'] = 4;
$transitData['amount'] = 1;

if(airtime_transaction_ussd($dbc,$senderData,$transitData)){
    echo "good";
}else{
    echo "bad";
}*/


function airtime_transaction_ussd($dbc,$senderData,$transitData,$transaction_uid,$airtimeData)
{

    $buyAirtime = buy_airtime($transitData['airtimePhoneNum'],$transitData['amount'],$transitData['network'],$transaction_uid);
    if($buyAirtime['status'] == "OK"){
        if(add_airtime_transaction($dbc,$transaction_uid,$senderData,$transitData,$buyAirtime['balance'],$airtimeData)){
            return true;


        } else{
            return false;
        }
    } else {
        return false;
    }

}

function add_airtime_transaction($dbc,$transaction_uid,$senderData,$transitData,$balance,$airtimeData){
    /*$q = mysqli_query($dbc,"INSERT INTO airTimeTransaction_tbl(id, senderID, phoneNumber, transaction_uid, amount, country,network,balance) VALUES
            (UUID(),'$senderData[id]','$transitData[airtimePhoneNum]','$transaction_uid','$transitData[amount]','$senderData[countryCode]','$transitData[network]','$balance')
        ");

    if($q){
        return true;
    }*/


    $q = mysqli_query($dbc,"INSERT INTO maintransaction_tbl(id,senderID,recipientID,airtimeNumber,transaction_uid,sendersAmount,recipientAmount,senderCountryCode,recipientCountryCode,airtimeChannel,airtimeTotalBalance,transactionTypes,transactionStatus,deviceType) VALUES 
      (UUID(),'$senderData[id]','$airtimeData[id]','$transitData[airtimePhoneNum]','$transaction_uid','$transitData[amount]','$transitData[amount]','$senderData[countryCode]','$senderData[countryCode]','$transitData[network]','$balance','AIRTIME','COMPLETED','USSD')
    ");

    if($q){
        return true;
    }
}

function update_airtime_transactions_balance($dbc,$transaction_uid,$customerBalance,$airtimeBalance){
    $q = mysqli_query($dbc,"UPDATE maintransaction_tbl SET senderBalance = '$customerBalance', recipientBalance = '$airtimeBalance' WHERE transaction_uid = '$transaction_uid'");
    if($q){
        return true;
    }
}

function add_airtime_transaction_by_smartPhone($dbc,$customerID,$airtimeID,$phoneNum,$senderAmount,$recipientAmount,$customerCountry,$airtimeCountry,$network,$transaction_uid,$balance,$customerBalance,$airtimeBalance){
    /*$q = mysqli_query($dbc,"INSERT INTO airTimeTransaction_tbl(id, senderID, phoneNumber, transaction_uid, amount, network, country,balance,customerBalance) VALUES
          (UUID(),'$customerID','$phoneNum','$transaction_uid','$amount','$network','$country','$balance','$customerBalance')
      ");

    if($q){
        return true;
    }*/
    $rate = get_conversion_rate($dbc,$customerCountry,$airtimeCountry);
    $q = mysqli_query($dbc,"INSERT INTO maintransaction_tbl(id,senderID,recipientID,airtimeNumber,transaction_uid,sendersAmount,recipientAmount,senderCountryCode,recipientCountryCode,airtimeChannel,airtimeTotalBalance,transactionTypes,transactionStatus,deviceType,senderBalance,recipientBalance,exRate) VALUES 
  (UUID(),'$customerID','$airtimeID','$phoneNum','$transaction_uid','$senderAmount','$recipientAmount','$customerCountry','$airtimeCountry','$network','$balance','AIRTIME','COMPLETED','SMART','$customerBalance','$airtimeBalance','$rate')
    ");

    if($q){
        return true;
    }
}

function get_airtime_network($network){
    //1. Airtel\n2. Expresso\n3. Glo\n4. MTN\n5. Tigo \n6. Vodafone

    if($network == "MTN"){
        $data = 4;
        return $data;
    } elseif($network == "EXPRESSO"){
        $data = 2;
        return $data;
    }elseif($network == "TIGO"){
        $data = 5;
        return $data;
    }elseif($network == "AIRTEL"){
        $data = 1;
        return $data;
    }elseif($network == "VODAFONE"){
        $data = 6;
        return $data;
    }elseif($network == "GLO"){
        $data = 3;
        return $data;
    }


}

function get_airtime_network_by_id($networkID){
    if($networkID == 4){
        $data = "MTN";
        return $data;
    } elseif($networkID == 2){
        $data = "EXPRESSO";
        return $data;
    }elseif($networkID == 5){
        $data = "TIGO";
        return $data;
    }elseif($networkID == 1){
        $data = "AIRTEL";
        return $data;
    }elseif($networkID == 6){
        $data = "VODAFONE";
        return $data;
    }elseif($networkID == 3){
        $data = "GLO";
        return $data;
    }
}
//$rest = buy_airtime(+233542370721,270,4,2258969116767);

//print_r($rest);
//print_r($rest);








