<?php
// Include the configuration file 
require_once 'config.php';

// Include the database connection file 
require_once 'dbConnect.php';

$payment_ref_id = $statusMsg = '';
$status = 'error';
// https://crowdkode.com/pay/payment-status.php?checkout_ref_id=2B839807CM6626219
// Check whether the payment ID is not empty
if(!empty($_GET['checkout_ref_id'])){
	$payment_txn_id  = base64_decode($_GET['checkout_ref_id']);
	//$payment_txn_id  = $_GET['checkout_ref_id'];
	//echo "Not empty: " . $payment_txn_id;
	// Fetch transaction data from the database
	$sqlQ = "SELECT id,item_name,payer_id,payer_name,payer_email,payer_country,order_id,transaction_id,paid_amount,paid_amount_currency,payment_source,payment_status,created,chID FROM transactions WHERE transaction_id = ?";
	$stmt = $db->prepare($sqlQ); 
	$stmt->bind_param("s", $payment_txn_id);
	$stmt->execute();
	$stmt->store_result();

	if($stmt->num_rows > 0){
		// Get transaction details
		$stmt->bind_result($payment_ref_id, $item_name, $payer_id, $payer_name, $payer_email, $payer_country, $order_id, $transaction_id, $paid_amount, $paid_amount_currency, $payment_source, $payment_status, $created, $challengeID);
		$stmt->fetch();
		
		$status = 'success';
		$statusMsg = 'Your Payment was Successful!';
	}else{
		$statusMsg = "The transaction failed!";
	}
	
	// GET THE CHALLENGE INFO 
	$sqlQ2 = "SELECT title FROM projects WHERE id = ?";
	$stmt2 = $db->prepare($sqlQ2);
	$stmt2->bind_param("s", $challengeID);
	$stmt2->execute();
	$stmt2->store_result();

	if ($stmt2->num_rows > 0) {
		// Get transaction details
		$stmt2->bind_result($challengeTitle);
		$stmt2->fetch();
	}
}else{
	header("Location: index.php");
	exit;
}
?>

<!DOCTYPE html>
<html lang="en-US">
<head>
	<title>CrowdKode Payment Status</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">

	<link rel="preconnect" href="https://fonts.googleapis.com">
	<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
	<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@100;200;300;400;500;600;700;800;900&display=swap" rel="stylesheet">
	<link href='//fonts.googleapis.com/css?family=Open+Sans:400,600,700,300' rel='stylesheet' type='text/css'>
	<script src="https://kit.fontawesome.com/528dc48127.js" crossorigin="anonymous"> </script>
	
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<link rel="apple-touch-icon" href="/assets/logo.png" />

	<!-- Stylesheet file -->
	<link href="css/style.css" rel="stylesheet">
	<style>
		body, html {
			font-family: Montserrat,Helvetica,Verdana,sans-serif;
		}
		a, a:active, a:visited, a:link {
			text-decoration: none;
		}
		h1, h2, h3, h4, h5, h6 {
			font-family: Montserrat,Helvetica,Verdana,sans-serif;
		}
		h4, .status h4 {
			text-transform: uppercase;
			color: #ccc;
			font-size:.7em;
			font-weight:600;
		}
		
		h1.success { color:#00C617; font-size:22px; }
		
		/* ################################### */
		/*            MOBILE STYLES            */
		/* ################################### */

		/* Portrait tablet to landscape and desktop */
		@media (min-width: 980px) and (max-width: 1024px) {

		}

		/* Portrait tablet to landscape and desktop */
		@media (min-width: 768px) and (max-width: 979px) {

		}

		/* Landscape phone to portrait tablet */
		@media (max-width: 767px) {

		}

		/* Landscape phones and down */
		@media (max-width: 586px) {
			h1.success {
				font-size: 24px;
			}
		}

		/* Landscape phones and down */
		@media (max-width: 480px) {

		}

		/* Older phones with super small screens, iPhone SE */
		@media (max-width: 375px) {

		}
	</style>
</head>
<body>
<div class="container">
	<div class="status" style="max-width:500px; margin:0 auto; background:#fff;">
		<?php if(!empty($payment_ref_id)){ ?>
		
			<center><img src="/static/client/crowd_kode_logo_black.png" 
				style="max-width:450px; width:85%; margin-bottom:-15px;" 
				alt="CrowdKode" /></center>
		
			<h1 class="<?php echo $status; ?>"><?php echo $statusMsg; ?></h1>
			
			<h4>Payment Information</h4>
			<p><b>Reference Number:</b> <?php echo $payment_ref_id; ?></p>
			<p><b>Order ID:</b> <?php echo $order_id; ?></p>
			<p><b>Transaction ID:</b> <?php echo $transaction_id; ?></p>
			<p><b>Paid Amount:</b> $<?php echo $paid_amount.' '.$paid_amount_currency; ?></p>
			<p><b>Payment Status:</b> <?php echo $payment_status; ?></p>
			<p><b>Date:</b> <?php echo $created; ?></p>
			<br />
			<h4 style="margin-top:10px;">Payer Information</h4>
			<p><b>Payer ID:</b> <?php echo $payer_id; ?></p>
			<p><b>Name:</b> <?php echo $payer_name; ?></p>
			<p><b>Email:</b> <?php echo $payer_email; ?></p>
			<p><b>Country:</b> <?php echo $payer_country; ?></p>
			<br />
			<h4 style="margin-top:10px;">Product Information</h4>
			<!--<p><b>Challenge:</b> <?php echo $challengeTitle; ?></p>-->
			<p><b>Name:</b> <?php echo $item_name; ?></p> <!-- was $itemName -->
			<!--<p><b>Price:</b> <?php echo $itemPrice.' '.$currency; ?></p>-->
			<p><b>Credits:</b> $<?php echo $paid_amount.' '.$currency; ?></p>
			<br />
		<?php }else{ ?>
			<h1 class="error">Ooops! Your Payment failed!</h1>
			<br />
			<p class="error"><?php echo $statusMsg; ?></p>
			<br /><br />
		<?php } ?>
	</div>
	<br />
	<center>
		<?php
			$parts = explode(':', $item_name);
			$challengeIDNum = array_pop($parts);
			//echo $challengeIDNum;
			//echo $challengeID;
			if (!empty($challengeID)){
		?>
			<!--<a href="/load" class="btn-link"><i class="fa fa-angle-left" 
			aria-hidden="true"></i>Back to Load page</a>-->
			<a href="/load" class="btn-link"><i class="fa fa-angle-left" 
			aria-hidden="true"></i>Back to Load page</a>
		<?php } elseif (!empty($challengeIDNum)) { ?>
			<!--<a href="/challenge/<?= $challengeIDNum ?>" class="btn-link">
			<i class="fa fa-angle-left" aria-hidden="true"></i> Back to Challenge</a>-->
			<a href="/load" class="btn-link"><i class="fa fa-angle-left" 
			aria-hidden="true"></i>Back to Load page</a>
		<?php } else { ?>
			<a href="/load" class="btn-link"><i class="fa fa-angle-left" 
			aria-hidden="true"></i>Back to Load page</a>
		<?php } ?>
		<!--<a href="index.php" class="btn-link">Back to Payments Page</a>-->
	</center>
	<br /><br />
</div>
</body>
</html>