<?php
include('Admin/config/setup.php');
require('config.php');
require('razorpay-php/Razorpay.php');
use Razorpay\Api\Api;
use Razorpay\Api\Errors\SignatureVerificationError;
$orderid = $payable = $types = "";
if(isset($_POST['orderid'])){
	$orderid = $_POST['orderid'];
} 
if(isset($_POST['payable'])){
	$payable = $_POST['payable'];
}
if(isset($_POST['types'])){
	$types = $_POST['types'];
}
if($types == "jobs"){
	$table = "jobpost";
}
if($types == "property"){
	$table = "propertypost";
}
if($types == "classified"){
	$table = "classifiedpost";
}

$success = true;
$error = "Payment Failed";
if($orderid == ""){
	$success = false;
	$error = "Payment Failed";
}
// $keySecret = 'D2zB3rVca85Kcgr1wCfZ0hIy';
// $keyId = 'rzp_live_HlfimhpIQSYwPl';

// $keySecret = 'rVU89NklWD3eZd5CM0BQ1E9w';
// $keyId = 'rzp_live_YPUXGpnosvSxZU';

if (empty($_POST['razorpay_payment_id']))
{
	$api = new Api($keyId, $keySecret);
	$check = 2;
	try
	{
		// Please note that the razorpay order ID must
		// come from a trusted source (session here, but
		// could be database or something else)
		$attributes = array(
			'razorpay_order_id' => $_SESSION['razorpay_order_id'],
			'razorpay_payment_id' => $_POST['razorpay_payment_id'],
			'razorpay_signature' => $_POST['razorpay_signature']
		);
		$api->utility->verifyPaymentSignature($attributes);
	}
	catch(SignatureVerificationError $e)
	{
		$success = false;
		$error = 'Razorpay Error : ' . $e->getMessage();
	}
}
?>
<div class="utf_dashboard_content"> 
	<div id="titlebar" class="dashboard_gradient">
		<div class="row">
		  <div class="col-md-12">
			<h2>Payment Status</h2>
			<nav id="breadcrumbs">
			  <ul>
				<li><a href="dashboard.php">Home</a></li>
				<li>Payment Status</li>
			  </ul> 
			</nav>
		  </div>
		</div>
	</div>
