<html>
	<head><TITLE> 工商银行电子商务测试页面 </TITLE><head>
<?php
	/*加载PHP签名模块，详细参加部署文档*/
	if (!extension_loaded('infosec'))
	{
		if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
		{
			dl('php_infosec.dll');
		}
   	else
		{
			dl('infosec.so');
		}
 	}
	/*获取表单输入*/
	if(isset($_POST["Submit"]))
	{
		//读取商户私钥文件
		$keyfile=$_POST["merCertKey"];
		if(strlen($keyfile) <= 0)
		{
			echo "WARNING : no key data input<br/>";
			exit();
		}
		
		$fp = fopen($keyfile,"rb");
		if($fp == NULL)
		{
			echo "open file error<br/>";
			exit();
		}
		
		fseek($fp,0,SEEK_END);
		$filelen=ftell($fp);
		fseek($fp,0,SEEK_SET);
		$contents = fread($fp,$filelen);
		fclose($fp);
		
		$key = substr($contents,2);
		//获取密钥口令
		$pass=$_POST["merCertKeyPasswd"];
		if(strlen($pass) <= 0)
		{
			echo "WARNING : no key password input<br/>";
			exit();
		}
		//获取订单提交数据
		$tranData=$_POST["tranData"];
		if(strlen($tranData) <= 0)
		{
			echo "WARNING : no tranData input<br/>";
			exit();
		}
		//读取商户公钥文件
		$merCert=$_POST["merCert"];
		if(strlen($merCert) <= 0)
		{
			echo "WARNING : no merCert input<br/>";
			exit();
		}
		else
		{
			$fp2 = fopen($merCert,"rb");
			if($fp2 == NULL)
			{
				echo "open file error<br/>";
				exit();
			}
			fseek($fp2,0,SEEK_END);
			$filelen2=ftell($fp2);
			fseek($fp2,0,SEEK_SET);
			$cert = fread($fp2,$filelen2);
			fclose($fp2);
		}
		
	}
	/*签名*/
	$signature = sign($tranData,$key,$pass);//签名
	$code = current($signature);//获取签名数据
	$len = next($signature);//获取签名数据长度
	$signcode = base64enc($code);//对签名数据BASE64编码
	$merSignMsg = current($signcode);
	$tranDataBase64 = base64enc($tranData);//对表单数据BASE64编码
	$merCertBase64 = base64enc($cert);//对证书BASE64编码
?>
	<body>
		<!-- 测试地址需向电子银行申请，将签名的数据提交银行 -->
		<FORM id=FORM1 name=FORM1 action="https://83.252.46.200:11491/servlet/NewB2cMerPayReqServlet" method=post">
			<font face='Arial' size='4' color='white'>商户订单数据签名页面</font>
			<table width="98%"  border="1">
				<tr>
					<td width="9%">接口名称</td>
					<td width="91%"><INPUT ID="interfaceName" NAME="interfaceName" TYPE="text" value="ICBC_PERBANK_B2C" size="120" ></td>
				</tr>
				<tr>
					<td width="9%">接口版本号</td>
					<td width="91%"><INPUT ID="interfaceVersion" NAME="interfaceVersion" TYPE="text" value="1.0.0.11" size="120"></td>
				</tr>
				<tr>
					<td width="9%">接口数据</td>
					<td width="91%"><textarea ID="tranData" name="tranData" cols="120" rows="5"><?php echo current($tranDataBase64);?></textarea>
				</tr>
				<tr>
					<td width="9%">签名数据</td>
					<td width="91%"><INPUT ID="merSignMsg" NAME="merSignMsg" TYPE="text" size="120" value="<?php echo $merSignMsg;?>">
				</tr>
				<tr>
					<td width="9%">证书数据</td>
					<td width="91%"><INPUT ID="merCert" NAME="merCert" TYPE="text" size="120" value="<?php echo current($merCertBase64);?>">
				</tr>
			</table>
			<table>
				<tr>
					<td><INPUT TYPE="submit" value=" 提 交 订 单 "></td>
					<td><INPUT  type="button" value=" 返 回 修 改 " onClick="self.history.back();"></td>
				</tr>
			</table>
		</FORM>
	</body>
</html>