<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include_once($_SERVER['DOCUMENT_ROOT'] . '/student_portal/acceptance_pay/database/includes/databaseFunctions.php');

function getHashValue($stringToHash) 
{
    return hash('sha512', $stringToHash);
}

function getRequeryURL($productID, $transactionReference, $payAmount) 
{
    $parameters = array(
                    "productid" =>$productID,
                    "transactionreference"=>$transactionReference,
                    "amount"    =>$payAmount
                        );
    $requeryURL = http_build_query($parameters);
    return "https://stageserv.interswitchng.com/test_paydirect/api/v1/gettransaction.json?$requeryURL";   //OUTSIDE INTERSWITCH
}

function getOrderIdValue() 
{
    return  "Student-ID"  .
                    intval( 
                            "0" . 
                            rand(1,9) . 
                            rand(0,9) . 
                            rand(0,9) . 
                            rand(0,9)
                        ); // random(ish) 7 digit int;
}

function getAmountFromDB($txnRef)
{
    openDatabase();

$db = getDBInstance();
if(!$db)
{
  echo $db->lastErrorMsg();
} 
else 
{
  //echo "Opened database successfully<br/>";
}

 $sql =<<<EOF
      SELECT AMOUNT from COLLEGEPAYLOGS WHERE TXNREF=:id;
EOF;
    //SELECT AMOUNT from WEBPAYLOGS WHERE TXNREF=$txnRef;
//    $ret = $db->query($sql);
//    $row = $ret->fetchArray(SQLITE3_ASSOC);
    $stmt = $db->prepare($sql);
    $stmt->bindValue(':id', $txnRef, SQLITE3_TEXT);
    
    //$row = $ret->fetchArray(SQLITE3_ASSOC);
    $result = $stmt->execute();
    $row = $result->fetchArray(SQLITE3_ASSOC);
    
    return $row['AMOUNT'];
}

function generateSplitXMLData($txnRef, $amount)
{   
    $split_amount_1 = round($amount/3, 2);
    $split_amount_2 = round($amount/3, 2);
    $split_amount_3 = round($amount/3, 2);
    
    if($split_amount_1 + $split_amount_2 + $split_amount_3 != $amount)
    {
        /*
         * Please note, the summation of the 3 split items ($split_amount_1,2&3) must be EXACTLY equal to the amount being split
         * (i.e. $amount), if not an error will be thrown.
         * The below adds whatever difference may arise (usually 1 kobo) to the 3rd split item to resolve this issue.
         */
        $split_amount_1 = round($amount/3, 2);
        $split_amount_2 = round($amount/3, 2);
        $split_amount_3 = round($amount/3, 2);
        $split_amount_3 += $amount - ($split_amount_1 + $split_amount_2 + $split_amount_3);
    }
    
    $xml_data = '<payment_item_detail> 
        <item_details detail_ref="' . $txnRef . '" college="maritime academy oron" department="NAUTICAL SCIENCE" faculty="NA"> 
            <item_detail item_id="1" item_name="school fees" item_amt="' . $split_amount_1 . '" bank_id="8" acct_num="12345678910" /> 
            <item_detail item_id="2" item_name="boardn fees" item_amt="' . $split_amount_2 . '" bank_id="16" acct_num="12346678910" /> 
            <item_detail item_id="3" item_name="sports fees" item_amt="' . $split_amount_3 . '" bank_id="8" acct_num="12345998910" /> 
        </item_details> 
    </payment_item_detail>';
    
    return $xml_data;
}

function getAmount($amount)
{
    //TODO: implement Method to pass N300 Txn Fee to Customer
    
    return $amount;
}