欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php版交通银行网银支付接口开发入门教程

程序员文章站 2024-04-02 14:04:46
本文实例讲述了php版交通银行网银支付接口实现方法。分享给大家供大家参考,具体如下: 概述:网银支付接口 和支付宝接口大体上差不多,主要的区别是 交通银行提供的 接口核心...

本文实例讲述了php版交通银行网银支付接口实现方法。分享给大家供大家参考,具体如下:

概述:网银支付接口 和支付宝接口大体上差不多,主要的区别是 交通银行提供的 接口核心的比如,加密等是通过java实现的,所以,要想办法使php和java能正常的通信,为此,官方也提供了两套实现方法,一个是通过 socket 进行通信,另一个方法是通过 java 桥接,下面演示的是 socket方法.

1. 配置运行环境

1.1  安装java,自行到oracle官网下载 java,然后安装,并配置正确的 环境变量.

1.2  把 测试的证书导入到java 虚拟机.

keytool " -import -keystore "java虚拟机放置证书的地址" -storepass changeit -alias test_bocommca -file "证书路径" 完成导入。

例子:keytool" -import -keystore "c:\program files\java\jre1.5\lib\security\cacerts" -storepass changeit -alias test_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"; #socket url
$config['merid'] ='301310063009501'; #商户id 3013100630095012

2.2 model

/**
 * 交通银行支付类
 */
class bocom extends ci_model {
 private $arrreturn=array();
 private $socket;
 public function __construct() {
 parent::__construct ();
 //加载交通银行配置文件
 $this->config->load('bocom');
 $this->socket=$this->config->item('socketurl');
 }
 /**
 * 支付方法
 *
 * @param unknown $arr_data=array(
 * 'bill_no'=>
 * )
 */
 public function pay($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->arrreturn as $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连接失败");
   return false;
   } else
   {
   $in = "<?xml version='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_arr as $k=>$v){
     $this->arrreturn[$k]=$v;
    }
    }else{
    return false;
    }
  }else{
   return false;
  }
  return $this->arrreturn;
 }
 /**
 * 解析xml
 */
 public function xmlparse($retmsg){
 $arr=array();
 //解析返回xml
 $dom = new domdocument;
 $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);
      return false;
    }
  $arr['mersignmsg'] = $signmsg_value;
  $arr['merid']   = $merid;
  $arr['orderurl']  = $orderurl_value;
  return $arr;
 }
 /**
 * 交通银行 支付通知
 * @return boolean|unknown
 */
 public function notify(){
 $trancode = "cb2200_verify";
 if(!isset($_request['notifymsg'])){
  log_message("error","网银支付通知·非法请求");
  return false;
 }
 $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 = "<?xml version='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 = new domdocument;
 $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>");
  return false;
 }else{
  $arr = preg_split("/\|{1,}/",$srcmsg);
  if($arr[9]=="1"){
  return $this->updatebill($arr[1]);
  }
  log_message("error","交易失败:".$arr[13]."<br/>");
  return false;
 }
 }
 private function updatebill($billno){
 // 更新 订单状态
 }
 //end class
}

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程序设计有所帮助。