来源:自学PHP网 时间:2015-04-14 12:58 作者: 阅读:次
[导读] 一、订单查询因为某一方技术的原因,可能导致商户在预期时间内都收不到最终支付通知,此时商户可以通过该API来查询订单的详细支付状态。订单查询API的URL为:https: api weixin qq com...
一、订单查询 因为某一方技术的原因,可能导致商户在预期时间内都收不到最终支付通知,此时商户可以通过该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 代码如下: 1 $appid = "wx0000000000000000"; 2 $appsecret = "e76050733c695748537fc4d4c21d0e2c"; 3 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; 4 $result = https_request($url); 5 $jsoninfo = json_decode($result, true); 6 $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 = new WxPayHelper(); //get_biz_sign函数受保护,需要先取消一下,否则会报错 $app_signature = $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); 完整代码如下所示: 1 include_once("WxPayHelper.php"); 2 3 //1. 获取access token 4 $appid = "wx0000000000000000"; 5 $appsecret = "e76050733ce76050733ce76050733cdd"; 6 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; 7 $result = https_request($url); 8 $jsoninfo = json_decode($result, true); 9 $access_token = $jsoninfo["access_token"]; 10 11 12 //2.准备参数 13 $timestamp = time(); 14 $sign= strtoupper(md5("out_trade_no=JfuKdiBig4zZnE4n&partner=1234567890&key=asdfasdfasdfasdfasdfasdfasdfasdf")); 15 $package = "out_trade_no=JfuKdiBig4zZnE4n&partner=1234567890&sign=".$sign; 16 17 //2.1构造最麻烦的app_signature 18 $obj['appid'] = "wx0000000000000000"; 19 $obj['appkey'] = "8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6k"; 20 $obj['package'] = $package; 21 $obj['timestamp'] = $timestamp; 22 $WxPayHelper = new WxPayHelper(); 23 //get_biz_sign函数受保护,需要先取消一下,否则会报错 24 $app_signature = $WxPayHelper->get_biz_sign($obj); 25 26 //3. 将构造的json提交给微信服务器,查询 27 $jsonmenu = ' 28 { 29 "appid" : "wx0000000000000000", 30 "package" : "'.$package.'", 31 "timestamp" : "'.$timestamp.'", 32 "app_signature" : "'.$app_signature.'", 33 "sign_method" : "sha1" 34 } 35 '; 36 37 $url = "https://api.weixin.qq.com/pay/orderquery?access_token=".$access_token; 38 $result = https_request($url, $jsonmenu); 39 var_dump($result); 40 41 function https_request($url, $data = null){ 42 $curl = curl_init(); 43 curl_setopt($curl, CURLOPT_URL, $url); 44 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 45 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); 46 if (!empty($data)){ 47 curl_setopt($curl, CURLOPT_POST, 1); 48 curl_setopt($curl, CURLOPT_POSTFIELDS, $data); 49 } 50 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 51 $output = curl_exec($curl); 52 curl_close($curl); 53 return $output; 54 } 三、订单结果上述程序执行后,获得订单结果如下 { "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描述出来。
|
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com