一、訂單查詢
因為某一方技術的原因,可能導致商家在預期時間內都收不到最終支付通知,此時商家可以透過該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"}
上述內容參數說明如表所示。
參數 |
##說明 |
#appid | #公眾平台帳號的AppId; |
##package |
查詢訂單的關鍵資訊數據,包含第三方唯一訂單號out_trade_no、財付通商戶身仹標識partner(即前文所述的partnerid)、簽章sign,其中sign是對參數字典序排序並使用&聯合起來,最後加上&key=partnerkey(唯一分配),進行md5運算,再轉成全大寫,最終得到sign |
timestamp | linux時間戳記; |
app_signature | 根據支付簽章(paySign)產生方法中所講的簽章方式產生的,參加簽章欄位為:appid、appkey、package、timestamp; |
这个很容易,参考微信公众平台开发(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);
这样各项参数都获得了
$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": "" } }
各个字段的含义如表所示。
参数 |
说明 |
ret_code |
查询结果状态码,0表明成功,其他表明错误; |
ret_msg |
查询结果出错信息; |
input_charset |
返回信息中的编码方式; |
trade_state |
订单状态,0为成功,其他为失败; |
trade_mode |
交易模式,1为即时到帐,其他保留; |
partner |
财付通商户号,即前文的partnerid; |
bank_type |
银行类型; |
bank_billno |
银行订单号; |
total_fee |
总金额,单位为分; |
fee_type |
币种,1为人民币; |
transaction_id |
财付通订单号; |
out_trade_no |
第三方订单号; |
is_split |
是否分账,false为无分账,true为有分账; |
is_refund |
是否退款,false为无退款,ture为退款; |
attach |
商户数据包,即生成订单package时商户填入的attach; |
time_end |
支付完成时间; |
transport_fee |
物流费用,单位为分; |
product_fee |
物品费用,单位为分; |
discount |
折扣价格,单位为分; |
rmb_total_fee |
换算成人民币之后的总金额,单位为分,一般看total_fee即可。 |
如果程式有誤,會在errcode和errmsg描述出來。
更多微信支付開發訂單查詢相關文章請關注PHP中文網!