程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php版交通銀行網銀支付接口開發入門教程

php版交通銀行網銀支付接口開發入門教程

編輯:PHP綜合

本文實例講述了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數學運算技巧總結》、《php正則表達式用法總結》、《PHP運算與運算符用法總結》及《php字符串(string)用法總結》

希望本文所述對大家PHP程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved