<?php
include 'config.php';

function get_openssl_key($publicKey){
    $key = "-----BEGIN PUBLIC KEY-----\n" . chunk_split($publicKey, 64, "\n") . '-----END PUBLIC KEY-----';
    $key = openssl_get_publickey($key);
    return $key;
}

    $publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArNAuWbJb9TZn/oaH/2nlQBAmcOj4NNEj7ZnDU4mkm9DnE7ft8xP+d82/8OHVN30h+yf2XDaEssFDWsAQftEvVh9jgytFrrFXRnZEZ6DgKrj9W7lWMDb172Cp37z994Wn6TIXnp/TCZbHYqFrIbq519tYToYh04iReWyq/mRpQMJiBe8kZPaOeG4Fw8VHQ/dN9WKg5+djmfgiDb8AkNiZW1pLbDXq4ZVDGHFwJ9/I6MRMeOcfHIgcgHxrcnOBwvkZf5gjVKxBqVr2EQxldURpaltSLAEJ3RmsCpfcXtp0s+HFR3b61nwwEt+JH3RTQf0z2IAEIgaDVb1icTwjaxjSpwIDAQAB";

    $creditIAPData["1paket"] = 50;
    $creditIAPData["2paket"] = 100;
    $creditIAPData["3paket"] = 250;
    $creditIAPData["4paket"] = 500;
    $creditIAPData["5paket"] = 1000;
    $creditIAPData["6paket"] = 999999;
    $creditIAPData["7paket"] = 9999999;
    $creditIAPData["visitor"] = 3152;
    $creditIAPData["callunlock"] = 1;
    $creditIAPData["visitorunlock"] = 2;
    





$uid = $_GET["uid"];
$userRow = $db->query("SELECT * FROM kullanicilar WHERE user_id = '$uid'")->fetch(PDO::FETCH_ASSOC);



function CheckPurchase($data, $signature){
    global $publicKey;
    global $db;
    global $creditIAPData;
    global $userRow;

$user_id = $userRow['user_id'];
$user_fullname = $userRow['user_fullname'];
$user_email = $userRow['user_email'];
$user_jeton = $userRow['user_jeton'];


$data = str_replace(" ","+",$data);

$signature = str_replace(" ","+",$signature);

//  $dosya_adi = "credit-tokens.txt"; 
// $deger = urldecode($_SERVER['REQUEST_URI']);
// $yazilacak_deger = "$data\n\n\n\n$signature"; 

// if ($deger) {  

// if (!file_exists($dosya_adi)){  
   
// touch($dosya_adi); 
// chmod($dosya_adi,0666); 
   
// } 

// $dosyaya_baglanti = fopen($dosya_adi,"a+"); 

// if (!fwrite($dosyaya_baglanti,$yazilacak_deger)){ 

// exit; 

// }  
// }





    $publicKey = get_openssl_key($publicKey);
    $result = openssl_verify($data, base64_decode($signature), $publicKey, OPENSSL_ALGO_SHA1);


if($result==1){
     $dataObj = json_decode($data);
     $gpaCode = $dataObj->orderId;
     $miktar = $creditIAPData[$dataObj->productId];
     $urun = $dataObj->productId;
     $zaman = substr($dataObj->purchaseTime,0,10);
     $newKredi = $user_jeton+$miktar;





$varMi = $db->query("SELECT COUNT(*) FROM odemeler WHERE gpa = '$gpaCode'")->fetchColumn();


if($varMi==0){



 if($miktar=="2"){

$sql = "UPDATE kullanicilar SET user_visit = ? WHERE user_id = ?";
$update = $db->prepare($sql)->execute([time(), $user_id]);

$o_type = "paket";

    }elseif($miktar=="1"){

$sql = "UPDATE kullanicilar SET user_call = ? WHERE user_id = ?";
$update = $db->prepare($sql)->execute([time(), $user_id]);

$o_type = "paket";

    }else{


$sql = "UPDATE kullanicilar SET user_jeton = ? WHERE user_id = ?";
$update = $db->prepare($sql)->execute([$newKredi, $user_id]);

$o_type = "jeton";

}

$tarih = time();
$query = $db->prepare("INSERT INTO odemeler SET
user_fullname = ?,
user_email = ?,
user_id = ?,
gpa = ?,
miktar = ?,
urun = ?,
purchaseToken = ?,
type = ?,
status = ?,
tarih = ?");
$insert = $query->execute(array(
     $user_fullname,$user_email,$user_id,$gpaCode,$miktar,$urun,$dataObj->purchaseToken,$o_type,1,$tarih
));




	if($insert AND $update){
		$array = array("result" => "Başarıyla Satın Aldınız!");
		echo json_encode($array);
	}else{
			$array = array("result" => "HATA KODU 1: Bizimle İletişime Geçin!");
			echo json_encode($array);
	}
		}else{
 $tarih = time();
$sql = "UPDATE kullanicilar SET user_ban=? WHERE user_id=?";
$db->prepare($sql)->execute([$tarih, $user_id]);

				$array = array("result" => "YASAKLANDINIZ - KOD: 2"); // TEKRARLAYAN KOD
				echo json_encode($array);
			  }

				}else{
 $tarih = time();
$sql = "UPDATE kullanicilar SET user_ban=? WHERE user_id=?";
$db->prepare($sql)->execute([$tarih, $user_id]);

					$array = array("result" => "YASAKLANDINIZ - KOD: 3"); // LUCKYPATCHER DETECT
					echo json_encode($array);
					 }



}



CheckPurchase($_GET["response_data"], $_GET["sig"]);

    //String sig = "QbvaavrpVreivrAjUVfFPPqGFA diMIoVkiodR/qmBL0xEYOIjZ15zv9VXjwwu/iLhxQIrrteMUJOcFp4or j4fVo zr6yhJ/dAF/qDfVBvVi2kFQLWXpkfICVB3U4eNJaPztNLrETocwTaEq0R456eLkgAwkWjDAJ5NvZKusUavgyFC5yl0fsty/BWptkhJJZpPfF5S8nDlx1Afk8btKrKDufqoLS xAlviJAIakApP4Qv6LYV4MwnVYiPHR67EE7JGtz4O2MEoXp3Nu6tGXgpabaj07IE9qhtqpUqSXrysQsELBPMohsrvPJxjdI8/GXYho/8oO00Y1zYAQlhgsQ==";
            

            //String response_data = "{\"orderId\":\"GPA.3369-4501-6435-25722\",\"packageName\":\"com.iddaatahminleri.bankobank\",\"productId\":\"krediler20\",\"purchaseTime\":1558259620017,\"purchaseState\":0,\"developerPayload\":\"inapp:krediler20:054b8329-d662-4578-8924-62b0e458d977\",\"purchaseToken\":\"fgonjabiemdldkfnkklomabe.AO-J1OzVNHkLHW3k2a0Ks3KJ6r5NL65r2nXgcYjgeOsTDHYBO1MFXxz1drRhjkbxNml5VnUcp8LZQg1KektNpg0I81rcVRoYw8E_PM9ikwRTovh0PdhSalRu-NR3-hD8bNwm2j5UenSt\"}";



?>