您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)如何入門php版交通銀行網(wǎng)銀支付接口開發(fā),小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
概述:網(wǎng)銀支付接口 和支付寶接口大體上差不多,主要的區(qū)別是 交通銀行提供的 接口核心的比如,加密等是通過java實(shí)現(xiàn)的,所以,要想辦法使php和java能正常的通信,為此,官方也提供了兩套實(shí)現(xiàn)方法,一個(gè)是通過 socket 進(jìn)行通信,另一個(gè)方法是通過 java 橋接,下面演示的是 socket方法.
1. 配置運(yùn)行環(huán)境
1.1 安裝java,自行到oracle官網(wǎng)下載 java,然后安裝,并配置正確的 環(huán)境變量.
1.2 把 測試的證書導(dǎo)入到j(luò)ava 虛擬機(jī).
keytool " -import -keystore "java虛擬機(jī)放置證書的地址" -storepass changeit -alias test_bocommca -file "證書路徑" 完成導(dǎo)入。
例子: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).
采用官方提供的測試 商號(hào)進(jìn)行測試時(shí),無需配置,否則要配置,具體看xml文件說明.
1.4 啟動(dòng) socket 服務(wù)
window:啟動(dòng) start.bat 及可.
linux:啟動(dòng) ohup sh.start,sh& //使當(dāng)前腳本脫離終端,并在后臺(tái)運(yùn)行。
2. 將網(wǎng)銀集成到現(xiàn)有的系統(tǒng),以mvc的結(jié)構(gòu)進(jìn)行說明.
2.1 將不變的參數(shù) 配置 寫入配置文件:
$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"; # 主動(dòng)通知url $config['goodsURL'] = '/goods/1.html'; #取貨url $config['jumpSeconds'] =3; #跳轉(zhuǎn)時(shí)間 $config['payBatchNo'] = ''; #商戶批次號(hào) $config['proxyMerName'] = ''; #代理商家名字 $config['proxyMerType'] = ''; #代理商類型 $config['proxyMerCredentials']= ''; #代理商家批次號(hào) $config['netType'] = 0; #渠道編號(hào) //以下是 新接口需要的參數(shù) $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){ //獲得表單傳過來的數(shù)據(jù) $this->arrReturn['interfaceVersion'] = $this->config->item('interfaceVersion'); $this->arrReturn['merID'] = $this->config->item('merID'); //商戶號(hào)為固定 $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"]): ''; #訂單內(nèi)容 $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'); //以下參數(shù) 不參與簽名 $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","交易錯(cuò)誤信息:".$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","網(wǎng)銀支付通知·非法請求"); return false; } $notifyMsg = $_REQUEST["notifyMsg"]; log_message("error",$notifyMsg."回調(diào)...."); $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","交易錯(cuò)誤信息:" .$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){ // 更新 訂單狀態(tài) } //end class }
2.3 控制器
$this->load->model("Bocom");
支付方法:
$this->arrData =$this->Bocom->pay($this->data);
通知:
$this->arrData =$this->Bocom->notify();
以上就是如何入門php版交通銀行網(wǎng)銀支付接口開發(fā),小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。