<?php
class Ips extends Pay{
	private $info;
	private $path;
	function __construct(){
		$path = str_replace("/Ips.class.php","/",str_replace("\\","/",__FILE__));
		$this->path = $path;
	}
	function pay($paymentId,$amount,$tradeNo){
		$payConfig = $this->getConfig($paymentId);
		if(!$payConfig){Msg::show('该支付接口未配置');}
		$out_trade_no = $tradeNo;
		$subject      = $_POST['subject'];
		$body         = $_POST['body'];
		$show_url			= 'http://'.$_SERVER['HTTP_HOST'];
		$info['return_url'] = "http://".$_SERVER['HTTP_HOST']."/pay/payReturn/ips";
		$info['notify_url'] = "http://".$_SERVER['HTTP_HOST']."/pay/payNotify/ips";
		$params['Mer_code'] = $payConfig['mercode'];
		$params['Billno'] = $tradeNo;
		$params['Amount']   = number_format($amount,2,".","");
		$params['Date']   = date('Ymd');
		$params['Currency_Type']    = 'RMB';
		$params['Gateway_Type']= '01';
		$params['Lang']    = 'GB';
		$params['Merchanturl']    = $show_url."/pay/result/status/success";
		$params['FailUrl']    = $show_url."/pay/result/status/failed";
		$params['OrderEncodeType']    = '5';
		$params['RetEncodeType']    = '17';
		$params['Rettype']    = '1';
		$params['ServerUrl']    =  $info['notify_url'];
		//$params['transport']    =;
		$orge = 'billno'.$params['Billno'].'currencytype'.$params['Currency_Type'].'amount'.$params['Amount'].'date'.$params['Date'].'orderencodetype'.$params['OrderEncodeType'].$payConfig['merkey'] ;
		$SignMD5 = md5($orge) ;
		$params['SignMD5']=$SignMD5;
		$submitUrl='https://pay.ips.com.cn/ipayment.aspx';
		if($payConfig['mercode']=='000015'){$submitUrl='https://pay.ips.net.cn/ipayment.aspx';}
		$html = '<form action="'.$submitUrl.'" method="post" id="form">';
		foreach($params as $key=>$val){$html .= '<input type="hidden" name="'.$key.'" value="'.$val.'" />';}
		$html .='<input type=submit value="确认支付"></form><script>document.getElementById("form").submit();</script>';
		echo $html;
	}
	
	function payReturn(){
		
	}
	function payNotify(){
		global $db;
		$outTradeNo	= trim($_GET['billno']);	//获取订单号
		if($outTradeNo){$log = '';foreach($_GET as $key=>$val){if($key!='msg'){$log .= $key.'='.$val.'&';}else{$log .= $key.'='.mb_convert_encoding($val,"UTF-8","GBK").'&';}}if($log){saveLog('环迅支付通知数据：'.$log);}}else{ exit('订单号丢失');return false; }
		$paymentId = $db->getOne("select paymentId from qk_onlinepay where outTradeNo='$outTradeNo'");
		$payConfig = $this->getConfig($paymentId);
		$payAmount = $_GET['amount'];
		$mydate = $_GET['date'];
		$succ = $_GET['succ'];
		$msg = $_GET['msg'];
		$attach = $_GET['attach'];
		$ipsbillno = $_GET['ipsbillno'];
		$retEncodeType = $_GET['retencodetype'];
		$currency_type = $_GET['Currency_type'];
		$signature = $_GET['signature'];
		$content = 'billno'.$outTradeNo.'currencytype'.$currency_type.'amount'.$payAmount.'date'.$mydate.'succ'.$succ.'ipsbillno'.$ipsbillno.'retencodetype'.$retEncodeType;
		$cert = $payConfig['merkey'];
		$signature_1ocal = md5($content . $cert);
		if ($signature_1ocal == $signature)
		{
			if ($succ == 'Y')
			{
				if($this->payResult($outTradeNo,$payAmount,'','')){
					$ret = "success";
				}else{
					$ret = "fail";
				}
				saveLog('环迅支付通知结果：交易成功，数据比对'.$ret);echo '交易成功';
			}
			else
			{
				saveLog('环迅支付通知结果：交易失败');echo '交易失败！';exit;
			}
		}
		else
		{
			saveLog('环迅支付通知结果：签名不正确');echo '签名不正确！';exit;
		}
	}
}
?>