<?php  //die($_POST['totalInfantWrapper']);



   // include "yonetim/inc/fonk/argento.php";
  //  connectdatabase ();
    require "class/security.php";
    require "class/session.php";
   // require "class/save_.php";
   // require "class/class.phpmailer.php";

	include('functions.php');
  include('language.php');




   //die();
  // MailGonder('23525235325','test','testest'); die(); //for test

	//MailGonder('325326236326235' ,'Yasin', '20'); die();

    function sendPost($data) {
        $ch = curl_init();
        // you should put here url of your getinfo.php script
        curl_setopt($ch, CURLOPT_URL, "http://www.clubexelsior.com/reservation/api/storereservation?apikey=8E19B3FE0C8603C3F8A6453F3EDBFB12");
        curl_setopt($ch,  CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec ($ch);
        curl_close ($ch);
        return $result;
    }

    $_POST['reservation_no'] = mt_rand();
	//$result = sendPost( $_POST );
	/* echo '<div class="alert alert-success">
          <strong>Your reservation completed successfully. Thanks for choosing us!</strong><br><br>
		  <a id="close" href="/">X Close Reservation</a>
        </div>';*/
	// MailGonder($_POST['XID'],$_POST['custName'],'deneme 2 GBP');
	// exit();

    //$session  =   new session();



    $_POST  = array_map('c_sec_userinput', $_POST); // posttan gelen verileri güvenlikten geçir.

    (
    $_POST && $_SERVER['REQUEST_METHOD'] == 'POST'  && $_POST['toplam_para'] > 0
    )

    or die('<script>alert("Book your building first!");window.location="reservation.php";</script>');

    require_once('Posnet Modules/Posnet XML/posnet.php');



    if ((floatval(phpversion()) >= 5) && ((ini_get('register_long_arrays') == '0') || (ini_get('register_long_arrays') == '')))
    {

        $POST   =& $_POST;
        $GET    =& $_GET;
    }
    else
    {
        $POST            =& array_map('c_sec_userinput', $HTTP_POST_VARS);
        $GET             =& $HTTP_GET_VARS;
    }
	include('doviz/doviz_func.php');


		if($POST['amount'] > 0){
			$toplam	=	doviz_hesapla($POST['amount']);
			if(!$toplam){
				'<script>alert("price empty error");</script>';
				die;
			}
		}else{
			'<script>alert("price empty error");</script>';
			die;
		}
	function yymm($year_month){
			if(strlen($year_month) == 4){
				$ay		=	substr($year_month,0,2);
				$yil	=	substr($year_month,2,4);
				return $yil.$ay;
			}else{
				die('<script>alert("Incorrect expiration date format: MMYY");</script>');
			}

	}

  //var_dump($_POST);
/*
	$doviz	=	mysql_query("select * from doviz where d_id=3");
	if(mysql_num_rows($doviz) > 0){
		$doviz	=	mysql_fetch_array($doviz);
		$amount = 	$POST['amount'] > 0 ? number_format($POST['amount'], 2, '.', '') : '<script>alert("price empty error");</script>';
		$toplam	=	$doviz['doviz_alis'] * $amount;
	}else{
		echo '<script>alert("incorrect currency conversion");</script>';
	}
	*/

$hostname       = "https://posnet.yapikredi.com.tr/PosnetWebService/XML"; //https://posnet.yapikredi.com.tr/PosnetWebService/XML"; test : http://setmpos.ykb.com/PosnetWebService/XML Gerçek url : https://www.posnet.ykb.com/PosnetWebService/XML
$mid 			= 6748129753;//6797752273;
$tid 			= 67620289;//67012267;
$trantype 		= "Sale";
$ccno 			= $POST['ccno']; // kart no
$expdate 		= yymm($POST['expdate']); // son kullanma "Örn 2002 yymm"
$cvc 			= $POST['cvv']; // cvc
$orderid 		= $POST['XID']; // referans NO
$amount 		= number_format($toplam, 2, '', '');//number_format(1.00, 2, '', '');//number_format($toplam, 2, '', '');//number_format('1.00', 2, '', '');
//$amount 		= 1;
$wpamount 		= '';//$POST['wpamount'];
$currencycode 	= "TL"; // EU
$instnumber 	= 00;
$multpoint 		= 00;//$POST['multpoint'];
$extpoint 		= '000000';//$POST['extpoint'];
$hostlogkey 	= '';//$POST['hostlogkey'];
$authcode 		= '';//$POST['authcode'];
$vftcode 		= 'K001';//$POST['vftcode'];
$koicode 		= '';//$POST['koicode'];

$posnet = new Posnet;
// $posnet->SetDebugLevel(2);
$posnet->UseOpenssl();
$posnet->SetURL($hostname);
$posnet->SetMid($mid);
$posnet->SetTid($tid);
// $posnet->SetUsername($username);
// $posnet->SetPassword($password);
$posnet->SetKOICode($koicode);

if ($trantype == "Auth") {
    $posnet->DoAuthTran(
        $ccno,
        $expdate, // Ex : 0703 - Format : YYMM
        $cvc,
        $orderid,
        $amount, // Ex : 1500->15.00 YTL
        $currencycode, // Ex : YT
        $instnumber // Ex : 05
    );
}
else if ($trantype == "Sale") {
    $posnet->DoSaleTran(
        $ccno,
        $expdate, // Ex : 0703 - Format : YYMM
        $cvc,
        $orderid,
        $amount, // Ex : 15000->150.00 YTL
        $currencycode, // Ex : YT
        $instnumber // Ex : 05
    );
}
else if ($trantype == "SaleWP") {
    $posnet->DoSaleWPTran(
        $ccno,
        $expdate, // Ex : 0703 - Format : YYMM
        $cvc,
        $orderid,
        $amount, // Ex : 1500->15.00 YTL
        $wpamount, // Ex : 1500->15.00 YTL
        $currencycode, // Ex : YT
        $instnumber // Ex : 05
    );
}
else if ($trantype == "Capture") {
    $posnet->DoCaptTran(
        $hostlogkey,
        $authcode,
        $amount,
        $currencycode, // Ex :YT
        $instnumber // Ex : 05
    );
}
else if ($trantype == "AuthRev") {
    $posnet->DoAuthReverseTran(
        $hostlogkey,
        $authcode );
}
else if ($trantype == "SaleRev") {
    $posnet->DoSaleReverseTran(
        $hostlogkey,
        $authcode );
}
else if ($trantype == "CaptureRev") {
    $posnet->DoCaptReverseTran(
        $hostlogkey,
        $authcode );
}
else if ($trantype == "Return") {
    $posnet->DoReturnTran(
        $hostlogkey,
        $amount,
        $currencycode // Ex :YT
    );
}
else if ($trantype == "PNTU") {
    $posnet->DoPointUsageTran(
        $ccno,
        $expdate, // Ex : 0703 - Format : YYMM
        $orderid,
        $amount, // Ex : 1500->15.00 YTL
        $currencycode // Ex : YT
    );
}
else if ($trantype == "PNTV") {
    $posnet->DoPointReverseTran(
        $hostlogkey);
}
else if ($trantype == "PNTR") {
    $posnet->DoPointReturnTran(
        $hostlogkey,
        $wpamount,
        $currencycode // Ex :YT
    );
}
else if ($trantype == "PNTI") {
    $posnet->DoPointInquiryTran(
        $ccno,
        $expdate // Ex : 0703 - Format : YYMM
    );
}
// VFT Transactions
else if ($trantype == "VFTI") {
    $posnet->DoVFTInquiry(
        $ccno,
        $amount, // Ex : 1500->15.00 YTL
        $instnumber, // Ex : 05
        $vftcode );
}
else if ($trantype == "VFTS") {
    $posnet->DoVFTSale(
        $ccno,
        $expdate, // Ex : 0703 - Format : YYMM
        $cvc,
        $orderid,
        $amount, // Ex : 1500->15.00 YTL
        $currencycode, // Ex : YT
        $instnumber, // Ex : 05
        $vftcode );
}
else if ($trantype == "VFTR") {
    $posnet->DoVFTSaleReverse(
        $hostlogkey,
        $authcode );
}
// KOI Transactions
else if ($trantype == "KOIInq") {
    $posnet->DoKOIInquiry($ccno);
}
else{
echo '<script type="text/javascript">alert("Hatalı İşlem kodu !");</script>';
}
    $onay_durumu = $posnet->GetApprovedCode();

	//die($posnet->GetApprovedCode().'-'.$posnet->GetResponseText()); //hata olarak ne döndüğü


	//$onay_durumu = 1;
    if ($onay_durumu == 1) {
		//connectdatabase ();
		MailGonder($POST['XID'],$POST['custName'],$toplam);
        $result = sendPost( $_POST );
        //KrediKartiRezervasyonSave($POST['XID'],$toplam);

        echo '<div class="alert alert-success">
          <strong>Your reservation completed successfully. Thank you for choosing us!</strong><br><br>
          <a id="close" href="/">X</a>
        </div>';
        header( "refresh:2;url=https://clubexelsior.com" );
    }else if($onay_durumu==0){
        echo '<script type="text/javascript">alert("Error Failed Payment.");</script>';
    }else if($onay_durumu==3){
         echo '<script type="text/javascript">alert("Previously Approved Payment");</script>';
    }else{
        echo '<script type="text/javascript">alert("illegal Operation");</script>';
    }
