<?php
	if($_POST["pb"]=="TRY"){
		$pb="TL";
	}else{
		$pb=$_POST["pb"];
	}
	if($_POST["Installment"]=="1"){
		$taksit="0";
	}else{
		$taksit=$_POST["Installment"];
	}
	$bol = explode(' / ',$_POST["Card_Expiry"]); $ay=$bol[0]; $yil=$bol[1];
	$moka_url = "https://service.moka.com/PaymentDealer/DoDirectPaymentThreeD";
	if($_SESSION["DealerCode"]==""){
		$dealer_code = tanim("pay+Moka","n3",0);
		$username = tanim("pay+Moka","n1",0);
		$password = tanim("pay+Moka","n2",0);
	}else{
		$dealer_code = $_SESSION["DealerCode"];
		$username = $_SESSION["Username"];
		$password = $_SESSION["Password"];
	}
	$currency = $pb;
	$InstallmentNumber = $taksit;
	$OtherTrxCode = "111";
	$SubMerchantName = "";
	$RedirectUrl = "".$rst["n1"]."/vpos/moka/return.php?MyTrxCode=".$OtherTrxCode."";
	$checkkey = hash("sha256",$dealer_code."MK".$username."PD".$password);
	$veri = array('PaymentDealerAuthentication'=>array('DealerCode'=>$dealer_code,'Username'=>$username,'Password'=>$password,'CheckKey'=>$checkkey),
	'PaymentDealerRequest'=>array('CardHolderFullName'=>$_POST['first-name'],
	'CardNumber'=>str_replace(" ", "", $_POST['number']),
	'ExpMonth'=>$ay,
	'ExpYear'=>$yil,
	'CvcNumber'=>$_POST['cvc'],
	'Amount'=>$_POST["tutar"],
	'Currency'=>$currency,
	'InstallmentNumber'=>$InstallmentNumber,
	'ClientIP'=>$_SERVER['REMOTE_ADDR'],
	'RedirectUrl'=>$RedirectUrl,
	'OtherTrxCode'=>$OtherTrxCode,
	'SubMerchantName'=>$SubMerchantName));
	$veri = json_encode($veri);
	//echo $veri;
	$ch = curl_init($moka_url); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
	curl_setopt($ch, CURLOPT_POST, 1); 
	curl_setopt($ch, CURLOPT_POSTFIELDS, $veri);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
	curl_setopt($ch, CURLOPT_SSLVERSION, 6);	  // TLS 1.2 baglanti destegi icin
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);    // ssl sayfa baglantilarinda aktif edilmeli
	$result = curl_exec ($ch); 
	curl_close ($ch);
	$result = json_decode($result);
	//echo $result;
	if($result->ResultCode == 'Success'){
		echo '<div class="alert alert-success">
			<p>KART BILGILERINIZ DOGRULANDI !!!
			<br><br>
				Simdi kart güvenligini dogrulamak için bankanin sayfasina yönlendirileceksiniz. 
			<br>
				Burada kart sahibinin bankada kayitli cep telefonuna gelecek SMS\'i girdikten sonra islemleriniz tamamlanacaktir.
			</p>
			<p>
				<a href="'.$result->Data.'" class="btn btn-primary">Yönlendirme çalismaz ise buraya tiklayin...</a>				
			</p>
		</div>';
		echo "<script>window.location.href='".$result->Data."'</script>";
		//header("Location: ".$result->Data);
		exit;
	}else{
		echo '<div class="alert alert-danger">
			<p>Kart bilgileri dogrulanamadi. Tekrar denemek için <a href="#" onclick="window.location.reload();">buraya tiklayin.</a></p>
		</div>';
	}
?>