工商银行网上支付接口PHP的demo(原创) - 发送篇
send.php
<?php
date_default_timezone_set('PRC');
include_once("icbc.php");
if(!empty($_POST['hide'])) {
$fdir = $_SERVER['DOCUMENT_ROOT']."/key/"; //商户证书所放路径
$target = "http://localhost/test.php";
$payAPI['interfaceName'] = "ICBC_PERBANK_B2C"; //接口方式
$payAPI['interfaceVersion'] = "1.0.0.1"; //接口版本
$payAPI['merID'] = "0200EC20000012"; //商户ID
$payAPI['merAcct'] = "0200029109000030106"; //商户银行帐号
$payAPI['merURL'] = "http://localhost/get.php"; //接收银行通知路径
$payAPI['notifyType'] = "HS"; //接口通知方式
$payAPI['orderid'] = $_POST['orderid'];
$payAPI['amount'] = $_POST['amount'];
$payAPI['curType'] = "001"; //货币类型 001为人民币
$payAPI['resultType'] = "0";
$payAPI['orderDate'] = date("YmdHis");
$payAPI['verifyJoinFlag'] = "0";
$icbc = new icbcpay();
$icbc->init($payAPI,$fdir);
$icbc->Send();
}
?>
<html>
<head>
<title>模拟B2C交易</title>
</head>
<body leftmargin="0" topmargin="0">
<table cellpadding="1" cellspacing="1" bgcolor="#E5E5E5" width="800" align="center">
<tr bgcolor="#F5F5F5">
<td height="50" align="center">交易平台</td>
</tr>
<form action="b2c.php" method="post">
<tr bgcolor="#FFFFFF">
<td height="440" align="center">
<table cellpadding="1" cellspacing="1" bgcolor="#E5E5E5" width="600">
<tr bgcolor="#F5F5F5">
<td height="30" align="center" width="120">订 单 号</td>
<td bgcolor="#FDFDFD" width="580">
<input type="text" name="orderid" value="000000001" style="width:300px" />
</td>
</tr>
<tr bgcolor="#F5F5F5">
<td height="30" align="center" width="120">商品金额</td>
<td bgcolor="#FDFDFD" width="580">
<input type="text" name="amount" value="100" style="width:300px" />
</td>
</tr>
<tr bgcolor="#FAFAFA">
<td colspan="2" height="30">
<input type="hidden" name="hide" value="1" />
<input type="submit" value="提交" />
</td>
</tr>
</table>
</td>
</tr>
</form>
</table>
</body>
</html>
icbc.php
<?php
class icbcpay
{
private $payAPI = Array();
private $fdir;
private $LongString;
private $POSTString;
private $target;
public function init($api,$fdir)
{
$this->payAPI = $api;
$this->fdir = $fdir;
$this->target = $target;
$this->getCOMAPI();
}
private function getCOMAPI()
{
$icbc = new COM("ICBCEBANKUTIL.B2CUtil");
$crt = $this->fdir."user.crt";
$key = $this->fdir."user.key";
$rc = $icbc->init($crt, $crt, $key, '11111111');
$this->setString();
if($rc == 0) {
$this->payAPI['merSignMsg'] = $icbc->signC($this->LongString, strlen($this->LongString));
$this->payAPI['merCert'] = $icbc->getCert(1);
}
}
private function setString()
{
$payAPI = $this->payAPI;
$this->LongString.= "";
foreach ($payAPI as $key => $value) {
$this->LongString.= $value;
}
}
private function setPOSTString()
{
$payAPI=$this->payAPI;
$this->POSTString = "";
foreach ($payAPI as $key => $value) {
if($key == "interfaceName") {
$this->POSTString.=$key."=".$value;
} else {
$this->POSTString.="&".$key."=".$value;
}
}
}
private function setFromTxt()
{
$payAPI = $this->payAPI;
foreach ($payAPI as $key => $value) {
$header.= '<input type="hidden" readonly="readonly" name="'.$key.'" value="'.$value.'">';
}
$header.= '</form>';
return $header;
}
public function Send()
{
$url = "http://localhost/test.php";
$header = '<form action="'.$url.'" id="sendform" method="post" name="order">';
$header.= $this->setFromTxt();
$header.= "<script type=\"text/javascript\">";
$header.= "document.getElementById(\"sendform\").submit();";
$header.= "</script>";
echo $header;
//$referrer = "";
//$url_info = parse_url($url);
//if($referrer == "")
//$referrer = $_SERVER['SCRIPT_URI'];
//$this->setPOSTString();
//$POSTString = $this->POSTString;
//$data_string = $POSTString;
//$request = "POST ".$url_info["path"]." HTTP/1.1\n";
//$request.= "Host: ".$url_info["host"]."\n";
//$request.= "Referer: $referrer\n";
//$request.= "Content-type: application/x-www-form-urlencoded\n";
//$request.= "Content-length: ".strlen($data_string)."\n";
//$request.= "Connection: close\n";
//$request.= "\n";
//$request.= $data_string."\n";
//$fp = fsockopen($url_info['host'], 80);
//fputs($fp, $request);
//while (!feof($fp)) {
//$request .= fgets($fp, 128);
//}
//echo $request;
//fclose($fp);
//header("location:$url");
//printf('<script type="text/javascript">');
//printf('window.location="'.$url.'";');
//printf('</script>');
}
}
?>