<?php
require_once '../../../../../classconn/class.user.php';
$actionParam = new USER();
//user class stored in variable actionParam
require_once 'include.php';

$invoice_id = $_GET['invoice_id'];
$transaction_hash = $_GET['transaction_hash'];
$value_in_btc = $_GET['value'] / 100000000;

$stmt = $actionParam->runQuery("SELECT * FROM tbl_user WHERE addressID=:id");
$stmt->execute(array(":id"=>$invoice_id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
$balance = $row['balance'];
$my_address = $row['address_given'];
$email = $row['email'];
$fullname = $row['fullname'];
$refpay = $row['ref_pay'];
$refemail = $row['ref_email'];

/*
if ($_GET['address'] != $my_address) {
    echo 'Incorrect Receiving Address';
    return;
}*/

if ($_GET['secret'] != $secret) {
    echo 'Invalid Secret';
    return;
}

$url='https://bitpay.com/api/rates';
$json=json_decode( file_get_contents( $url ) );
$dollar=$btc=0;

foreach( $json as $obj ){
    if( $obj->code=='USD' )$btc=$obj->rate;
}

//get value of satoshi in dollar
$dollar = $value_in_btc * $btc;
$new_balance = $balance + $dollar;

$value_btc = round($value_in_btc,4);
//update user with incoming btc
$stmt = $actionParam->runQuery("UPDATE tbl_user SET incoming_btc=:ibtc WHERE addressID=:id");
$stmt->bindparam(":ibtc",$value_btc);
$stmt->bindparam(":id",$invoice_id);
$stmt->execute();


//send user mail on incoming bitcoin

 $message = '
              <img src=  http://bitxprofits.com/pip.png alt= BitXprofits>
              <p>Hello '.$fullname.',<br>
              You have an incoming deposit of '.round($value_in_btc,4).' BTC equivalent to $'.number_format($dollar,2).' to your account.<br><br>
              Best Regards;<br>
              BitXprofits Team.
              </p>            
            ';
 $subject = "Deposit Request";
 $actionParam->send_mail_cron($email,$message,$subject);

$itemAmount = round($dollar,2);
$txnid = $invoice_id;
//waiting for 2 blockchain confirmation before running this code
if ($_GET['confirmations'] >= 2) {
    
  //put up user bitcoin for sale
  $date = strtotime('now');
  
     $stmt = $actionParam->runQuery("SELECT * FROM tbl_user WHERE addressID=:id");
     $stmt->execute(array(":id"=>$txnid));
     $row=$stmt->fetch(PDO::FETCH_ASSOC);
     if($stmt->rowCount() > 0)
     {
       //update users deposite here
       $stmt = $actionParam->runQuery("UPDATE tbl_user SET last_deposite=:last WHERE email=:email");
       $stmt->bindparam(":last",$itemAmount);
       $stmt->bindparam(":email",$email);
       $stmt->execute();
     
       $stmt = $actionParam->runQuery("SELECT * FROM tbl_user WHERE email=:email");
       $stmt->execute(array(":email"=>$email));
       $uRow=$stmt->fetch(PDO::FETCH_ASSOC);
       $refemail = $uRow['ref_email'];
       $refpay = $uRow['ref_pay'];
       //get users profit first then add daily profit to it
       $bal = $uRow['balance'];
       $newbal = $bal + $itemAmount;
       //update users balance here
       $stmt = $actionParam->runQuery("UPDATE tbl_user SET balance=:bal,addressID=:txn WHERE email=:email");
       $stmt->bindparam(":bal",$newbal);
       $stmt->bindparam(":txn",$txnid);
       $stmt->bindparam(":email",$email);
       $stmt->execute();
       //insert deposite into deposite history
       $date = date("d/m/Y");
       $stmt = $actionParam->runQuery("INSERT INTO tbl_deposite(email,amount,date)
       VALUES(:email, :amt, :date)");
       $stmt->bindparam(":email",$email);
       $stmt->bindparam(":amt",$itemAmount);
       $stmt->bindparam(":date",$date);
       $stmt->execute();
       //reward user's referrer if user has a referrer
       if(!empty($refemail))
        {
          if($refpay == 0)
           {
	          $pay = 1;
              $percent = 5/100;
	          $pay_amt = $percent * $itemAmount;
	          //select the exact referrer
              $stmt = $actionParam->runQuery("SELECT * FROM tbl_user WHERE email=:email");
              $stmt->execute(array(":email"=>$refemail));
              $refrow = $stmt->fetch(PDO::FETCH_ASSOC);
              $prof = $refrow['profit'];
	          $bal = $refrow['balance'];
              $newbal = $prof + $pay_amt ;
	          //calculate and update referrer's account balance
              $stmt = $actionParam->runQuery("UPDATE tbl_user SET profit=:amt WHERE email=:email");
              $stmt->execute(array(":email"=>$refemail,":amt"=>$newbal));
	          //set users ref pay to 0, referral bonus is continuous
	          $stmt = $actionParam->runQuery("UPDATE tbl_user SET ref_pay=:pay WHERE email=:email");
              $stmt->execute(array(":email"=>$email,":pay"=>$pay));
              
              $date = date("d/m/Y");
              $type = 'Referral bonus';
              //insert earnings into earning history
              $stmt = $actionParam->runQuery("INSERT INTO tbl_earning(email,amount,type,date)
              VALUES(:email, :amt, :type, :date)");
              $stmt->bindparam(":email",$refemail);
              $stmt->bindparam(":amt",$pay_amt);
              $stmt->bindparam(":type",$type);
              $stmt->bindparam(":date",$date);
              $stmt->execute();
           }
        }
         
     }
  
    $incoming = '';
    //update user with incoming btc
    $stmt = $actionParam->runQuery("UPDATE tbl_user SET incoming_btc=:ibtc WHERE addressID=:id");
    $stmt->bindparam(":ibtc",$incoming);
    $stmt->bindparam(":id",$invoice_id);
    $stmt->execute();
    //send mail to user to confirm the bitcoin is up for sale
  
    $message = '
              <img src=  http://bitxprofits.com/pip.png alt= BitXprofits>
              <p>Hello '.$fullname.',<br>
              You have successfully recieved '.round($value_in_btc, 4).' BTC equivalent to $'.number_format($dollar,2).' in your wallet.<br><br>
              Best Regards;<br>
              BitXprofits Team.
              
              </p>
            ';
    $subject = "Deposit Confirmed";
    $actionParam->send_mail_cron($email,$message,$subject);
 
     //send mail to referrer
      if(!empty($refemail))
        {
          if($refpay == 0)
           {
              $percent = 5/100;
	          $pay_amt = $percent * $itemAmount;
              
              $message = '
              <p>Hello '.$fullname.',<br>
              You have successfully recieved a referral bonus of $'.number_format($pay_amt,2).' in your wallet.
              </p>
                        ';
             $subject = "Referral bonus received";
              $email = $refemail;
             $actionParam->send_mail_cron($email,$message,$subject);
           }
        }
 echo "*ok*";
  

}

?>