PHP达成的中信银行网银在线支付接口ECSHOP插件和平运动用例子

}

global $_LANG;

 代码如下

$_LANG[‘bankcomm’] = ‘交通银行在线支付’;
$_LANG[‘bankcomm_desc’] = ‘交通银行在线支付’;
$_LANG[‘pay_button’] = ‘交通银行支付’;

/**

  1. 将网银集成到现有的系统:(以mvc的结构进行说明):

 /* 版本号 */
 $modules[$i][‘version’] = ‘1.0.0.0’;

//连接地址

 1.3  修改配置文件(in/B2CMerchantSocket.xml):
  采用官方提供的测试
商号进行测试时,无需配置,否则要配置。具体看xml文件说明。

/**
 * 交通银行语言文件
 * by: Reson
 * 2014/03/31
 */

//获得表单传过来的数据

  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”

插件源码(includes/modules/payment/bankcomm.php):
复制代码 代码如下:

$orderUrl_value = $orderUrl->item(0)->nodeValue;

2.1  将不变的参数 配置 写入配置文件:

 }

$order_[‘orderMono’] = ‘测试’; //商家备注

  //以下是 新接口需要的参数
  $config[‘socketUrl’]         =”tcp://127.0.0.1:8891″;  #socket
url
  $config[‘merID’]     =’301310063009501′;    #商户id
3013100630095012
2.2 Model

 /* 配置信息 */
 $modules[$i][‘config’]  = array();

/**

 $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;                 #渠道编号

‘;
  return $pay_html;
 }

{

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

语言包源码(languages/zh_cn/payment/bankcomm.php):
复制代码 代码如下:

$merID = $MerchID->item(0)->nodeValue;

1.4  启动 socket 服务
        window:启动  start.bat 及可;
        linux: 启动  ohup 
sh.start,sh&     //使当前脚本脱离终端,并在后台运行。

$payment_lang = ROOT_PATH . ‘languages/’
.$GLOBALS[‘_CFG’][‘lang’]. ‘/payment/bankcomm.php’;

<input type = “hidden” name = “merSignMsg” value =
“‘.$param[‘signMsg_value’].'”>

/**
 * 交通银行支付类
 */
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();

  $retCode = $dom->getElementsByTagName(‘retCode’);
  $retCode_value = $retCode->item(0)->nodeValue;

$pay_id = $arr[1];

 return;
}

return false;

  htmlentities($param[‘orderMono’],”ENT_QUOTES”,”utf-8″);
  //连接字符串
  $source = ”;
  foreach($param as $key=>$val){
   if($key != ‘issBankNo’)
   $source .= $val.’|’;
  }
  $source = substr($source,0,strlen($source)-1);

return;

/**
 * 交通银行在线支付插件 For Ecshop
 * Author: Reson
 * Date: 2014/03/31
 */

$param[‘orderDate’] = local_date(“Ymd”,gmtime()); //商户订单日期*
yyyyMMdd

以下是调用的范例:

function __construct()

 /* 是否支持货到付款 */
 $modules[$i][‘is_cod’]  = ‘0’;

$param[‘orderContent’] = ”;

 /* 代码 */
 $modules[$i][‘code’]    = basename(apizl_file, ‘.php’);

 /**
  * 提交函数
  */
 function get_code($order)
 {
  //获得表单传过来的数据
  $param[‘interfaceVersion’] = ‘1.0.0.0’; //消息版本号*
  $param[‘merID’] = ‘301310063009501’; //商户号
(测试号,后期可自行更改)
  $param[‘orderid’] = $order[‘orderid’]; //订单号*
  $param[‘orderDate’] = local_date(“Ymd”,gmtime()); //商户订单日期*
yyyyMMdd
  $param[‘orderTime’] = local_date(“His”,gmtime()); //商户订单时间*
HHmmss
  $param[‘tranType’] = 0; //交易类别* 0:B2C
  $param[‘amount’] = $order[‘amount’]; //订单金额*
  $param[‘curType’] = ‘CNY’; //交易币种* 默认CNY
  $param[‘orderContent’] = ”;
  $param[‘orderMono’] = $order[‘orderMono’]; //商家备注
  $param[‘phdFlag’] = ”; //物流配送标志
  $param[‘notifyType’] = 1; //通知方式* 1 通知
  $param[‘merURL’] = ”; 
  $param[‘goodsURL’] = $order[‘goodsURL’]; //取货URL
  $param[‘jumpSeconds’] = ”;
  $param[‘payBatchNo’] = ”;
  $param[‘proxyMerName’] = ”;
  $param[‘proxyMerType’] = ”;
  $param[‘proxyMerCredentials’] = ”;
  $param[‘netType’] = 0; //渠道编号* 0:html渠道
  $param[‘issBankNo’] = ”;

$dom = new DOMDocument;

 /* 描述对应的语言项 */
 $modules[$i][‘desc’]    = ‘bankcomm_desc’;

echo “$errstr ($errno)<br /> “;

最近,一个项目要求做交通银行在线支付,ecshop本身没有这方面的接口,于是通过一些时间的专研,做了一个插件出来。有好的东西,当然要分享,在此特地分享出来,希望能够帮助到跟我一样有需要的人,为大家减轻一下开发的负担,也多请大家指出一些好的方法和建议,相互的学习、进步!

$order_[‘orderid’] = $order[‘order_sn’];

  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  

$in .= “<Message>”;

  $tranCode = “cb2200_sign”;

* @return void

if (!defined(‘IN_ECS’))
{
 die(‘Hacking attempt’);
}

//echo “$errstr ($errno)<br /> “;

  $retCode = $dom->getElementsByTagName(‘retCode’);
  $retCode_value = $retCode->item(0)->nodeValue;

/* 代码 */

  ///////////////// respond END ///////////////
 }

