在微信支付 開發者文檔頁面 下載最新的 php sdk
http://mch.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=11_1
這里假設你已經申請完微信支付
1. 微信后臺配置 如圖
我們先進行測試,所以先把測試授權目錄和 測試白名單添加上。測試授權目錄是你要發起微信請求的哪個文件所在的目錄。
例如jsapi 發起請求一般是jsapi.php所在目錄 為測試目錄,測試白名單即開發人員的微信號。
正式的支付授權目錄不能和測試的一樣否則會報錯。不填寫或者填錯授權目錄以及測試白名單都會報錯。
報錯樣例:
nansystem:access_denied
不在測試白名單
2. 配置 lib/wxpay.config.php文件
最主要配置一下四項:
const appid = '';
const mchid = '';
const key = '';
const appsecret = '';
appid 和 appsecret都可以在微信后臺中找到。
mchid 在申請微信支付后發來的郵件中可以找到,key 則根據郵件提示
去商戶平臺配置即可。
3. 訪問起始 index.php
首先訪問 index.php 你可以看到界面
我們首先需要的是 jsapi支付。但是看代碼 index.php 最下面的鏈接。他默認是個demo的鏈接,改為我們自定義的即可
<ul>
<li style="background-color:#ff7f24"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/jsapi.php';?>">jsapi支付</a></li>
<li style="background-color:#698b22"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/micropay.php';?>">刷卡支付</a></li>
<li style="background-color:#8b6914"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/native.php';?>">掃碼支付</a></li>
<li style="background-color:#cdcd00"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/orderquery.php';?>">訂單查詢</a></li>
<li style="background-color:#cd3278"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/refund.php';?>">訂單退款</a></li>
<li style="background-color:#848484"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/refundquery.php';?>">退款查詢</a></li>
<li style="background-color:#8ee5ee"><a href="<?php echo 'http://'.$_server['http_host'].$_server['request_uri'].'example/download.php';?>">下載訂單</a></li>
</ul>
當然你也可以直接寫死為自己的訪問鏈接。
4. jsapi 支付
必要代碼解析:
$loghandler= new clogfilehandler("../logs/".date('y-m-d').'.log');
$log = log::init($loghandler, 15);
調用日志類 可以通過 $log->debug(‘test‘); 打印調試信息。其實也可以直接使用 $log::debug(‘test‘); 來調試
$tools = new jsapipay();
$openid = $tools->getopenid();
主要是為了獲取 openid 其中getopenid() 函數定義在 文件 wxpay.jsapipay.php 文件中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public function getopenid() { //通過code獲得openid if (!isset( $_get [ 'code' ])){ //觸發微信返回code碼 $baseurl = urlencode( 'http://' . $_server [ 'http_host' ]. $_server [ 'php_self' ]. $_server [ 'query_string' ]); $url = $this ->__createoauthurlforcode( $baseurl ); header( "location: $url" ); exit (); } else { //獲取code碼,以獲取openid $code = $_get [ 'code' ]; $openid = $this ->getopenidfrommp( $code ); return $openid ; } } |
$baseurl 其實就是為了在跳轉回來這個頁面。 可以繼續跟蹤函數__createoauthurlforcode() 其實就是通過微信的auth2.0 來獲取openid
參考鏈接:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
這就需要你把微信的 網頁授權接口也設置好。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
獲取到 openid 就可以調用微信支付的統一下單接口了。回到 文件 jsapi.php 如下代碼 $input = new wxpayunifiedorder(); $input ->setbody( "test" ); $input ->setattach( "test" ); $input ->setout_trade_no(wxpayconfig::mchid. date ( "ymdhis" )); $input ->settotal_fee( "1" ); $input ->settime_start( date ( "ymdhis" )); $input ->settime_expire( date ( "ymdhis" , time() + 600)); $input ->setgoods_tag( "test" ); $input ->setnotify_url( "http://paysdk.weixin.qq.com/example/notify.php" ); $input ->settrade_type( "jsapi" ); $input ->setopenid( $openid ); $order = wxpayapi::unifiedorder( $input ); echo '<font color="#f00"><b>統一下單支付單信息</b></font><br/>' ; printf_info( $order ); $jsapiparameters = $tools ->getjsapiparameters( $order ); |
這里面的代碼:
$input->setattach("test");
如果 把值改為 $input->setattach("test this is attach");就會存在bug 后面再說,其實這個參數不是必須的干脆可以去掉。
代碼:
$input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");
是設置接收支付結果通知的url 這里是默認的demo 鏈接我們可以設置成我們的:
$input->setnotify_url(dirname('http://'.$_server['http_host'].$_server['request_uri']).'/notify.php');
當然你也可以選擇直接寫死。
其中的函數 unifiedorder($input) 可以到wxpay.api.php 中文件跟蹤,其實就是調用統一下單接口。
在 wxpay.api.php 中需要更改的一處代碼是:
1
2
3
4
|
//異步通知url未設置,則使用配置文件中的url if (! $inputobj ->isnotify_urlset()){ $inputobj ->setnotify_url(wxpayconfig::notify_url); //異步通知url } |
就是當沒設置 notifyurl 的時候回去配置文件中找,但是配置文件中根本沒有設置。
所以你可以選擇在 配置文件wxpay.config.php 中加上這個配置,也可以直接寫一個默認的notify鏈接。
函數 getjsapiparameters() 是獲取jsapi支付的參數給變量 $jsapiparameters 方便在下面的js中調用
jsapi.php 中js的代碼:
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
|
function jsapicall() { weixinjsbridge.invoke( 'getbrandwcpayrequest' , <?php echo $jsapiparameters ; ?>, function (res){ weixinjsbridge.log(res.err_msg); alert(res.err_code+res.err_desc+res.err_msg); } ); } function callpay() { if (typeof weixinjsbridge == "undefined" ){ if ( document.addeventlistener ){ document.addeventlistener( 'weixinjsbridgeready' , jsapicall, false); } else if (document.attachevent){ document.attachevent( 'weixinjsbridgeready' , jsapicall); document.attachevent( 'onweixinjsbridgeready' , jsapicall); } } else { jsapicall(); } } |
其中點擊立即支付按鈕調用的就是 callpay() 函數,他有會調用jsapicall() 函數打開支付程序。
此后輸入密碼完成支付。
在完成支付頁面點擊完成會回到這個支付頁面,并彈出 支付成功的提示框
這個其實就是 js函數 jsapicall 里面的alter 彈出的對話框
其中 res.err_msg 為get_brand_wcpay_request:ok 表明前端判斷的支付成功,我們可以根據這個將支付跳轉到成功頁面。
但是這個并不可信。確認是否支付成功還是應當 通過notify.php 處理業務邏輯。
5. 支付結果通知 notify.php
其實這個頁面最主要的代碼就兩行
$notify = new paynotifycallback();
$notify->handle(false);
其中大部分邏輯在 handle 函數中處理 文件 wxpay.notify.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
final public function handle( $needsign = true) { $msg = "ok" ; //當返回false的時候,表示notify中調用notifycallback回調失敗獲取簽名校驗失敗,此時直接回復失敗 $result = wxpayapi::notify( array ( $this , 'notifycallback' ), $msg ); if ( $result == false){ $this ->setreturn_code( "fail" ); $this ->setreturn_msg( $msg ); $this ->replynotify(false); return ; } else { //該分支在成功回調到notifycallback方法,處理完成之后流程 $this ->setreturn_code( "success" ); $this ->setreturn_msg( "ok" ); } $this ->replynotify( $needsign ); } |
主要代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
$result = wxpayapi::notify( array ( $this , 'notifycallback' ), $msg ); //跟蹤函數 notify 文件wxpay.api.php public static function notify( $callback , & $msg ) { //獲取通知的數據 $xml = $globals [ 'http_raw_post_data' ]; //如果返回成功則驗證簽名 try { $result = wxpayresults::init( $xml ); } catch (wxpayexception $e ){ $msg = $e ->errormessage(); return false; } return call_user_func( $callback , $result ); } |
通過 $globals[‘http_raw_post_data‘]; 獲取同志數據 然后 init 函數驗證簽名等。驗簽成功運行代碼
return call_user_func($callback, $result);
即調用了一個回調函數,notifycallback() 函數并傳遞參數 $result 在notifycallback函數中會調用我們重寫的notifyprocess()函數(此函數在notify.php 中被重寫)
notifyprocess() 判斷也沒有問題就會 設置返回 success的xml信息
$this->setreturn_code("success");
$this->setreturn_msg("ok");
并最終調用函數 $this->replynotify($needsign); echo success的結果
函數replynotify 需要修改一處代碼:
1
2
3
4
5
6
7
8
9
10
|
final private function replynotify( $needsign = true) { //如果需要簽名 if ( $needsign == true && $this ->getreturn_code( $return_code ) == "success" ) { $this ->setsign(); } wxpayapi::replynotify( $this ->toxml()); } |
$this->getreturn_code($return_code) == "success")
改為
$this->getreturn_code() == "success")
即可。
這樣整個流程就結束了。上面提到了 傳遞訂單參數
$input->setattach("test");
如果我設置 值為 test this is attach (其實只要有空格就會存在bug)
如圖 傳遞的訂單信息
可以看到 attach 信息正常,當然支付也是正常的沒有任何問題。
但是發現總是會收到notify 通知,即意味著沒有返回給微信服務器正確的結果通知。
打印服務器發來的通知數據
可以看到 attach 是 test+this+is+attach 即空格被轉化為加號
打印接收到的簽名和程序算出來的簽名發現 簽名不同,即認為接收結果異常。
所以我們要是想使用attach 這個值就不能有空格,要么干脆不使用這個參數
(等待微信修復這個bug, 也可能是我這邊有哪個地方不會? - -#)
這樣 微信支付的 jsapi支付就大致分析完成了。
以上就是對微信支付php sdk 支付代碼的詳解,后續繼續補充相關資料,謝謝大家對本站的支持!