微信支付java版本之退款申请

来源:互联网 发布:翠微居网络小手论坛 编辑:程序博客网 时间:2024/04/27 20:54

1.接口简介



2.代码实现

[java] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. package com.pay.controller.weixin;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.security.KeyStore;  
  6. import java.text.SimpleDateFormat;  
  7. import java.util.Date;  
  8.   
  9. import javax.net.ssl.SSLContext;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import net.sf.json.JSONObject;  
  13.   
  14. import org.apache.http.HttpEntity;  
  15. import org.apache.http.client.methods.CloseableHttpResponse;  
  16. import org.apache.http.client.methods.HttpPost;  
  17. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;  
  18. import org.apache.http.conn.ssl.SSLContexts;  
  19. import org.apache.http.entity.StringEntity;  
  20. import org.apache.http.impl.client.CloseableHttpClient;  
  21. import org.apache.http.impl.client.HttpClients;  
  22. import org.apache.http.util.EntityUtils;  
  23. import org.dom4j.Document;  
  24. import org.dom4j.Element;  
  25. import org.dom4j.io.SAXReader;  
  26. import org.springframework.beans.factory.annotation.Autowired;  
  27. import org.springframework.http.HttpStatus;  
  28. import org.springframework.web.bind.annotation.RequestBody;  
  29. import org.springframework.web.bind.annotation.RequestMapping;  
  30. import org.springframework.web.bind.annotation.RequestMethod;  
  31. import org.springframework.web.bind.annotation.ResponseStatus;  
  32. import org.springframework.web.bind.annotation.RestController;  
  33.   
  34. import com.pay.bo.ReturnHist;  
  35. import com.pay.service.WeiXinRefundService;  
  36. import com.pay.utils.weixin.ClientCustomSSL;  
  37. import com.pay.utils.weixin.CustomizedPropertyPlaceholderConfigurer;  
  38.   
  39.   
  40. @RestController  
  41. @RequestMapping("/pay/refund")  
  42. public class WeiXinRefundController {  
  43.       
  44.       
  45.     @Autowired  
  46.     WeiXinRefundService weiXinRefundService;  
  47.       
  48.     /** 
  49.      * 微信退款 
  50.      * @param request 
  51.      * @param response 
  52.      * @return 
  53.      */  
  54.     @RequestMapping(value="/weiXinRefund",method=RequestMethod.POST)  
  55.     @ResponseStatus(HttpStatus.OK)  
  56.     public Object refund(HttpServletResponse response,@RequestBody String body){  
  57.         System.out.println("==========================微信退款开始!!========================");  
  58.     try{  
  59.         JSONObject jsonO = JSONObject.fromObject(body);  
  60.             KeyStore keyStore = KeyStore.getInstance("PKCS12");  
  61.             FileInputStream instream = new FileInputStream(new File(  
  62.                     CustomizedPropertyPlaceholderConfigurer.getContextProperty("wx.cert").toString()));  
  63.             try {  
  64.                 keyStore.load(instream,"见邮件".toCharArray());  
  65.             }finally {  
  66.                 instream.close();  
  67.             }  
  68.   
  69.             // Trust own CA and all self-signed certs  
  70.             SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,  
  71.                     "见邮件".toCharArray()).build();  
  72.             // Allow TLSv1 protocol only  
  73.             SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(  
  74.                     sslcontext, new String[] { "TLSv1" }, null,  
  75.                     SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
  76.             CloseableHttpClient httpclient = HttpClients.custom()  
  77.                     .setSSLSocketFactory(sslsf).build();  
  78.             // HttpGet httpget = new  
  79.             // HttpGet("https://api.mch.weixin.qq.com/secapi/pay/refund");  
  80.             HttpPost httppost = new HttpPost(  
  81.                     "https://api.mch.weixin.qq.com/secapi/pay/refund");  
  82.               
  83.                 String  totalFee = jsonO.get("totalFee").toString();  
  84.                   
  85.                 String outTradeNo = weiXinRefundService.getTradePayNo(jsonO.getString("orderNo"));//"所退款订单编号"  
  86.                 String refundFee = jsonO.get("refundFee").toString();//"退款金额"  
  87.                 Date dt = new Date();  
  88.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");  
  89.                 String nonceStr = sdf.format(dt).toString();//"随机字符串";  
  90.                 String xml = ClientCustomSSL.RefundNativePackage(outTradeNo,nonceStr,totalFee,refundFee,nonceStr);  
  91.                 try {  
  92.                       
  93.                     StringEntity se = new StringEntity(xml);  
  94.                       
  95.                     httppost.setEntity(se);  
  96.   
  97.                     System.out.println("executing request" + httppost.getRequestLine());  
  98.   
  99.                     CloseableHttpResponse responseEntry = httpclient.execute(httppost);  
  100.                     try {  
  101.                         HttpEntity entity = responseEntry.getEntity();  
  102.   
  103.                         System.out.println("----------------------------------------");  
  104.                         System.out.println(responseEntry.getStatusLine());  
  105.                         if (entity != null) {  
  106.                             System.out.println("Response content length: "  
  107.                                     + entity.getContentLength());  
  108.                               
  109.                             SAXReader saxReader = new SAXReader();  
  110.                             Document document = saxReader.read(entity.getContent());  
  111.                             Element rootElt = document.getRootElement();  
  112.                             System.out.println("根节点:" + rootElt.getName());  
  113.                             System.out.println("==="+rootElt.elementText("return_code"));  
  114.                             System.out.println("==="+rootElt.elementText("result_code"));  
  115.                             System.out.println("==="+rootElt.elementText("transaction_id"));  
  116.                             System.out.println("==="+rootElt.elementText("out_trade_no"));  
  117.                             System.out.println("==="+rootElt.elementText("out_refund_no"));  
  118.                             System.out.println("==="+rootElt.elementText("refund_id"));  
  119.                             System.out.println("==="+rootElt.elementText("refund_channel"));  
  120.                             System.out.println("==="+rootElt.elementText("refund_fee"));  
  121.                             System.out.println("==="+rootElt.elementText("coupon_refund_fee"));  
  122.                             String returnCode = rootElt.elementText("return_code");  
  123.                             JSONObject result = new JSONObject();  
  124.                             if(returnCode.equals("SUCCESS")){  
  125.                                 ReturnHist rh = new ReturnHist();  
  126.                                 rh.setOtherRefundId(rootElt.elementText("out_refund_no"));  
  127.                                 rh.setReturnChannelType(rootElt.elementText("refund_channel"));  
  128.                                 rh.setReturnMoneyBak(Float.parseFloat(totalFee)/100);  
  129.                                 rh.setReturnMoneyBalance((Float.parseFloat(totalFee)-Float.parseFloat(rootElt.elementText("refund_fee")))/100);  
  130.                                 rh.setReturnOrderNo(rootElt.elementText("out_refund_no"));  
  131.                                 rh.setAppKey(jsonO.getString("appKey"));  
  132.                                 weiXinRefundService.addRefundInfo(rh);  
  133.                                 System.out.println("======================微信退款成功=================");  
  134.                                   
  135.                                 result.put("status","success");  
  136.                                 result.put("msg","success");  
  137.                                 result.put("returnChannelType", rh.getReturnChannelType());  
  138.                                 result.put("otherRefundId", rh.getOtherRefundId());  
  139.                                 result.put("returnMoneyBak", rh.getReturnMoneyBak());  
  140.                                 result.put("returnMoneyBalance", rh.getReturnMoneyBalance());  
  141.                                 result.put("returnOrderNo", rh.getReturnOrderNo());  
  142.                             }else{  
  143.                                 result.put("status","false");  
  144.                                 result.put("msg",rootElt.elementText("err_code_des"));  
  145.                             }  
  146.                               
  147.                               
  148.                             return result;  
  149.   
  150.                         }  
  151.                         EntityUtils.consume(entity);  
  152.                     }  
  153.                     finally {  
  154.                         responseEntry.close();  
  155.                     }  
  156.                 }  
  157.                 finally {  
  158.                     httpclient.close();  
  159.                 }  
  160.                 return null;  
  161.                   
  162.             }catch(Exception e){  
  163.                 e.printStackTrace();  
  164.                 JSONObject result = new JSONObject();  
  165.                 result.put("status","error");  
  166.                 result.put("msg",e.getMessage());  
  167.                 return result;  
  168.             }  
  169.                   
  170.                   
  171.             }  
  172.               
  173.               
  174.       
  175.           
  176.     }  
  177.       
  178.       
  179.       
  180.       
  181.       
  182.       
  183.       



0 0
原创粉丝点击