<?php include("../Header/Connection.php"); ?>
<!DOCTYPE html>
<html>
<?php 

error_reporting(0);

//localhost/mygoldsdnbhd.com/System/Page/U/checkout_proceed_B_after_payment_package_toyyibpay.php?status_id=3&billcode=b1651t1t&order_id=ft5yvgnv&msg=ok&transaction_id=TP8573989079202101310521
//status : Payment status. 1= success, 2=pending, 3=fail

if(isset($_GET['status_id']) && isset($_GET['order_id']) && isset($_GET['msg']) && isset($_GET['transaction_id']) && isset($_GET['billcode']))
{
	$AP_status_id = $_GET['status_id'] ;
	$AP_order_id = $_GET['order_id'] ;
	$AP_msg = $_GET['msg'] ;
	$AP_transaction_id = $_GET['transaction_id'] ;
	$AP_billcode = $_GET['billcode'] ;

	//$AP_status_id = '1';

	$sql_payment = "SELECT * 
	FROM payment 
	WHERE payment_code='$AP_billcode' ";
	$result_payment = $mysqli-> query($sql_payment);
	if ($result_payment -> num_rows > 0)
	{
		$row_payment = $result_payment ->fetch_assoc();
		$user_ref_code = $row_payment['ref_code'];
		$payment_code = $row_payment['trans_code'];
		$Name = $row_payment['name'];
		$Email = $row_payment['email'];
		$Tel = $row_payment['phone_number'];
		$amount = $row_payment['amount'];
	}

	$sql_order_item = "SELECT *
						FROM package_payment
						WHERE payment_code = '$payment_code'
						LIMIT 1";
	$result_order_item = $mysqli-> query($sql_order_item);
	if ($result_order_item -> num_rows > 0)
	{
		$row_order_item = $result_order_item ->fetch_assoc();
		$order_id  = $row_order_item['order_id'];
		$trans_code  = $row_order_item['trans_code'];
	}

	//If success payment
	if($AP_status_id == '1') 
	{
		$status = 1;

		$sql_order_item = "SELECT *
				   FROM woocommerce_order_info
				   WHERE order_id  = '$order_id'
                   AND trans_code  = '$trans_code'
				   AND status='1'";
		$result_order_item = $mysqli-> query($sql_order_item);
		if ($result_order_item -> num_rows > 0)
		{
			$row_order_item = $result_order_item ->fetch_assoc();
			$order_items  = $row_order_item['order_items'];
			$order_full_total = $row_order_item['order_full_total'];
		}

		$data_total_payment = 
		"SELECT SUM(amount) As num 
		FROM package_payment
		WHERE order_id = '$order_id'
		AND trans_code = '$trans_code'
		AND ref_code = '$user_ref_code'
		AND status = '1'";    
		$result_total_payment = $mysqli->query($data_total_payment);
		if ($result_total_payment ->num_rows > 0)
		{
			$row_total_payment = $result_total_payment ->fetch_assoc();
			$full_total_payment = $row_total_payment['num'];
		}

		$order_balance = $order_full_total - $full_total_payment - $amount;

		//Update Payment Info
		$Update_payment_info = "UPDATE payment SET
		transaction_id='$AP_transaction_id',
		status='1',
		payment_status='1'
		WHERE payment_code = '$AP_billcode' ";
		$result_payment_info = $mysqli->query($Update_payment_info);
		
		//Update Package Info
		$Update_package_info = "UPDATE package_payment SET
        balance = '$order_balance',
		status='1'
		WHERE payment_code = '$payment_code' ";
		$result_package_info = $mysqli->query($Update_package_info);

		if ($result_payment_info && $result_package_info)
		{
			//echo "<script type='text/javascript'>alert('Succesfully Paid');</script>";
			echo "<script type='text/javascript'> document.location='pay_package.php?order_id=$order_id&trans_code=$trans_code'; </script>";
			exit();
		}
		else
		{
			//echo "<script type='text/javascript'>alert('Payment Fail');</script>";
			echo "<script type='text/javascript'> document.location='pay_package.php?order_id=$order_id&trans_code=$trans_code'; </script>";
			exit();
		}						
	}
	else // If not success
	{									
		//echo "<script type='text/javascript'>alert('Payment Fail');</script>";
		echo "<script type='text/javascript'> document.location='pay_package.php?order_id=$order_id&trans_code=$trans_code'; </script>";
		exit();
	}	
}
?>
</html>