<?php
class WorldKassa {
	private $id = 7553;
	private $hash = "hashGFkkwe87udfjhg2";
	private $db = null;
	
	public function __construct($db){
		$this->db = $db;
	}
	
	public function generationPay($summa=0,$com="Пополнение баланса"){
		return 'http://worldkassa.ru/user/oplata.php?id_shop='.$this->id.'&summa='.$summa.'&hash='.$this->hash.'&desc='.urlencode($com);
	}
	
	public function getData($url){
		 $ch = curl_init();
		 curl_setopt($ch, CURLOPT_URL, $url);
		 curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
		 $out = curl_exec($ch);
		 curl_close($ch);
		 return $out;
	}
	
	public function redirectPay($out,$sid,$summa){
		if (is_numeric($out)){
			$this->db->Query("INSERT INTO `worldkassa`(`sid`, `pid`, `summa`, `timePay`,`lastError`) VALUES ('".$sid."','".$out."','".$summa."','".(time())."','')");
			header("Location: http://worldkassa.ru/user/oplata.php?uniq=".$out);
			exit();
		} else {
			return ["status"=>"error","msg"=>$out];
		}
	}
	
	public function dataPay($id_pay){
		$out = 'http://worldkassa.ru/api.php?id_shop='.$this->id.'&hash='.$this->hash.'&func=info&id_buy='.$id_pay;
		$o = $this->getData($out);
		return json_decode($o,true);
	}
	
	public function checkHash($hash,$id_pay,$summa){
		$h = md5($this->hash.$this->id.$id_pay.$summa);
		if ($hash != $h){
			return false;
		} else {
			return true;
		}
	}
}
?>