首页 >php教程 >php手册 >ecshop交通银行在线支付插件使用例子

ecshop交通银行在线支付插件使用例子

WBOY
WBOY原创
2016-05-25 16:46:392215浏览

最近,一个项目要求做交通银行在线支付,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
/**
 * 交通银行在线支付插件 For Ecshop
 * Author: Reson
 * Date: 2014/03/31
 */
if (!defined(&#39;IN_ECS&#39;)) {
    die(&#39;Hacking attempt&#39;);
}
$payment_lang = ROOT_PATH . &#39;languages/&#39; . $GLOBALS[&#39;_CFG&#39;][&#39;lang&#39;] . &#39;/payment/bankcomm.php&#39;;
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][&#39;code&#39;] = basename(__FILE__, &#39;.php&#39;);
    /* 描述对应的语言项 */
    $modules[$i][&#39;desc&#39;] = &#39;bankcomm_desc&#39;;
    /* 是否支持货到付款 */
    $modules[$i][&#39;is_cod&#39;] = &#39;0&#39;;
    /* 是否支持在线支付 */
    $modules[$i][&#39;is_online&#39;] = &#39;1&#39;;
    /* 支付费用,由配送决定 */
    $modules[$i][&#39;pay_fee&#39;] = &#39;0&#39;;
    /* 作者 */
    $modules[$i][&#39;author&#39;] = &#39;Reson&#39;;
    /* 网址 */
    $modules[$i][&#39;website&#39;] = &#39;http://www.daixiaorui.com&#39;;
    /* 版本号 */
    $modules[$i][&#39;version&#39;] = &#39;1.0.0.0&#39;;
    /* 配置信息 */
    $modules[$i][&#39;config&#39;] = array();
    return;
}
/**
 * 类
 */
class bankcomm {
    /**
     * 构造函数
     *
     * @return void
     */
    function bankcomm() {
    }
    function __construct() {
        $this->bankcomm();
    }
    /**
     * 提交函数
     */
    function get_code($order) {
        //获得表单传过来的数据
        $param[&#39;interfaceVersion&#39;] = &#39;1.0.0.0&#39;; //消息版本号*
        $param[&#39;merID&#39;] = &#39;301310063009501&#39;; //商户号 (测试号,后期可自行更改)
        $param[&#39;orderid&#39;] = $order[&#39;orderid&#39;]; //订单号*
        $param[&#39;orderDate&#39;] = local_date("Ymd", gmtime()); //商户订单日期* yyyyMMdd
        $param[&#39;orderTime&#39;] = local_date("His", gmtime()); //商户订单时间* HHmmss
        $param[&#39;tranType&#39;] = 0; //交易类别* 0:B2C
        $param[&#39;amount&#39;] = $order[&#39;amount&#39;]; //订单金额*
        $param[&#39;curType&#39;] = &#39;CNY&#39;; //交易币种* 默认CNY
        $param[&#39;orderContent&#39;] = &#39;&#39;;
        $param[&#39;orderMono&#39;] = $order[&#39;orderMono&#39;]; //商家备注
        $param[&#39;phdFlag&#39;] = &#39;&#39;; //物流配送标志
        $param[&#39;notifyType&#39;] = 1; //通知方式* 1 通知
        $param[&#39;merURL&#39;] = &#39;&#39;;
        $param[&#39;goodsURL&#39;] = $order[&#39;goodsURL&#39;]; //取货URL
        $param[&#39;jumpSeconds&#39;] = &#39;&#39;;
        $param[&#39;payBatchNo&#39;] = &#39;&#39;;
        $param[&#39;proxyMerName&#39;] = &#39;&#39;;
        $param[&#39;proxyMerType&#39;] = &#39;&#39;;
        $param[&#39;proxyMerCredentials&#39;] = &#39;&#39;;
        $param[&#39;netType&#39;] = 0; //渠道编号* 0:html渠道
        $param[&#39;issBankNo&#39;] = &#39;&#39;;
        $tranCode = "cb2200_sign";
        htmlentities($param[&#39;orderMono&#39;], "ENT_QUOTES", "utf-8");
        //连接字符串
        $source = &#39;&#39;;
        foreach ($param as $key => $val) {
            if ($key != &#39;issBankNo&#39;) $source.= $val . &#39;|&#39;;
        }
        $source = substr($source, 0, strlen($source) - 1);
        //连接地址 http://www.phprm.com
        $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=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?>";
            $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(&#39;retCode&#39;);
        $retCode_value = $retCode->item(0)->nodeValue;
        $errMsg = $dom->getElementsByTagName(&#39;errMsg&#39;);
        $errMsg_value = $errMsg->item(0)->nodeValue;
        $signMsg = $dom->getElementsByTagName(&#39;signMsg&#39;);
        $signMsg_value = $signMsg->item(0)->nodeValue;
        $orderUrl = $dom->getElementsByTagName(&#39;orderUrl&#39;);
        $orderUrl_value = $orderUrl->item(0)->nodeValue;
        $MerchID = $dom->getElementsByTagName(&#39;MerchID&#39;);
        $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[&#39;signMsg_value&#39;] = $signMsg_value;
            $param[&#39;orderUrl_value&#39;] = $orderUrl_value;
            $form_code = $this->create_html($param); //创建提交表单
            return $form_code;
        }
    }
    /**
     * 创建提交表单
     */
    function create_html($param) {
        $pay_html = &#39;<form name = "form1" method = "post" action = "&#39; . $param[&#39;orderUrl_value&#39;] . &#39;">
<input type = "hidden" name = "interfaceVersion" value = "&#39; . $param[&#39;interfaceVersion&#39;] . &#39;">
<input type = "hidden" name = "merID" value = "&#39; . $param[&#39;merID&#39;] . &#39;">
<input type = "hidden" name = "orderid" value = "&#39; . $param[&#39;orderid&#39;] . &#39;">
<input type = "hidden" name = "orderDate" value = "&#39; . $param[&#39;orderDate&#39;] . &#39;">
<input type = "hidden" name = "orderTime" value = "&#39; . $param[&#39;orderTime&#39;] . &#39;">
<input type = "hidden" name = "tranType" value = "&#39; . $param[&#39;tranType&#39;] . &#39;">
<input type = "hidden" name = "amount" value = "&#39; . $param[&#39;amount&#39;] . &#39;">
<input type = "hidden" name = "curType" value = "&#39; . $param[&#39;curType&#39;] . &#39;">
<input type = "hidden" name = "orderContent" value = "&#39; . $param[&#39;orderContent&#39;] . &#39;">
<input type = "hidden" name = "orderMono" value = "&#39; . $param[&#39;orderMono&#39;] . &#39;">
<input type = "hidden" name = "phdFlag" value = "&#39; . $param[&#39;phdFlag&#39;] . &#39;">
<input type = "hidden" name = "notifyType" value = "&#39; . $param[&#39;notifyType&#39;] . &#39;">
<input type = "hidden" name = "merURL" value = "&#39; . $param[&#39;merURL&#39;] . &#39;">
<input type = "hidden" name = "goodsURL" value = "&#39; . $param[&#39;goodsURL&#39;] . &#39;">
<input type = "hidden" name = "jumpSeconds" value = "&#39; . $param[&#39;jumpSeconds&#39;] . &#39;">
<input type = "hidden" name = "payBatchNo" value = "&#39; . $param[&#39;payBatchNo&#39;] . &#39;">
<input type = "hidden" name = "proxyMerName" value = "&#39; . $param[&#39;proxyMerName&#39;] . &#39;">
<input type = "hidden" name = "proxyMerType" value = "&#39; . $param[&#39;proxyMerType&#39;] . &#39;">
<input type = "hidden" name = "proxyMerCredentials" value = "&#39; . $param[&#39;proxyMerCredentials&#39;] . &#39;">
<input type = "hidden" name = "netType" value = "&#39; . $param[&#39;netType&#39;] . &#39;">
<input type = "hidden" name = "merSignMsg" value = "&#39; . $param[&#39;signMsg_value&#39;] . &#39;">
<input type = "hidden" name = "issBankNo" value = "&#39; . $param[&#39;issBankNo&#39;] . &#39;">
<input type="submit" value=" " class="pay_button" />
</form>&#39;;
        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 /> ";
            return false;
        } else {
            $in = "<?xml version=&#39;1.0&#39; encoding=&#39;UTF-8&#39;?>";
            $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(&#39;retCode&#39;);
        $retCode_value = $retCode->item(0)->nodeValue;
        $errMsg = $dom->getElementsByTagName(&#39;errMsg&#39;);
        $errMsg_value = $errMsg->item(0)->nodeValue;
        $signMsg = $dom->getElementsByTagName(&#39;signMsg&#39;);
        $signMsg_value = $signMsg->item(0)->nodeValue;
        if ($retCode_value != &#39;&#39;) {
            //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 http://www.phprm.com
 * 2014/03/31
 */
global $_LANG;
$_LANG[&#39;bankcomm&#39;] = &#39;交通银行在线支付&#39;;
$_LANG[&#39;bankcomm_desc&#39;] = &#39;交通银行在线支付&#39;;
$_LANG[&#39;pay_button&#39;] = &#39;交通银行支付&#39;;
?>

下载地址:ecshop交通银行在线支付插件使用例子 13962827995368.zip


教程网址:

欢迎收藏∩_∩但请保留本文链接。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn