<?php
	/**
	 * This Is the Kit File To Be Included For Transaction Request
	 */
	include 'wlpg/standard/AWLMEAPI.php';
	date_default_timezone_set("Asia/Calcutta");
	$con = mysqli_connect('localhost','lifelinehospital_user', 'YO77U^YFI3nS','lifelinehospital_db');
	$select = "SELECT order_no FROM `tbl_hims_appointment` WHERE `order_no`='$_REQUEST[OrderId]'";
	$result = mysqli_query($con, $sql);
	if(mysqli_num_rows($result)==0){
		if($_REQUEST['submitapp']){
		//create an Object of the above included class
		$obj = new AWLMEAPI();

		//create an object of Request Message
		$reqMsgDTO = new ReqMsgDTO();

		/* Populate the above DTO Object On the Basis Of The Received Values */
		// PG MID
		$reqMsgDTO->setMid($_REQUEST['mid']);
		// Merchant Unique order id
		$reqMsgDTO->setOrderId($_REQUEST['OrderId']);
		//Transaction amount in paisa format
		$reqMsgDTO->setTrnAmt($_REQUEST['amount']);
		//Transaction remarks
		$reqMsgDTO->setTrnRemarks("Any Remarks");
		// Merchant transaction type (S/P/R)
		$reqMsgDTO->setMeTransReqType($_REQUEST['meTransReqType']);
		// Merchant encryption key
		$reqMsgDTO->setEnckey($_REQUEST['enckey']);
		// Merchant transaction currency
		$reqMsgDTO->setTrnCurrency($_REQUEST['currencyName']);
		// Recurring period, if merchant transaction type is R
		$reqMsgDTO->setRecurrPeriod($_REQUEST['recurPeriod']);
		// Recurring day, if merchant transaction type is R
		$reqMsgDTO->setRecurrDay($_REQUEST['recurDay']);
		// No of recurring, if merchant transaction type is R
		$reqMsgDTO->setNoOfRecurring($_REQUEST['numberRecurring']);
		// Merchant response URl
		$reqMsgDTO->setResponseUrl($_REQUEST['responseUrl']);
		// Optional additional fields for merchant
		$reqMsgDTO->setAddField1($_REQUEST['addField1']);
		$reqMsgDTO->setAddField2($_REQUEST['addField2']);
		$reqMsgDTO->setAddField3($_REQUEST['addField3']);
		$reqMsgDTO->setAddField4($_REQUEST['addField4']);
		$reqMsgDTO->setAddField5($_REQUEST['addField5']);
		$reqMsgDTO->setAddField6($_REQUEST['addField6']);
		$reqMsgDTO->setAddField7($_REQUEST['addField7']);
		$reqMsgDTO->setAddField8($_REQUEST['addField8']);
		
		/* 
		* After Making Request Message Send It To Generate Request 
		* The variable `$urlParameter` contains encrypted request message
		*/
		//Generate transaction request message
		$merchantRequest = "";
		
		$reqMsgDTO = $obj->generateTrnReqMsg($reqMsgDTO);
		
		if ($reqMsgDTO->getStatusDesc() == "Success"){
			$merchantRequest = $reqMsgDTO->getReqMsg();
		}

		/**payment insertion */
		$names = $_REQUEST['names'];
		$gender = $_REQUEST['gender'];
		$phone = $_REQUEST['phone'];
		$email = $_REQUEST['email'];
		$nationality = $_REQUEST['nationality'];
		$opdnum = $_REQUEST['opdnum'];
		$department = $_REQUEST['department'];
		$select_doctor = $_REQUEST['select_doctor'];
		$txt_appdate = $_REQUEST['txt_appdate'];
		$message = $_REQUEST['message'];
		$created_at = date("Y-m-d H:i:s");

		$sql = "INSERT INTO `tbl_hims_appointment`(`name`, `gender`, `mobile`, `email`, `opdnum`, `nationality`, `depid`, `docid`, `app_date`, `message`, `order_no`, `amount`, `currency`, `created_at`) VALUES ('$names','$gender','$phone','$email','$opdnum','$nationality','$department','$select_doctor','$txt_appdate','$message','$_REQUEST[OrderId]','$_REQUEST[amount]','$_REQUEST[currencyName]','$created_at')";
		$result = mysqli_query($con, $sql);

		if ($result !== false) {
			// Query was successful, print a success message
			$msg = "<h2 class='sub-heading-one text-center'>data inserted.</h2>";
		} else {
			// Query failed, print an error message
			$msg = "<h2 class='sub-heading-one text-center'>Error: " . mysqli_error($con)."</h2>";
		}
		/**payment insertion */
	
		?>


		<form action="https://cgt.in.worldline-solutions.com/ipg/doMEPayRequest" method="post" name="txnSubmitFrm">
			<h4 align="center">Redirecting To Payment Please Wait..</h4>
			<h4 align="center">Please Do Not Press Back Button OR Refresh Page</h4>
			<input type="hidden" size="200" name="merchantRequest" id="merchantRequest" value="<?php echo $merchantRequest; ?>"  />
			<input type="hidden" name="MID" id="MID" value="<?php echo $reqMsgDTO->getMid(); ?>"/>
		</form>
		<script  type="text/javascript">
			//submit the form to the worldline
			document.txnSubmitFrm.submit();
		</script>
<?php
		}
	}else{
		echo "Invalid form resubmission";
	}
?>