<?php

function lr_buy_airtime($phoneNumber,$amount,$network,$transaction_uid){
  
  $networks =[
      'MTN'   => ['operator'=>'10','productId'=>'19'],
      'ORANGE'=> ['operator'=>'151','productId'=>'1018']
    ];
  
  $salt     = substr(sha1(time()), 0, 40); 
  $password = SHA1($salt.SHA1("princeboateng"));

  $post_fields = [
              'auth' => 
                [
                  'username' => 'gnbankliberiaapi',
                  'salt'     => $salt,
                  'password' => $password,
                ],
              'version'   => 5,
              'command'   => 'execTransaction',
              'operator'  => $networks[$network]['operator'],
              'msisdn'    => $phoneNumber,
              'amount'    => $amount,
              'productId' => $networks[$network]['productId'],
              //'userReference' => $transaction_uid,
             ];

    $curl = curl_init();

  curl_setopt_array($curl, array(
    CURLOPT_URL => "https://gnbankliberia.artxportal.com/api.php",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => json_encode($post_fields),
    CURLOPT_HTTPHEADER => array(
    "Content-Type: application/json",
    "Postman-Token: e532c5f9-6928-4bbc-913f-071eec60d399",
    "cache-control: no-cache"
    ),
  ));

  $response = curl_exec($curl);

  curl_close($curl);

    $res= json_decode($response,true);
  
  return $res;
}


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

    $buyAirtime = lr_buy_airtime($transitData['airtimePhoneNum'],$transitData['amount'],$transitData['network'],$transaction_uid);
    if($buyAirtime['status']['typeName'] == "Success"){
        if(lr_add_airtime_transaction($dbc,$transaction_uid,$senderData,$transitData,0.00,$airtimeData)){
            return true;


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

}

function lr_add_airtime_transaction($dbc,$transaction_uid,$senderData,$transitData,$balance,$airtimeData){


    $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]','lr','lr','$transitData[network]','$balance','AIRTIME','COMPLETED','USSD')
    ");

    if($q){
        return true;
    }
}

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

function lr_add_airtime_transaction_by_smartPhone($dbc,$customerID,$airtimeID,$phoneNum,$senderAmount,$recipientAmount,$customerCountry,$airtimeCountry,$network,$transaction_uid,$balance,$customerBalance,$airtimeBalance="0.00"){
  
    $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','WAITING','SMART','$customerBalance','$airtimeBalance','$rate')
    ");

    if($q){
        return true;
    }
}

function lr_get_airtime_network($network){

    if($network == "MTN"){
        $data = 'MTN';
        return $data;
    } elseif($network == "ORANGE"){
        $data = 'ORANGE';
        return $data;
    }

}

function lr_get_airtime_network_by_id($networkID){
    if($networkID == 1){
        $data = "MTN";
        return $data;
    } elseif($networkID == 2){
        $data = "ORANGE";
        return $data;
    }
}









