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

PHP实现的交通银行网银在线支付接口ECSHOP插件和使用例子

程序员文章站 2023-11-26 15:02:52
最近,一个项目要求做交通银行在线支付,ecshop本身没有这方面的接口,于是通过一些时间的专研,做了一个插件出来。有好的东西,当然要分享,在此特地分享出来,希望能够帮助到跟...

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


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


插件源码(includes/modules/payment/bankcomm.php):

复制代码 代码如下:

<?php

/**
 * 交通银行在线支付插件 for ecshop
 * author: reson
 * date: 2014/03/31
 */

if (!defined('in_ecs'))
{
 die('hacking attempt');
}

$payment_lang = root_path . 'languages/' .$globals['_cfg']['lang']. '/payment/bankcomm.php';

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

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

 /* 代码 */
 $modules[$i]['code']    = basename(__file__, '.php');

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

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

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

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

 /* 作者 */
 $modules[$i]['author']  = 'reson';

 /* 网址 */
 $modules[$i]['website'] = '//www.jb51.net';

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

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

 return;
}

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

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

 /**
  * 提交函数
  */
 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'] = '';

  $trancode = "cb2200_sign";

  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);

  //连接地址
  $socketurl = "tcp://127.0.0.1:8080"; //这里的端口根据自己配置的情况
  $fp = stream_socket_client($socketurl, $errno, $errstr, 30);
  $retmsg="";
  //
  if (!$fp) {
   echo "$errstr ($errno)<br />\n";
  } else
  {
   $in  = "<?xml version='1.0' encoding='utf-8'?>";
   $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);
  } 
  //解析返回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;
  //echo "retmsg=".$retmsg;
  //echo $retcode_value." ".$errmsg_value." ".$signmsg_value." ".$orderurl_value;
  if($retcode_value != "0"){
   //echo "交易返回码:".$retcode_value."<br>";
   //echo "交易错误信息:" .$errmsg_value."<br>";
   return "交易错误信息:" .$errmsg_value."<br>";
  }else{
   $param['signmsg_value'] = $signmsg_value;
   $param['orderurl_value'] = $orderurl_value;
   $form_code = $this->create_html($param); //创建提交表单
   return $form_code;
  }

 }

 /**
  * 创建提交表单
  */
 function create_html($param){
  $pay_html ='<form name = "form1" method = "post" action = "'.$param['orderurl_value'].'">
  <input type = "hidden" name = "interfaceversion" value = "'.$param['interfaceversion'].'">
  <input type = "hidden" name = "merid" value = "'.$param['merid'].'">
  <input type = "hidden" name = "orderid" value = "'.$param['orderid'].'">
  <input type = "hidden" name = "orderdate" value = "'.$param['orderdate'].'">
  <input type = "hidden" name = "ordertime" value = "'.$param['ordertime'].'">
  <input type = "hidden" name = "trantype" value = "'.$param['trantype'].'">
  <input type = "hidden" name = "amount" value = "'.$param['amount'].'">
  <input type = "hidden" name = "curtype" value = "'.$param['curtype'].'">
  <input type = "hidden" name = "ordercontent" value = "'.$param['ordercontent'].'">
  <input type = "hidden" name = "ordermono" value = "'.$param['ordermono'].'">
  <input type = "hidden" name = "phdflag" value = "'.$param['phdflag'].'">
  <input type = "hidden" name = "notifytype" value = "'.$param['notifytype'].'">
  <input type = "hidden" name = "merurl" value = "'.$param['merurl'].'">
  <input type = "hidden" name = "goodsurl" value = "'.$param['goodsurl'].'">
  <input type = "hidden" name = "jumpseconds" value = "'.$param['jumpseconds'].'">
  <input type = "hidden" name = "paybatchno" value = "'.$param['paybatchno'].'">
  <input type = "hidden" name = "proxymername" value = "'.$param['proxymername'].'">
  <input type = "hidden" name = "proxymertype" value = "'.$param['proxymertype'].'">
  <input type = "hidden" name = "proxymercredentials" value = "'.$param['proxymercredentials'].'">
  <input type = "hidden" name = "nettype" value = "'.$param['nettype'].'">
  <input type = "hidden" name = "mersignmsg" value = "'.$param['signmsg_value'].'">
  <input type = "hidden" name = "issbankno" value = "'.$param['issbankno'].'">

  <input type="submit" value=" " class="pay_button" />
  </form>';
  return $pay_html;
 }

 /**
  * 处理函数
  */
 function respond()
 {  
  $trancode = "cb2200_verify";
  $notifymsg = $_request["notifymsg"];  
  $lastindex = strripos($notifymsg,"|");
  $signmsg = substr($notifymsg,$lastindex+1); //签名信息
  $srcmsg = substr($notifymsg,0,$lastindex+1);//原文

  //连接地址
  $socketurl = "tcp://127.0.0.1:8080";
  $fp = stream_socket_client($socketurl, $errno, $errstr, 30);
  $retmsg="";
  if (!$fp) {
   //echo "$errstr ($errno)<br />\n";
   return false;
  }else{
   $in  = "<?xml version='1.0' encoding='utf-8'?>";
   $in .= "<message>";
   $in .= "<trancode>".$trancode."</trancode>";
   $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;

  $signmsg = $dom->getelementsbytagname('signmsg');
  $signmsg_value = $signmsg->item(0)->nodevalue;

  if($retcode_value != ''){
   //echo "交易返回码:".$retcode_value."<br>";
   //echo "交易错误信息:" .$errmsg_value."<br>";
   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; 
  }

  ///////////////// respond end ///////////////
 }

}

?>


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

<?php

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

global $_lang;

$_lang['bankcomm'] = '交通银行在线支付';
$_lang['bankcomm_desc'] = '交通银行在线支付';
$_lang['pay_button'] = '交通银行支付';

?>

以下是调用的范例:

复制代码 代码如下:

include_once(root_path . 'includes/modules/payment/bankcomm.php');
$order_['orderid'] = $order['order_sn'];
$order_['amount'] = $order['yd_price'];
$order_['ordermono'] = '测试'; //商家备注
$order_['goodsurl'] = "http://".$_server['http_host'].'/respond.php?code=bankcomm'; //取货url
$pay_code = new bankcomm;
$pay_button = $pay_code->get_code($order_);
$order['pay_button'] = $pay_button; //即此时已经生成了一个支付按钮