<?php 
$razorpay_payment_id = "";
if(isset($_POST['razorpay_payment_id'])){
	$razorpay_payment_id = $_POST['razorpay_payment_id'];
}
if ($razorpay_payment_id !== "")
{
  $html = "
		<center><img src='../images/success.png' width='300px' /><br><h3>Thank you for your Interest</h3><br><p>Your payment was successful. </p>
			<p>Payment ID: {$_POST['razorpay_payment_id']}</p></center>";
		date_default_timezone_set('Asia/Kolkata');
		$payment_date = date('Y-m-d H:i:s');
		$payment_id = $_POST['razorpay_payment_id'];		$paymentstatus = "success";	
	
	
	$usersms = "Your post was added successfully and your post id is . ".$orderid." with gross amount of Rs. ".$payable." - addmyentry.com.";
	$adminsms = "You received one order from . ".$name." and the order id is ".$orderid." with gross amount of Rs. ".$payable." - addmyentry.com.";
	sendsms($usersms, $mobile);
	sendsms($adminsms, $storemobile);
	/*$mailbody = '<html><body>';
	$mailbody .= '<img src="../images/logo.jpeg" />';
	$mailbody .= '<p>Thank you for your interest in KVS Shoppee. Your order has been received and will be processed once payment has been confirmed.</p>'; 
	$mailbody .= '<table>';
	$mailbody .= '<tr>';
	$mailbody .= '<td colspan="2" style="background: #000; width: 100%; Padding: 5px 0px;">Order Details</td>';
	$mailbody .= '</tr>';
	$mailbody .= '<tr>';
	$mailbody .= '<td>
				Order Id:'.$orderid.'<br>Created At:'.$currenttimeformt.'<br>Payment Method:'.$paymentmethod.'
				</td>';
	$mailbody .= '<td>
				Email:'.$email.'<br>Mobile:'.$mobile.'<br>Order Status:Pending
				</td>';
	$mailbody .= '</tr>';
	
	//$address1',`address2`='$address2',`city`='$city',`state`='$state',`country`='India',`pincode`='$postcode'
	
	$mailbody .= '<tr>';
	$mailbody .= '<td style="background: #000; width: 100%; Padding: 5px 0px;">Payment Address</td>';
	$mailbody .= '<td style="background: #000; width: 100%; Padding: 5px 0px;">Delivery Address</td>';
	$mailbody .= '</tr>';
	$mailbody .= '<tr>';
	$mailbody .= '<td>
				'.$name.'<br>'.$address1.'<br>'.$address2.'<br>'.$city.' - '.$postcode.'<br>'.$state.'<br>India
				</td>';
	$mailbody .= '<td>
				'.$name.'<br>'.$address1.'<br>'.$address2.'<br>'.$city.' - '.$postcode.'<br>'.$state.'<br>India
				</td>';
	$mailbody .= '</tr>';
	$mailbody .= '</table>';
	
	$mailbody .= '<table>';
	$mailbody .= '<tr><td>Product</td><td>Qty</td><td>Rate</td><td>Tax</td><td>Total</td></tr>';
	$query="SELECT * FROM `orderdetail` WHERE orderid='$orderid' ";
	$results = $conn->query($query);
	while($rows = $results->fetch_assoc()) { 
		$cartpdtqty = $rows['qty'];
		$pdtid = $rows['pdtid'];
		$rate = $rows['rate'];
		$igst = $rows['igst'];
		$total = $rows['total'];
		$name = "";
		$querys="SELECT * FROM `product` WHERE pdtid='$pdtid' ";
		$result = $conn->query($querys);
		while($row = $result->fetch_assoc()) { 
			$name = $row['name'];
			$image1 = $row['image1'];
		}
		
		$mailbody .= '<tr><td>'.$name.'</td><td>'.$cartpdtqty.'</td><td>'.$rate.'</td><td>'.$igst.'</td><td>'.$total.'</td></tr>';
	}
	$mailbody .= '<tr><td colspan="4" align="right">Subtotal</td><td align="right">'.$subtotal.'</td></tr>';
	if($discount > 0){
		$mailbody .= '<tr><td colspan="4" align="right">Discount</td><td align="right">'.$discount.'</td></tr>';
	}
	$mailbody .= '<tr><td colspan="4" align="right">Total</td><td align="right">'.$gross.'</td></tr>';
	$mailbody .= '</table>';
	$mailbody .= '<p>Please reply to this e-mail if you have any questions.</p>';
	$mailbody .= '</body></html>';
	$subject = "KVS Shoppee - Order No ".$orderid;
	$from = $storeemail;
	 
	// To send HTML mail, the Content-type header must be set
	$headers  = 'MIME-Version: 1.0' . "\r\n";
	$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
	$headers .= 'Cc: cschandru08@gmail.com\r\n';
	 
	// Create email headers
	$headers .= 'From: '.$from."\r\n".
		'Reply-To: '.$from."\r\n" .
		'X-Mailer: PHP/' . phpversion();
	//echo $mailbody;
	if(mail($email, $subject, $mailbody, $headers)){
		//echo "Mail Sent";
	} else {
		//echo "Mail Not Sent";
	}	*/
	
	echo "<div class='container'><br><br>";
	echo $html;
	echo "<br><br></div>";
	$razorpay_order_id = $_SESSION['razorpay_order_id'];
	$razorpay_payment_id = $_POST['razorpay_payment_id'];
	$razorpay_signature = $_POST['razorpay_signature'];	

	$new = "UPDATE `".$table."` SET `paymentid`='$razorpay_payment_id', `signature`='$razorpay_signature', `transactionid`='$razorpay_order_id', `paymentstatus`='$paymentstatus' WHERE `postid`='$orderid' ";
	$newsquery=mysqli_query($conn, $new);

} else {
	$html = "<center><img src='assets/img/failed.jpg' width='300px' /></center><br><p class='text-center'>Something went Wrong. Please tryagain Later!!!</p>
	<p class='text-center'>{$error}</p>";	
	$paymentstatus = "failed";
	echo $html;
	if($orderid !== ""){
		$new = "UPDATE `".$table."` SET `paymentid`='', `signature`='', `transactionid`='', `paymentstatus`='$paymentstatus' WHERE `postid`='$orderid' ";
		$newsquery=mysqli_query($conn, $new);
		echo $new;
	}
}


?>
</div>  
<?php include('footer.php'); ?>