<?php

/*
* @package BANANA WARS
* @copyright: (c) 2017 G.P. aka Mvcshka (mvcshka@yandex.ru)
* @license: GNU General Public License version 2; see LICENSE.txt
* @files: /process/fill.php
*
*/


session_start();
header('Content-type: application/json');
Header("Content-Type: text/html;charset=UTF-8");
if($_SERVER['HTTP_X_REQUESTED_WITH'] != 'XMLHttpRequest' ) { exit();}
include($_SERVER['DOCUMENT_ROOT'].'/lib/connect.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/func.php');
include($_SERVER['DOCUMENT_ROOT'].'/lib/CofigFarm.php');

if(isset($_POST['cash']))
{
	$sum = sprintf ("%01.2f", str_replace(',', '.', $_POST['cash']));
	$ps = intval($_POST['payment']);
	if($sum >= 1)
	{
		if($ps >= 1 and $ps <= 3)
		{
			if($ps == 1) $pl = 'Payeer.com';
			if($ps == 2) $pl = 'FreeKassa';
			if($ps == 3) $pl = 'PerfectMoney';
			$q = $mysql->prepare("INSERT INTO db_enter SET UserId = ?, Login = ?, Summa = ?, PlatSystem = ?, DateAdd = ?, Status = ?");
			$q->execute(array($_SESSION['id'], $_SESSION['login'], $sum, $pl, time(), 0));
			$lid = $mysql->lastInsertId();
			if($ps == 1)
			{
				//Payeer
				$m_shop = $IdShopPayeer;
				$m_orderid = $lid;
				$m_amount = $sum;
				$m_curr = 'RUB';
				$m_desc = base64_encode('Пополнение баланса для пользователя '.$_SESSION['login']);
				$m_key = $PassShopPayeer;

				$arHash = array(
					$m_shop,
					$m_orderid,
					$m_amount,
					$m_curr,
					$m_desc,
					$m_key
				);
				$sign = strtoupper(hash('sha256', implode(':', $arHash)));
				echo json_encode($result = array("error" => '<div style="display:none; position:absolute; left:-9999px;"> 
				<form method="GET" action="https://payeer.com/merchant/" id="payForm">
				<input type="hidden" name="m_shop" value="'.$m_shop.'">
				<input type="hidden" name="m_orderid" value="'.$m_orderid.'">
				<input type="hidden" name="m_amount" value="'.$m_amount.'">
				<input type="hidden" name="m_curr" value="'.$m_curr.'">
				<input type="hidden" name="m_desc" value="'.$m_desc.'">
				<input type="hidden" name="m_sign" value="'.$sign.'">
				</form> 
				</div>
				<script type="text/javascript"> $("#payForm").submit(); </script><span class="green">Ожидайте перенаправления на платёжную систему</span>'));
			}
			elseif($ps == 2)
			{
				//Freekassa
				$merchant_id = $IdMerchantFreekassa;
				$secret_word = $SecretKeyFreeKassa1;
				$order_id = $lid;
				$order_amount = $sum;
				$sign = md5($merchant_id.':'.$order_amount.':'.$secret_word.':'.$order_id);
				
				echo json_encode($result = array("error" => "<div style=\"display:none; position:absolute; left:-9999px;\"><form method='get' action='http://www.free-kassa.ru/merchant/cash.php' id=\"payForm\">
    <input type='hidden' name='m' value='$merchant_id'>
    <input type='hidden' name='oa' value='$order_amount'>
    <input type='hidden' name='o' value='$order_id'>
    <input type='hidden' name='s' value='$sign'>
    <input type='hidden' name='lang' value='ru'>
    <input type='submit' name='pay' value='Оплатить'>
</form>
</div><script type=\"text/javascript\"> $(\"#payForm\").submit(); </script><span class=\"green\">Ожидайте перенаправления на платёжную систему</span>"));
			}
			else
			{
				//Perfectmoney
				
				
				
				echo json_encode($result = array("error" => '<div style="display:none; position:absolute; left:-9999px;"><form action="https://perfectmoney.is/api/step1.asp" method="POST" id="payForm">
					<input type="hidden" name="PAYEE_ACCOUNT" value="'.$cfgPerfect.'">
					<input type="hidden" name="PAYEE_NAME" value="'.$_SERVER['HTTP_HOST'].'">
					<input type="hidden" name="PAYMENT_ID" value="'.$lid.'">
					<input type="hidden" name="PAYMENT_AMOUNT" value="'.$sum.'">
					<input type="hidden" name="PAYMENT_UNITS" value="USD">
					<input type="hidden" name="STATUS_URL" value="http://'.$_SERVER['HTTP_HOST'].'/pmresult.php">
					<input type="hidden" name="PAYMENT_URL" value="http://'.$_SERVER['HTTP_HOST'].'/shop/">
					<input type="hidden" name="PAYMENT_URL_METHOD" value="POST">
					<input type="hidden" name="NOPAYMENT_URL" value="http://'.$_SERVER['HTTP_HOST'].'/enter/">
					<input type="hidden" name="NOPAYMENT_URL_METHOD" value="POST">
					<input type="hidden" name="BAGGAGE_FIELDS" value="">
					<input type="hidden" name="SUGGESTED_MEMO" value="'.$_SERVER['HTTP_HOST'].'">
					</div><script type="text/javascript"> $("#payForm").submit(); </script><span class="green">Ожидайте перенаправления на платёжную систему</span>'));
			}
		}
		else
		{
			echo json_encode($result = array("error" => "Укажите платежную систему"));
		}
	}
	else
	{
		echo json_encode($result = array("error" => "Минимум для пополнения 1 руб. или 1 USD"));
	}
}


?>