社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
package com.pay.controller.weixin;
import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.net.ssl.SSLContext;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.pay.bo.ReturnHist;
import com.pay.service.WeiXinRefundService;
import com.pay.utils.weixin.ClientCustomSSL;
import com.pay.utils.weixin.CustomizedPropertyPlaceholderConfigurer;
@RestController
@RequestMapping("/pay/refund")
public class WeiXinRefundController {
@Autowired
WeiXinRefundService weiXinRefundService;
/**
* 微信退款
* @param request
* @param response
* @return
*/
@RequestMapping(value="/weiXinRefund",method=RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public Object refund(HttpServletResponse response,@RequestBody String body){
System.out.println("==========================微信退款开始!!========================");
try{
JSONObject jsonO = JSONObject.fromObject(body);
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File(
CustomizedPropertyPlaceholderConfigurer.getContextProperty("wx.cert").toString()));
try {
keyStore.load(instream,"见邮件".toCharArray());
}finally {
instream.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,
"见邮件".toCharArray()).build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf).build();
// HttpGet httpget = new
// HttpGet("https://api.mch.weixin.qq.com/secapi/pay/refund");
HttpPost httppost = new HttpPost(
"https://api.mch.weixin.qq.com/secapi/pay/refund");
String totalFee = jsonO.get("totalFee").toString();
String outTradeNo = weiXinRefundService.getTradePayNo(jsonO.getString("orderNo"));//"所退款订单编号"
String refundFee = jsonO.get("refundFee").toString();//"退款金额"
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String nonceStr = sdf.format(dt).toString();//"随机字符串";
String xml = ClientCustomSSL.RefundNativePackage(outTradeNo,nonceStr,totalFee,refundFee,nonceStr);
try {
StringEntity se = new StringEntity(xml);
httppost.setEntity(se);
System.out.println("executing request" + httppost.getRequestLine());
CloseableHttpResponse responseEntry = httpclient.execute(httppost);
try {
HttpEntity entity = responseEntry.getEntity();
System.out.println("----------------------------------------");
System.out.println(responseEntry.getStatusLine());
if (entity != null) {
System.out.println("Response content length: "
+ entity.getContentLength());
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(entity.getContent());
Element rootElt = document.getRootElement();
System.out.println("根节点:" + rootElt.getName());
System.out.println("==="+rootElt.elementText("return_code"));
System.out.println("==="+rootElt.elementText("result_code"));
System.out.println("==="+rootElt.elementText("transaction_id"));
System.out.println("==="+rootElt.elementText("out_trade_no"));
System.out.println("==="+rootElt.elementText("out_refund_no"));
System.out.println("==="+rootElt.elementText("refund_id"));
System.out.println("==="+rootElt.elementText("refund_channel"));
System.out.println("==="+rootElt.elementText("refund_fee"));
System.out.println("==="+rootElt.elementText("coupon_refund_fee"));
String returnCode = rootElt.elementText("return_code");
JSONObject result = new JSONObject();
if(returnCode.equals("SUCCESS")){
ReturnHist rh = new ReturnHist();
rh.setOtherRefundId(rootElt.elementText("out_refund_no"));
rh.setReturnChannelType(rootElt.elementText("refund_channel"));
rh.setReturnMoneyBak(Float.parseFloat(totalFee)/100);
rh.setReturnMoneyBalance((Float.parseFloat(totalFee)-Float.parseFloat(rootElt.elementText("refund_fee")))/100);
rh.setReturnOrderNo(rootElt.elementText("out_refund_no"));
rh.setAppKey(jsonO.getString("appKey"));
weiXinRefundService.addRefundInfo(rh);
System.out.println("======================微信退款成功=================");
result.put("status","success");
result.put("msg","success");
result.put("returnChannelType", rh.getReturnChannelType());
result.put("otherRefundId", rh.getOtherRefundId());
result.put("returnMoneyBak", rh.getReturnMoneyBak());
result.put("returnMoneyBalance", rh.getReturnMoneyBalance());
result.put("returnOrderNo", rh.getReturnOrderNo());
}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;
}
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!