Java微信支付之關閉訂單
本文實例為大家分享了java微信支付之關閉訂單的具體代碼,供大家參考,具體內容如下
一、應用場景
商戶訂單支付失敗需要生成新單號重新發(fā)起支付,要對原訂單號調用關單,避免重復支付
系統(tǒng)下單后,用戶支付超時,系統(tǒng)退出不再受理,避免用戶繼續(xù),請調用關單接口
注意:訂單生成后不能馬上調用關單接口,最短調用時間間隔為5分鐘。
二、接口地址
https://api.mch.weixin.qq.com/pay/closeorder
三、請求參數
只能根據自己商戶系統(tǒng)的訂單號關閉
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;
}
}
四、返回結果
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; // 返回狀態(tài)碼SUCCESS/FAIL
@NotRequire
private String return_msg; //返回信息
/**** return_code 為SUCCESS ****/
private String result_code;// 業(yè)務結果
private String result_msg;// 業(yè)務結果描述
@NotRequire
private String err_code;// 錯誤返回的信息描述
@NotRequire
private String err_code_des;// 錯誤返回的信息描述
}
五、關閉訂單
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;
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
解決JAVA8 Collectors.toMap value為null報錯的問題
這篇文章主要介紹了解決JAVA8 Collectors.toMap value為null報錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
SpringBoot項目中使用@Scheduled讀取動態(tài)參數
這篇文章主要介紹了SpringBoot項目中使用@Scheduled讀取動態(tài)參數,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot @JsonDeserialize自定義Json序列化方式
這篇文章主要介紹了SpringBoot @JsonDeserialize自定義Json序列化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
springboot3.X 無法解析parameter參數問題分析
本文介紹了Spring Boot 3.2.1版本中調用接口時出現的參數解析問題,該錯誤是由Spring新版本加強的錯誤校驗和報錯提示導致的,在Spring 6.1之后,官方要求URL中的傳參必須使用`@PathVariable`聲明用于接收的變量,而不能省略`@RequestParam`注解,感興趣的朋友一起看看吧2025-03-03
在MyBatis中使用 # 和 $ 書寫占位符的區(qū)別說明
這篇文章主要介紹了在MyBatis中使用 # 和 $ 書寫占位符的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
java 服務器接口快速開發(fā)之servlet詳細教程
Servlet(Server Applet)是Java Servlet的簡稱,稱為小服務程序或服務連接器,用Java編寫的服務器端程序,具有獨立于平臺和協(xié)議的特性,主要功能在于交互式地瀏覽和生成數據,生成動態(tài)Web內容2021-06-06

