<?php
	header('Content-type: text/html; charset=utf-8');
	$path="../../../../../";
	include("".$path."deppo/tools/script/cevir.php");
	$path2="config/"; include("".$path."config/map.php");
	include("".$path."deppo/tools/script/fonksiyon".$ver.".php");
    ####################### DÜZENLEMESİ ZORUNLU ALANLAR #######################
    #
    ## API Entegrasyon Bilgileri - Mağaza paneline giriş yaparak BİLGİ sayfasından alabilirsiniz.
	$app_id = tanim("pay+qnbpay","n1",0);
	$app_secret = tanim("pay+qnbpay","n2",0);
	$merchant_key = tanim("pay+qnbpay","n3",0);
    ###########################################################################
	$order_no = $_GET['order_no'] ?? null;
	$order_id = $_GET['order_id'] ?? null;
	$invoice_id = $_GET['invoice_id'] ?? null;
	$status_code = $_GET['status_code'] ?? null;
	$status_description = $_GET['status_description'] ?? null;
	$payment_status = $_GET['payment_status'] ?? null;
	$error = $_GET['error'] ?? null;
	$hash_key = $_GET['hash_key'] ?? null;
	$merchant_commission = $_GET['merchant_commission'] ?? null;
	$user_commission = $_GET['user_commission'] ?? null;
	function validateHashKey($hashKey, $secretKey){
		$status = $currencyCode = "";
		$total = $invoiceId = $orderId = 0;
		if (!empty($hashKey)) {
			$hashKey = str_replace('__', '/', $hashKey);
			$password = sha1($secretKey);
			$components = explode(':', $hashKey);
			if (count($components) > 2) {
				$iv = isset($components[0]) ? $components[0] : "";
				$salt = isset($components[1]) ? $components[1] : "";
				$salt = hash('sha256', $password . $salt);
				$encryptedMsg = isset($components[2]) ? $components[2] : "";
				$decryptedMsg = openssl_decrypt($encryptedMsg, 'aes-256-cbc', $salt, null, $iv);
				if (strpos($decryptedMsg, '|') !== false) {
					$array = explode('|', $decryptedMsg);
					$status = isset($array[0]) ? $array[0] : 0;
					$total = isset($array[1]) ? $array[1] : 0;
					$invoiceId = isset($array[2]) ? $array[2] : '0';
					$orderId = isset($array[3]) ? $array[3] : 0;
					$currencyCode = isset($array[4]) ? $array[4] : '';
				}
			}
		}
		return [$status, $total, $invoiceId, $orderId, $currencyCode];
	}
	$is_valid_hash = validateHashKey($hashKey, $app_secret);
	if (!$is_valid_hash) {
		die("Geçersiz hash doğrulama! Ödeme sonucu güvenilir değil.");
	}
	if ($payment_status == "1") {
		echo "<script>window.location.href=\"".$_SESSION["burl"]."ok/".$order_no."\"</script>";
	} else {
		echo "<script>window.location.href=\"".$_SESSION["burl"]."err/".base64_encode($error)."\"</script>";
	}
?>