本文實例講述了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 將不變的參數 配置 寫入配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
$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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/** * 交通銀行支付類 */ 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 控制器
1
|
$this ->load->model( "Bocom" ); |
支付方法:
1
|
$this ->arrData = $this ->Bocom->pay( $this ->data); |
通知:
1
|
$this ->arrData = $this ->Bocom->notify(); |
希望本文所述對大家PHP程序設計有所幫助。