因為某一方技術的原因,可能導致商戶在預期時間內都收不到最終支付通知,此時商戶可以通過該API來查詢訂單的詳細支付狀態。
訂單查詢API的URL為:
https://api.weixin.qq.com/pay/orderquery?access_token=xxxxxx
URL中的參數只包含目前微信公眾平台憑證access_token,而訂單查詢的真正數據是放在PostData中的,格式如下:
{
"appid" : "wwwwb4f85f3a797777",
"package" : "out_trade_no=11122&partner=1900090055&sign=4e8d0df3da0c3d0df38f",
"timestamp" : "1369745073",
"app_signature" : "53cca9d47b883bd4a5c85a9300df3da0cb48565c",
"sign_method" : "sha1"
}
上述內容參數說明如表所示。
二、實現細節
1. 獲得access token
這個很容易,參考微信公眾平台開發(26) ACCESS TOKEN
代碼如下:
$appid = "wx0000000000000000"; $appsecret = "e76050733c695748537fc4d4c21d0e2c"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $result = https_request($url); $jsoninfo = json_decode($result, true); $access_token = $jsoninfo["access_token"];
2. 參數生成
appid: 直接賦值
timestamp:程序直接獲取
$timestamp = time();
sign_method:這裡為sha1
難點1:package 值的獲得
先要獲得sign
sign是out_trade_no,partner,key(partnerkey)三項信息的字典序排序,再MD5運算,再轉為大寫
$sign= strtoupper(md5("out_trade_no=JfuKdiBig4zZnE4n&partner=1234567890&key=ebf5cf381de2d716d432bfda34fa9e57"));
package 是查詢訂單的關鍵信息數據,包含第三方唯一訂單號 out_trade_no、財付通商戶身仹標識 partner(即前文所述的 partnerid) 、簽名 sign
$package = "out_trade_no=JfuKdiBig4zZnE4n&partner=1234567890&sign=".$sign;
難點2:獲得app_signature
app_signature 依然是根據支付簽名(paySign)生成方法中所講的簽名方式生成的,參加簽名字段為:appid、appkey、package、timestamp;
$obj['appid'] = "wx0000000000000000"; $obj['appkey'] = "8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6k"; $obj['package'] = $package; $obj['timestamp'] = $timestamp; $WxPayHelper->get_biz_sign($obj);
這樣各項參數都獲得了
3.提交查詢
$jsonmenu = ' { "appid" : "wx0000000000000000", "package" : "'.$package.'", "timestamp" : "'.$timestamp.'", "app_signature" : "'.$app_signature.'", "sign_method" : "sha1" } '; $url = "https://api.weixin.qq.com/pay/orderquery?access_token=".$access_token; $result = https_request($url, $jsonmenu); var_dump($result);
完整代碼如下所示:
include_once("WxPayHelper.php"); //1. 獲取access token $appid = "wx0000000000000000"; $appsecret = "e76050733ce76050733ce76050733cdd"; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; $result = https_request($url); $jsoninfo = json_decode($result, true); $access_token = $jsoninfo["access_token"]; //2.准備參數 $timestamp = time(); $sign= strtoupper(md5("out_trade_no=JfuKdiBig4zZnE4n&partner=1234567890&key=asdfasdfasdfasdfasdfasdfasdfasdf")); $package = "out_trade_no=JfuKdiBig4zZnE4n&partner=1234567890&sign=".$sign; //2.1構造最麻煩的app_signature $obj['appid'] = "wx0000000000000000"; $obj['appkey'] = "8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6k"; $obj['package'] = $package; $obj['timestamp'] = $timestamp; $WxPayHelper = new WxPayHelper(); //get_biz_sign函數受保護,需要先取消一下,否則會報錯 $app_signature = $WxPayHelper->get_biz_sign($obj); //3. 將構造的json提交給微信服務器,查詢 $jsonmenu = ' { "appid" : "wx0000000000000000", "package" : "'.$package.'", "timestamp" : "'.$timestamp.'", "app_signature" : "'.$app_signature.'", "sign_method" : "sha1" } '; $url = "https://api.weixin.qq.com/pay/orderquery?access_token=".$access_token; $result = https_request($url, $jsonmenu); var_dump($result); function https_request($url, $data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)){ curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; }
三、訂單結果
上述程序執行後,獲得訂單結果如下
{ "errcode": 0, "errmsg": "ok", "order_info": { "ret_code": 0, "ret_msg": "", "input_charset": "GBK", "trade_state": "0", "trade_mode": "1", "partner": "1234567890", "bank_type": "CMB_FP", "bank_billno": "201405273540085997", "total_fee": "1", "fee_type": "1", "transaction_id": "1218614901201405273313473135", "out_trade_no": "JfuKdiBig4zZnE4n", "is_split": "false", "is_refund": "false", "attach": "", "time_end": "20140527194139", "transport_fee": "0", "product_fee": "1", "discount": "0", "rmb_total_fee": "" } }
各個字段的含義如表所示
如果程序有誤,會在errcode和errmsg描述出來。
以上就是對微信支付開發中查詢功能的資料整理,希望能幫到微信開發支付的朋友。