<?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();
		$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'];
	}


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

		$sql_last_user = "SELECT ref_code FROM user
	              Where status='1'
				  And ref_code Like 'MGP%'
				  And ref_code Not Like 'MGPC%'
		          Order by user_id Desc
				  LIMIT 1 ";
		$result_last_user = $mysqli-> query($sql_last_user);
		if ($result_last_user -> num_rows > 0)
		{
			$row_last_user = $result_last_user ->fetch_assoc();
			$Last_ref_code = $row_last_user['ref_code'];
			$Last_ref_code = str_replace("MGP1000","",$Last_ref_code);
			$New_ref_code_count = $Last_ref_code + 1;
			$New_ref_code = "MGP1000".$New_ref_code_count;
		}

		//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 User Info
		$Update_user_info = "UPDATE user SET
		ref_code = '$New_ref_code',
		username = '$New_ref_code',
		trans_code = '$ref_code',
		user_id = '$New_ref_code_count',
		status = '1',
		activation_status = '1',
		verification_status = '1'
		WHERE ref_code = '$ref_code' ";
		$result_user_info = $mysqli->query($Update_user_info);

		//Update Wallet info
		$Update_wallet_info = "UPDATE wallet SET
		ref_code = '$New_ref_code',
		status = '1'
		WHERE ref_code = '$ref_code' ";
		$result_wallet_info = $mysqli->query($Update_wallet_info);
		
		if ($result_payment_info && $result_user_info && $result_wallet_info)
		{
			echo "<script type='text/javascript'>alert('Pendaftaran Berjaya. Sila log masuk.');</script>";
			echo "<script type='text/javascript'> document.location='https://mygoldsdnbhd.com/log-masuk/'; </script>";
			exit();
		}
		else
		{
			echo "<script type='text/javascript'>alert('Pembayaran untuk pendaftaran tidak berjaya.');</script>";
			echo "<script type='text/javascript'> document.location='https://mygoldsdnbhd.com/pendaftaran/'; </script>";
			exit();
		}						
	}
	else // If not success
	{									
		echo "<script type='text/javascript'>alert('Pembayaran untuk pendaftaran tidak berjaya.');</script>";
		echo "<script type='text/javascript'> document.location='https://mygoldsdnbhd.com/pendaftaran/'; </script>";
		exit();
	}	
}
?>
</html>