<?php

class PlatnosciController extends Zend_Controller_Action {




	
	
	
		function cr($url,$referer,$postFields)
		{
			$ch = @curl_init();
			@curl_setopt($ch, CURLOPT_URL,$url);
			@curl_setopt($ch, CURLOPT_HEADER, 1);
			@curl_setopt($ch, CURLOPT_VERBOSE, 0);
			@curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			@curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; pl; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3');
			@curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
			@curl_setopt($ch, CURLOPT_ENCODING, 'deflate');
			@curl_setopt($ch, CURLOPT_ENCODING, '');
			@curl_setopt($ch, CURLOPT_REFERER, $referer);
			@curl_setopt($ch, CURLOPT_COOKIEJAR, __FILE__.'/cookie.txt');
			@curl_setopt($ch, CURLOPT_COOKIEFILE, __FILE__.'/cookie.txt');
			@curl_setopt($ch, CURLOPT_POST, 1);
			@curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
			@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			$returned = @curl_exec($ch);
			$info = @curl_getinfo($ch);
			@curl_close($ch); 
			return array($returned,$info);
		}
		

		function createPostString($aPostFields) 
		{
				foreach ($aPostFields as $key => $value) {
						$aPostFields[$key] = urlencode($key) . '=' . urlencode($value);
				}
				return implode('&', $aPostFields);
		}




	public function init() {		
	
	}	
	
	
	public function negatywnaAction() {			
		$v = $this->_request->getParams();
		$vv = $this->_request->getPost();
		var_dump($vv);
	}
	
	
	public function pozytywnaAction() {			
		$v = $this->_request->getParams();
	}

	public function raportplatnosciAction() {	
		$this->_helper->viewRenderer->setNoRender();
		$v = $this->_request->getParams();
	//	echo PAY_pos_auth_key;
	
		$post['pos_id'] = $v['pos_id'];
		$post['session_id'] = $v['session_id'];
		$post['ts'] = $v['ts'];
		$post['sig'] = md5($v['pos_id'].$v['session_id'].$v['ts'].PAY_key);
		$url = 'https://secure.payu.com/paygw/ISO/Payment/get/txt';			
		$post = $this->createPostString($post);
		$bufor  = $this->cr($url,$_SERVER['SCRIPT_URI'],$post);	
			
		$arr  = explode("\n",$bufor[0]);
		foreach( $arr as $key => $value ){
			
			$t = explode(':',$value);
			$t[0] = trim($t[0]);
			$t[1] = trim($t[1]);
				
			if( $t[0] == 'trans_order_id' ) $order_id = $t[1];
			if( $t[0] == 'trans_amount' ) $paid = $t[1];
			if( $t[0] == 'trans_status' ) $status = $t[1];
			if( $t[0] == 'trans_pay_type' ) $paytype = $t[1];
			
		}
	
		$PayRaport = new PayRaport();	
		$vt = var_export($bufor, true);
		
		$data = array(
			'order_id' => $order_id,
			'pay_raport_desc' => $vt,
			'pay_raport_date' => Lib_Date::datetime()
		);
		$PayRaport->insert($data);
		
		
		if(eregi('status: OK',$bufor[0]) && eregi('trans_status: 99',$bufor[0]))
		{
			$Order = new Order();
			$data = array(
				'order_pay' => 1,
				'order_pay_date' => Lib_Date::datetime()
			);
			$Order->update( $data , 'order_id='.$order_id );
		}
		mail('kuchniepw@gmail.com', 'raport i-meble '.time(), $vt);
		
		echo'OK';
	
	}
	
}
