//退款代码
@RequestMapping(value = “/refundMoney”)
@ResponseBody
public String refundMoney(@LoginUser Member member,HttpServletRequest request,String orderNumber)
throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException,
IOException, CertificateException {
String ip = request.getHeader("X-Forwarded-For");
if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
//多次反向代理后会有多个ip值,第一个ip才是真实ip
int index = ip.indexOf(",");
if(index != -1){
ip= ip.substring(0,index);
}
}
//Order order=orderService.queryByOrderNumber(orderNumber);
String currTime = TenpayUtil.getCurrTime();
String strTime = currTime.substring(8, currTime.length());
String strRandom = TenpayUtil.buildRandom(4) + "";
String nonce_str = strTime + strRandom;
//String order_price = order.getAmount().multiply(new BigDecimal(100)).toBigInteger().toString();
SortedMap<String, String> packageParams = new TreeMap<String, String>();
String check_name = "NO_CHECK"; //是否验证真实姓名呢
//NO_CHECK:不校验真实姓名
//FORCE_CHECK:强校验真实姓名
packageParams.put("mch_appid", GlobalConfig.APPID); //微信公众号的appid
packageParams.put("mchid", GlobalConfig.MCH_ID); //商务号
packageParams.put("nonce_str",nonce_str); //随机生成后数字,保证安全性
packageParams.put("partner_trade_no",orderNumber); //生成商户订单号
packageParams.put("openid",member.getOpenId()); // 支付给用户openid
packageParams.put("check_name",check_name); //是否验证真实姓名呢
packageParams.put("re_user_name","hu");//收款用户姓名
packageParams.put("amount","100"); //企业付款金额,单位为分
packageParams.put("desc","测试功能"); //企业付款操作说明信息。必填。
packageParams.put("spbill_create_ip",ip); //调用接口的机器Ip地址
Map map = wechatPayService.forRefund2(packageParams);
if (map != null) {
String return_code = (String) map.get("return_code");
String result_code = (String) map.get("result_code");
if (return_code.equals("SUCCESS") && result_code.equals("SUCCESS")) {
// 退款成功
orderService.refundSuccess(map,(String) map.get("out_trade_no"));
return "退款成功";
} else {
return (String) map.get("err_code_des");
}
} else {
return "未知的错误";
}
}
记得需要证书
public Map<String, Object> forRefund2(SortedMap<String, String> packageParams) throws KeyStoreException, IOException,
UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException, CertificateException {
String sign = createSign(packageParams);
String xml = “”
+ “<mch_appid><![CDATA[" + packageParams.get("mch_appid") + "]]></mch_appid>”
+ “<![CDATA[" + packageParams.get("mchid") + "]]>”
+ “<nonce_str><![CDATA[" + packageParams.get("nonce_str") + "]]></nonce_str>”
+ “<partner_trade_no><![CDATA[" + packageParams.get("partner_trade_no") + "]]></partner_trade_no>”
+ “<![CDATA[" + packageParams.get("openid") + "]]>”
+ “<check_name><![CDATA[" + packageParams.get("check_name") + "]]></check_name>”
+ “<re_user_name><![CDATA[" + packageParams.get("re_user_name") + "]]></re_user_name>”
+ “<![CDATA[" + packageParams.get("amount") + "]]>”
+ “
+ “<spbill_create_ip><![CDATA[" + packageParams.get("spbill_create_ip") + "]]></spbill_create_ip>”
+ “” + sign + “” + “”;
Map doXMLtoMap = new HashMap();
KeyStore keyStore = KeyStore.getInstance(“PKCS12”);
String P12_PASSWORD = GlobalConfig.MCH_ID;
FileInputStream inputStream = new FileInputStream(cretUrl + “//apiclient_cert.p12”);
try {
keyStore.load(inputStream,P12_PASSWORD.toCharArray());
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} finally {
inputStream.close();
}
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, P12_PASSWORD.toCharArray()).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { “TLSv1” }, null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
DefaultHttpClient client = new DefaultHttpClient();
client.getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
String refundUrl=“https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers”;
HttpPost httpPost = new HttpPost(refundUrl);
try {
httpPost.setEntity(new StringEntity(xml, "UTF-8"));
CloseableHttpResponse response = httpClient.execute(httpPost);
String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
if (jsonStr.indexOf("FAIL") >= 0) {
return null;
}
doXMLtoMap = XMLUtil.doXMLParse(jsonStr);
return doXMLtoMap;
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.close();
}
return null;
}