php版交通银行网银支付接口开发入门教程
本文实例讲述了php版交通银行网银支付接口实现方法。分享给大家供大家参考,具体如下:
概述:网银支付接口和支付宝接口大体上差不多,主要的区别是交通银行提供的接口核心的比如,加密等是通过java实现的,所以,要想办法使php和java能正常的通信,为此,官方也提供了两套实现方法,一个是通过socket进行通信,另一个方法是通过java桥接,下面演示的是socket方法.
1.配置运行环境
1.1 安装java,自行到oracle官网下载java,然后安装,并配置正确的环境变量.
1.2 把测试的证书导入到java虚拟机.
keytool"-import-keystore"java虚拟机放置证书的地址"-storepasschangeit-aliastest_bocommca-file"证书路径"完成导入。
例子:keytool"-import-keystore"C:\ProgramFiles\Java\jre1.5\lib\security\cacerts"-storepasschangeit-aliastest_bocommca-file"C:\socket\cert\test_root.cer"
1.3 修改配置文件(in/B2CMerchantSocket.xml).
采用官方提供的测试商号进行测试时,无需配置,否则要配置,具体看xml文件说明.
1.4 启动socket服务
window:启动 start.bat及可.
linux:启动 ohup sh.start,sh& //使当前脚本脱离终端,并在后台运行。
2.将网银集成到现有的系统,以mvc的结构进行说明.
2.1 将不变的参数配置写入配置文件:
$config['interfaceVersion']="1.0.0.0";#接口版本 $config['tranType']=0;#交易类别0:B:C $config['curType']='CNY';#交易币种 $config['notifyType']=1;#0=不通知1=通知2=抓取 $config['merURL']="/pay/notify";#主动通知url $config['goodsURL']='/goods/1.html';#取货url $config['jumpSeconds']=3;#跳转时间 $config['payBatchNo']='';#商户批次号 $config['proxyMerName']='';#代理商家名字 $config['proxyMerType']='';#代理商类型 $config['proxyMerCredentials']='';#代理商家批次号 $config['netType']=0;#渠道编号 //以下是新接口需要的参数 $config['socketUrl']="tcp://127.0.0.1:8891";#socketurl $config['merID']='301310063009501';#商户id3013100630095012
2.2Model
/**
*交通银行支付类
*/
classBocomextendsCI_Model{
private$arrReturn=array();
private$socket;
publicfunction__construct(){
parent::__construct();
//加载交通银行配置文件
$this->config->load('bocom');
$this->socket=$this->config->item('socketUrl');
}
/**
*支付方法
*
*@paramunknown$arr_data=array(
*'bill_no'=>
*)
*/
publicfunctionpay($arr_data){
//获得表单传过来的数据
$this->arrReturn['interfaceVersion']=$this->config->item('interfaceVersion');
$this->arrReturn['merID']=$this->config->item('merID');//商户号为固定
$this->arrReturn['orderid']=$arr_data['bill_no'];
$this->arrReturn['orderDate']=$arr_data['bill_date'];
$this->arrReturn['orderTime']=$arr_data['bill_time'];
$this->arrReturn['tranType']=$this->config->item('tranType');
$this->arrReturn['amount']=$arr_data['bill_fee'];
$this->arrReturn['curType']=$this->config->item('curType');
$this->arrReturn['orderContent']=isset($arr_data['bill_title'])?iconv('utf-8','gb2312',$arr_data["bill_title"]):'';#订单内容
$this->arrReturn['orderMono']=isset($arr_data['bill_mono'])?iconv('utf-8','gb2312',$arr_data['bill_mono']):'';#商家备注
$this->arrReturn['phdFlag']=isset($arr_data['phpFlag'])?$arr_data['phpFlag']:'';
$this->arrReturn['notifyType']=$this->config->item('notifyType');
$this->arrReturn['merURL']=$this->config->item('merURL');
$this->arrReturn['goodsURL']=$this->config->item('goodsURL');
$this->arrReturn['jumpSeconds']=$this->config->item('jumpSeconds');
$this->arrReturn['payBatchNo']=$this->config->item('payBatchNo');
$this->arrReturn['proxyMerName']=$this->config->item('proxyMerName');
$this->arrReturn['proxyMerType']=$this->config->item('proxyMerType');
$this->arrReturn['proxyMerCredentials']=$this->config->item('proxyMerCredentials');
$this->arrReturn['netType']=$this->config->item('netType');
//以下参数不参与签名
$this->arrReturn['issBankNo']=isset($arr_data['code_id'])?trim($arr_data['code_id']):'';
$tranCode="cb2200_sign";
$source='';
$len=count($this->arrReturn)-1;$j=1;
foreach($this->arrReturnas$v){
if($j<=$len){
$source.=$v."|";
}
$j++;
}
$source=substr($source,0,strlen($source)-1);
$fp=stream_socket_client($this->socket,$errno,$errstr,30);
$retMsg="";
if(!$fp){
log_message("info","socket连接失败");
returnfalse;
}else
{
$in="<?xmlversion='1.0'encoding='gbk2312'?>";
$in.="<Message>";
$in.="<TranCode>".$tranCode."</TranCode>";
$in.="<MsgContent>".$source."</MsgContent>";
$in.="</Message>";
fwrite($fp,$in);
while(!feof($fp)){
$retMsg=$retMsg.fgets($fp,1024);
}
fclose($fp);
}
if(false!==$xml_arr=$this->xmlParse($retMsg)){
if(is_array($xml_arr)){
foreach($xml_arras$k=>$v){
$this->arrReturn[$k]=$v;
}
}else{
returnfalse;
}
}else{
returnfalse;
}
return$this->arrReturn;
}
/**
*解析XML
*/
publicfunctionxmlParse($retMsg){
$arr=array();
//解析返回xml
$dom=newDOMDocument;
$dom->loadXML($retMsg);
$retCode=$dom->getElementsByTagName('retCode');
$retCode_value=$retCode->item(0)->nodeValue;
$errMsg=$dom->getElementsByTagName('errMsg');
$errMsg_value=$errMsg->item(0)->nodeValue;
$signMsg=$dom->getElementsByTagName('signMsg');
$signMsg_value=$signMsg->item(0)->nodeValue;
$orderUrl=$dom->getElementsByTagName('orderUrl');
$orderUrl_value=$orderUrl->item(0)->nodeValue;
$MerchID=$dom->getElementsByTagName('MerchID');
$merID=$MerchID->item(0)->nodeValue;
if($retCode_value!="0"){
log_message("info","交易返回码:".$retCode_value);
log_message("info","交易错误信息:".$errMsg_value);
returnfalse;
}
$arr['merSignMsg']=$signMsg_value;
$arr['merID']=$merID;
$arr['orderUrl']=$orderUrl_value;
return$arr;
}
/**
*交通银行支付通知
*@returnboolean|unknown
*/
publicfunctionnotify(){
$tranCode="cb2200_verify";
if(!isset($_REQUEST['notifyMsg'])){
log_message("error","网银支付通知·非法请求");
returnfalse;
}
$notifyMsg=$_REQUEST["notifyMsg"];
log_message("error",$notifyMsg."回调....");
$lastIndex=strripos($notifyMsg,"|");
$signMsg=substr($notifyMsg,$lastIndex+1);//签名信息
$srcMsg=substr($notifyMsg,0,$lastIndex+1);//原文
$merID=$this->config->item('merID');
$fp=stream_socket_client($this->socket,$errno,$errstr,30);
$retMsg="";
//
if(!$fp){
echo"$errstr($errno)<br/>\n";
log_message("error","$errstr($errno)<br/>\n");
}else{
$in="<?xmlversion='1.0'encoding='gb2312'?>";
$in.="<Message>";
$in.="<TranCode>".$tranCode."</TranCode>";
$in.="<merchantID>".$merID."</merchantID>";
$in.="<MsgContent>".$notifyMsg."</MsgContent>";
$in.="</Message>";
fwrite($fp,$in);
while(!feof($fp)){
$retMsg=$retMsg.fgets($fp,1024);
}
fclose($fp);
}
//解析返回xml
$dom=newDOMDocument;
$dom->loadXML($retMsg);
$retCode=$dom->getElementsByTagName('retCode');
$retCode_value=$retCode->item(0)->nodeValue;
$errMsg=$dom->getElementsByTagName('errMsg');
$errMsg_value=$errMsg->item(0)->nodeValue;
if($retCode_value!='0')
{
log_message("error","交易错误信息:".$errMsg_value."<br>");
returnfalse;
}else{
$arr=preg_split("/\|{1,}/",$srcMsg);
if($arr[9]=="1"){
return$this->updateBill($arr[1]);
}
log_message("error","交易失败:".$arr[13]."<br/>");
returnfalse;
}
}
privatefunctionupdateBill($billNo){
//更新订单状态
}
//endclass
}
2.3控制器
$this->load->model("Bocom");
支付方法:
$this->arrData=$this->Bocom->pay($this->data);
通知:
$this->arrData=$this->Bocom->notify();
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php常见数据库操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《php排序算法总结》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》及《php字符串(string)用法总结》
希望本文所述对大家PHP程序设计有所帮助。