<?php 
require_once('includes/check-login.php');
require_once('includes/config.php');
$result=$con->query("SELECT * from payment_settings");
if(mysqli_num_rows($result)>0){
 $payrow=mysqli_fetch_array($result);
 $salt=$payrow['salt_key']; 
 $_POST['api_key']=$payrow['api_key'];
}
$user_id=$_SESSION['userid'];
$sql=$con->query("select ud.* FROM users U JOIN user_details ud ON U.user_id = ud.user_id JOIN user_roles ur ON U.user_id = ur.user_id JOIN roles r ON ur.role_id = r.role_id WHERE U.active='1' and U.user_id='$user_id'");	
$name="";
$email="";
$phone="";
$city="";
$state="";
$country="";
$zip_code="";
if(mysqli_num_rows($sql)>0){
$row=mysqli_fetch_array($sql);
$name=$row['name'];
$email=$row['email'];
$phone=$row['phone'];
$city=isset($row['city'])? $row['city']
 : 'chennai';
$state=isset($row['state']) ? $row['state'] :'tamilnadu';
$country=isset($row['country']) ? $row['country'] :'india';
$zip_code=isset($row['pincode']) ? $row['pincode'] : '600001';   
}

$amount=isset($_GET['amount']) ? $_GET['amount']:0;
$_POST['order_id']=$_SESSION['userid'];
$_POST['amount']=$amount;
$_POST['currency']="INR";
$_POST['name']=$name;
$_POST['email']=$email;
$_POST['phone']=$phone;
$_POST['description']="Add Wallet Amount";
$_POST['city']=$city;
$_POST['state']=$state;
$_POST['zip_code']=$zip_code;
$_POST['country']=$country;
$_POST['return_url']="".$base_url."addwallet.php"; 
$hash = hashCalculate($salt, $_POST);

function hashCalculate($salt,$input){
	/* Columns used for hash calculation, Donot add or remove values from $hash_columns array */
	$hash_columns = ['address_line_1', 'address_line_2', 'amount', 'api_key', 'city', 'country', 'currency', 'description', 'email', 'mode', 'name', 'order_id', 'phone', 'return_url', 'state', 'udf1', 'udf2', 'udf3', 'udf4', 'udf5', 'zip_code',];
	/*Sort the array before hashing*/
	sort($hash_columns);

	/*Create a | (pipe) separated string of all the $input values which are available in $hash_columns*/
	$hash_data = $salt;
	foreach ($hash_columns as $column) {
		if (isset($input[$column])) {
			if (strlen($input[$column]) > 0) {
				$hash_data .= '|' . trim($input[$column]);
			}
		}
	}
	$hash = strtoupper(hash("sha512", $hash_data));
	
	return $hash;
}
?>
<p>Redirecting...</p>
<form action="https://pay.basispay.in/v2/paymentrequest" id="payment_form" method="POST">
<input type="hidden" value="<?php echo $hash; ?>"  name="hash"/>
<input type="hidden" value="<?php echo $_POST['api_key'];?>"        name="api_key"/>
<input type="hidden" value="<?php echo $_POST['return_url']; ?>"    name="return_url"/>
<input type="hidden" value="<?php echo $_POST['mode'];?>"           name="mode"/>
<input type="hidden" value="<?php echo $_POST['order_id'];?>"       name="order_id"/>
<input type="hidden" value="<?php echo $_POST['amount'];?>"         name="amount"/>
<input type="hidden" value="<?php echo $_POST['currency'];?>"       name="currency"/>
<input type="hidden" value="<?php echo $_POST['description'];?>"    name="description"/>
<input type="hidden" value="<?php echo $_POST['name'];?>"           name="name"/>
<input type="hidden" value="<?php echo $_POST['email'];?>"          name="email"/>
<input type="hidden" value="<?php echo $_POST['phone'];?>"          name="phone"/>
<input type="hidden" value="<?php echo $_POST['address_line_1'];?>" name="address_line_1"/>
<input type="hidden" value="<?php echo $_POST['address_line_2'];?>" name="address_line_2"/>
<input type="hidden" value="<?php echo $_POST['city'];?>"           name="city"/>
<input type="hidden" value="<?php echo $_POST['state'];?>"          name="state"/>
<input type="hidden" value="<?php echo $_POST['zip_code'];?>"       name="zip_code"/>
<input type="hidden" value="<?php echo $_POST['country'];?>"        name="country"/>
<input type="hidden" value="<?php echo $_POST['udf1'];?>"           name="udf1"/>
<input type="hidden" value="<?php echo $_POST['udf2'];?>"           name="udf2"/>
<input type="hidden" value="<?php echo $_POST['udf3'];?>"           name="udf3"/>
<input type="hidden" value="<?php echo $_POST['udf4'];?>"           name="udf4"/>
<input type="hidden" value="<?php echo $_POST['udf5'];?>"           name="udf5"/>
<noscript><input type="submit" value="Continue"/></noscript>
</form>
<script>
function formAutoSubmit () {
	var payform = document.getElementById("payment_form");
	payform.submit();
}
window.onload = formAutoSubmit;
</script>
