WeChat payment development (5) order query_PHP tutorial
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
1. Order inquiry
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);<🎜> |
二、实现细节
1. 获得access token
这个很容易,参考微信公众平台开发(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"];
2. 参数生成
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>);
这样各项参数都获得了
3.提交查询
<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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In WeChat, users can enter their payment password to make purchases, but how do they retrieve their payment password if they forget it? Users need to go to My-Services-Wallet-Payment Settings-to recover their payment password if they forget it. This introduction to how to retrieve your payment password if you forget it will tell you the specific operation method. The following is a detailed introduction, so take a look! WeChat usage tutorial. How to find the WeChat payment password if you forget it? Answer: My-Service-Wallet-Payment Settings-Forgot payment password. Specific method: 1. First, click My. 2. Click on the service inside. 3. Click on the wallet inside. 4. Find the payment settings. 5. Click Forgot payment password. 6. Enter your own information for verification. 7. Then enter the new payment password to change it.

Solution for forgetting WeChat payment password: 1. Open WeChat APP, click "I" in the lower right corner to enter the personal center page; 2. In the personal center page, click "Pay" to enter the payment page; 3. On the payment page , click "..." in the upper right corner to enter the payment management page; 4. In the payment management page, find and click "Forgot payment password"; 5. Follow the page prompts and enter personal information for identity verification. After successful verification, you can Choose the method of "retrieve by swiping your face" or "retrieve by verifying bank card information" to retrieve your password, etc.

There are many food and snack shops provided in the Meituan takeout app, and all mobile phone users log in through their accounts. Add your personal delivery address and contact number to enjoy the most convenient takeout service. Open the homepage of the software, enter product keywords, and search online to find the corresponding product results. Just swipe up or down to purchase and place an order. The platform will also recommend dozens of nearby restaurants with high reviews based on the delivery address provided by the user. The store can also set up different payment methods. You can place an order with one click to complete the order. The rider can arrange the delivery immediately and the delivery speed is very fast. There are also takeout red envelopes of different amounts for use. Now the editor is online in detail for Meituan takeout users. We show you how to set up WeChat payment. 1. After selecting the product, submit the order and click Now

Steps to set the order of deductions for WeChat payment: 1. Open the WeChat APP, click on the "Me" interface, click on "Services", and then click on "Collect and Payment"; 2. Click on "Prioritize Use This Payment Method" under the payment code on the collection and payment interface; 3. Select the preferred payment method you need.

When everyone has nothing to do, they will choose to browse the Xianyu platform. Everyone can find that there are a large number of products on this platform, which can allow everyone to see various second-hand products. Although these products are second-hand products, there is absolutely no problem with the quality of these products, so everyone can buy them with confidence. The prices are very affordable, and they still allow everyone to face-to-face with these products. It is entirely possible for sellers to communicate and conduct some price bargaining operations. As long as everyone negotiates properly, then you can choose to conduct transactions, and when everyone pays here, they want to make WeChat payment, but it seems that the platform It's not allowed. Please follow the editor to find out what the specific situation is. Xianyu

WeChat payment cannot be canceled immediately after successful payment. Refunds usually need to meet the following conditions: 1. The merchant's refund policy. The merchant will formulate its own refund policy, including the refund time window, refund amount and refund method; 2. Payment time, refunds usually require Apply within a certain time frame, and refunds may not be possible beyond this time frame; 3. Goods or service status. If the user has received the goods or enjoyed the service, the merchant may require the user to return the goods or provide corresponding proof; 4. Refund process, etc.

1. First, we need to open the WeChat APP on the mobile phone, and then click to log in to the WeChat account, so that we enter the WeChat homepage. 2. Click the [Me] button in the lower right corner of the WeChat homepage, then select the [Payment] option. We click to enter the payment page. 3. After entering the [Payment] page, click the [Wallet] option to enter, and click [Bill] in the upper right corner of the [Wallet] page.

Alibaba 1688 is a purchasing and wholesale website, and the items there are much cheaper than Taobao. So how does Alibaba use WeChat payment? The editor has compiled some relevant content to share with you. Friends in need can come and take a look. How does Alibaba use WeChat payment? Answer: WeChat payment cannot be used for the time being; 1. On the page where we purchase goods, we click [Change payment method] 2. Then in the pop-up page, we can only go to [Alipay, staged payment] , cashier] can be selected;
