<?php include('Crypto.php')?>
<?php

	error_reporting(0);
	date_default_timezone_get("	Asia/Kolkata");
	$workingKey='5EDD35AF4A89BF071C4374A9A9BD6743';		//Working Key should be provided here.
	$encResponse=$_POST["encResp"];			//This is the response sent by the CCAvenue Server
	$rcvdString=decrypt($encResponse,$workingKey);		//Crypto Decryption used as per the specified working key.
	$order_status="";
	$decryptValues=explode('&', $rcvdString);
	$dataSize=sizeof($decryptValues);
	//print_r($decryptValues); die();
	for($i = 0; $i < $dataSize; $i++) 
	{
		$information = explode('=',$decryptValues[$i]);
		if($i==0)	$order_id = $information[1];
		if($i==1)	$tracking_id = $information[1];
		if($i==2)	$bank_ref_no = $information[1];
		if($i==3)	$order_status = $information[1];
		if($i==4)	$failure_message = $information[1];
		if($i==5)	$payment_mode = $information[1];
		if($i==6)	$card_name = $information[1];
		if($i==7)	$status_code = $information[1];
		if($i==8)	$status_message = $information[1];
		if($i==9)	$currency = $information[1];
		if($i==10)	$amount = $information[1];
		if($i==11)	$billing_name = $information[1];
		if($i==12)	$billing_address = $information[1];
		if($i==13)	$billing_city = $information[1];
		if($i==14)	$billing_state = $information[1];
		if($i==15)	$billing_zip = $information[1];
		if($i==16)	$billing_country = $information[1];
		if($i==17)	$billing_tel = $information[1];
		if($i==18)	$billing_email = $information[1];
		if($i==19)	$delivery_name = $information[1];
		if($i==20)	$delivery_address = $information[1];
		if($i==21)	$delivery_city = $information[1];
		if($i==22)	$delivery_state = $information[1];
		if($i==23)	$delivery_zip = $information[1];
		if($i==24)	$delivery_country = $information[1];
		if($i==25)	$delivery_tel = $information[1];
		if($i==26)	$merchant_param1 = $information[1];
		if($i==27)	$merchant_param2 = $information[1];
		if($i==28)	$merchant_param3 = $information[1];
		if($i==29)	$merchant_param4 = $information[1];
		if($i==30)	$merchant_param5 = $information[1];
		if($i==31)	$vault = $information[1];
		if($i==32)	$offer_type = $information[1];
		if($i==33)	$offer_code = $information[1];
		if($i==34)	$discount_value = $information[1];
		if($i==35)	$mer_amount = $information[1];
		if($i==36)	$eci_value = $information[1];
		if($i==37)	$retry = $information[1];
		if($i==38)	$response_code = $information[1];
	}

	//Insert into payment table
	$servername = "localhost";
	$username = "adclubuser";
	$password = "W;yCg0H0k4z5";
	$dbname = "adclubmadras";
	$paymentDate = date("Y-m-d H:i:s");

	// Create connection
	$conn = mysqli_connect($servername, $username, $password, $dbname);
	// Check connection
	if(!$conn){
	    die("Connection failed: " . mysqli_connect_error());
	}
	$sql = "INSERT INTO wp_payments (order_id,tracking_id,bank_ref_no,order_status,failure_message,payment_mode,card_name,status_code,status_message,currency,amount,billing_name,billing_address,billing_city,billing_state,billing_zip,billing_country,billing_tel,billing_email,delivery_name,delivery_address,delivery_city,delivery_state,delivery_zip,delivery_country,delivery_tel,merchant_param1,merchant_param2,merchant_param3,merchant_param4,merchant_param5,vault,offer_type,offer_code,discount_value,mer_amount,eci_value,retry,response_code,paymentDate)
		VALUES ('$order_id','$tracking_id','$bank_ref_no','$order_status','$failure_message','$payment_mode','$card_name','$status_code','$status_message','$currency','$amount','$billing_name','$billing_address','$billing_city','$billing_state','$billing_zip','$billing_country','$billing_tel','$billing_email','$delivery_name','$delivery_address','$delivery_city','$delivery_state','$delivery_zip','$delivery_country','$delivery_tel','$merchant_param1','$merchant_param2','$merchant_param3','$merchant_param4','$merchant_param5','$vault','$offer_type','$offer_code','$discount_value','$mer_amount','$eci_value','$retry','$response_code','$paymentDate')";
	if($conn->query($sql) === TRUE){
	    $_SESSION["orderID"] = $order_id;
	    $_SESSION["insert"] = "true";
	}else{
	    echo "Error: " . $sql . "<br>" . $conn->error;
	    echo "<br>";
	    die();
	    $_SESSION["error"] = "true";
	}

	if($merchant_param1 == "members" || $merchant_param1 == "editMembers"){
		//Update payment details
		$sql = "UPDATE wp_members SET paymentStatus = '$order_status', paymentMode = '$payment_mode', trackingID = '$tracking_id', paymentDate = '$paymentDate' WHERE unique_id = '$order_id' order by id DESC limit 1";
		if($conn->query($sql) === TRUE && $order_status === "Success"){
		    $_SESSION["orderID"] = $order_id;
		    $_SESSION["insert"] = "true";
		}elseif($order_status != "Success"){
			$_SESSION["error"] = "true";
		}else{
		    echo "Error: " . $sql . "<br>" . $conn->error;
		    echo "<br>";
		    die();
		    $_SESSION["error"] = "true";
		}
	}

	if($merchant_param1 == "course"){
		//Update payment details
		$sql = "UPDATE wp_course_application SET paymentStatus = '$order_status', paymentMode = '$payment_mode', trackingID = '$tracking_id', paymentDate = '$paymentDate', paymentType = 'online', status = 'active' WHERE unique_id = '$order_id' order by id DESC limit 1";
		if($conn->query($sql) === TRUE && $order_status === "Success"){
		    $_SESSION["orderID"] = $order_id;
		    $_SESSION["courseApplication"] = "true";
		}elseif($order_status != "Success"){
			$_SESSION["error"] = "true";
		}else{
		    echo "Error: " . $sql . "<br>" . $conn->error;
		    echo "<br>";
		    die();
		    $_SESSION["error"] = "true";
		}
	}

	if($merchant_param1 == "maddysregistration"){
		//Update payment details
		$sql = "UPDATE wp_maddys_registration SET paymentStatus = '$order_status', paymentMode = '$payment_mode', trackingID = '$tracking_id', paymentDate = '$paymentDate' WHERE registrationID = '$order_id' order by id DESC limit 1";
		if($conn->query($sql) === TRUE && $order_status === "Success"){
		    $_SESSION["orderID"] = $order_id;
		    $_SESSION["registrationMaddys"] = "true";
		}elseif($order_status != "Success"){
		    $_SESSION["registrationMaddysError"] = "true";
		}else{
			echo "Error: " . $sql . "<br>" . $conn->error;
		    echo "<br>";
		    die();
		    $_SESSION["registrationMaddysError"] = "true";
		}
	}

	if($merchant_param1 == "maddysentryticket"){
		//Update payment details
		$sql = "UPDATE wp_maddys_entry_ticket SET paymentStatus = '$order_status', paymentMode = '$payment_mode', trackingID = '$tracking_id', paymentDate = '$paymentDate' WHERE entryID = '$order_id' order by id DESC limit 1";
		if($conn->query($sql) === TRUE && $order_status === "Success"){
		    $_SESSION["orderID"] = $order_id;
		    $_SESSION["maddysEntryTicket"] = "true";
		}elseif($order_status != "Success"){
		    $_SESSION["maddysEntryTicketError"] = "true";
		}else{
			echo "Error: " . $sql . "<br>" . $conn->error;
		    echo "<br>";
		    die();
		    $_SESSION["maddysEntryTicketError"] = "true";
		}
	}

	if($merchant_param1 == "Maddys2018Entry"){
		//Update payment details
		$sql = "UPDATE wp_maddys_2018_step2 SET paymentStatus = '$order_status', paymentMode = '$payment_mode', trackingID = '$tracking_id', paymentDate = '$paymentDate' WHERE entryID = '$order_id' order by id DESC limit 1";
		if($conn->query($sql) === TRUE && $order_status === "Success"){
			$sql = "UPDATE wp_maddys_2018_step1 SET status = 'confirmed' WHERE entryID = '$order_id' order by id DESC limit 1";
			if($conn->query($sql) === TRUE){
				$_SESSION["orderID"] = $order_id;
		    	$_SESSION["Maddys2018Entry"] = "true";
			} else{
				$_SESSION["Maddys2018EntryError"] = "true";
			}
		}elseif($order_status != "Success"){
		    $_SESSION["Maddys2018EntryError"] = "true";
		}else{
			echo "Error: " . $sql . "<br>" . $conn->error;
		    echo "<br>";
		    die();
		    $_SESSION["Maddys2018EntryError"] = "true";
		}
	}
	
	$conn->close();
	echo '  <center>
				<h4>Please wait...</h4>
				<p>We are redirecting to your website</p>
				<div><img src="http://adclubmadras.com/media/2014/08/logo.jpg"></div>
				<div><img src="http://adclubmadras.com/wp-content/uploads/2016/09/loading-icon.gif"></div>
			</center>';
