本文執行個體講述了php版交通銀行網銀支付介面實現方法。分享給大家供大家參考,具體如下:
概述:網銀支付介面 和支付寶介面大體上差不多,主要的區別是 交通銀行提供的 介面核心的比如,加密等是通過java實現的,所以,要想辦法使php和java能正常的通訊,為此,官方也提供了兩套實現方法,一個是通過 socket 進行通訊,另一個方法是通過 java 橋接,下面示範的是 socket方法.
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"
1.3 修改設定檔(in/B2CMerchantSocket.xml).
採用官方提供的測試 商號進行測試時,無需配置,否則要配置,具體看xml檔案說明.
1.4 啟動 socket 服務
window:啟動 start.bat 及可.
linux:啟動 ohup sh.start,sh& //使當前指令碼脫離終端,並在後台運行。
2. 將網銀整合到現有的系統,以mvc的結構進行說明.
2.1 將不變的參數 配置 寫入設定檔:
$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; #渠道編號//以下是 新介面需要的參數$config['socketUrl'] ="tcp://127.0.0.1:8891"; #socket url$config['merID'] ='301310063009501'; #商戶id 3013100630095012
2.2 Model
/** * 交通銀行支付類 */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();
更多關於PHP相關內容感興趣的讀者可查看本站專題:《php常見資料庫操作技巧匯總》、《PHP數組(Array)操作技巧大全》、《php排序演算法總結》、《PHP常用遍曆演算法與技巧總結》、《PHP資料結構與演算法教程》、《php程式設計演算法總結》、《PHP數學運算技巧總結》、《phpRegex用法總結》、《PHP運算與運算子用法總結》及《php字串(string)用法總結》
希望本文所述對大家PHP程式設計有所協助。