<?php include("panel/system/ayar.php"); ?>
<?php include("panel/system/fonksiyon.php"); ?>
<!doctype html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <title>Ortak Ödeme sayfası</title>
</head>
<body>

<div style="width: 100%;margin: 0 auto;display: table;">
<?php 

$zorunlular = array('merchant_id','user_ip','merchant_oid','email','payment_amount','paytr_token','user_basket','debug_on','no_installment','max_installment','user_name','user_address','user_phone','merchant_ok_url','merchant_fail_url','timeout_limit','currency');

if ($_POST) {
	$guvenli = true;
	foreach ($zorunlular as $v) {
		if(!isset($_POST[$v])) $guvenli = false;
	}
	if(!isset($_SESSION["urunler"])) die("Hatalı sess.");
	if(!isset($_SESSION["adres"])) die("Hatalı sess adrs.");
	if(!$guvenli) die("Parametreler hatalı!");

	$ch=curl_init();
	curl_setopt($ch, CURLOPT_URL, "https://www.paytr.com/odeme/api/get-token");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1) ;
	curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
	curl_setopt($ch, CURLOPT_TIMEOUT, 20);
	$result = @curl_exec($ch);

	if(curl_errno($ch)) die("PAYTR IFRAME connection error. err:".curl_error($ch));

	curl_close($ch);

	$result=json_decode($result,1);


	if($result['status']=='success')
		$token=$result['token'];
	else
		die("PAYTR IFRAME failed. reason:".$result['reason']);

	$urunler = "";
	$secenek = "";
	foreach( $_SESSION["urunler"] as $urun) {
		$Query = Sorgu("SELECT * FROM urunler WHERE id='$urun[id]'");
		if(mysql_num_rows($Query)>0) {
			$Row = mysql_fetch_array($Query);
			$urunler.= $Row["id"]."|".$urun["adet"]."//";
			$secenek[$Row['id']] = array( "id" => $urun['secenek']);
			//$stokazalt = Sorgu("UPDATE urunler SET stok=stok-$urun[adet] WHERE id='$urun[id]' ");
		}
	}

	$adres 		= $_SESSION["adres"];
	$fiyat 		= ($_POST['payment_amount']/100);
	$uyeid 		= $_SESSION['uyeid'];
	$sno 		=  $_POST['merchant_oid'];
	$ip			= ip();
	$t			= date("Y-m-d H:i:s");
	$tarih		= tarih($t);
	if(isset($secenek) && is_array($secenek)){
		$secenek = json_encode($secenek,true);
	}else{
		$secenek = json_encode(array(),true);
	}
	$siparis_ver = Sorgu("INSERT INTO siparisler SET
					urun		=	'$urunler',
					uyeid		=	'$uyeid',
					fiyat		=	'$fiyat',
					adres		=	'$adres',
					secenek		=	'$secenek',
					odeme_sekli	=	'PAYTR',
					sno			=	'$sno',
					ip			=	'$ip',
					tarih		=	'$tarih'");

	unset($_SESSION["urunler"]);
	unset($_SESSION["adres"]);
	$_SESSION["paytrsno"] = $sno;

}

 ?>

 	<!-- Ödeme formunun açılması için gereken HTML kodlar / Başlangıç -->
    <script src="https://www.paytr.com/js/iframeResizer.min.js"></script>
    <iframe src="https://www.paytr.com/odeme/guvenli/<?php echo $token;?>" id="paytriframe" frameborder="0" scrolling="no" style="width: 100%;"></iframe>
	<script>iFrameResize({},'#paytriframe');</script>
	<!-- Ödeme formunun açılması için gereken HTML kodlar / Bitiş -->

</div>

<br><br>
</body>
</html>
