<?php include("../Header/Session.php"); ?>
<?php include("../Header/Connection.php"); ?>

<html>
<head>
    <script src="https://www.paypalobjects.com/api/checkout.js"></script>
</head>

<?php
	// Include and initialize paypal class 
	include_once 'Paypal/PaypalExpress.class.php'; 
	$paypal = new PaypalExpress; 


	$payment_code = $_GET['payment_code'];
	$BillCode =  randomRefferalCode();

	$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();
		$amount  = $row_order_item['amount'];
		$user_ref_code  = $row_order_item['ref_code'];
		$order_id  = $row_order_item['order_id'];
		$trans_code  = $row_order_item['trans_code'];
	}

	$amount1 = $amount;
	$amount = $amount1;
	$amount_Pay = $amount1 * 100 ;
	$status = '100';

	$sql = "SELECT * 
	FROM user
	WHERE ref_code = '$user_ref_code'
	And status='1'";
	$result = $mysqli-> query($sql);
	if ($result -> num_rows > 0)
	{
		$row = $result ->fetch_assoc();
		$name = $row['name'];
		$phone_number = $row['phone_number'];
		$email = $row['email'];
	}
	
	date_default_timezone_set("Asia/Kuala_Lumpur");
	$created_date = date('Y-m-d H:i:s');

	//Get User Info
	$user_name = $name;
	$user_phone_number = $phone_number;
	$user_email = $email;

	$query_payment  = " INSERT INTO payment
	(
	payment_code,
	trans_code,
	ref_code,
	name,
	email,
	phone_number,
	amount,
	status,
	created_date
	)
	VALUES
	(
	'$payment_code',
	'$payment_code',
	'$ref_code',
	'$user_name',
	'$user_email',
	'$user_phone_number',
	'$amount',	
	'$status',
	'$created_date'
	)";
	$res_payment = $mysqli->query($query_payment);
	?>

	<div class="item">
		<!-- Product details -->
		<p>Amount: USD <?php echo $amount; ?></p>
    
		<!-- Checkout button -->
		Click Button Below To Continue<br><br>
		<a href="pay_package.php?order_id=<?php echo $order_id; ?>&trans_code=<?php echo $trans_code; ?>" class="btn btn-success">Back</a>
		<div id="paypal-button"></div>
	</div>

	<script>
	paypal.Button.render({
		// Configure environment
		env: '<?php echo $paypal->paypalEnv; ?>',
		client: {
			sandbox: '<?php echo $paypal->paypalClientID; ?>',
			production: '<?php echo $paypal->paypalClientID; ?>'
		},
		// Customize button (optional)
		locale: 'en_US',
		style: {
			size: 'small',
			color: 'gold',
			shape: 'pill',
		},
		// Set up a payment
		payment: function (data, actions) {
			return actions.payment.create({
				transactions: [{
					amount: {
						total: '<?php echo $amount; ?>',
						currency: 'USD'
					}
				}]
		  });
		},
		// Execute the payment
		onAuthorize: function (data, actions) {
			return actions.payment.execute()
			.then(function () {
				// Show a confirmation message to the buyer
				//window.alert('Thank you for your purchase!');
            
				// Redirect to the payment process page
				window.location = "checkout_proceed_B_after_payment_package_paypal.php?paymentID="+data.paymentID+"&token="+data.paymentToken+"&payerID="+data.payerID+"&pid=<?php echo $order_id; ?>&payment_code=<?php echo $payment_code; ?>";
			});
		}
	}, '#paypal-button');
	</script>
	</html>
	