<?php
session_start();

include "_functions.php";
require "conn.php";

$status         = mysqli_real_escape_string($con, $_GET['status_id']);
$billcode       = mysqli_real_escape_string($con, $_GET['billcode']);
$order_id       = mysqli_real_escape_string($con, $_GET['order_id']);
$msg            = mysqli_real_escape_string($con, $_GET['msg']);
$transaction_id = mysqli_real_escape_string($con, $_GET['transaction_id']);


//echo $status.$billcode.$order_id.$msg.$transaction_id;
 mysqli_query($con, "UPDATE transactions SET billStatus = '".$status."' , billoder_id = '".$order_id."' , billoder_tran_id = '".$transaction_id."' WHERE billcode = '".$billcode."' ");

//generate PIN 
if ($status==1)  {
    
    $sql        = "SELECT * FROM transactions WHERE billcode = '".$billcode."' ";  
    $result     = mysqli_query($con, $sql);
    $row        = mysqli_fetch_array($result);
    $nilaipin   = reKod("nilaipin", "komisen", "id", 1);
    $kuantiti   = $row[19] / $nilaipin;
    $tarikh     = $row[13];
    
            // janaan pin
            for ($x = 1; $x <= $kuantiti; $x++) {
                //echo $kuantiti."</br>";
                //echo randomPin()."</br>";
                //echo $_SESSION['aidi']."</br>";
                //echo $tarikh."</br>";
                //echo $_SERVER['REMOTE_ADDR']."</br>";
                mysqli_query($con, "INSERT INTO bestpin (no, userid, datesold, ip) VALUES ('".randomPin()."', ".$_SESSION['aidi'].", '".$tarikh."', '".$_SERVER['REMOTE_ADDR']."')");
            }
}


 $some_data = array(
    'billCode' => $billcode,
    'billpaymentStatus' => $status
  );  

  $curl = curl_init();

  curl_setopt($curl, CURLOPT_POST, 1);
  curl_setopt($curl, CURLOPT_URL, 'https://toyyibpay.com/index.php/api/getBillTransactions');  
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $some_data);

  $result = curl_exec($curl);
  $info = curl_getinfo($curl);  
  curl_close($curl);

  //echo $result;


header('Location: https://vips.wepayyourcost.com/buy_pin_fpx.php');
exit;
?>

