/**
*
* @param orderId 订单id(回写业务逻辑使用)
* @param ptOrderCode 订单编号
* @param transactionNo 微信支付返回的流水单号(和订单编号二选一)
* @param totalFee 退款金额
* @return
*/
@Transactional
public Object refundPay(Integer orderId,String ptOrderCode,String transactionNo,double totalFee) {
try{
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream("微商城支付平台的证书");
try {
keyStore.load(instream, "商户号".toCharArray());
}finally {
instream.close();
}
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, "商户号".toCharArray()).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
HttpPost httppost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund");
String xml =wxPayRefund(ptOrderCode,transactionNo,String.valueOf((int)(totalFee*100)));
try {
StringEntity se = new StringEntity(xml);
httppost.setEntity(se);
CloseableHttpResponse responseEntry = httpclient.execute(httppost);
try {
HttpEntity entity = responseEntry.getEntity();
if (entity != null) {
JSONObject result = new JSONObject();
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(entity.getContent());
Element rootElt = document.getRootElement();
String resultcode ="";
String return_msg = rootElt.elementText("return_msg");
String err_code = rootElt.elementText("err_code");
String err_code_des = rootElt.elementText("err_code_des");
String return_code = rootElt.elementText("return_code");
resultcode = rootElt.elementText("result_code");
if(resultcode.equals("SUCCESS")){
result.put("weixinPayUrl", rootElt.elementText("code_url"));
result.put("prepayId", rootElt.elementText("prepay_id"));
result.put("status","success");
result.put("msg","success");
//成功,更改订单状态
ptOrderDao.upPtOrderStatus(orderId,6);
}else{
result.put("status","false");
result.put("msg",rootElt.elementText("err_code_des"));
}
return result;
}
EntityUtils.consume(entity);
}
finally {
responseEntry.close();
}
}
}
finally {
httpclient.close();
}
return null;
}catch(Exception e){
e.printStackTrace();
JSONObject result = new JSONObject();
result.put("status","error");
result.put("msg",e.getMessage());
return result;
}
}
public String wxPayRefund(String out_trade_no, String transaction_id,String total_fee) {
StringBuffer xml = new StringBuffer();
String data = null;
try {
String nonceStr = this.generateOrderNo();//获取随机字符串
xml.append("</xml>");
SortedMap<String,String> parameters = new TreeMap<String,String>();
parameters.put("appid", WeChatConfigs.getAppID());//公众账号ID
parameters.put("mch_id", WeChatConfigs.getMchID());//商户号
parameters.put("nonce_str", nonceStr);//随机字符串
parameters.put("out_trade_no", out_trade_no);//订单号
//parameters.put("transaction_id", transaction_id);//微信返回的流水单号
parameters.put("out_refund_no", nonceStr);//退款单号
parameters.put("fee_type", "CNY");//币总
parameters.put("total_fee", total_fee);//订单金额
parameters.put("refund_fee", total_fee);//退款金额
parameters.put("op_user_id", WeChatConfigs.getMchID());//商户号
parameters.put("sign", createSign(parameters, WeChatConfigs.getApiSecret()));//API密钥
data = PayCommonUtil.getRequestXml(parameters);
} catch (Exception e) {
System.err.println(e.getMessage());
return null;
}
return data;
}
public String createSign(SortedMap<String, String> packageParams, String AppKey) {
StringBuffer sb = new StringBuffer();
Set es = packageParams.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + AppKey);
String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
return sign;
}
本文介绍了一种基于微信支付平台的退款流程实现方法。通过调用微信支付提供的退款接口,完成退款业务逻辑。涉及证书加载、HTTPS请求构造及XML数据解析等关键技术。
4135

被折叠的 条评论
为什么被折叠?



