ecshop交通银行在线支付插件使用例子
最近,一个项目要求做交通银行在线支付,ecshop本身没有这方面的接口,于是通过一些时间的专研,做了一个插件出来,有好的东西,当然要分享,在此特地分享出来,希望能够帮助到跟我一样有需要的人,为大家减轻一下开发的负担,也多请大家指出一些好的方法和建议,相互的学习、进步.
在使用插件之前,请配置好交通银行在线支付的环境,具体安装方法,交行提供的demo会有,也不是很难,注意好细节就行,安装好之后,请将includes文件夹和languages文件夹拷贝到ecshop站点的根目录覆盖,最后进入后台的支付模块安装即可.
以下是调用的范例,代码如下:
- 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;
源码,includes/modules/payment/bankcomm.php,代码如下:
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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'] = 'http://www.daixiaorui.com';
-
-
-
-
- $modules[$i]['version'] = '1.0.0.0';
-
-
-
-
- $modules[$i]['config'] = array();
-
- return;
-
- }
-
-
-
-
-
-
-
-
- class bankcomm
-
- {
-
-
-
-
-
-
-
-
-
-
-
- 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());
-
- $param['orderTime'] = local_date("His",gmtime());
-
- $param['tranType'] = 0;
-
- $param['amount'] = $order['amount'];
-
- $param['curType'] = 'CNY';
-
- $param['orderContent'] = '';
-
- $param['orderMono'] = $order['orderMono'];
-
- $param['phdFlag'] = '';
-
- $param['notifyType'] = 1;
-
- $param['merURL'] = '';
-
- $param['goodsURL'] = $order['goodsURL'];
-
- $param['jumpSeconds'] = '';
-
- $param['payBatchNo'] = '';
-
- $param['proxyMerName'] = '';
-
- $param['proxyMerType'] = '';
-
- $param['proxyMerCredentials'] = '';
-
- $param['netType'] = 0;
-
- $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 /> ";
-
- } 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);
-
- }
-
-
-
- $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"){
-
-
-
-
-
- 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) {
-
-
-
- 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);
-
- }
-
-
-
- $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 != ''){
-
-
-
-
-
- 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;
-
- }
-
-
-
- }
-
- }
-
-
- ?>
-
-
- 源码(languages/zh_cn/payment/bankcomm.php):
-
-
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
- global $_LANG;
-
-
- $_LANG['bankcomm'] = '交通银行在线支付';
-
- $_LANG['bankcomm_desc'] = '交通银行在线支付';
-
- $_LANG['pay_button'] = '交通银行支付';
-
-
- ?>