?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		<?php if(isset($_SESSION['insert'])) { ?>
			document.location.href = "http://adclubmadras.com/payment-confirmation"+"?unique="+"<?php echo $order_id ?>";
		<?php unset($_SESSION['insert']); } ?>//http://adclubmadras.com/payment-confirmation/?unique=ACM20160909001

		<?php if(isset($_SESSION['registrationMaddys'])) { ?>
			document.location.href = "http://adclubmadras.com/maddys2017/payment-confirmation"+"?registrationID="+"<?php echo $order_id ?>";
		<?php unset($_SESSION['registrationMaddys']); } ?>//http://adclubmadras.com/payment-confirmation/?unique=ACM20160909001

		<?php if(isset($_SESSION['registrationMaddysError'])) { ?>
			document.location.href = "http://adclubmadras.com/maddys2017/payment-failure"+"?registrationID="+"<?php echo $order_id ?>";
		<?php unset($_SESSION['registrationMaddysError']); } ?>//http://adclubmadras.com/payment-confirmation/?unique=ACM20160909001

		<?php if(isset($_SESSION['maddysEntryTicket'])) { ?>
			document.location.href = "http://adclubmadras.com/maddys2017/payment-confirmation-entry-ticket"+"?entryID="+"<?php echo $order_id ?>";
		<?php unset($_SESSION['maddysEntryTicket']); } ?>//http://adclubmadras.com/payment-confirmation/?unique=ACM20160909001

		<?php if(isset($_SESSION['Maddys2018Entry'])) { ?>
			document.location.href = "http://adclubmadras.com/maddys2018/payment-confirmation"+"?entryID="+"<?php echo $order_id ?>";
		<?php unset($_SESSION['Maddys2018Entry']); } ?>//http://adclubmadras.com/payment-confirmation/?unique=ACM20160909001

		<?php if(isset($_SESSION['maddysEntryTicketError'])) { ?>
			document.location.href = "http://adclubmadras.com/maddys2017/payment-failure-entry-ticket"+"?entryID="+"<?php echo $order_id ?>";
		<?php unset($_SESSION['maddysEntryTicketError']); } ?>//http://adclubmadras.com/payment-confirmation/?unique=ACM20160909001

		<?php if(isset($_SESSION['Maddys2018EntryError'])) { ?>
			document.location.href = "http://adclubmadras.com/maddys2018/payment-failure"+"?entryID="+"<?php echo $order_id ?>";
		<?php unset($_SESSION['Maddys2018EntryError']); } ?>//http://adclubmadras.com/payment-confirmation/?unique=ACM20160909001

		<?php if(isset($_SESSION['courseApplication'])) { ?>
			document.location.href = "http://adclubmadras.com/course-offered/course-payment-confirmation"+"?unique="+"<?php echo $order_id ?>";
		<?php unset($_SESSION['courseApplication']); } ?>//http://adclubmadras.com/payment-confirmation/?unique=ACM20160909001

		<?php if(isset($_SESSION['error'])) { ?>
			document.location.href = "http://adclubmadras.com/payment-failure"+"?unique="+"<?php echo $order_id ?>";
		<?php unset($_SESSION['error']); } ?>//http://adclubmadras.com/payment-confirmation/?unique=ACM20160909001
	});
</script>