本文實例為大家分享了java微信支付之關閉訂單的具體代碼,供大家參考,具體內容如下
一、應用場景
商戶訂單支付失敗需要生成新單號重新發起支付,要對原訂單號調用關單,避免重復支付
系統下單后,用戶支付超時,系統退出不再受理,避免用戶繼續,請調用關單接口
注意:訂單生成后不能馬上調用關單接口,最短調用時間間隔為5分鐘。
二、接口地址
https://api.mch.weixin.qq.com/pay/closeorder
三、請求參數
只能根據自己商戶系統的訂單號關閉
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
|
package com.phil.wechatpay.model.rep; import java.io.serializable; /** * 關閉訂單請求參數(正常xml) * @author phil * @date 2017年7月25日 * */ public class closeorderparams extends abstractpayparams implements serializable{ /** * */ private static final long serialversionuid = -4206464928803827244l; private string out_trade_no; //商戶訂單號 public string getout_trade_no() { return out_trade_no; } public void setout_trade_no(string out_trade_no) { this .out_trade_no = out_trade_no; } } |
四、返回結果
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
|
package com.phil.wechatpay.model.resp; import java.io.serializable; import com.phil.common.annotation.notrequire; /** * 關閉訂單返回參數(帶<![cdata[]]>xml格式) * * @author phil * @date 2017年7月25日 * */ public class closeorderresult extends abstractpayresult implements serializable { private static final long serialversionuid = -1996103742747816922l; private string return_code; // 返回狀態碼success/fail @notrequire private string return_msg; //返回信息 /**** return_code 為success ****/ private string result_code; // 業務結果 private string result_msg; // 業務結果描述 @notrequire private string err_code; // 錯誤返回的信息描述 @notrequire private string err_code_des; // 錯誤返回的信息描述 } |
五、關閉訂單
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
|
package com.phil.wechatpay.controller; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responsebody; import com.phil.common.config.wechatconfig; import com.phil.common.util.httprequtil; import com.phil.common.util.payutil; import com.phil.common.util.signatureutil; import com.phil.common.util.xmlutil; import com.phil.wechatpay.model.rep.closeorderparams; import com.phil.wechatpay.model.resp.closeorderresult; import com.phil.wechatpay.service.wechatpayservice; /** * 關閉訂單 * @author phil * @date 2017年7月25日 * */ @controller @requestmapping ( "/wxpay/" ) public class wechatpaycloseordercontroller { @autowired private wechatpayservice wechatpayservice; @responsebody @requestmapping ( "closeorder" ) public closeorderresult closeorder(httpservletrequest request, httpservletresponse response) throws exception { closeorderresult closeorderresult = null ; closeorderparams closeorderparams = new closeorderparams(); closeorderparams.setappid(wechatconfig.app_id); closeorderparams.setmch_id(wechatconfig.mch_id); closeorderparams.setnonce_str(payutil.createnoncestr()); closeorderparams.setout_trade_no( "" ); //自己傳入 //請求的xml string closeorderxml = wechatpayservice.abstractpaytoxml(closeorderparams); //簽名合并到service // 返回<![cdata[success]]>格式的xml string closeorderresultxml = httprequtil.httpsdefaultexecute(httprequtil.post_method,wechatconfig.close_order_url, null , closeorderxml); // 進行簽名校驗 if (signatureutil.checkissignvalidfromweixin(closeorderresultxml)) { closeorderresult = xmlutil.getobjectfromxml(closeorderresultxml, closeorderresult. class ); } return closeorderresult; } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/phil_jing/article/details/77948695