function bankcomm()

  $errMsg = $dom->getElementsByTagName(‘errMsg’);
  $errMsg_value = $errMsg->item(0)->nodeValue;

$form_code = $this->create_html($param); //创建提交表单

  
  

$in .= “<TranCode>”.$tranCode.”</TranCode>”;

  if($retCode_value != ”){
   //echo “交易返回码:”.$retCode_value.”
“;
   //echo “交易错误信息:” .$errMsg_value.”
“;
   return false;
  }else{
   $arr = preg_split(“/|{1,}/”,$srcMsg); 
   $pay_id = $arr[1];
   $action_note = base64_decode($arr[16]);
   // 完成订单。
   order_paid($pay_id, PS_PAYED, $action_note);
   //告诉用户交易完成
   return true; 
  }

fclose($fp);

  $errMsg = $dom->getElementsByTagName(‘errMsg’);
  $errMsg_value = $errMsg->item(0)->nodeValue;

 * Author: Reson

?>

$errMsg_value = $errMsg->item(0)->nodeValue;

?>

//解析返回xml

/**
 * 类
 */
class bankcomm
{
 /**
  * 构造函数
  *
  * @return void
  */
 function bankcomm()
 {
 }

$retCode = $dom->getElementsByTagName(‘retCode’);

 function __construct()
 {
  $this->bankcomm();
 }

if (file_exists($payment_lang))

/* 模块的基本信息 */
if (isset($set_modules) && $set_modules == TRUE)
{
 $i = isset($modules) ? count($modules) : 0;

$_LANG[‘bankcomm_desc’] = ‘交通银行在线支付’;

global $_LANG;

$param[‘interfaceVersion’] = ‘1.0.0.0’; //消息版本号*

 /* 是否支持在线支付 */
 $modules[$i][‘is_online’]  = ‘1’;

*/

 /**
  * 处理函数
  */
 function respond()
 {  
  $tranCode = “cb2200_verify”;
  $notifyMsg = $_REQUEST[“notifyMsg”];  
  $lastIndex = strripos($notifyMsg,”|”);
  $signMsg = substr($notifyMsg,$lastIndex+1); //签名信息
  $srcMsg = substr($notifyMsg,0,$lastIndex+1);//原文

$param[‘phdFlag’] = ”; //物流配送标志

 /* 作者 */
 $modules[$i][‘author’]  = ‘Reson’;

$in  = “<?xml version=’1.0′ encoding=’UTF-8′?>”;

if (file_exists($payment_lang))
{
 global $_LANG;
 include_once($payment_lang);
}

 /* 支付费用,由配送决定 */
 $modules[$i][‘pay_fee’] = ‘0’;

“;
   $in .= “”;
   $in .= “”.$tranCode.””;
   $in .= “”.$notifyMsg.””;
   $in .= “”;
   fwrite($fp, $in);
   while (!feof($fp)) {
    $retMsg =$retMsg.fgets($fp, 1024); 
   }
   fclose($fp);
  } 

<input type = “hidden” name = “proxyMerCredentials” value =
“‘.$param[‘proxyMerCredentials’].'”>

  $MerchID = $dom->getElementsByTagName(‘MerchID’);
  $merID = $MerchID->item(0)->nodeValue;
  //echo “retMsg=”.$retMsg;
  //echo $retCode_value.” “.$errMsg_value.” “.$signMsg_value.”
“.$orderUrl_value;
  if($retCode_value != “0”){
   //echo “交易返回码:”.$retCode_value.”
“;
   //echo “交易错误信息:” .$errMsg_value.”
“;
   return “交易错误信息:” .$errMsg_value.”
“;
  }else{
   $param[‘signMsg_value’] = $signMsg_value;
   $param[‘orderUrl_value’] = $orderUrl_value;
   $form_code = $this->create_html($param); //创建提交表单
   return $form_code;
  }

fclose($fp);

  $orderUrl = $dom->getElementsByTagName(‘orderUrl’);
  $orderUrl_value = $orderUrl->item(0)->nodeValue;

$param[‘proxyMerName’] = ”;

  $signMsg = $dom->getElementsByTagName(‘signMsg’);
  $signMsg_value = $signMsg->item(0)->nodeValue;

global $_LANG;

复制代码 代码如下:
include_once(ROOT_PATH . ‘includes/modules/payment/bankcomm.php’);
$order_[‘orderid’] = $order[‘order_sn’];
$order_[‘amount’] = $order[‘yd_price’];
$order_[‘orderMono’] = ‘测试’; //商家备注
$order_[‘goodsURL’] =
“‘; //取货URL
$pay_code = new bankcomm;
$pay_button = $pay_code->get_code($order_);
$order[‘pay_button’] = $pay_button; //即此时已经生成了一个支付按钮

<input type = “hidden” name = “issBankNo” value =
“‘.$param[‘issBankNo’].'”>

 /**
  * 创建提交表单
  */
 function create_html($param){
  $pay_html =’

$param[‘jumpSeconds’] = ”;

  //连接地址
  $socketUrl = “tcp://127.0.0.1:8080”;
  $fp = stream_socket_client($socketUrl, $errno, $errstr, 30);
  $retMsg=””;
  if (!$fp) {
   //echo “$errstr ($errno)
n”;
   return false;
  }else{
   $in  = “

<input type = “hidden” name = “proxyMerName” value =
“‘.$param[‘proxyMerName’].'”>

  //解析返回xml
  $dom = new DOMDocument;
  $dom->loadXML($retMsg);

$modules[$i][‘is_online’]  = ‘1’;

  //连接地址
  $socketUrl = “tcp://127.0.0.1:8080”; //这里的端口根据自己配置的情况
  $fp = stream_socket_client($socketUrl, $errno, $errstr, 30);
  $retMsg=””;
  //
  if (!$fp) {
   echo “$errstr ($errno)
n”;
  } else
  {
   $in  = “

$source = ”;

“;
   $in .= “”;
   $in .= “”.$tranCode.””;
   $in .= “”.$source.””;
   $in .= “”;
   fwrite($fp, $in);
   while (!feof($fp)) {
    $retMsg =$retMsg.fgets($fp, 1024); 
   }
   fclose($fp);
  } 
  //解析返回xml
  $dom = new DOMDocument;
  $dom->loadXML($retMsg);

$srcMsg = substr($notifyMsg,0,$lastIndex+1);//原文

在使用插件之前,请配置好交通银行在线支付的环境(具体安装方法,交行提供的demo会有,也不是很难,注意好细节就行)。安装好之后,请将把下面的插件源码和语言包源码按路径保存到相应文件,最后进入后台的支付模块安装即可。

$dom = new DOMDocument;

  $signMsg = $dom->getElementsByTagName(‘signMsg’);
  $signMsg_value = $signMsg->item(0)->nodeValue;

$param[‘proxyMerType’] = ”;

 /* 网址 */
 $modules[$i][‘website’] = ”;

return $form_code;

源码(languages/zh_cn/payment/bankcomm.php):

$in .= “<TranCode>”.$tranCode.”</TranCode>”;

$fp = stream_socket_client($socketUrl, $errno, $errstr, 30);

return true; 

}

/* 版本号 */

//echo “交易返回码:”.$retCode_value.”<br>”;

}

$param[‘tranType’] = 0; //交易类别* 0:B2C

<input type = “hidden” name = “orderTime” value =
“‘.$param[‘orderTime’].'”>

$signMsg = substr($notifyMsg,$lastIndex+1); //签名信息

function create_html($param){

}else{

htmlentities($param[‘orderMono’],”ENT_QUOTES”,”utf-8″);

<?php

$retCode_value = $retCode->item(0)->nodeValue;

$retMsg =$retMsg.fgets($fp, 1024); 

}

 * 2014/03/31

* 处理函数

foreach($param as
$key=>$val){

if (!$fp) {

/* 作者 */

}

include_once(ROOT_PATH . ‘includes/modules/payment/bankcomm.php’);

$tranCode = “cb2200_verify”;

$signMsg = $dom->getElementsByTagName(‘signMsg’);

function respond()

}

}

以下是调用的范例:

$modules[$i][‘code’]    = basename(__FILE__, ‘.php’);

发表评论

电子邮件地址不会被公开。 必填项已用*标注