导入相关的jar包
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>1.3.3</version>
</dependency>
创建退款实体
public class WxReturnFeeEntity implements Serializable {
private String return_code;
private String return_msg;
private String appid;
private String mch_id;
private String nonce_str;
private String sign;
private String result_code;
private String out_refund_no;
private String err_code;
}
退款的详细代码
/**
* 微信退款
* <p>
* 微信退款详情信息
*
* @param order 订单
* @return WxReturnFeeEntity 退款实体
*/
public WxReturnFeeEntity refundFee(OrderEntity order) {
String nonce_str = System.currentTimeMillis() + "";
/** 当前时间 yyyyMMddHHmmss */
String currTime = getCurrTime();
/** 8位日期 */
String strTime = currTime.substring(8, currTime.length());
/** 四位随机数 */
String strRandom = buildRandom(4) + "";
/** 订单号 */
String out_refund_no = strTime + strRandom;
SortedMap<String, String> packageParams = new TreeMap<String, String>();
packageParams.put("appid", "wx84254d2616e3c80d");
packageParams.put("mch_id", "1275811201");
packageParams.put("nonce_str", nonce_str);
packageParams.put("out_trade_no", order.getOutTradeNo());
packageParams.put("out_refund_no", out_refund_no);
packageParams.put("total_fee", (int) (order.getPayMoney() * hundred) + "");
packageParams.put("refund_fee", (int) (order.getPayMoney() * hundred) + "");
packageParams.put("refund_fee_type", "CNY");
packageParams.put("op_user_id", "1275811201");
String sign = WxCryptUtil.createSign(packageParams, "kuaileguojiang20151212kingberry1");
String xml = "<xml>" + "<appid>wx84254d2616e3c80d</appid>" + "<mch_id>1275811201</mch_id>" + "<nonce_str>"
+ nonce_str + "</nonce_str>" + "<sign>" + sign + "</sign>" + "<out_trade_no>" + order.getOutTradeNo()
+ "</out_trade_no>" + "<out_refund_no>" + out_refund_no + "</out_refund_no>" + "<total_fee>"
+ packageParams.get("total_fee") + "</total_fee>" + "<refund_fee>" + packageParams.get("refund_fee")
+ "</refund_fee>" + "<refund_fee_type>" + packageParams.get("refund_fee_type") + "</refund_fee_type>"
+ "<op_user_id>1275811201</op_user_id>" + "</xml>";
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund");
try {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File(kvConfigs.getValues().get("static")
+ "/apiclient_cert.p12"));
try {
keyStore.load(instream, "1275811201".toCharArray());
} finally {
instream.close();
}
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "1275811201".toCharArray()).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" },
null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
StringEntity entity = new StringEntity(xml, Consts.UTF_8);
httpPost.setEntity(entity);
CloseableHttpResponse response = httpclient.execute(httpPost);
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
XStream xstream = XStreamInitializer.getInstance();
xstream.alias("xml", WxReturnFeeEntity.class);
WxReturnFeeEntity wxReturnFeeEntity = (WxReturnFeeEntity) xstream.fromXML(responseContent);
return wxReturnFeeEntity;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}