支付完成后,微信會(huì)把相關(guān)支付結(jié)果和用戶信息發(fā)送給商戶,商戶需要接收處理,并返回應(yīng)答。
對(duì)后臺(tái)通知交互時(shí),如果微信收到商戶的應(yīng)答不是成功或超時(shí),微信認(rèn)為通知失敗,微信會(huì)通過(guò)一定的策略定期重新發(fā)起通知,盡可能提高通知的成功率,但微信不保證通知最終能成功。 (通知頻率為15/15/30/180/1800/1800/1800/1800/3600,單位:秒)
注意:同樣的通知可能會(huì)多次發(fā)送給商戶系統(tǒng)。商戶系統(tǒng)必須能夠正確處理重復(fù)的通知。
推薦的做法是,當(dāng)收到通知進(jìn)行處理時(shí),首先檢查對(duì)應(yīng)業(yè)務(wù)數(shù)據(jù)的狀態(tài),判斷該通知是否已經(jīng)處理過(guò),如果沒(méi)有處理過(guò)再進(jìn)行處理,如果處理過(guò)直接返回結(jié)果成功。在對(duì)業(yè)務(wù)數(shù)據(jù)進(jìn)行狀態(tài)檢查和處理之前,要采用數(shù)據(jù)鎖進(jìn)行并發(fā)控制,以避免函數(shù)重入造成的數(shù)據(jù)混亂。
特別提醒:商戶系統(tǒng)對(duì)于支付結(jié)果通知的內(nèi)容一定要做簽名驗(yàn)證,防止數(shù)據(jù)泄漏導(dǎo)致出現(xiàn)“假通知”,造成資金損失。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
//支付結(jié)果通知接口 @requestmapping ( "/qlydweixinotify.do" ) public void weixinotify(httpservletrequest request, httpservletresponse response) { printwriter out = null ; stringbuffer xmlstr = new stringbuffer(); try { bufferedreader reader = request.getreader(); string line = null ; while ((line = reader.readline()) != null ) { xmlstr.append(line); } logger.getlogger(getclass()).debug( "支付回調(diào)通知:" +xmlstr.tostring()); //檢查xml是否有效 boolean flag=signature.checkissignvalidfromresponsestring(xmlstr.tostring()); weixinnotifyresult result= null ; if (flag){ notifyresdata wxdata=(notifyresdata) util.getobjectfromxml(xmlstr.tostring(),notifyresdata. class ); if (wxdata != null ){ if ( "success" .equals(wxdata.getreturn_code())&& "success" .equals(wxdata.getresult_code())){ orderpayinfo orderpayinfo = new orderpayinfo(); orderpayinfo.setordernum(wxdata.getout_trade_no()); orderpayinfo.setpaynum(wxdata.gettransaction_id()); orderpayinfo.setpayprice(( double )wxdata.gettotal_fee()/ 100 + "" ); orderpayinfo.setpaysource(wxdata.getopenid()); orderpayinfo.setpaytime(wxdata.gettime_end()); ordermessage returnmessage = orderproductserver .completeproductorder(orderpayinfo); if (orderstatus.fail.equals(returnmessage .getorderstatus())) { logger.getlogger(getclass()).error( "遠(yuǎn)程接口完成訂單失敗" ); result= new weixinnotifyresult( "fail" ); result.setreturn_msg( "遠(yuǎn)程接口完成訂單失敗" ); } else { result= new weixinnotifyresult( "success" ); result.setreturn_msg( "成功" ); } } else { result= new weixinnotifyresult( "fail" ); result.setreturn_msg( "失敗" ); } } else { result= new weixinnotifyresult( "fail" ); result.setreturn_msg( "解析參數(shù)格式失敗" ); } } else { result= new weixinnotifyresult( "fail" ); result.setreturn_msg( "簽名失敗" ); } response.getwriter().write(result.tostring()); } catch (exception e) { logger.getlogger(getclass()).error( "qlydweixinotify.do" , e); responedeal.getinstance().sendresponsestr(response, "404" , "連接超時(shí)" ); } finally { if (out != null ) { out.close(); } } } |
模擬http請(qǐng)求工具類:
httpsrequestutil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
package com.qlwb.weixin.util; import java.io.ioexception; import org.apache.commons.httpclient.httpclient; import org.apache.commons.httpclient.httpexception; import org.apache.commons.httpclient.methods.postmethod; import org.apache.commons.httpclient.methods.requestentity; import org.apache.commons.httpclient.methods.stringrequestentity; import org.apache.log4j.logger; import com.qlwb.weixin.common.configure; import com.qlwb.weixin.common.util; import com.qlwb.weixin.protocol.pay_protocol.wxpayreqdata; import com.qlwb.weixin.protocol.payquery_protocol.payqueryreqdata; public class httpsrequestutil { /** * * @方法名稱:sendwxpayrequest * @內(nèi)容摘要: <發(fā)送統(tǒng)一下單請(qǐng)求> * @param body * @param outtradeno * @param totalfee * @param spbillcreateip * @return * string * @exception * @author:鹿偉偉 * @創(chuàng)建日期:2016年2月19日-下午2:24:05 */ public string sendwxpayrequest(string body,string detail,string outtradeno, int totalfee,string spbillcreateip ) { // 構(gòu)造http請(qǐng)求 httpclient httpclient = new httpclient(); postmethod postmethod = new postmethod(configure.pay_api); wxpayreqdata wxdata = new wxpayreqdata(body,detail,outtradeno,totalfee,spbillcreateip); string requeststr= "" ; requeststr=util.convertobj2xml(wxdata); // 發(fā)送請(qǐng)求 string strresponse = null ; try { requestentity entity = new stringrequestentity( requeststr.tostring(), "text/xml" , "utf-8" ); postmethod.setrequestentity(entity); httpclient.executemethod(postmethod); strresponse = new string(postmethod.getresponsebody(), "utf-8" ); logger.getlogger(getclass()).debug(strresponse); } catch (httpexception e) { logger.getlogger(getclass()).error( "sendwxpayrequest" , e); } catch (ioexception e) { logger.getlogger(getclass()).error( "sendwxpayrequest" , e); } finally { postmethod.releaseconnection(); } return strresponse; } /** * * @方法名稱:orderqueryrequest * @內(nèi)容摘要: <查詢訂單信息> * @param transaction_id 微信的訂單號(hào),優(yōu)先使用 * @return * string * @exception * @author:鹿偉偉 * @創(chuàng)建日期:2016年2月19日-下午2:44:11 */ public string orderqueryrequest(string transactionid, string outtradeno ) { // 構(gòu)造http請(qǐng)求 httpclient httpclient = new httpclient(); postmethod postmethod = new postmethod(configure.pay_query_api); payqueryreqdata wxdata = new payqueryreqdata(transactionid,outtradeno); string requeststr= "" ; requeststr=util.convertobj2xml(wxdata); // 發(fā)送請(qǐng)求 string strresponse = null ; try { requestentity entity = new stringrequestentity( requeststr.tostring(), "text/xml" , "utf-8" ); postmethod.setrequestentity(entity); httpclient.executemethod(postmethod); strresponse = new string(postmethod.getresponsebody(), "utf-8" ); } catch (httpexception e) { logger.getlogger(getclass()).error( "orderqueryrequest" , e); } catch (ioexception e) { logger.getlogger(getclass()).error( "orderqueryrequest" , e); } finally { postmethod.releaseconnection(); } return strresponse; } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/yelin042/article/details/80636540