This article introduces the implementation of the order query function in WeChat payment.
Author: Fangbei Studio
Address: http://www.cnblogs.com/txw1958/p/wxpay-order-query.html
Due to technical reasons on one side, the merchant may not receive the final payment notification within the expected time. At this time, the merchant can query the detailed payment status of the order through this API.
The URL of the order query API is:
https://api.weixin.qq.com/pay/orderquery?access_token=xxxxxx
The parameters in the URL only contain the current WeChat public platform credential access_token, while the real data of the order query is placed in PostData with the following format:
<span>{ </span>"appid" : "wwwwb4f85f3a797777", "package" : "out_trade_no=11122&partner=1900090055&sign=4e8d0df3da0c3d0df38f", "timestamp" : "1369745073", "app_signature" : "53cca9d47b883bd4a5c85a9300df3da0cb48565c", "sign_method" : "sha1"<span> }</span>
The description of the above content parameters is shown in the table.
Parameters
|
Description<🎜> |
||||||||||||
appid<🎜> |
AppId of the public platform account;<🎜> |
||||||||||||
package<🎜> |
Query the key information data of the order, including the third-party unique order number out_trade_no, the Tenpay merchant ID partner (the partnerid mentioned above), and the signature sign, where sign is the dictionary sequence of the parameters. Sort and combine with &, finally add &key=partnerkey (unique allocation), perform md5 operation, and then convert to all uppercase, finally get sign<🎜> |
||||||||||||
timestamp<🎜> |
linux timestamp;<🎜> |
||||||||||||
app_signature<🎜> |
Generated according to the signature method mentioned in the payment signature (paySign) generation method, the participating signature fields are: appid, appkey, package, timestamp; <🎜> |
||||||||||||
sign_method<🎜> |
Signature method (not counted in signature generation);<🎜> |
这个很容易,参考微信公众平台开发(26) ACCESS TOKEN
代码如下:
<span>1</span> <span>$appid</span> = "wx0000000000000000"<span>; </span><span>2</span> <span>$appsecret</span> = "e76050733c695748537fc4d4c21d0e2c"<span>; </span><span>3</span> <span>$url</span> = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=<span>$appid</span>&secret=<span>$appsecret</span>"<span>; </span><span>4</span> <span>$result</span> = https_request(<span>$url</span><span>); </span><span>5</span> <span>$jsoninfo</span> = json_decode(<span>$result</span>, <span>true</span><span>); </span><span>6</span> <span>$access_token</span> = <span>$jsoninfo</span>["access_token"];
appid: 直接赋值
timestamp:程序直接获取
<span>$timestamp</span> = <span>time</span>();
sign_method:这里为sha1
难点1:package 值的获得
先要获得sign
sign是out_trade_no,partner,key(partnerkey)三项信息的字典序排序,再MD5运算,再转为大写
<span>$sign</span>= <span>strtoupper</span>(<span>md5</span>("out_trade_no=JfuKdiBig4zZnE4n&partner=1234567890&key=ebf5cf381de2d716d432bfda34fa9e57"));
package 是查询订单的关键信息数据,包含第三方唯一订单号 out_trade_no、财付通商户身仹标识 partner(即前文所述的 partnerid) 、签名 sign
<span>$package</span> = "out_trade_no=JfuKdiBig4zZnE4n&partner=1234567890&sign=".<span>$sign</span>;
难点2:获得app_signature
app_signature 依然是根据支付签名(paySign)生成方法中所讲的签名方式生成的,参加签名字段为:appid、appkey、package、timestamp;
<span>$obj</span>['appid'] = "wx0000000000000000"<span>; </span><span>$obj</span>['appkey'] = "8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6k"<span>; </span><span>$obj</span>['package'] = <span>$package</span><span>; </span><span>$obj</span>['timestamp'] = <span>$timestamp</span><span>; </span><span>$WxPayHelper</span> = <span>new</span><span> WxPayHelper(); </span><span>//</span><span>get_biz_sign函数受保护,需要先取消一下,否则会报错</span> <span>$app_signature</span> = <span>$WxPayHelper</span>->get_biz_sign(<span>$obj</span>);
这样各项参数都获得了
<span>$jsonmenu</span> = '<span> { "appid" : "wx0000000000000000", "package" : "</span>'.<span>$package</span>.'<span>", "timestamp" : "</span>'.<span>$timestamp</span>.'<span>", "app_signature" : "</span>'.<span>$app_signature</span>.'<span>", "sign_method" : "sha1" } </span>'<span>; </span><span>$url</span> = "https://api.weixin.qq.com/pay/orderquery?access_token=".<span>$access_token</span><span>; </span><span>$result</span> = https_request(<span>$url</span>, <span>$jsonmenu</span><span>); </span><span>var_dump</span>(<span>$result</span>);
完整代码如下所示:
<span> 1</span> <span>include_once</span>("WxPayHelper.php"<span>); </span><span> 2</span> <span> 3</span> <span>//</span><span>1. 获取access token</span> <span> 4</span> <span>$appid</span> = "wx0000000000000000"<span>; </span><span> 5</span> <span>$appsecret</span> = "e76050733ce76050733ce76050733cdd"<span>; </span><span> 6</span> <span>$url</span> = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=<span>$appid</span>&secret=<span>$appsecret</span>"<span>; </span><span> 7</span> <span>$result</span> = https_request(<span>$url</span><span>); </span><span> 8</span> <span>$jsoninfo</span> = json_decode(<span>$result</span>, <span>true</span><span>); </span><span> 9</span> <span>$access_token</span> = <span>$jsoninfo</span>["access_token"<span>]; </span><span>10</span> <span>11</span> <span>12</span> <span>//</span><span>2.准备参数</span> <span>13</span> <span>$timestamp</span> = <span>time</span><span>(); </span><span>14</span> <span>$sign</span>= <span>strtoupper</span>(<span>md5</span>("out_trade_no=JfuKdiBig4zZnE4n&partner=1234567890&key=asdfasdfasdfasdfasdfasdfasdfasdf"<span>)); </span><span>15</span> <span>$package</span> = "out_trade_no=JfuKdiBig4zZnE4n&partner=1234567890&sign=".<span>$sign</span><span>; </span><span>16</span> <span>17</span> <span>//</span><span>2.1构造最麻烦的app_signature</span> <span>18</span> <span>$obj</span>['appid'] = "wx0000000000000000"<span>; </span><span>19</span> <span>$obj</span>['appkey'] = "8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6kxCRvdJENpWpw8mruTNOGeX8OVUlIYxIyw6k"<span>; </span><span>20</span> <span>$obj</span>['package'] = <span>$package</span><span>; </span><span>21</span> <span>$obj</span>['timestamp'] = <span>$timestamp</span><span>; </span><span>22</span> <span>$WxPayHelper</span> = <span>new</span><span> WxPayHelper(); </span><span>23</span> <span>//</span><span>get_biz_sign函数受保护,需要先取消一下,否则会报错</span> <span>24</span> <span>$app_signature</span> = <span>$WxPayHelper</span>->get_biz_sign(<span>$obj</span><span>); </span><span>25</span> <span>26</span> <span>//</span><span>3. 将构造的json提交给微信服务器,查询</span> <span>27</span> <span>$jsonmenu</span> = ' <span>28</span> <span>{ </span><span>29</span> <span> "appid" : "wx0000000000000000", </span><span>30</span> "package" : "'.<span>$package</span>.'<span>", </span><span>31</span> "timestamp" : "'.<span>$timestamp</span>.'<span>", </span><span>32</span> "app_signature" : "'.<span>$app_signature</span>.'<span>", </span><span>33</span> <span> "sign_method" : "sha1" </span><span>34</span> <span>} </span><span>35</span> '<span>; </span><span>36</span> <span>37</span> <span>$url</span> = "https://api.weixin.qq.com/pay/orderquery?access_token=".<span>$access_token</span><span>; </span><span>38</span> <span>$result</span> = https_request(<span>$url</span>, <span>$jsonmenu</span><span>); </span><span>39</span> <span>var_dump</span>(<span>$result</span><span>); </span><span>40</span> <span>41</span> <span>function</span> https_request(<span>$url</span>, <span>$data</span> = <span>null</span><span>){ </span><span>42</span> <span>$curl</span> =<span> curl_init(); </span><span>43</span> curl_setopt(<span>$curl</span>, CURLOPT_URL, <span>$url</span><span>); </span><span>44</span> curl_setopt(<span>$curl</span>, CURLOPT_SSL_VERIFYPEER, <span>FALSE</span><span>); </span><span>45</span> curl_setopt(<span>$curl</span>, CURLOPT_SSL_VERIFYHOST, <span>FALSE</span><span>); </span><span>46</span> <span>if</span> (!<span>empty</span>(<span>$data</span><span>)){ </span><span>47</span> curl_setopt(<span>$curl</span>, CURLOPT_POST, 1<span>); </span><span>48</span> curl_setopt(<span>$curl</span>, CURLOPT_POSTFIELDS, <span>$data</span><span>); </span><span>49</span> <span> } </span><span>50</span> curl_setopt(<span>$curl</span>, CURLOPT_RETURNTRANSFER, 1<span>); </span><span>51</span> <span>$output</span> = curl_exec(<span>$curl</span><span>); </span><span>52</span> curl_close(<span>$curl</span><span>); </span><span>53</span> <span>return</span> <span>$output</span><span>; </span><span>54</span> }
上述程序执行后,获得订单结果如下
<span>{ </span>"errcode": 0<span>, </span>"errmsg": "ok"<span>, </span>"order_info"<span>: { </span>"ret_code": 0<span>, </span>"ret_msg": ""<span>, </span>"input_charset": "GBK"<span>, </span>"trade_state": "0"<span>, </span>"trade_mode": "1"<span>, </span>"partner": "1234567890"<span>, </span>"bank_type": "CMB_FP"<span>, </span>"bank_billno": "201405273540085997"<span>, </span>"total_fee": "1"<span>, </span>"fee_type": "1"<span>, </span>"transaction_id": "1218614901201405273313473135"<span>, </span>"out_trade_no": "JfuKdiBig4zZnE4n"<span>, </span>"is_split": "false"<span>, </span>"is_refund": "false"<span>, </span>"attach": ""<span>, </span>"time_end": "20140527194139"<span>, </span>"transport_fee": "0"<span>, </span>"product_fee": "1"<span>, </span>"discount": "0"<span>, </span>"rmb_total_fee": ""<span> } }</span>
各个字段的含义如表所示。
参数 |
说明 |
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即可。 |
If the program is wrong, will be described in errcode and errmsg.