最近,一個項目要求做交通銀行線上支付,ecshop本身沒有這方面的介面,於是通過一些時間的專研,做了一個外掛程式出來。有好的東西,當然要分享,在此特地分享出來,希望能夠協助到跟我一樣有需要的人,為大家減輕一下開發的負擔,也多請大家指出一些好的方法和建議,相互的學習、進步!
在使用外掛程式之前,請配置好交通銀行線上支付的環境(具體安裝方法,交行提供的demo會有,也不是很難,注意好細節就行)。安裝好之後,請將把下面的外掛程式源碼和語言套件源碼按路徑儲存到相應檔案,最後進入背景支付模組安裝即可。
<?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'] = 'http://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 ///////////////
}
}
?>