<?php
class KuaiQian extends Pay{
	private $info;
	private $path;
	private $debug = false;
	function __construct(){
		$path = str_replace("/KuaiQian.class.php","/",str_replace("\\","/",__FILE__));
		$this->path = $path;
	}
	function pay($paymentId,$amount,$outTradeNo){
		$domain = $_SERVER['HTTP_HOST'];
		$payConfig = $this->getConfig($paymentId);
		$merchantAcctId = $payConfig['merchantAcctId'];
		$key = $payConfig['secret'];
		$inputCharset = '3';
		$bgUrl = 'http://'.$domain.'/pay/payReturn/kuaiqian';
		$version = 'v2.0';
		$language = '1';
		$signType = '1';
		$payerName = '';
		$payerContactType = '1';
		$payerContact = '';
		$orderId = $outTradeNo;
		$orderAmount = $amount*100;	 //以分为单位，必须是整型数字
		$orderTime = date('YmdHis');
		$productName = '';
		$productNum = '1';
		$productId = '';
		$productDesc = '';
		$ext1 = '';
		$ext2 = '';
		$payType = '00';
		$redoFlag ='1';
		$pid = '';	//合作伙伴在快钱的用户编号 如未和快钱签订代理合作协议，不需要填写本参数
		$signMsgVal=$this->appendParam($signMsgVal,"inputCharset",$inputCharset);
		$signMsgVal=$this->appendParam($signMsgVal,"bgUrl",$bgUrl);
		$signMsgVal=$this->appendParam($signMsgVal,"version",$version);
		$signMsgVal=$this->appendParam($signMsgVal,"language",$language);
		$signMsgVal=$this->appendParam($signMsgVal,"signType",$signType);
		$signMsgVal=$this->appendParam($signMsgVal,"merchantAcctId",$merchantAcctId);
		$signMsgVal=$this->appendParam($signMsgVal,"payerName",$payerName);
		$signMsgVal=$this->appendParam($signMsgVal,"payerContactType",$payerContactType);
		$signMsgVal=$this->appendParam($signMsgVal,"payerContact",$payerContact);
		$signMsgVal=$this->appendParam($signMsgVal,"orderId",$orderId);
		$signMsgVal=$this->appendParam($signMsgVal,"orderAmount",$orderAmount);
		$signMsgVal=$this->appendParam($signMsgVal,"orderTime",$orderTime);
		$signMsgVal=$this->appendParam($signMsgVal,"productName",$productName);
		$signMsgVal=$this->appendParam($signMsgVal,"productNum",$productNum);
		$signMsgVal=$this->appendParam($signMsgVal,"productId",$productId);
		$signMsgVal=$this->appendParam($signMsgVal,"productDesc",$productDesc);
		$signMsgVal=$this->appendParam($signMsgVal,"ext1",$ext1);
		$signMsgVal=$this->appendParam($signMsgVal,"ext2",$ext2);
		$signMsgVal=$this->appendParam($signMsgVal,"payType",$payType);	
		$signMsgVal=$this->appendParam($signMsgVal,"redoFlag",$redoFlag);
		$signMsgVal=$this->appendParam($signMsgVal,"pid",$pid);
		$signMsgVal=$this->appendParam($signMsgVal,"key",$key);
		$signMsg= strtoupper(md5($signMsgVal));

		$form = '<html><body><form id="kqPay" name="kqPay" method="post" action="https://www.99bill.com/gateway/recvMerchantInfoAction.htm">';
		$form .= '<input type="hidden" name="inputCharset" value="'.$inputCharset.'"/>';
		$form .= '<input type="hidden" name="bgUrl" value="'.$bgUrl.'"/>';
		$form .= '<input type="hidden" name="version" value="'.$version.'"/>';
		$form .= '<input type="hidden" name="language" value="'.$language.'"/>';
		$form .= '<input type="hidden" name="signType" value="'.$signType.'"/>';
		$form .= '<input type="hidden" name="signMsg" value="'.$signMsg.'"/>';
		$form .= '<input type="hidden" name="merchantAcctId" value="'.$merchantAcctId.'"/>';
		$form .= '<input type="hidden" name="payerName" value="'.$payerName.'"/>';
		$form .= '<input type="hidden" name="payerContactType" value="'.$payerContactType.'"/>';
		$form .= '<input type="hidden" name="payerContact" value="'.$payerContact.'"/>';
		$form .= '<input type="hidden" name="orderId" value="'.$orderId.'"/>';
		$form .= '<input type="hidden" name="orderAmount" value="'.$orderAmount.'"/>';
		$form .= '<input type="hidden" name="orderTime" value="'.$orderTime.'"/>';
		$form .= '<input type="hidden" name="productName" value="'.$productName.'"/>';
		$form .= '<input type="hidden" name="productNum" value="'.$productNum.'"/>';
		$form .= '<input type="hidden" name="productId" value="'.$productId.'"/>';
		$form .= '<input type="hidden" name="productDesc" value="'.$productDesc.'"/>';
		$form .= '<input type="hidden" name="ext1" value="'.$ext1.'"/>';
		$form .= '<input type="hidden" name="ext2" value="'.$ext2.'"/>';
		$form .= '<input type="hidden" name="payType" value="'.$payType.'"/>';
		$form .= '<input type="hidden" name="redoFlag" value="'.$redoFlag.'"/>';
		$form .= '<input type="hidden" name="pid" value="'.$pid.'"/>';
		$form .= '<input type="submit" value="提交到快钱">';
		$form .= '</form>';
		if(!$this->debug){
			$form.= "\n<script>window.onload=function(){document.forms['kqPay'].submit();}</script>";
		}
		$form.="</body></html>";
		echo $form;
	}
	
	function payReturn(){
		global $db,$baseUrl;
		$orderId = trim($_REQUEST['orderId']);
		//if($orderId){$log = '';foreach($_REQUEST as $key=>$val){$log .= $key.'='.$val.'&';}if($log){saveLog('快钱通知数据：'.$log);}}
		$paymentId = $db->getOne("select paymentId from qk_onlinepay where outTradeNo='$orderId'");
		$payConfig = $this->getConfig($paymentId);
		$key = $payConfig['secret'];
		$merchantAcctId=trim($_REQUEST['merchantAcctId']);
		$version=trim($_REQUEST['version']);
		$language=trim($_REQUEST['language']);
		$signType=trim($_REQUEST['signType']);
		$payType=trim($_REQUEST['payType']);
		$bankId=trim($_REQUEST['bankId']);
		$orderTime=trim($_REQUEST['orderTime']);
		$orderAmount=trim($_REQUEST['orderAmount']);
		$dealId=trim($_REQUEST['dealId']);
		$bankDealId=trim($_REQUEST['bankDealId']);
		$dealTime=trim($_REQUEST['dealTime']);
		$payAmount=trim($_REQUEST['payAmount']);
		$fee=trim($_REQUEST['fee']);
		$ext1=trim($_REQUEST['ext1']);
		$ext2=trim($_REQUEST['ext2']);
		$payResult=trim($_REQUEST['payResult']);
		$errCode=trim($_REQUEST['errCode']);
		$signMsg=trim($_REQUEST['signMsg']);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"merchantAcctId",$merchantAcctId);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"version",$version);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"language",$language);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"signType",$signType);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"payType",$payType);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"bankId",$bankId);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"orderId",$orderId);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"orderTime",$orderTime);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"orderAmount",$orderAmount);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"dealId",$dealId);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"bankDealId",$bankDealId);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"dealTime",$dealTime);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"payAmount",$payAmount);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"fee",$fee);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"ext1",$ext1);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"ext2",$ext2);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"payResult",$payResult);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"errCode",$errCode);
		$merchantSignMsgVal=$this->appendParam($merchantSignMsgVal,"key",$key);
		$merchantSignMsg= md5($merchantSignMsgVal);
		$rtnOk=0;
		$rtnUrl="";
		$ret = false;
		$tmp = "支付接口：快钱，状态：".$payResult."，订单时间：".$orderTime."，订单金额：".($orderAmount/100)."，快钱交易号：".$dealId."，银行：".$bankId."，银行交易号：".$bankDealId."，交易时间：".$dealTime."，支付金额：".($payAmount/100)."，交易手续费：".($fee/100);
		
		if(strtoupper($signMsg)==strtoupper($merchantSignMsg)){
			$this->payResult($orderId,$payAmount/100,$tmp,$dealId);
			$ret=true;
			$rtnOk=1;
		}Else{
			//return false;
			$rtnOk=1;
			$rtnUrl="";
			saveLog('签名错误：'.$tmp);
		}
		$domain = $_SERVER['HTTP_HOST'];
		if($ret===true){
			$ret = "success";
		}else{
			$ret = "failed";
		}
		$rtnUrl = "http://".$domain.$baseUrl."pay/result/status/".$ret;
		echo "<result>$rtnOk</result><redirecturl>".$rtnUrl."</redirecturl>";exit;
		return false;
	}

	function appendParam($returnStr,$paramId,$paramValue){
		if($returnStr!=""){
				if($paramValue!=""){
					$returnStr.="&".$paramId."=".$paramValue;
				}
		}else{
			If($paramValue!=""){
				$returnStr=$paramId."=".$paramValue;
			}
		}
		return $returnStr;
	}
}
?>